rwlock, rw_init,
- Provided by: freebsd-manpages (Version: 9.2+1-1)
- Report a bug
#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(struct
rwlock *rw);
int
rw_wowned(struct
rwlock *rw);
options INVARIANTS
options INVARIANT_SUPPORT
void
rw_assert(struct
rwlock *rw, int
what);
#include
<sys/kernel.h>
RW_SYSINIT(name,
struct rwlock *rw,
const char *desc);
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 an
exclusive holder. This limitation comes from the fact that shared owners are
anonymous. Another important property is that shared holders of
rwlock can 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_DUPOKRW_NOPROFILERW_NOWITNESSRW_QUIETRW_RECURSErw_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(struct
rwlock *rw)rw_destroy(struct
rwlock *rw)rw_init(). The rw lock must
be unlocked.rw_wowned(struct
rwlock *rw)rw_assert(struct
rwlock *rw, int what)options INVARIANTS and
options INVARIANT_SUPPORT, the kernel will panic.
Currently the following base assertions are supported:
RA_LOCKEDRA_RLOCKEDRA_WLOCKEDRA_UNLOCKEDIn addition, one of the following optional flags may be
specified with RA_LOCKED,
RA_RLOCKED, or
RA_WLOCKED:
RA_RECURSEDRA_NOTRECURSEDThese functions appeared in FreeBSD 7.0.
The rwlock facility was written by
John Baldwin. This manual page was written by
Gleb Smirnoff.
If WITNESS is not included in the kernel,
then it is impossible to assert that the current thread does or does not
hold a read lock. In the non-WITNESS case, the
RA_LOCKED and RA_RLOCKED
assertions merely check that some thread holds a read lock.
Reader/writer is a bit of an awkward name. An
rwlock can also be called a “Robert
Watson” lock if desired.