p .Cd "options DIAGNOSTIC" .Cd "options LOCKDEBUG" .Sh DESCRIPTION Condition variables (CVs) are used in the kernel to synchronize access to resources that are limited (for example, memory) and to wait for pending I/O operations to complete.
p The .Vt kcondvar_t type provides storage for the CV object. This should be treated as an opaque object and not examined directly by consumers. .Sh OPTIONS l -tag -width abcd t Cd "options DIAGNOSTIC"
p Kernels compiled with the .Dv DIAGNOSTIC option perform basic sanity checks on CV operations. t Cd "options LOCKDEBUG"
p Kernels compiled with the .Dv LOCKDEBUG option perform potentially CPU intensive sanity checks on CV operations. .El .Sh FUNCTIONS l -tag -width abcd t Fn cv_init "cv" "wmesg"
p Initialize a CV for use. No other operations can be performed on the CV until it has been initialized.
p The .Fa wmesg argument specifies a string of no more than 8 characters that describes the resource or condition associated with the CV. The kernel does not use this argument directly but makes it available for utilities such as .Xr ps 1 to display. t Fn cv_destroy "cv"
p Release resources used by a CV. If there could be waiters, they should be awoken first with .Fn cv_broadcast . The CV must not be used afterwards. t Fn cv_wait "cv" "mtx"
p Cause the current LWP to wait non-interruptably for access to a resource, or for an I/O operation to complete. The LWP will resume execution when awoken by another thread using .Fn cv_signal or .Fn cv_broadcast .
p .Fa mtx specifies a kernel mutex to be used as an interlock, and must be held by the calling LWP on entry to .Fn cv_wait . It will be released once the LWP has prepared to sleep, and will be reacquired before .Fn cv_wait returns.
p A small window exists between testing for availability of a resource and waiting for the resource with .Fn cv_wait , in which the resource may become available again. The interlock is used to guarantee that the resource will not be signalled as available until the calling LWP has begun to wait for it.
p Non-interruptable waits have the potential to deadlock the system, and so must be kept short (typically, under one second).
p .Fn cv_wait is typically used within a loop or restartable code sequence, because it may awaken spuriously. The calling LWP should re-check the condition that caused the wait. If necessary, the calling LWP may call .Fn cv_wait again to continue waiting. t Fn cv_wait_sig "cv" "mtx"
p As per .Fn cv_wait , but causes the current LWP to wait interruptably. If the LWP receives a signal, or is interrupted by another condition such as its containing process exiting, the wait is ended early and an error code returned.
p If .Fn cv_wait_sig returns as a result of a signal, the return value is .Er ERESTART if the signal has the .Dv SA_RESTART property. If awoken normally, the value is zero, and .Er EINTR under all other conditions. t Fn cv_timedwait "cv" "mtx" "ticks"
p As per .Fn cv_wait , but will return early if a timeout specified by the .Fa ticks argument expires.
p .Fa ticks is an architecture and system dependent value related to the number of clock interrupts per second. See .Xr hz 9 for details. The .Xr mstohz 9 macro can be used to convert a timeout expressed in milliseconds to one suitable for .Fn cv_timedwait . If the .Fa ticks argument is zero, .Fn cv_timedwait behaves exactly like .Fn cv_wait .
p If the timeout expires before the LWP is awoken, the return value is .Er EWOULDBLOCK . If awoken normally, the return value is zero. t Fn cv_timedwait_sig "cv" "mtx" "ticks"
p As per .Fn cv_wait_sig , but also accepts a timeout value and will return .Er EWOULDBLOCK if the timeout expires. t Fn cv_timedwaitbt "cv" "mtx" "bt" "epsilon" t Fn cv_timedwaitbt_sig "cv" "mtx" "bt" "epsilon"
p As per .Fn cv_wait and .Fn cv_wait_sig , but will return early if the duration .Fa bt has elapsed, immediately if .Fa bt is zero. On return, .Fn cv_timedwaitbt and .Fn cv_timedwaitbt_sig subtract the time elapsed from .Fa bt in place, or set it to zero if there is no time remaining.
p Note that .Fn cv_timedwaitbt and .Fn cv_timedwaitbt_sig may return zero indicating success, rather than .Er EWOULDBLOCK , even if they set the timeout to zero; this means that the caller must re-check the condition in order to avoid potentially losing a .Fn cv_signal , but the .Em next wait will time out immediately.
p The hint .Fa epsilon , which can be .Dv DEFAULT_TIMEOUT_EPSILON if in doubt, requests that the wakeup not be delayed more than .Fa bt Li "+" Fa epsilon , so that the system can coalesce multiple wakeups within their respective epsilons into a single high-resolution clock interrupt or choose to use cheaper low-resolution clock interrupts instead.
p However, the system is still limited by its best clock interrupt resolution and by scheduling competition, which may delay the wakeup by more than .Fa bt Li "+" Fa epsilon . t Fn cv_signal "cv"
p Awaken one LWP waiting on the specified condition variable. Where there are waiters sleeping non-interruptaby, more than one LWP may be awoken. This can be used to avoid a "thundering herd" problem, where a large number of LWPs are awoken following an event, but only one LWP can process the event.
p The mutex passed to the wait function
o Fa mtx Pc should be held or have been released immediately before .Fn cv_signal is called.
p (Note that .Fn cv_signal is erroneously named in that it does not send a signal in the traditional sense to LWPs waiting on a CV.) t Fn cv_broadcast "cv"
p Awaken all LWPs waiting on the specified condition variable.
p As with .Fn cv_signal , the mutex passed to the wait function
o Fa mtx Pc should be held or have been released immediately before .Fn cv_broadcast is called. t Fn cv_has_waiters "cv"
p Return .Dv true if one or more LWPs are waiting on the specified condition variable.
p .Fn cv_has_waiters cannot test reliably for interruptable waits. It should only be used to test for non-interruptable waits made using .Fn cv_wait .
p .Fn cv_has_waiters should only be used when making diagnostic assertions, and must be called while holding the interlocking mutex passed to .Fn cv_wait . .El .Sh EXAMPLES Consuming a resource: d -literal /* * Lock the resource. Its mutex will also serve as the * interlock. */ mutex_enter(&res->mutex); /* * Wait for the resource to become available. Timeout after * five seconds. If the resource is not available within the * allotted time, return an error. */ struct bintime timeout = { .sec = 5, .frac = 0 }; while (res->state == BUSY) { error = cv_timedwaitbt(&res->condvar, &res->mutex, &timeout, DEFAULT_TIMEOUT_EPSILON); if (error) { KASSERT(error == EWOULDBLOCK); mutex_exit(&res->mutex); return ETIMEDOUT; } } /* * It's now available to us. Take ownership of the * resource, and consume it. */ res->state = BUSY; mutex_exit(&res->mutex); consume(res); .Ed
p Releasing a resource for the next consumer to use: d -literal mutex_enter(&res->mutex); res->state = IDLE; cv_signal(&res->condvar); mutex_exit(&res->mutex); .Ed .Sh CODE REFERENCES The core of the CV implementation is in
a sys/kern/kern_condvar.c .
p The header file
a sys/sys/condvar.h describes the public interface. .Sh SEE ALSO .Xr sigaction 2 , .Xr membar_ops 3 , .Xr errno 9 , .Xr mstohz 9 , .Xr mutex 9 , .Xr rwlock 9
p .Rs .%A Jim Mauro .%A Richard McDougall .%T Solaris Internals: Core Kernel Architecture .%I Prentice Hall .%D 2001 .%O ISBN 0-13-022496-0 .Re .Sh HISTORY The CV primitives first appeared in .Nx 5.0 . The .Fn cv_timedwaitbt and .Fn cv_timedwaitbt_sig primitives first appeared in .Nx 9.0 .