[M3devel] condition variables/win32

Jay K jay.krell at cornell.edu
Thu Oct 8 16:22:51 CEST 2009


But, is it common? Ok to make it contribute significantly to spurious wakeups?

That is, I have this crazy theory...really need to come back to this fresh, try coding it, testing it..where you can implement a condition variable simply by waking everyone whenever a mutex is exited.

 

 

I had this thought that what a condition variable represents is, instead of telling the kernel, here is one bit, an event, I am waiting for, instead you are telling it, hey, I have some custom code to evaluate, but it is false currently, and can only change when some exits such and such a lock, so just let me know when that lock is exited.

 

 

The guy releasing the lock, or signal or broadcast..if he is signaling or broadcasting, he knows more specifically what he changed, not everything computable based on data protected by the lock, just something specific, but you can just wake everyone waiting on any of the conditions associated with the lock and it isn't maximally efficient but it should be correct.

 

Basically, condition variable equals "wake me when someone changes some data and exits its lock".

A better condition variable is that when there are multiple "conditions" in the data, the code making the change can target the wakeup better. But it isn't requires. And sometimes might not even matter much -- if in fact the ratio of locks to conditions is close to or equal to 1.

 

 - Jay

 


CC: m3devel at elegosoft.com
From: hosking at cs.purdue.edu
To: jay.krell at cornell.edu
Subject: Re: [M3devel] condition variables/win32
Date: Thu, 8 Oct 2009 10:09:31 -0400





In general, it is OK in M3 to associate multiple conditions with the same mutex.  But not vice versa.



On 8 Oct 2009, at 09:32, Jay K wrote:

condition variables/win32
 

So..one way I think about condition variables
is that you want to be woken when someone else
leaves the mutex that guards the data that you are dealing with.
You want to know when another thread modifies the data.
(If you have a reader/writer lock, you only want to be
woken when someone exits a write.)
 

Now, if you consider a producer/consumer queue.
There are two interesting occurences.
Transitions from empty to non-empty
and transitions from full to non-full (optionally,
if it is fixed size).
 

Consumers wait for empty to non-empty.
Consumers signal full to non-full.
Producers wait for full to non-full.
Producers signal non-empty to empty.
 

So, in this case, one mutex is likely used with with two condition variables.
 

But, what if we take a simplifying deoptimization and assume that a condition
variable is only ever associated with one mutex?
Anyone existing that mutex wakes up anyone waiting on any condition associated with it?
Like, a condition variable I think becomes stateless and everything is
about the mutex?
 
 
What is the downside?
 

Condition variables are allowed to have spurious wakeups.
This would "just" increase them. Too much?
 

So, therefore, what would be wrong with the following design?
 a mutex contains an event 
 and a number of waiters, zero or non-zero 
 if a mutex is exiting with a non-zero number of waiters, signal the event
 

To handle Signal vs. Broadcast
method 1:
 the number of waiters might be interlocked
 the woken would decrement it
 if it isn't zero, signal the event again
 

method 2:
 the number of waiters is both an integer and a semaphore
 and the lock exiter raises the semaphore by the the integer

 
method 3:
 it is not an auto-reset event and there is a count
  and when the count goes to 0, reset the event
 I think in this case you have to maintain a "wait generation" 
 so that new waiters don't prevent the count from ever hitting 0.
 I think this #3 is what Java might be doing, and is described here:
http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
 "3.3. The Generation Count Solution"

 
also:
http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
3.2. The SetEvent Solution
Evaluating the SetEvent Solution
Incorrectness -- 
 

Is that incorrect case really necessarily incorrect?
It seems unfair, since first waiter should be first woken, but..?

 
Am I missing something? A lot?
 

 - Jay

 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20091008/b99f08a8/attachment-0002.html>


More information about the M3devel mailing list