__CPU_SIMPLE_LOCK(9)
okay bestie, welcome to the __CPU_SIMPLE_LOCK(9) page β it's from the Kernel Developer's Manual, and it's all about simple spin locks. This is the group-chat recap where every detail stays. π
π NAME π
__cpu_simple_lock β simple spin locks. No cap.
β¨ SYNOPSIS β¨
Here's the shape of it, exactly:
#include <sys/lock.h>
void
__cpu_simple_lock_init(__cpu_simple_lock_t *lock);
#define __SIMPLELOCK_UNLOCKED ...
void
__cpu_simple_lock(__cpu_simple_lock_t *lock);
int
__cpu_simple_lock_try(__cpu_simple_lock *lock);
void
__cpu_simple_unlock(__cpu_simple_lock_t *lock);
int
__SIMPLELOCK_LOCKED_P(__cpu_simple_lock *lock);
/* obsolete and for ABI compat only -- do not use */
void
__cpu_simple_lock_set(__cpu_simple_Lock *lock);
void
__cpu_simple_lock_clear(__cpu_simple_lock *lock);
int
__SIMPLELOCK_UNLOCKED_P(__cpu_simple_lock *lock);
π¦ DESCRIPTION π¦
The __cpu_simple_lock functions give you a simple spin-lock facility for limited purposes that mutex(9) cannot serve β like inside the implementation of mutex(9) itself on platforms with limited atomic read/modify/write operations. It's giving "tiny specialized tool, not a main character mutex." π
__cpu_simple_lock is very limited, and the page is not subtle about it:
- It provides no debugging or diagnostic support through the
LOCKDEBUGoption. π - It does not synchronize between code on a CPU and interrupt handlers running on that CPU β you must use it with
spl(9)for any locks that may be taken in interrupt context; failing to do so will likely lead to hard-to-debug deadlock. π - It does not block preemption, so a thread holding a lock may be preempted, potentially requiring other callers to spin for long durations until the scheduler runs the holder again. Spin spin spin. π
- It does no exponential backoff to reduce memory traffic during contention.
Unless you know what you are doing, you should use mutex(9) instead. That's the vibe check. β
π INITIALIZATION π
The macro __SIMPLELOCK_UNLOCKED expands to an initializer for the type __cpu_simple_lock_t:
__cpu_simple_lock_t lock = __SIMPLELOCK_UNLOCKED;
A __cpu_simple_lock_t object can also be initialized with __cpu_simple_lock_init().
No actions are needed to destroy a __cpu_simple_lock_t object. Cleanup? None. Slay. β¨
π FUNCTIONS π
__cpu_simple_lock_init(lock)
Initialize lock for use with the other __cpu_simple_lock functions.
The caller is responsible for ensuring __cpu_simple_lock_init() happens before any use of the other functions. __cpu_simple_lock_init() implies no particular memory ordering on its own.
__cpu_simple_lock(lock)
Acquire lock, waiting until it is released if currently held.
Any memory operations preceding the previous __cpu_simple_unlock() call that released the lock happen before any memory operations after the next __cpu_simple_lock() call that acquires it.
__cpu_simple_lock_try(lock)
Try to acquire lock, without waiting if it is currently held. Return 1 if successful, 0 if not.
Any memory operations preceding the previous __cpu_simple_unlock() call that released the lock happen before any memory operations after the next successful __cpu_simple_lock_try() call that acquires it.
__cpu_simple_unlock(lock)
Release lock.
Any memory operations preceding __cpu_simple_unlock() happen before the next call to __cpu_simple_lock(), or the next successful call to __cpu_simple_lock_try(), that acquires lock.
__SIMPLELOCK_LOCKED_P(lock)
True if lock is currently locked, by anyone.
This is normally only used for diagnostic assertions, or for loops around __cpu_simple_lock_try() that also have higher-level functions like blocking interrupts and performing exponential backoff.
No memory ordering is implied.
π OBSOLETE FUNCTIONS π
The following functions abuse the __cpu_simple_lock_t type to store a boolean. They are used inside the pthread(3) library, and were included in the library ABI, so they can't be removed without breaking the pthread(3) ABI. Do not use these in new code (except __SIMPLELOCK_LOCKED_P()). Periodt. π
__cpu_simple_lock_set(lock)β Setlockto true.__cpu_simple_lock_clear(lock)β Setlockto false.__SIMPLELOCK_LOCKED_P(lock)β True ifflockis true.__SIMPLELOCK_UNLOCKED_P(lock)β True ifflockis false.
β CODE REFERENCES β
The __cpu_simple_lock functions are implemented in sys/arch/$ARCH/include/lock.h.
A machine-independent implementation, using compiler support for atomic and memory barrier builtins, is available in sys/sys/common_lock.h.
π SEE ALSO π
πΈ HISTORY πΈ
__cpu_simple_lock appeared a long time ago. Ancient lore. π«Ά
xoxo, this manual page is from Debian, dated October 25, 2024, and it's the __CPU_SIMPLE_LOCK(9) page. ππ
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2022 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