OpenBSD manual page server

Manual Page Search Parameters

RWLOCK(9) Kernel Developer's Manual RWLOCK(9)

rwlock, rw_init, rw_init_flags, rw_enter, rw_exit, rw_enter_read, rw_enter_write, rw_exit_read, rw_exit_write, rw_assert_wrlock, rw_assert_rdlock, rw_assert_anylock, rw_assert_unlocked, rw_status, RWLOCK_INITIALIZER, rrw_init, rrw_init_flags, rrw_enter, rrw_exit, rrw_statusinterface to read/write locks

#include <sys/rwlock.h>

rw_init(struct rwlock *rwl, const char *name);

rw_init_flags(struct rwlock *rwl, const char *name, int flags);

rw_enter(struct rwlock *rwl, int flags);

rw_exit(struct rwlock *rwl);

rw_enter_read(struct rwlock *rwl);

rw_enter_write(struct rwlock *rwl);

rw_exit_read(struct rwlock *rwl);

rw_exit_write(struct rwlock *rwl);

rw_assert_wrlock(struct rwlock *rwl);

rw_assert_rdlock(struct rwlock *rwl);

rw_assert_anylock(struct rwlock *rwl);

rw_assert_unlocked(struct rwlock *rwl);

rw_status(struct rwlock *rwl);

RWLOCK_INITIALIZER(const char *name);

rrw_init(struct rrwlock *rrwl, const char *name);

rrw_init_flags(struct rrwlock *rrwl, const char *name, int flags);

rrw_enter(struct rrwlock *rrwl, int flags);

rrw_exit(struct rrwlock *rrwl);

rrw_status(struct rrwlock *rrwl);

The rwlock set of functions provides a multiple-reader, single-writer locking mechanism to ensure mutual exclusion between different threads.

Read locks can be acquired while the write lock is not held, and may coexist in distinct threads at any time. A write lock, however, can only be acquired when there are no read locks held, granting exclusive access to a single thread.

The () function is used to initiate the lock pointed to by rwl. The name argument specifies the name of the lock, which is used as the wait message if the thread needs to sleep.

The () macro is similar to rw_init(), but it additionally accepts a bitwise OR of the following flags:

Prevents witness(4) from logging when a thread acquires more than one lock of this lock type.
Make witness(4) ignore lock order issues between this lock type and any other lock type tagged with the RWL_IS_VNODE flag.
Instructs witness(4) to ignore this lock.

The () function acquires a lock. The flags argument specifies what kind of lock should be obtained and also modifies the operation. The possible flags are:

Acquire a shared lock.
Acquire an exclusive lock.
Safely release an exclusive lock and acquire a shared lock without letting other exclusive locks in between.
When waiting for a lock, allow signals to interrupt the sleep.
Do not wait for busy locks, fail with EBUSY instead.
Wait for busy locks, but do not obtain them, fail with EAGAIN instead.
Prevents witness(4), for just this (), from logging when this thread already has a lock of this lock type.

The () function is used to release a held lock.

The () function acquires a read lock, sleeping if necessary.

The () function acquires a write lock, sleeping if necessary.

The () function releases a read lock.

The () function releases a write lock.

The (), (), (), and () functions check the status rwl, panicking if it is not write-, read-, any-, or unlocked, respectively.

rw_status returns the current state of the lock.

A lock declaration may be initialised with the () macro. The name argument specifies the name of the lock, which is used as the wait message if the thread needs to sleep.

The rrwlock functions support recursive write locking by the same process. They otherwise behave the same as their rwlock counterparts.

rw_init(), rw_init_flags(), rrw_init() and rrw_init_flags() can be called during autoconf, from process context, or from interrupt context.

All other functions can be called during autoconf or from process context.

rw_enter and rrw_enter return 0 on success, or an errno(2) style value on failure.

rw_status and rrw_status return the state of the lock:

Lock is write locked by the calling thread.
Lock is write locked by a different thread.
Lock is read locked. The current thread may be one of the threads that has it locked.
Lock is not locked.

witness(4), mutex(9), spl(9)

The rwlock functions first appeared in OpenBSD 3.5.

The rwlock functions were written by Artur Grabowski <>.

February 25, 2019 OpenBSD-6.5