RWLOCK(9) | Kernel Developer's Manual | RWLOCK(9) |
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
#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);
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) locks, their usage pattern is different.
Reader/writer locks can be treated as mutexes (see
mutex(9)) with shared/exclusive semantics. Unlike
sx(9), 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.
rw_init
(struct
rwlock *rw, const char *name)rw_init_flags
(struct
rwlock *rw, const char *name,
int opts)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
RW_NOPROFILE
RW_NOWITNESS
RW_QUIET
RW_RECURSE
RW_NEW
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)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)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)rw_try_wlock
(struct
rwlock *rw)rw_runlock
(struct
rwlock *rw)rw_rlock
().rw_wunlock
(struct
rwlock *rw)rw_wlock
().rw_unlock
(struct
rwlock *rw)rw_rlock
() or an exclusive lock previously
acquired by rw_wlock
().rw_try_upgrade
(struct
rwlock *rw)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)rw_sleep
(void
*chan, struct rwlock *rw, int
priority, const char *wmesg, int
timo)rw_initialized
(const
struct rwlock *rw)rw_destroy
(struct rwlock
*rw)rw_init
(). The rw lock must
be unlocked.rw_wowned
(const
struct rwlock *rw)rw_assert
(const
struct rwlock *rw, int what)options INVARIANTS
and
options INVARIANT_SUPPORT
, the kernel will panic.
Currently the following base assertions are supported:
RA_LOCKED
RA_RLOCKED
RA_WLOCKED
RA_UNLOCKED
In addition, one of the following optional flags may be
specified with RA_LOCKED
,
RA_RLOCKED
, or
RA_WLOCKED
:
RA_RECURSED
RA_NOTRECURSED
These functions appeared in FreeBSD 7.0.
The rwlock
facility was written by
John Baldwin. This manual page was written by
Gleb Smirnoff.
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.
November 11, 2017 | Debian |