Motr
M0
|
Data Structures | |
struct | m0_cond |
Functions | |
M0_INTERNAL void | m0_cond_init (struct m0_cond *cond, struct m0_mutex *mutex) |
M0_INTERNAL void | m0_cond_fini (struct m0_cond *cond) |
M0_INTERNAL void | m0_cond_wait (struct m0_cond *cond) |
M0_INTERNAL bool | m0_cond_timedwait (struct m0_cond *cond, const m0_time_t abs_timeout) |
M0_INTERNAL void | m0_cond_signal (struct m0_cond *cond) |
M0_INTERNAL void | m0_cond_broadcast (struct m0_cond *cond) |
Condition variable is a widely known and convenient synchronization mechanism.
Notionally, a condition variable packages two things: a predicate ("a condition", hence the name) on computation state, e.g., "a free buffer is available", "an incoming request waits for processing" or "all worker threads have finished", and a mutex (m0_mutex) protecting changes to the state affecting the predicate.
There are two parts in using condition variable:
Note that one has to re-check the predicate after m0_cond_wait() returns, because it might, generally, be false if multiple threads are waiting for predicate change (in the above example, if there are multiple concurrent calls to buffer_get()). This introduces one of the nicer features of condition variables: de-coupling of producers and consumers.
Condition variables are more reliable and structured synchronization primitive than channels (m0_chan), because the lock, protecting the predicate is part of the interface and locking state can be checked. On the other hand, channels can be used with predicates protected by read-write locks, atomic variables, etc.—where condition variables are not applicable.
Very simple implementation of condition variables on top of waiting channels.
Self-explanatory.
M0_INTERNAL void m0_cond_broadcast | ( | struct m0_cond * | cond | ) |
M0_INTERNAL void m0_cond_fini | ( | struct m0_cond * | cond | ) |
M0_INTERNAL void m0_cond_signal | ( | struct m0_cond * | cond | ) |
This is the same as m0_cond_wait, except that it has a timeout value. If the time expires before event is pending, this function will return false.
abs_timeout | this is the time since Epoch (00:00:00, 1 January 1970). |
Definition at line 74 of file cond.c.
M0_INTERNAL void m0_cond_wait | ( | struct m0_cond * | cond | ) |
Atomically unlocks the mutex, waits on the condition variable and locks the mutex again before returning.
Definition at line 52 of file cond.c.