[<<][ecos][>>][..]
Fri Nov 4 11:47:53 EDT 2011

Condition Variables : cyg_cond_wait()

For a brief general explanation on condition variables, see
wikipedia[2].  The eCos implementation is quite standard.  See
docs[1].

In general it works like this.  A condition variable is protected by a
mutex to guarantee atomitcity of the variable access, and to guarantee
that a condition can be signalled inside a critical section.

// RECEIVER

LOCK()
WHILE(check_variable()) {
   WAIT()  // UNLOCK, WAIT, LOCK
}                        

// SENDER

LOCK()
change_variable()
SIGNAL()
UNLOCK()


The WAIT() operation needs to unlock the resource before blocking to
allow senders to perform a signal.

The WHILE loop is necessary because there might be more than one
thread waiting on a condition.  Even while receiving a signal (a
wake-up call) there is no guarantee about the *semantics* of the data
that is protected by the lock, because another thread might have woken
up before us and changed the data before we get woken up.

The sender side is simpler.  SIGNAL or BROADCAST wake up one or more
threads that are waiting for the condition variable.  It's not
necessary to do this inside the critical section, but often useful to
do so to atomically change the protected data before signalling.


[1] http://ecos.sourceware.org/docs-latest/ref/kernel-condition-variables.html
[2] http://en.wikipedia.org/wiki/Monitor_(synchronization)#Blocking_condition_variables




[Reply][About]
[<<][ecos][>>][..]