<br><br><div class="gmail_quote">On Tue, Nov 10, 2009 at 6:45 PM, John M McIntosh <span dir="ltr">&lt;<a href="mailto:johnmci@smalltalkconsulting.com">johnmci@smalltalkconsulting.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><br>
On 2009-11-10, at 6:17 PM, Eliot Miranda wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
With the threaded Squeak VM I&#39;m working on one can go one better and have a number of image-level processes that block in the FFI and a number of worker threads in the VM that block on OS semaphores waiting for the VM to give them something to do.<br>

</blockquote>
<br></div>
Obviously now you have to give a bit more details on this. Is it like the hydra VM? Or entirely different?</blockquote><div><br></div><div>Orthogonal, in that it might work well with Hydra.  The basic scheme is to have a natively multi-threaded VM that is not concurrent.  Multiple native threads share the Vm such that there is only one thread running Vm code at any one time.  This the VM can make non-blocking calls to the outside world but neither the VM nor the image need to be modified to handle true concurrency.  This is the same basic architecture as in the Strongtalk and V8 VMs and notably in David Simmons&#39; various Smalltalk VMs.</div>
<div><br></div><div>The cool thing about the system is David&#39;s design.  He&#39;s been extremely generous in explaining to me his scheme, which is extremely efficient.  I&#39;ve merely implemented this scheme in the context of the Cog VM.  The idea is to arrange that a threaded callout is so cheap that any and all callouts can be threaded.  This is done by arranging that a callout does not switch to another thread, instead the thread merely &quot;disowns&quot; the VM.  It is the job of a background heartbeat thread to detect tat a callout is long-runnijng and that the VM has effectively blocked.  The heartbeat then activates a new thread to run the VM and the new thread attempts to take ownership and will run Smalltalk code if it succeeds.</div>
<div><br></div><div>On return form a callout a thread must attempt to take ownership of the VM, and if it fails, add itself to a queue of threads waiting to take back the VM and then wait on an OS semaphore until the thread owning the VM decides to give up ownership to it.</div>
<div><br></div><div>Every VM thread has a unique index.  The vmOwner variable holds the index of the owning thread or 0 if the VM is unowned.  To disown the VM all a thread has to do is zero vmOwner, while remembering the value of vmOwner in a temporary.  To take ownership a thread must use a low-level lock to gain exclusive access to vmOwner, and if vmOwner is zero, set it back to the thread&#39;s index, and release the lock.  If it finds vmOwner is non-zero it releases the lock and enters the wanting ownership queue.</div>
<div><br></div><div>In the Cog VM the heartbeat beats at 1KHz, so any call that takes less than 0.5ms is likely to complete without the heartbeat detecting that the VM is blocked.  So any and all callouts can be threaded.  Quite brilliant.  All the work of changing the active process when switching between threads is deferred from callout time to when a different thread takes ownership of the VM, saving the VM state for the process that surrendered the VM and installing its own.</div>
<div><br></div><div>The major wrinkle in this is that in David&#39;s VM he has a pinning garbage collector which arranges that any arguments passed out through the FFI are implicitly pinned.  We don&#39;t yet have a pinning garbage collector.  I do plan to do one.  But in the interim one quick hack, a neat idea of Andreas&#39;, is to fail calls that attempt to pass objects in new space, allowing only old objects to be passed, and to prevent the full garbage collector from running while any threaded calls are in progress.</div>
<div><br></div><div>Having cheap non-blocking calls allows e.g.</div><div>- the Hydra inter-VM channels to be implemented in Smalltalk code above the threaded FFI</div><div>- socket calls to be blocking calls in the image</div>
<div>- Smalltalk code to call select/poll/WaitForMultipleEvents</div><div><br></div><div>There are still plenty of sticky issues to do with e.g. identifying threads that can do specific functions, such as the UI thread, and issuing OpenGL calls from the right thread, etc, etc.  But these are all doable, if potentially tricky to get right.  If this kind of code does migrate from the VM innards up to the image I think that&#39;s a really good thing (tm) but one will really have to know what one is doing to get it right.</div>
<div><br></div><div>HTH</div><div>eliot</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div></div><div class="h5"><br>
<br>
<br>
--<br>
===========================================================================<br>
John M. McIntosh &lt;<a href="mailto:johnmci@smalltalkconsulting.com" target="_blank">johnmci@smalltalkconsulting.com</a>&gt;   Twitter:  squeaker68882<br>
Corporate Smalltalk Consulting Ltd.  <a href="http://www.smalltalkconsulting.com" target="_blank">http://www.smalltalkconsulting.com</a><br>
===========================================================================<br>
<br>
<br>
<br>
<br>
</div></div></blockquote></div><br>