LTSLEEP(9)
π€ LTSLEEP(9) β process context sleep and wakeup π€
okay bestie, this is LTSLEEP(9) from the Kernel Developer's Manual, and it's giving kernel sleep/wakeup lore. no cap, we're gonna get through it together. π«Ά
πΈ NAME πΈ
ltsleep, mtsleep, tsleep, wakeup β process context sleep and wakeup. literally the whole group chat: sleeping in process context and waking up again. slay. π
β¨ SYNOPSIS β¨
the include and signatures, exactly as they are, bestie:
#include <sys/proc.h>
int
mtsleep(wchan_t ident, pri_t priority, const char *wmesg, int timo,
kmutex_t *mtx);
int
tsleep(wchan_t ident, pri_t priority, const char *wmesg, int timo);
void
wakeup(wchan_t ident);
π€ DESCRIPTION π€
π¨ obsolete alert, bestie π¨ The interfaces described in this manual page are obsolete and will be removed from a future version of the system.
The ltsleep() interface has been obsoleted and removed from the system. not me showing up to the function party after it got deleted π.
Please see the condvar(9), mutex(9), and rwlock(9) manual pages for information on kernel synchronisation primitives. those are the current main characters for this kind of thing. β¨
These functions implement voluntary context switching. tsleep() and mtsleep() are used throughout the kernel whenever processing in the current context can not continue for any of the following reasons:
- The current process needs to await the results of a pending I/O operation.
- The current process needs resources (e.g., memory) which are temporarily unavailable.
The function wakeup() is used to notify sleeping processes of possible changes to the condition that caused them to go to sleep. Typically, an awakened process will β after it has acquired a context again β retry the action that blocked its operation to see if the βblockingβ condition has cleared. lowkey iconic behavior, highkey the blueprint. π
the arguments, bestie
The tsleep() and mtsleep() functions take the following arguments:
identβ An identifier of the βwait channelβ representing the resource for which the current process needs to wait. This typically is the virtual address of some kernel data-structure related to the resource for which the process is contending. The same identifier must be used in a call towakeup()to get the process going again.identshould not beNULL. π«priorityβ The process priority to be used when the process is awakened and put on the queue of runnable processes. This mechanism is used to optimize βthroughputβ of processes executing in kernel mode. If the flagPCATCHis OR'ed intoprioritythe process checks for posted signals before and after sleeping.wmesgβ A pointer to a character string indicating the reason a process is sleeping. The kernel does not use the string, but makes it available (through the process structure fieldp_wmesg) for user level utilities such asps(1).timoβ If non-zero, the process will sleep for at mosttimo/hzseconds. If this amount of time elapses and nowakeup(ident)has occurred, and no signal (ifPCATCHwas set) was posted,tsleep()will returnEWOULDBLOCK.
The mtsleep() function takes an additional argument and flag:
mtxβ Amutex(9)representing the lock protecting the data-structures. On entrymtsleep()will release the lock and re-acquire the lock on return.priorityβ If the flagPNORELOCKis OR'ed intoprioritythenmtsleep()will not re-acquire the lock.
The wakeup() function will mark all processes which are currently sleeping on the identifier ident as runnable. Eventually, each of the processes will resume execution in the kernel context, causing a return from tsleep() or mtsleep().
β οΈ Note that processes returning from sleep should always re-evaluate the conditions that blocked them, since a call to wakeup() merely signals a possible change to the blocking conditions. fr fr, do the vibe check again. π
π RETURN VALUES π
tsleep()andmtsleep()return0if they return as a result of awakeup().- If
tsleep()andmtsleep()return as a result of a signal, the return value isERESTARTif the signal has theSA_RESTARTproperty (seesigaction(2)), andEINTRotherwise. - If
tsleep()andmtsleep()return because of a timeout, the return value isEWOULDBLOCK.
π¦ MIGRATING TO CONDVAR π¦
okay so the conversion from tsleep/wakeup into condvar(9) should not be done mechanically, i.e. βblindlyβ. Code logic should be understood before changing, and it may also need to be revisited for the change. Please also read the condvar(9) man page. this is a βread the room firstβ situation, bestie. π
The tsleep() and mtsleep(), and wakeup() pairs should generally be replaced by cv_wait(9) / cv_wait_sig(9) / cv_timedwait(9) / cv_timedwait_sig(9) and cv_signal(9) / cv_broadcast(9) pairs. The cv_wait*() variant to use can be determined from looking at the corresponding tsleep() usage.
There are two arguments of interest: timo and priority. The priority value may have OR'ed the flag PCATCH.
- The
PCATCHflag means that the blocking thread should be awoken on signal, and the sleep call should be replaced withcv_wait_sig(9). - The
timovalue, if it is not zero, indicates how long to sleep, and the sleep call should be replaced withcv_timedwait(9). - If both the
PCATCHflag and a non-zerotimovalue are specified, thencv_timedwait_sig(9)should be used.
A mutex(9) (interlock) must be held across cv_wait() and cv_broadcast() calls, in order to protect state. Most old code will require the addition of locking, whereas some will require amending to remove PNORELOCK. iykyk, locking is the whole plot. π«Ά
π SEE ALSO π
β HISTORY β
The sleep/wakeup process synchronization mechanism is very old. It appeared in a very early version of Unix. tsleep() appeared in 4.4BSD. ltsleep() appeared in NetBSD 1.5. history really said βwe've been here foreverβ πβ¨
xoxo π this page is from Debian, dated May 7, 2024, and its manual title is LTSLEEP(9). bye bestie π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 1996, 2002, 2007 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms