Shared/Exclusive Lock Management Routines

The FreeBSD kernel provides the following 14 functions for working with sx locks:

#include <sys/param.h>
#include <sys/lock.h>
#include <sys/sx.h>

void
sx_init(struct sx sx, const char description);

void
sx_init_flags(struct sx sx, const char description, int opts);

void
sx_slock(struct sx sx);

void
sx_xlock(struct sx sx);

int
sx_slock_sig(struct sx sx);

int
sx_xlock_sig(struct sx sx);

int
sx_try_slock(struct sx sx);

int
sx_try_xlock(struct sx sx);

void
sx_sunlock(struct sx sx);

void
sx_xunlock(struct sx sx);

void
sx_unlock(struct sx sx);

int
sx_try_upgrade(struct sx sx);

void
sx_downgrade(struct sx sx);

void
sx_destroy(struct sx sx);

The sx_init function initializes the sx lock sx. The description argument is used during debugging to identify sx.

The sx_init_flags function is an alternative to sx_init. The opts argument modifies sx_init_flags’s behavior. Valid values for opts are shown in Table 4-2.

Table 4-2. sx_init_flags Symbolic Constants

Constant

Description

SX_NOADAPTIVE

If this bit is passed and the kernel is compiled without options NO_ADAPTIVE_SX, then threads holding sx will spin instead of sleeping.

SX_RECURSE

Specifies that sx is a recursive lock

SX_QUIET

Instructs the system to not log the operations done on this lock

SX_NOWITNESS

Causes witness(4) to ignore this lock

SX_DUPOK

Causes witness(4) to ignore duplicates of this lock

SX_NOPROFILE

Instructs the system to not profile this lock

Threads acquire a shared hold on sx by calling sx_slock. If another thread currently has an exclusive hold on sx, the caller will sleep until sx is available.

Threads acquire an exclusive hold on sx by calling sx_xlock. If any threads currently have a shared or exclusive hold on sx, the caller will sleep until sx is available.

The sx_slock_sig and sx_xlock_sig functions are identical to sx_slock and sx_xlock except that when the caller sleeps it can be woken up by signals. If this occurs, a nonzero value is returned.

Note

Normally, threads sleeping on locks cannot be woken up early.

The sx_try_slock and sx_try_xlock functions are identical to sx_slock and sx_xlock except that if sx cannot be acquired, they return 0 (that is, the caller does not sleep).

Threads release a shared hold on sx by calling sx_sunlock, and they release an exclusive hold by calling sx_xunlock.

The sx_unlock function is a front end to sx_sunlock and sx_xunlock. This function is used when the hold state on sx is unknown.

Threads can upgrade a shared hold to an exclusive hold by calling sx_try_upgrade. If the hold cannot be immediately upgraded, 0 is returned. Threads can downgrade an exclusive hold to a shared hold by calling sx_downgrade.

The sx_destroy function destroys the sx lock sx. Note that sx cannot be held when it is destroyed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.115.155