A small, fast, scalable reader‐writer lock.

Synopsis

Declared in <folly/SharedMutex.h>

template<
    bool ReaderPriority,
    typename Tag_ = void,
    template<typename> typename Atom = atomic,
    typename Policy = SharedMutexPolicyDefault>
class SharedMutexImpl;

Description

SharedMutex provides exclusive, upgrade, and shared locking modes, with blocking, try, try‐for, and try‐until variants. Each instance is only a few bytes and records shared acquisitions in a shared set of core‐local stripes so that concurrent readers scale linearly.

Type Aliases

Name

Description

DeferredReaderSlot

Storage for one deferred reader slot; holds a tagged mutex pointer.

Tag

Tag type used to create an independent instantiation.

Token

Token type used to speed up paired shared lock and unlock calls.

Member Functions

Name

Description

SharedMutexImpl [constructor]

Constructors

~SharedMutexImpl [destructor]

Destroys the mutex.

operator=

Assignment operators

eligible_for_lock_elision

Checks whether an exclusive lock could succeed so that lock elision can be enabled.

eligible_for_lock_shared_elision

Checks whether a shared lock could succeed so that lock elision can be enabled.

eligible_for_lock_upgrade_elision

Checks whether an upgrade lock could succeed so that lock elision can be enabled.

lock

Acquires the lock in exclusive mode, blocking until it is available.

lock_shared

lock_shared overloads

lock_upgrade

Acquires the lock in upgrade mode, blocking until it is available.

release_token

Invalidates the given token so the tokenless unlock_shared() can be used.

try_lock

Tries to acquire the lock in exclusive mode without blocking.

try_lock_for

Tries to acquire the lock in exclusive mode within a duration.

try_lock_shared

try_lock_shared overloads

try_lock_shared_for

try_lock_shared_for overloads

try_lock_shared_until

try_lock_shared_until overloads

try_lock_until

Tries to acquire the lock in exclusive mode by a deadline.

try_lock_upgrade

Tries to acquire the lock in upgrade mode without blocking.

try_lock_upgrade_for

Tries to acquire the lock in upgrade mode within a duration.

try_lock_upgrade_until

Tries to acquire the lock in upgrade mode by a deadline.

unlock

Releases the lock held in exclusive mode.

unlock_and_lock_shared

unlock_and_lock_shared overloads

unlock_and_lock_upgrade

Atomically converts an exclusive lock to an upgrade lock.

unlock_shared

unlock_shared overloads

unlock_upgrade

Releases a lock held in upgrade mode.

unlock_upgrade_and_lock

Atomically upgrades an upgrade lock to an exclusive lock.

unlock_upgrade_and_lock_shared

unlock_upgrade_and_lock_shared overloads

Static Data Members

Name

Description

kDeferredSearchDistance

Number of deferred reader slots searched when acquiring or releasing.

kDeferredSeparationFactor

Spacing between used deferred reader slots, to avoid cache contention.

kReaderPriority

Whether this instantiation gives priority to readers.

Template Parameters

Name

Description

ReaderPriority

Whether readers take priority over writers.

Tag_

Tag type used to create an independent instantiation with its own deferred‐reader storage.

Atom

Atomic template used for the lock state and deferred slots.

Policy

Tuning and feature policy; see SharedMutexPolicyDefault.

Created with MrDocs