SEMA(9)
β¨ SEMA(9) β kernel counting semaphore β¨
okay bestie, we're in the Kernel Developer's Manual today and the topic is sema β the kernel's counting semaphore, plus its whole little friend group: sema_init, sema_destroy, sema_post, sema_wait, sema_timedwait, sema_trywait, and sema_value. π
πΈ NAME πΈ
sema, sema_init, sema_destroy, sema_post, sema_wait, sema_timedwait, sema_trywait, sema_value β kernel counting semaphore. yes queen, that's the whole roster. π
β¨ SYNOPSIS β¨
#include <sys/types.h>
#include <sys/lock.h>
#include <sys/sema.h>
void
sema_init(struct sema *sema, int value, const char *description);
void
sema_destroy(struct sema *sema);
void
sema_post(struct sema *sema);
void
sema_wait(struct sema *sema);
int
sema_timedwait(struct sema *sema, int timo);
int
sema_trywait(struct sema *sema);
int
sema_value(struct sema *sema);
like. look at that. so organized. π
π¦ DESCRIPTION π¦
okay so counting semaphores are a mechanism for synchronizing access to a pool of resources. and here's the thing that makes them β¨differentβ¨ from mutexes: semaphores do not have the concept of an owner. so they're also useful in situations where one thread needs to acquire a resource and <em>another</em> thread needs to release it. not me being flexible and unbothered. π
every semaphore has an integer value attached to it. posting (incrementing) always succeeds β no notes, iconic behavior. but waiting (decrementing) can only successfully complete if the resulting value of the semaphore is greater than or equal to zero. so if it'd go negative? nope. denied. π
lowkey important: semaphores should not be used where mutexes and condition variables will suffice. semaphores are a <em>more complex</em> synchronization mechanism than mutexes and condition variables, and they are <em>not as efficient</em>. bestie I'm just the messenger. π
here's the whole lifecycle, step by step:
- semaphores are created with
sema_init(), wheresemais a pointer to space for astruct sema,valueis the initial value of the semaphore, anddescriptionis a pointer to a null-terminated character string that describes the semaphore. - semaphores are destroyed with
sema_destroy(). - a semaphore is posted (incremented) with
sema_post(). - a semaphore is waited on (decremented) with
sema_wait(),sema_timedwait(), orsema_trywait(). thetimoargument tosema_timedwait()specifies the minimum time in ticks to wait before returning with failure. sema_value()is used to read the current value of the semaphore.
π RETURN VALUES π
the sema_value() function returns the current value of the semaphore. simple. clean. ate. β¨
if decrementing the semaphore would result in its value being negative, sema_trywait() returns 0 to indicate failure. otherwise, a non-zero value is returned to indicate success. (yes bestie, that's backwards from what your gut says. read it again. it's fine. π«Ά)
the sema_timedwait() function returns 0 if waiting on the semaphore succeeded; otherwise a non-zero error code is returned.
π ERRORS π
the sema_timedwait() function will fail if:
[EWOULDBLOCK]β Timeout expired.
that's it. that's the list. one (1) whole error. she's low maintenance. π
π SEE ALSO π
condvar(9), locking(9), mtx_pool(9), mutex(9), rwlock(9), sx(9)
go read those too, they're all in the same friend group fr fr π§
and that's the whole page bestie!! this one came from <strong>Debian</strong>, dated <strong>February 1, 2006</strong> β yes, she's vintage, she's classic, she's still out here synchronizing your kernel. periodt. πβ¨π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (C) 2001 Jason Evans <jasone@FreeBSD.org>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms