<html><body bgcolor="#FFFFFF"><div>I will read the paper, thanks.</div><div><br></div><div>The java code demonstrates I believe some important applicable methods. I hope to have a "new" ThreadWin32.m3 "soon". In particular, no per-thread event, no wait lists, and counter to help matching up condition waits and signals. And no giant lock. And stil an efficient mutex with no kernel involvement unless there is contention, could/might use win32 criticalsection with little extra to avoid recursion, or could use something smaller. And no use of SignalObjectAndWait whose documentation recently changed to remove the atomicity claim!<br><br> - Jay (phone<span class="Apple-style-span" style="-webkit-composition-fill-color: rgba(175, 192, 227, 0.231373); -webkit-composition-frame-color: rgba(77, 128, 180, 0.231373); ">)</span></div><div><br>On Oct 20, 2009, at 9:23 AM, "Randy Coleburn" <<a href="mailto:rcoleburn@scires.com">rcoleburn@scires.com</a>> wrote:<br><br></div><div></div><blockquote type="cite"><div>
<div>Jay:</div>
<div> </div>
<div>I think we would need to delve deep into the implementation to be able to answer all your questions precisely.</div>
<div> </div>
<div>I've attached a short paper by Andrew Birrell "Implementing Condition Variables with Semaphores" that you may find interesting / enlightening.</div>
<div> </div>
<div>My concern about using multiple mutex with same condition lies in the queuing operations.  My recollection is that I've always associated only one mutex with a condition variable, but that you can have multiple conditions associated with the same mutex.</div>
<div> </div>
<div>I will go back and re-read Nelson again--its been a few years.</div>
<div> </div>
<div>Regards,</div>
<div>Randy Coleburn<br><br>>>> Jay K <<a href="mailto:jay.krell@cornell.edu">jay.krell@cornell.edu</a>> 10/18/2009 4:16 AM >>><br>I still have questions here.<br> <br>1)<br>Page 93 of the Nelson book:<br>A monitor consists of some data, a mutex, and zero or more condition<br>variables. A particular condition variable is always used<br>in conjunction with the same mutex and its data.<br><br>Doesn't this contradict the point made here?<br>Does a condition variable always map to the same mutex<br>or not?<br> <br>Or is this merely describing a typical usage pattern that is<br>a subset of what interface Thread allows?<br> <br><br>2)<br>Can Wait only be satisfied by Signal/Broadcast,<br>or also just via UnlockMutex?<br> <br><br>Depending on the answer to these questions,<br>it seems you can largely merge mutex and condition variable.<br> <br><br>Condition variable is basically waiting for a<br>thread to exit a mutex.<br>Which is very very similar to LockMutex, except<br>that it doesn't want to take the mutex in the uncontended<br>case, it actually wants to wait for another thread<br>to both acquire and release the mutex.<br> <br> <br>I suspect I'm wrong on both of these.<br>That condition variable really can use multiple mutexes.<br>That exiting a mutex has no obligation to wake condition variables,<br>  though it might be in good faith to do so...er..if it is<br>  in good faith to not require programmer to use Signal/Broadcast.<br> <br> <br>Thanks,<br> - Jay<br><br><br> <br></div>
<div>
<hr id="stopSpelling">
</div>
<div>From: <a href="mailto:jay.krell@cornell.edu"><a href="mailto:jay.krell@cornell.edu">jay.krell@cornell.edu</a></a><br>To: <a href="mailto:hosking@cs.purdue.edu">hosking@cs.purdue.edu</a>; <a href="mailto:mika@async.async.caltech.edu"><a href="mailto:mika@async.async.caltech.edu">mika@async.async.caltech.edu</a></a><br>Date: Thu, 8 Oct 2009 19:13:03 +0000<br>CC: <a href="mailto:m3devel@elegosoft.com"><a href="mailto:m3devel@elegosoft.com">m3devel@elegosoft.com</a></a><br>Subject: Re: [M3devel] condition variables/win32<br><br>
<style>
.ExternalClass .ecxhmmessage P
{padding:0px;}
.ExternalClass body.ecxhmmessage
{font-size:10pt;font-family:Verdana;}
</style>
That seems a little strange to me but I guess I'll have to keep it in mind.<br> <br> - Jay<br><br> <br></div>
<div>
<hr id="ecxstopSpelling">
</div>
<div>From: <a href="mailto:hosking@cs.purdue.edu"><a href="mailto:hosking@cs.purdue.edu">hosking@cs.purdue.edu</a></a><br>To: <a href="mailto:mika@async.async.caltech.edu"><a href="mailto:mika@async.async.caltech.edu">mika@async.async.caltech.edu</a></a><br>Date: Thu, 8 Oct 2009 11:00:36 -0400<br>CC: <a href="mailto:m3devel@elegosoft.com"><a href="mailto:m3devel@elegosoft.com">m3devel@elegosoft.com</a></a><br>Subject: Re: [M3devel] condition variables/win32<br><br>Sorry, yes, you are right of course!  The Modula-3 spec (and the current pthreads-based implementation as also the win32 implementation I expect) do allow a condition variable being mediated by different mutexes.  My comment was clouded by my recollection from the pthreads spec that for pthread mutex/cv behavior for other than 1 mutex per cv is undefined.  This confusion may have been the source of prior bugs in the pthreads threading implementation, but those bugs are gone now.  We support the M3 spec properly. </div>
<div><font class="ecxecxApple-style-span" face="'Gill Sans'" color="#0000ff"><br></font></div>
<div>
<div>
<div>On 8 Oct 2009, at 10:34, Mika Nystrom wrote:</div><br class="ecxecxApple-interchange-newline">
<blockquote>
<div>Why can't you use the same condition variable with different mutexes?<br><br>This is dynamic, up to the M3 programmer, no?<br><br>Tony Hosking writes:<br>
<blockquote><br></blockquote>
<blockquote>--Apple-Mail-96--321618545<br></blockquote>
<blockquote>Content-Type: text/plain;<br></blockquote>
<blockquote><span class="ecxecxApple-tab-span" style="WHITE-SPACE: pre"></span>charset=US-ASCII;<br></blockquote>
<blockquote><span class="ecxecxApple-tab-span" style="WHITE-SPACE: pre"></span>format=flowed;<br></blockquote>
<blockquote><span class="ecxecxApple-tab-span" style="WHITE-SPACE: pre"></span>delsp=yes<br></blockquote>
<blockquote>Content-Transfer-Encoding: 7bit<br></blockquote>
<blockquote><br></blockquote>
<blockquote>In general, it is OK in M3 to associate multiple conditions with the  <br></blockquote>
<blockquote>same mutex.  But not vice versa.<br></blockquote>
<blockquote><br></blockquote>
<blockquote>On 8 Oct 2009, at 09:32, Jay K wrote:<br></blockquote>
<blockquote><br></blockquote>
<blockquote>
<blockquote>condition variables/win32<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>So..one way I think about condition variables<br></blockquote></blockquote>
<blockquote>
<blockquote>is that you want to be woken when someone else<br></blockquote></blockquote>
<blockquote>
<blockquote>leaves the mutex that guards the data that you are dealing with.<br></blockquote></blockquote>
<blockquote>
<blockquote>You want to know when another thread modifies the data.<br></blockquote></blockquote>
<blockquote>
<blockquote>(If you have a reader/writer lock, you only want to be<br></blockquote></blockquote>
<blockquote>
<blockquote>woken when someone exits a write.)<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>Now, if you consider a producer/consumer queue.<br></blockquote></blockquote>
<blockquote>
<blockquote>There are two interesting occurences.<br></blockquote></blockquote>
<blockquote>
<blockquote>Transitions from empty to non-empty<br></blockquote></blockquote>
<blockquote>
<blockquote>and transitions from full to non-full (optionally,<br></blockquote></blockquote>
<blockquote>
<blockquote>if it is fixed size).<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>Consumers wait for empty to non-empty.<br></blockquote></blockquote>
<blockquote>
<blockquote>Consumers signal full to non-full.<br></blockquote></blockquote>
<blockquote>
<blockquote>Producers wait for full to non-full.<br></blockquote></blockquote>
<blockquote>
<blockquote>Producers signal non-empty to empty.<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>So, in this case, one mutex is likely used with with two condition  <br></blockquote></blockquote>
<blockquote>
<blockquote>variables.<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>But, what if we take a simplifying deoptimization and assume that a  <br></blockquote></blockquote>
<blockquote>
<blockquote>condition<br></blockquote></blockquote>
<blockquote>
<blockquote>variable is only ever associated with one mutex?<br></blockquote></blockquote>
<blockquote>
<blockquote>Anyone existing that mutex wakes up anyone waiting on any condition  <br></blockquote></blockquote>
<blockquote>
<blockquote>associated with it?<br></blockquote></blockquote>
<blockquote>
<blockquote>Like, a condition variable I think becomes stateless and everything is<br></blockquote></blockquote>
<blockquote>
<blockquote>about the mutex?<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>What is the downside?<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>Condition variables are allowed to have spurious wakeups.<br></blockquote></blockquote>
<blockquote>
<blockquote>This would "just" increase them. Too much?<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>So, therefore, what would be wrong with the following design?<br></blockquote></blockquote>
<blockquote>
<blockquote>a mutex contains an event<br></blockquote></blockquote>
<blockquote>
<blockquote>and a number of waiters, zero or non-zero<br></blockquote></blockquote>
<blockquote>
<blockquote>if a mutex is exiting with a non-zero number of waiters, signal the  <br></blockquote></blockquote>
<blockquote>
<blockquote>event<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>To handle Signal vs. Broadcast<br></blockquote></blockquote>
<blockquote>
<blockquote>method 1:<br></blockquote></blockquote>
<blockquote>
<blockquote>the number of waiters might be interlocked<br></blockquote></blockquote>
<blockquote>
<blockquote>the woken would decrement it<br></blockquote></blockquote>
<blockquote>
<blockquote>if it isn't zero, signal the event again<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>method 2:<br></blockquote></blockquote>
<blockquote>
<blockquote>the number of waiters is both an integer and a semaphore<br></blockquote></blockquote>
<blockquote>
<blockquote>and the lock exiter raises the semaphore by the the integer<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>method 3:<br></blockquote></blockquote>
<blockquote>
<blockquote>it is not an auto-reset event and there is a count<br></blockquote></blockquote>
<blockquote>
<blockquote> and when the count goes to 0, reset the event<br></blockquote></blockquote>
<blockquote>
<blockquote>I think in this case you have to maintain a "wait generation"<br></blockquote></blockquote>
<blockquote>
<blockquote>so that new waiters don't prevent the count from ever hitting 0.<br></blockquote></blockquote>
<blockquote>
<blockquote>I think this #3 is what Java might be doing, and is described here:<br></blockquote></blockquote>
<blockquote>
<blockquote><a href="http://www.cs.wustl.edu/~schmidt/win32-cv-1.html"><a href="http://www.cs.wustl.edu/~schmidt/win32-cv-1.html">http://www.cs.wustl.edu/~schmidt/win32-cv-1.html</a></a><br></blockquote></blockquote>
<blockquote>
<blockquote>"3.3. The Generation Count Solution"<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>also:<br></blockquote></blockquote>
<blockquote>
<blockquote><a href="http://www.cs.wustl.edu/~schmidt/win32-cv-1.html"><a href="http://www.cs.wustl.edu/~schmidt/win32-cv-1.html">http://www.cs.wustl.edu/~schmidt/win32-cv-1.html</a></a><br></blockquote></blockquote>
<blockquote>
<blockquote>3.2. The SetEvent Solution<br></blockquote></blockquote>
<blockquote>
<blockquote>Evaluating the SetEvent Solution<br></blockquote></blockquote>
<blockquote>
<blockquote>Incorrectness --<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>Is that incorrect case really necessarily incorrect?<br></blockquote></blockquote>
<blockquote>
<blockquote>It seems unfair, since first waiter should be first woken, but..?<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>Am I missing something? A lot?<br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote><br></blockquote></blockquote>
<blockquote>
<blockquote>- Jay<br></blockquote></blockquote>
<blockquote><br></blockquote>
<blockquote><br></blockquote>
<blockquote>--Apple-Mail-96--321618545<br></blockquote>
<blockquote>Content-Type: text/html;<br></blockquote>
<blockquote><span class="ecxecxApple-tab-span" style="WHITE-SPACE: pre"></span>charset=US-ASCII<br></blockquote>
<blockquote>Content-Transfer-Encoding: quoted-printable<br></blockquote>
<blockquote><br></blockquote>
<blockquote><html><body style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =<br></blockquote>
<blockquote>-webkit-line-break: after-white-space; "><div =<br></blockquote>
<blockquote>apple-content-edited=3D"true"><span class=3D"Apple-style-span" =<br></blockquote>
<blockquote>style=3D"border-collapse: separate; color: rgb(0, 0, 0); font-family: =<br></blockquote>
<blockquote>Helvetica; font-size: 12px; font-style: normal; font-variant: normal; =<br></blockquote>
<blockquote>font-weight: normal; letter-spacing: normal; line-height: normal; =<br></blockquote>
<blockquote>orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; =<br></blockquote>
<blockquote>white-space: normal; widows: 2; word-spacing: 0px; =<br></blockquote>
<blockquote>-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: =<br></blockquote>
<blockquote>0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =<br></blockquote>
<blockquote>auto; -webkit-text-stroke-width: 0; "><div style=3D"word-wrap: =<br></blockquote>
<blockquote>break-word; -webkit-nbsp-mode: space; -webkit-line-break: =<br></blockquote>
<blockquote>after-white-space; "><span class=3D"Apple-style-span" =<br></blockquote>
<blockquote>style=3D"border-collapse: separate; -webkit-border-horizontal-spacing: =<br></blockquote>
<blockquote>0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); =<br></blockquote>
<blockquote>font-family: Helvetica; font-size: 12px; font-style: normal; =<br></blockquote>
<blockquote>font-variant: normal; font-weight: normal; letter-spacing: normal; =<br></blockquote>
<blockquote>line-height: normal; -webkit-text-decorations-in-effect: none; =<br></blockquote>
<blockquote>text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; =<br></blockquote>
<blockquote>orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div =<br></blockquote>
<blockquote>style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =<br></blockquote>
<blockquote>-webkit-line-break: after-white-space; "><span class=3D"Apple-style-span" =<br></blockquote>
<blockquote>style=3D"border-collapse: separate; -webkit-border-horizontal-spacing: =<br></blockquote>
<blockquote>0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); =<br></blockquote>
<blockquote>font-family: Helvetica; font-size: 12px; font-style: normal; =<br></blockquote>
<blockquote>font-variant: normal; font-weight: normal; letter-spacing: normal; =<br></blockquote>
<blockquote>line-height: normal; -webkit-text-decorations-in-effect: none; =<br></blockquote>
<blockquote>text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; =<br></blockquote>
<blockquote>orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><span =<br></blockquote>
<blockquote>class=3D"Apple-style-span" style=3D"border-collapse: separate; =<br></blockquote>
<blockquote>-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: =<br></blockquote>
<blockquote>0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; =<br></blockquote>
<blockquote>font-style: normal; font-variant: normal; font-weight: normal; =<br></blockquote>
<blockquote>letter-spacing: normal; line-height: normal; =<br></blockquote>
<blockquote>-webkit-text-decorations-in-effect: none; text-indent: 0px; =<br></blockquote>
<blockquote>-webkit-text-size-adjust: auto; text-transform: none; orphans: 2; =<br></blockquote>
<blockquote>white-space: normal; widows: 2; word-spacing: 0px; "><span =<br></blockquote>
<blockquote>class=3D"Apple-style-span" style=3D"border-collapse: separate; =<br></blockquote>
<blockquote>-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: =<br></blockquote>
<blockquote>0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; =<br></blockquote>
<blockquote>font-style: normal; font-variant: normal; font-weight: normal; =<br></blockquote>
<blockquote>letter-spacing: normal; line-height: normal; =<br></blockquote>
<blockquote>-webkit-text-decorations-in-effect: none; text-indent: 0px; =<br></blockquote>
<blockquote>-webkit-text-size-adjust: auto; text-transform: none; orphans: 2; =<br></blockquote>
<blockquote>white-space: normal; widows: 2; word-spacing: 0px; "><span =<br></blockquote>
<blockquote>class=3D"Apple-style-span" style=3D"border-collapse: separate; =<br></blockquote>
<blockquote>-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: =<br></blockquote>
<blockquote>0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; =<br></blockquote>
<blockquote>font-style: normal; font-variant: normal; font-weight: normal; =<br></blockquote>
<blockquote>letter-spacing: normal; line-height: normal; =<br></blockquote>
<blockquote>-webkit-text-decorations-in-effect: none; text-indent: 0px; =<br></blockquote>
<blockquote>-webkit-text-size-adjust: auto; text-transform: none; orphans: 2; =<br></blockquote>
<blockquote>white-space: normal; widows: 2; word-spacing: 0px; "><span =<br></blockquote>
<blockquote>class=3D"Apple-style-span" style=3D"border-collapse: separate; =<br></blockquote>
<blockquote>-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: =<br></blockquote>
<blockquote>0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; =<br></blockquote>
<blockquote>font-style: normal; font-variant: normal; font-weight: normal; =<br></blockquote>
<blockquote>letter-spacing: normal; line-height: normal; =<br></blockquote>
<blockquote>-webkit-text-decorations-in-effect: none; text-indent: 0px; =<br></blockquote>
<blockquote>-webkit-text-size-adjust: auto; text-transform: none; orphans: 2; =<br></blockquote>
<blockquote>white-space: normal; widows: 2; word-spacing: 0px; "><span =<br></blockquote>
<blockquote>class=3D"Apple-style-span" style=3D"border-collapse: separate; =<br></blockquote>
<blockquote>-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: =<br></blockquote>
<blockquote>0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; =<br></blockquote>
<blockquote>font-style: normal; font-variant: normal; font-weight: normal; =<br></blockquote>
<blockquote>letter-spacing: normal; line-height: normal; =<br></blockquote>
<blockquote>-webkit-text-decorations-in-effect: none; text-indent: 0px; =<br></blockquote>
<blockquote>-webkit-text-size-adjust: auto; text-transform: none; orphans: 2; =<br></blockquote>
<blockquote>white-space: normal; widows: 2; word-spacing: 0px; "><span =<br></blockquote>
<blockquote>class=3D"Apple-style-span" style=3D"border-collapse: separate; =<br></blockquote>
<blockquote>-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: =<br></blockquote>
<blockquote>0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; =<br></blockquote>
<blockquote>font-style: normal; font-variant: normal; font-weight: normal; =<br></blockquote>
<blockquote>letter-spacing: normal; line-height: normal; =<br></blockquote>
<blockquote>-webkit-text-decorations-in-effect: none; text-indent: 0px; =<br></blockquote>
<blockquote>-webkit-text-size-adjust: auto; text-transform: none; orphans: 2; =<br></blockquote>
<blockquote>white-space: normal; widows: 2; word-spacing: 0px; "><span =<br></blockquote>
<blockquote>class=3D"Apple-style-span" style=3D"border-collapse: separate; =<br></blockquote>
<blockquote>-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: =<br></blockquote>
<blockquote>0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; =<br></blockquote>
<blockquote>font-style: normal; font-variant: normal; font-weight: normal; =<br></blockquote>
<blockquote>letter-spacing: normal; line-height: normal; =<br></blockquote>
<blockquote>-webkit-text-decorations-in-effect: none; text-indent: 0px; =<br></blockquote>
<blockquote>-webkit-text-size-adjust: auto; text-transform: none; orphans: 2; =<br></blockquote>
<blockquote>white-space: normal; widows: 2; word-spacing: 0px; "><div><span =<br></blockquote>
<blockquote>class=3D"Apple-style-span" style=3D"font-size: medium;"><font =<br></blockquote>
<blockquote>class=3D"Apple-style-span" color=3D"#0000FF" face=3D"'Gill Sans'">In =<br></blockquote>
<blockquote>general, it is OK in M3 to associate multiple conditions with the same =<br></blockquote>
<blockquote>mutex. &nbsp;But not vice versa.</font></span></div><div><font =<br></blockquote>
<blockquote>class=3D"Apple-style-span" color=3D"#0000FF" face=3D"'Gill Sans'"><span =<br></blockquote>
<blockquote>class=3D"Apple-style-span" style=3D"font-size: =<br></blockquote>
<blockquote>medium;"><br></span></font></div></span></span></span></span></span></span=<br></blockquote>
<blockquote>
<blockquote></span></span></div></span></div></span></div><div><div>On 8 Oct 2009, =<br></blockquote></blockquote>
<blockquote>at 09:32, Jay K wrote:</div><br =<br></blockquote>
<blockquote>class=3D"Apple-interchange-newline"><blockquote type=3D"cite"><span =<br></blockquote>
<blockquote>class=3D"Apple-style-span" style=3D"border-collapse: separate; color: =<br></blockquote>
<blockquote>rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: =<br></blockquote>
<blockquote>normal; font-variant: normal; font-weight: normal; letter-spacing: =<br></blockquote>
<blockquote>normal; line-height: normal; orphans: 2; text-align: auto; text-indent: =<br></blockquote>
<blockquote>0px; text-transform: none; white-space: normal; widows: 2; word-spacing: =<br></blockquote>
<blockquote>0px; -webkit-border-horizontal-spacing: 0px; =<br></blockquote>
<blockquote>-webkit-border-vertical-spacing: 0px; =<br></blockquote>
<blockquote>-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =<br></blockquote>
<blockquote>auto; -webkit-text-stroke-width: 0px; "><div class=3D"hmmessage" =<br></blockquote>
<blockquote>style=3D"font-size: 10pt; font-family: Verdana; ">condition =<br></blockquote>
<blockquote>variables/win32<br>&nbsp;<br><br>So..one way I think about condition =<br></blockquote>
<blockquote>variables<br>is that you want to be woken when someone else<br>leaves =<br></blockquote>
<blockquote>the mutex that guards the data that you are dealing with.<br>You want to =<br></blockquote>
<blockquote>know when another thread modifies the data.<br>(If you have a =<br></blockquote>
<blockquote>reader/writer lock, you only want to be<br>woken when someone exits a =<br></blockquote>
<blockquote>write.)<br>&nbsp;<br><br>Now, if you consider a producer/consumer =<br></blockquote>
<blockquote>queue.<br>There are two interesting occurences.<br>Transitions from =<br></blockquote>
<blockquote>empty to non-empty<br>and transitions from full to non-full =<br></blockquote>
<blockquote>(optionally,<br>if it is fixed size).<br>&nbsp;<br><br>Consumers wait =<br></blockquote>
<blockquote>for empty to non-empty.<br>Consumers signal full to =<br></blockquote>
<blockquote>non-full.<br>Producers wait for full to non-full.<br>Producers signal =<br></blockquote>
<blockquote>non-empty to empty.<br>&nbsp;<br><br>So, in this case, one mutex is =<br></blockquote>
<blockquote>likely&nbsp;used with&nbsp;with two condition =<br></blockquote>
<blockquote>variables.<br>&nbsp;<br><br>But, what if we take a simplifying =<br></blockquote>
<blockquote>deoptimization and assume that a condition<br>variable is only ever =<br></blockquote>
<blockquote>associated with one mutex?<br>Anyone existing that mutex wakes up anyone =<br></blockquote>
<blockquote>waiting on any condition associated with it?<br>Like, a condition =<br></blockquote>
<blockquote>variable I think becomes stateless and everything is<br>about the =<br></blockquote>
<blockquote>mutex?<br>&nbsp;<br>&nbsp;<br>What is the =<br></blockquote>
<blockquote>downside?<br>&nbsp;<br><br>Condition variables are allowed to have =<br></blockquote>
<blockquote>spurious wakeups.<br>This would "just" increase them. Too =<br></blockquote>
<blockquote>much?<br>&nbsp;<br><br>So, therefore, what would be wrong with the =<br></blockquote>
<blockquote>following design?<br>&nbsp;a mutex contains an event<span =<br></blockquote>
<blockquote>class=3D"Apple-converted-space">&nbsp;</span><br>&nbsp;and a number of =<br></blockquote>
<blockquote>waiters, zero or non-zero<span =<br></blockquote>
<blockquote>class=3D"Apple-converted-space">&nbsp;</span><br>&nbsp;if a mutex is =<br></blockquote>
<blockquote>exiting with a non-zero number of waiters, signal the =<br></blockquote>
<blockquote>event<br>&nbsp;<br><br>To handle Signal vs. Broadcast<br>method =<br></blockquote>
<blockquote>1:<br>&nbsp;the number of waiters might be interlocked<br>&nbsp;the =<br></blockquote>
<blockquote>woken would decrement it<br>&nbsp;if it isn't zero, signal the event =<br></blockquote>
<blockquote>again<br>&nbsp;<br><br>method 2:<br>&nbsp;the number of waiters is both =<br></blockquote>
<blockquote>an integer and a semaphore<br>&nbsp;and the lock exiter raises the =<br></blockquote>
<blockquote>semaphore by the the integer<br><br>&nbsp;<br>method 3:<br>&nbsp;it is =<br></blockquote>
<blockquote>not an auto-reset event and there is a count<br>&nbsp; and when the =<br></blockquote>
<blockquote>count goes to 0, reset the event<br>&nbsp;I think in this case you have =<br></blockquote>
<blockquote>to maintain a "wait generation"<span =<br></blockquote>
<blockquote>class=3D"Apple-converted-space">&nbsp;</span><br>&nbsp;so that new =<br></blockquote>
<blockquote>waiters don't prevent the count from ever hitting 0.<br>&nbsp;I think =<br></blockquote>
<blockquote>this #3 is what Java might be doing, and is described here:<br><a =<br></blockquote>
<blockquote>href=3D"<a href="http://www.cs.wustl.edu/~schmidt/win32-cv-1.html"><a href="http://www.cs.wustl.edu/~schmidt/win32-cv-1.html">http://www.cs.wustl.edu/~schmidt/win32-cv-1.html</a></a>"><a href="http://www.cs.wu/"><a href="http://www.cs.wu">http://www.cs.wu</a></a>=<br></blockquote>
<blockquote><a href="http://stl.edu/~schmidt">stl.edu/~schmidt</a>/win32-cv-1.html</a><br>&nbsp;"3.3. The Generation Count =<br></blockquote>
<blockquote>Solution"<br><br>&nbsp;<br>also:<br><a =<br></blockquote>
<blockquote>href=3D"<a href="http://www.cs.wustl.edu/~schmidt/win32-cv-1.html"><a href="http://www.cs.wustl.edu/~schmidt/win32-cv-1.html">http://www.cs.wustl.edu/~schmidt/win32-cv-1.html</a></a>"><a href="http://www.cs.wu/"><a href="http://www.cs.wu">http://www.cs.wu</a></a>=<br></blockquote>
<blockquote><a href="http://stl.edu/~schmidt">stl.edu/~schmidt</a>/win32-cv-1.html</a><br>3.2. The SetEvent =<br></blockquote>
<blockquote>Solution<br>Evaluating the SetEvent Solution<br>Incorrectness --<span =<br></blockquote>
<blockquote>class=3D"Apple-converted-space">&nbsp;</span><br>&nbsp;<br><br>Is that =<br></blockquote>
<blockquote>incorrect case really necessarily incorrect?<br>It seems unfair, since =<br></blockquote>
<blockquote>first waiter should be first woken, but..?<br><br>&nbsp;<br>Am I missing =<br></blockquote>
<blockquote>something? A lot?<br>&nbsp;<br><br>&nbsp;- =<br></blockquote>
<blockquote>Jay<br></div></span></blockquote></div><br></body></html>=<br></blockquote>
<blockquote><br></blockquote>
<blockquote>--Apple-Mail-96--321618545--<br></blockquote></div></blockquote></div><br></div>
</div></blockquote><blockquote type="cite"><div><ImplementingCVs.pdf></div></blockquote></body></html>