RMLOCK(9)
πΈ RMLOCK(9) β kernel reader/writer locks that are literally built different πΈ
okay bestie, welcome to the RMLOCK(9) page from the Kernel Developer's Manual π
this is the read-mostly lock of your dreams β rmlock, plus all its little friends rm_init, rm_init_flags, rm_destroy, rm_rlock, rm_try_rlock, rm_wlock, rm_runlock, rm_wunlock, rm_wowned, rm_sleep, rm_assert, RM_SYSINIT, RM_SYSINIT_FLAGS, rms_init, rms_destroy, rms_rlock, rms_wlock, rms_runlock, and rms_wunlock. the vibe? a kernel reader/writer lock optimized for read-mostly access patterns β¨ like, it's giving main character energy for data that nobody ever writes to.
β¨ SYNOPSIS β¨
#include <sys/param.h>
#include <sys/lock.h>
#include <sys/rmlock.h>
void
rm_init(struct rmlock *rm, const char *name);
void
rm_init_flags(struct rmlock *rm, const char *name, int opts);
void
rm_destroy(struct rmlock *rm);
void
rm_rlock(struct rmlock *rm, struct rm_priotracker* tracker);
int
rm_try_rlock(struct rmlock *rm, struct rm_priotracker* tracker);
void
rm_wlock(struct rmlock *rm);
void
rm_runlock(struct rmlock *rm, struct rm_priotracker* tracker);
void
rm_wunlock(struct rmlock *rm);
int
rm_wowned(const struct rmlock *rm);
int
rm_sleep(void *wchan, struct rmlock *rm, int priority,
const char *wmesg, int timo);
options INVARIANTS
options INVARIANT_SUPPORT
void
rm_assert(struct rmlock *rm, int what);
#include <sys/kernel.h>
RM_SYSINIT(name, struct rmlock *rm, const char *desc);
RM_SYSINIT_FLAGS(name, struct rmlock *rm, const char *desc, int flags);
void
rms_init(struct rmslock *rms, const char *name);
void
rms_destroy(struct rmslock *rms);
void
rms_rlock(struct rmslock *rms);
void
rms_wlock(struct rmslock *rms);
void
rms_runlock(struct rmslock *rms);
void
rms_wunlock(struct rmslock *rms);
π¦ DESCRIPTION π¦
okay so read-mostly locks let multiple threads share access to your protected data, or let one single thread have exclusive access. the shared-access girlies are called readers (obvi, they only read the protected data) and the exclusive-access one is called a writer (she modifies the protected data). iconic division of labor π
read-mostly locks are designed to be efficient for locks that are almost exclusively used as reader locks, so use them for protecting data that rarely changes. and heads up: acquiring an exclusive lock after the lock has been locked for shared access? that's an expensive operation. like, expensive expensive π
normal read-mostly locks are similar to rwlock(9) locks and follow the same lock ordering rules as rwlock(9) locks. read-mostly locks have full priority propagation like mutexes. but unlike rwlock(9), read-mostly locks propagate priority to both readers and writers. how? through the rm_priotracker structure argument you hand to rm_rlock() and rm_runlock() π«Ά readers can recurse if the lock was initialized with the RM_RECURSE option β however, writers are never allowed to recurse. periodt.
sleeping for writers can be allowed by passing RM_SLEEPABLE to rm_init_flags(). that changes lock ordering rules to be the same as for sx(9) locks. they do not propagate priority to writers, but they do propagate priority to readers. note that readers are not permitted to sleep regardless of the flag β
sleepable read-mostly locks (created with rms_init()) allow sleeping for both readers and writers, but don't do priority propagation for either. they follow sx(9) lock ordering. slay.
π Macros and Functions π
rm_init(struct rmlock *rm, const char *name)β initialize the read-mostly lock rm. the name description is used solely for debugging purposes. this function must be called before any other operations on the lock, no cap.rm_init_flags(struct rmlock *rm, const char *name, int opts)β similar torm_init(), initializes the read-mostly lock rm with a set of optional flags. the opts argument contains one or more of these flags:RM_NOWITNESSβ instructwitness(4)to ignore this lock.RM_RECURSEβ allow threads to recursively acquire shared locks for rm.RM_SLEEPABLEβ create a sleepable read-mostly lock.RM_NEWβ if the kernel has been compiled with optionINVARIANTS,rm_init_flags()will assert that the rm has not been initialized multiple times without intervening calls torm_destroy()unless this option is specified.RM_DUPOKβwitness(4)should not log messages about duplicate locks being acquired.
rm_rlock(struct rmlock *rm, struct rm_priotracker* tracker)β lock rm as a reader, using tracker to track read owners of a lock for priority propagation. this data structure is only used internally by rmlock and must persist untilrm_runlock()has been called. it can be allocated on the stack since readers cannot sleep π§ if any thread holds this lock exclusively, the current thread blocks, and its priority is propagated to the exclusive holder. if the lock was initialized with theRM_RECURSEoption,rm_rlock()can be called when the current thread has already acquired reader access on rm.rm_try_rlock(struct rmlock *rm, struct rm_priotracker* tracker)β try to lock rm as a reader.rm_try_rlock()returns0if the lock cannot be acquired immediately; otherwise the lock will be acquired and a non-zero value will be returned. note thatrm_try_rlock()may fail even while the lock is not currently held by a writer π if the lock was initialized with theRM_RECURSEoption,rm_try_rlock()will succeed if the current thread has already acquired reader access.rm_wlock(struct rmlock *rm)β lock rm as a writer. if there are any shared owners of the lock, the current thread blocks. andrm_wlock()cannot be called recursively π«rm_runlock(struct rmlock *rm, struct rm_priotracker* tracker)β releases a shared lock previously acquired byrm_rlock(). the tracker argument must match the tracker argument used for acquiring the shared lock.rm_wunlock(struct rmlock *rm)β releases an exclusive lock previously acquired byrm_wlock().rm_destroy(struct rmlock *rm)β destroys a lock previously initialized withrm_init(). the rm lock must be unlocked β οΈrm_wowned(const struct rmlock *rm)β returns a non-zero value if the current thread owns an exclusive lock on rm.rm_sleep(void *wchan, struct rmlock *rm, int priority, const char *wmesg, int timo)β atomically releases rm while waiting for an event. the rm lock must be exclusively locked. for more details on the parameters to this function, seesleep(9).rm_assert(struct rmlock *rm, int what)β asserts that the rm lock is in the state specified by what. if the assertions are not true and the kernel is compiled with optionsINVARIANTSand optionsINVARIANT_SUPPORT, the kernel will panic π currently the following base assertions are supported:RA_LOCKEDβ assert that current thread holds either a shared or exclusive lock of rm.RA_RLOCKEDβ assert that current thread holds a shared lock of rm.RA_WLOCKEDβ assert that current thread holds an exclusive lock of rm.RA_UNLOCKEDβ assert that current thread holds neither a shared nor exclusive lock of rm.
in addition, one of the following optional flags may be specified with RA_LOCKED, RA_RLOCKED, or RA_WLOCKED:
RA_RECURSEDβ assert that the current thread holds a recursive lock of rm.RA_NOTRECURSEDβ assert that the current thread does not hold a recursive lock of rm.
rms_init(struct rmslock *rms, const char *name)β initialize the sleepable read-mostly lock rms. the name description is used as the wmesg parameter to themsleep(9)routine. this function must be called before any other operations on the lock.rms_rlock(struct rmlock *rm)β lock rms as a reader. if any thread holds this lock exclusively, the current thread blocks.rms_wlock(struct rmslock *rms)β lock rms as a writer. if the lock is already taken, the current thread blocks.rms_wlock()cannot be called recursively π«rms_runlock(struct rmslock *rms)β releases a shared lock previously acquired byrms_rlock().rms_wunlock(struct rmslock *rms)β releases an exclusive lock previously acquired byrms_wlock().rms_destroy(struct rmslock *rms)β destroys a lock previously initialized withrms_init(). the rms lock must be unlocked β οΈ
bestie note:RM_SYSINIT(name, struct rmlock *rm, const char *desc)andRM_SYSINIT_FLAGS(name, struct rmlock *rm, const char *desc, int flags)come from<sys/kernel.h>β they're in the synopsis up there, go look, i'm not retyping the whole thing π
π₯ SEE ALSO π₯
locking(9), mutex(9), panic(9), rwlock(9), sema(9), sleep(9), sx(9)
the whole squad, fr fr π
π HISTORY π
these functions appeared in FreeBSD 7.0. a whole era π
π AUTHORS π
the rmlock facility was written by Stephan Uphoff. this manual page was written by Gleb Smirnoff for rwlock and modified to reflect rmlock by Stephan Uphoff. kings, both of them π«Ά
π BUGS π
- the rmlock implementation is currently not optimized for single processor systems. ngl, awkward.
rm_try_rlock()can fail transiently even when there is no writer, while another reader updates the state on the local CPU. yeah. that's a thing. π- the rmlock implementation uses a single per CPU list shared by all rmlocks in the system. if rmlocks become popular, hashing to multiple per CPU queues may be needed to speed up the writer lock process.
okay that's the whole page, you're officially an rmlock girlie now π
and that's a wrap on this one, bestie β brought to you by Debian, dated April 12, 2021 β¨π¦ go forth and lock responsibly π
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org>
- Copyright (c) 2006 Gleb Smirnoff <glebius@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms