The FreeBSD kernel provides the following 11 functions for working with condition variables:
#include <sys/param.h> #include <sys/proc.h> #include <sys/condvar.h> void cv_init(struct cv *cvp, const char *d); const char * cv_wmesg(struct cv *cvp); void cv_wait(struct cv *cvp, lock); void cv_wait_unlock(struct cv *cvp, lock); int cv_wait_sig(struct cv *cvp, lock); int cv_timedwait(struct cv *cvp, lock, int timo); int cv_timedwait_sig(struct cv *cvp, lock, int timo); void cv_signal(struct cv *cvp); void cv_broadcast(struct cv *cvp); void cv_broadcastpri(struct cv *cvp, int pri); void cv_destroy(struct cv *cvp);
The cv_init
function initializes the condition variable cvp
. The d
argument describes cvp
.
The cv_wmesg
function gets the description of cvp
. This function is primarily used in error reporting.
Threads sleep on cvp
by calling cv_wait
. The lock
argument demands a sleep mutex, sx lock
, or rw
lock. Threads must hold lock before calling cv_wait
. Threads must not sleep on cvp
with lock
held recursively.
The cv_wait_unlock
function is a variant of cv_wait
. When threads wake up from sleeping on cvp
, they forgo reacquiring lock
.
The cv_wait_sig
function is identical to cv_wait
except that when the caller is asleep it can be woken up by signals. If this occurs, the error code EINTR
or ERESTART
is returned.
Normally, threads sleeping on condition variables cannot be woken up early.
The cv_timedwait
function is identical to cv_wait
except that the caller sleeps at most timo
/ hz
seconds. If the sleep times out, the error code EWOULDBLOCK
is returned.
The cv_timedwait_sig
function is like cv_wait_sig
and cv_timedwait
. The caller can be woken up by signals and sleeps at most timo
/ hz
seconds.
Threads wake up one thread sleeping on cvp
by calling cv_signal
, and they wake up every thread sleeping on cvp
by calling cv_broadcast
.
The cv_broadcastpri
function is identical to cv_broadcast
except that all threads woken up have their priority raised to pri
. Threads with a priority higher than pri
do not have their priority lowered.
The cv_destroy
function destroys the condition variable cvp
.
We’ll walk through an example that uses condition variables in Chapter 5.
3.135.183.1