---
name: "rwlock"
section: "9"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/RW_SYSINIT"
---

# rwlock(9)

kernel reader/writer lock

Part of [RW\_SYSINIT(9)](/RW_SYSINIT/9.md).

## NAME

**rwlock**, **rw\_init**, **rw\_init\_flags**, **rw\_destroy**, **rw\_rlock**, **rw\_wlock**, **rw\_runlock**, **rw\_wunlock**, **rw\_unlock**, **rw\_try\_rlock**, **rw\_try\_upgrade**, **rw\_try\_wlock**, **rw\_downgrade**, **rw\_sleep**, **rw\_initialized**, **rw\_wowned**, **rw\_assert**, **RW\_SYSINIT**, **RW\_SYSINIT\_FLAGS** – kernel reader/writer lock

## SYNOPSIS

**#include \<sys/param.h\>**

**#include \<sys/lock.h\>**

**#include \<sys/rwlock.h\>**

*void*\

**rw\_init**(*struct rwlock \*rw*, *const char \*name*);

*void*\

**rw\_init\_flags**(*struct rwlock \*rw*, *const char \*name*, *int opts*);

*void*\

**rw\_destroy**(*struct rwlock \*rw*);

*void*\

**rw\_rlock**(*struct rwlock \*rw*);

*void*\

**rw\_wlock**(*struct rwlock \*rw*);

*int*\

**rw\_try\_rlock**(*struct rwlock \*rw*);

*int*\

**rw\_try\_wlock**(*struct rwlock \*rw*);

*void*\

**rw\_runlock**(*struct rwlock \*rw*);

*void*\

**rw\_wunlock**(*struct rwlock \*rw*);

*void*\

**rw\_unlock**(*struct rwlock \*rw*);

*int*\

**rw\_try\_upgrade**(*struct rwlock \*rw*);

*void*\

**rw\_downgrade**(*struct rwlock \*rw*);

*int*\

**rw\_sleep**(*void \*chan*, *struct rwlock \*rw*, *int priority*, *const char \*wmesg*, *int timo*);

*int*\

**rw\_initialized**(*const struct rwlock \*rw*);

*int*\

**rw\_wowned**(*const struct rwlock \*rw*);

**options INVARIANTS** **options INVARIANT\_SUPPORT**

*void*\

**rw\_assert**(*const struct rwlock \*rw*, *int what*);

**#include \<sys/kernel.h\>**

**RW\_SYSINIT**(*name*, *struct rwlock \*rw*, *const char \*desc*);

**RW\_SYSINIT\_FLAGS**(*name*, *struct rwlock \*rw*, *const char \*desc*, *int flags*);

## DESCRIPTION

Reader/writer locks allow shared access to protected data by multiple threads, or exclusive access by a single thread. The threads with shared access are known as *readers* since they only read the protected data. A thread with exclusive access is known as a *writer* since it can modify protected data.

Although reader/writer locks look very similar to [sx(9)](/sx/9.md) locks, their usage pattern is different. Reader/writer locks can be treated as mutexes (see [mutex(9)](/mutex/9.md)) with shared/exclusive semantics. Unlike [sx(9)](/sx/9.md), an **rwlock** can be locked while holding a non-spin mutex, and an **rwlock** cannot be held while sleeping. The **rwlock** locks have priority propagation like mutexes, but priority can be propagated only to writers. This limitation comes from the fact that readers are anonymous. Another important property is that readers can always recurse, and exclusive locks can be made recursive selectively.

### Macros and Functions

- **rw\_init**(*struct rwlock \*rw*, *const char \*name*)

  Initialize structure located at *rw* as reader/writer lock, described by name *name*. The description is used solely for debugging purposes. This function must be called before any other operations on the lock.

- **rw\_init\_flags**(*struct rwlock \*rw*, *const char \*name*, *int opts*)

  Initialize the rw lock just like the **rw\_init**() function, but specifying a set of optional flags to alter the behaviour of *rw*, through the *opts* argument. It contains one or more of the following flags:

  - RW\_DUPOK

    Witness should not log messages about duplicate locks being acquired.

  - RW\_NOPROFILE

    Do not profile this lock.

  - RW\_NOWITNESS

    Instruct [witness(4)](/witness/4.md) to ignore this lock.

  - RW\_QUIET

    Do not log any operations for this lock via [ktr(4)](/ktr/4.md).

  - RW\_RECURSE

    Allow threads to recursively acquire exclusive locks for *rw*.

  - RW\_NEW

    If the kernel has been compiled with **option INVARIANTS**, **rw\_init\_flags**() will assert that the *rw* has not been initialized multiple times without intervening calls to **rw\_destroy**() unless this option is specified.

- **rw\_rlock**(*struct rwlock \*rw*)

  Lock *rw* as a reader. If any thread holds this lock exclusively, the current thread blocks, and its priority is propagated to the exclusive holder. The **rw\_rlock**() function can be called when the thread has already acquired reader access on *rw*. This is called “recursing on a lock”.

- **rw\_wlock**(*struct rwlock \*rw*)

  Lock *rw* as a writer. If there are any shared owners of the lock, the current thread blocks. The **rw\_wlock**() function can be called recursively only if *rw* has been initialized with the RW\_RECURSE option enabled.

- **rw\_try\_rlock**(*struct rwlock \*rw*)

  Try to lock *rw* as a reader. This function will return true if the operation succeeds, otherwise 0 will be returned.

- **rw\_try\_wlock**(*struct rwlock \*rw*)

  Try to lock *rw* as a writer. This function will return true if the operation succeeds, otherwise 0 will be returned.

- **rw\_runlock**(*struct rwlock \*rw*)

  This function releases a shared lock previously acquired by **rw\_rlock**().

- **rw\_wunlock**(*struct rwlock \*rw*)

  This function releases an exclusive lock previously acquired by **rw\_wlock**().

- **rw\_unlock**(*struct rwlock \*rw*)

  This function releases a shared lock previously acquired by **rw\_rlock**() or an exclusive lock previously acquired by **rw\_wlock**().

- **rw\_try\_upgrade**(*struct rwlock \*rw*)

  Attempt to upgrade a single shared lock to an exclusive lock. The current thread must hold a shared lock of *rw*. This will only succeed if the current thread holds the only shared lock on *rw*, and it only holds a single shared lock. If the attempt succeeds **rw\_try\_upgrade**() will return a non-zero value, and the current thread will hold an exclusive lock. If the attempt fails **rw\_try\_upgrade**() will return zero, and the current thread will still hold a shared lock.

- **rw\_downgrade**(*struct rwlock \*rw*)

  Convert an exclusive lock into a single shared lock. The current thread must hold an exclusive lock of *rw*.

- **rw\_sleep**(*void \*chan*, *struct rwlock \*rw*, *int priority*, *const char \*wmesg*, *int timo*)

  Atomically release *rw* while waiting for an event. For more details on the parameters to this function, see [sleep(9)](/sleep/9.md).

- **rw\_initialized**(*const struct rwlock \*rw*)

  This function returns non-zero if *rw* has been initialized, and zero otherwise.

- **rw\_destroy**(*struct rwlock \*rw*)

  This functions destroys a lock previously initialized with **rw\_init**(). The *rw* lock must be unlocked.

- **rw\_wowned**(*const struct rwlock \*rw*)

  This function returns a non-zero value if the current thread owns an exclusive lock on *rw*.

- **rw\_assert**(*const struct rwlock \*rw*, *int what*)

  This function allows assertions specified in *what* to be made about *rw*. If the assertions are not true and the kernel is compiled with **options INVARIANTS** and **options INVARIANT\_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 *rw*.

  - RA\_RLOCKED

    Assert that current thread holds a shared lock of *rw*.

  - RA\_WLOCKED

    Assert that current thread holds an exclusive lock of *rw*.

  - RA\_UNLOCKED

    Assert that current thread holds neither a shared nor exclusive lock of *rw*.

    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 *rw*.

    - RA\_NOTRECURSED

      Assert that the current thread does not hold a recursive lock of *rw*.

## SEE ALSO

[locking(9)](/locking/9.md), [mutex(9)](/mutex/9.md), [panic(9)](/panic/9.md), [sema(9)](/sema/9.md), [sx(9)](/sx/9.md)

## HISTORY

These functions appeared in FreeBSD 7.0.

## AUTHORS

The **rwlock** facility was written by John Baldwin. This manual page was written by Gleb Smirnoff.

## BUGS

A kernel without WITNESS cannot assert whether the current thread does or does not hold a read lock. RA\_LOCKED and RA\_RLOCKED can only assert that *any* thread holds a read lock. They cannot ensure that the current thread holds a read lock. Further, RA\_UNLOCKED can only assert that the current thread does not hold a write lock.

Reader/writer is a bit of an awkward name. An **rwlock** can also be called a “Robert Watson” lock if desired.
