![]() |
![]() |
![]() |
Block a thread on a synchronization object
#include <sys/neutrino.h> int SyncCondvarWait( sync_t * sync, sync_t * mutex ); int SyncCondvarWait_r( sync_t * sync, sync_t * mutex );
libc
The SyncCondvarWait() and SyncCondvarWait_r() functions are identical except in the way they indicate errors. See the Returns section for details.
These kernel calls block the calling thread on the synchronization object sync. If more than one thread is blocked on the object they're queued in priority order.
The mutex passed as the second argument must be locked by the caller using SyncMutexLock() (or the POSIX pthread_mutex_lock() cover routine). The kernel releases the mutex lock in the kernel when it blocks the thread on sync.
The blocked thread can be unblocked by any one of the following four conditions:
Before returning from SyncCondvarWait(), mutex is reacquired. If mutex is locked, the thread enters into the STATE_MUTEX state waiting for mutex to be unlocked. At this point it's as though you had called SyncMutexLock(mutex).
Before returning from SyncCondvarWait(), mutex is reacquired. If mutex is locked, the thread enters into the STATE_MUTEX state waiting for mutex to be unlocked. At this point it's as though you had called SyncMutexLock(mutex).
In all cases, mutex is reacquired before the call returns. If the thread enters the STATE_MUTEX state, the rules governing SyncMutexLock() are in effect.
With condition variables, there is always a boolean predicate involving boolean shared variables associated with the condition wait that is true if the thread should proceed. Spurious wakeups may occur on timeout, signals and broadcast condition variable signals. Therefore, the predicate should always be reevaluated upon even a successful return. This is most easily accomplished with a while loop. For example:
SyncMutexLock(&mutex); while(some_condition) { SyncCondvarWait(&condvar, &mutex); } SyncMutexUnlock(&mutex);
The sync argument must have been initialized by a call to SyncTypeCreate() or have been statically initialized by the manifest PTHREAD_COND_INITIALIZER.
The only difference between these functions is the way they indicate errors:
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
pthread_cond_broadcast(), pthread_cond_signal(), pthread_cond_wait(), SyncCondvarSignal()
![]() |
![]() |
![]() |