<br><br><div class="gmail_quote">On Sun, Sep 20, 2009 at 3:02 PM, Joshua Gargus <span dir="ltr">&lt;<a href="mailto:schwa@fastmail.us">schwa@fastmail.us</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
Eliot Miranda wrote:<br>
&gt;<br>
&gt;<br>
&gt; ------------------------------------------------------------------------<br>
<div class="im">&gt;<br>
&gt;<br>
&gt;<br>
&gt; On Sun, Sep 20, 2009 at 1:00 PM, Eliot Miranda<br>
</div><div class="im">&gt; &lt;<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a> &lt;mailto:<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>&gt;&gt; wrote:<br>
&gt;<br>
&gt;     Hi Josh,<br>
&gt;<br>
&gt;     On Sun, Sep 20, 2009 at 12:43 PM, Joshua Gargus &lt;<a href="mailto:schwa@fastmail.us">schwa@fastmail.us</a><br>
</div><div class="im">&gt;     &lt;mailto:<a href="mailto:schwa@fastmail.us">schwa@fastmail.us</a>&gt;&gt; wrote:<br>
&gt;<br>
&gt;<br>
&gt;         Hi,<br>
&gt;<br>
&gt;         I&#39;ve always assumed that signalSemaphoreWithIndex() must be<br>
&gt;         thread-safe;<br>
&gt;         after all, it&#39;s the &quot;official&quot; mechanism for notifying the<br>
&gt;         image that an<br>
&gt;         asynchronous event has occurred.  However, a pang of paranoia<br>
&gt;         prompted<br>
&gt;         me to actually look at the code, and it seems clearly unsafe.<br>
&gt;          This is<br>
&gt;         bad, because I&#39;ve been using it to signal events from separate<br>
&gt;         native<br>
&gt;         threads.<br>
&gt;<br>
&gt;         What should we do about this?  It seems to me that it should<br>
&gt;         be wrapped<br>
&gt;         in a critical section, using the appropriate platform-specific<br>
&gt;         synchronization primitives.<br>
&gt;<br>
&gt;<br>
&gt;     There&#39;s no need for this heavyweight approach because the VM can<br>
&gt;     only respond to these signals synchronously.<br>
&gt;<br>
&gt;<br>
&gt; On second reading I think what I wrote below is exactly what you<br>
&gt; implied.  Sorry.<br>
<br>
</div>Nope, you&#39;re always giving me too much credit ;-)  I wasn&#39;t thinking<br>
about any particular implementation of thread-safety, only raising the<br>
issue that (in my understanding) we don&#39;t have it.<br>
<div><div></div><div class="h5"><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;      Instead we can use three variables per index to implement an<br>
&gt;     exclusion-free solution, thusly:<br>
&gt;<br>
&gt;     typedef struct { int lock; // accessed via e.g. XCHG to protect<br>
&gt;     signalRequest<br>
&gt;                            int requests;<br>
&gt;                            int responses;<br>
&gt;                } ExternalSignalRequest;<br>
&gt;<br>
&gt;     ExternalSignalRequest *externalSignalRequests;<br>
&gt;     int checkExternalSignalRequests;<br>
&gt;<br>
&gt;     void<br>
&gt;     requestSignal(int i)<br>
&gt;     {<br>
&gt;            while (!lock(&amp;externalSignalRequests[i].lock))<br>
&gt;                  usleep(1);<br>
&gt;<br>
&gt;            ++externalSignalRequests[i].requests;<br>
&gt;<br>
&gt;             unlock(&amp;externalSignalRequests[i].lock);<br>
&gt;<br>
&gt;             checkExternalSignalRequests = 1;<br>
&gt;             forceInterruptCheck();  // set a flag to cause the VM to<br>
&gt;     check for interrupts; in the stack VM this is stackLimit :=<br>
&gt;     (usqInt)-1;<br>
&gt;     }<br>
&gt;<br>
&gt;<br>
&gt;     The VM responds in checkEvents:<br>
&gt;<br>
&gt;         if (checkExternalSignalRequests)<br>
&gt;             for (i = 0; i &lt; numExternalSemaphores; i++)<br>
&gt;                 while (externalSignalRequests[i]. responses<br>
&gt;     != externalSignalRequests[i]. requests) {<br>
&gt;                     signalSemaphoreWithIndex(i);<br>
&gt;                     externalSignalRequests[i]. responses;<br>
&gt;                 }<br>
&gt;<br>
<br>
<br>
</div></div>Am I correct that ExternalSignalRequest.reponses is never reset to zero,<br>
instead eventually wrapping around?  That worried me for a minute, but I<br>
see that it doesn&#39;t matter.  Neeto.<br></blockquote><div><br></div><div>Yes.  This could be Allen Shiffman&#39;s or Peter Deutsch&#39;s idea.  Its used in the VW VM.  Very nice.  It can only fail if the VM ignores at least 2^32 successive requests :)</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
Your solution looks good; it&#39;s certainly nicer than a heavyweight<br>
critical section, especially since contention will be infrequent.<br></blockquote><div><br></div><div>Right.  Of course if any one external semaphore is never signalled from more than one thread we don&#39;t even need that but the cost of the XCHG should be very low compared to the overall cost of the signal.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
Cheers,<br>
Josh<br>
<br>
<br>
<br>
<br>
&gt;<br>
&gt;<br>
&gt;         Thanks,<br>
&gt;         Josh<br>
&gt;<br>
&gt;<br>
&gt;<br>
<br>
</blockquote></div><br>