A tiny one‐byte spinlock with a configurable spin and yield budget.

Synopsis

Declared in <folly/MicroLock.h>

template<
    unsigned int MaxSpins = 1000,
    unsigned int MaxYields = 0>
class MicroLockBase
    : public MicroLockCore

Base Classes

Name

Description

MicroLockCore

Base class holding the lock byte and the bit‐packing helpers shared by every MicroLock specialization.

Types

Name

Description

LockGuardWithData

A lock guard which allows reading and writing to the unused bits of the lock as data.

Member Functions

Name

Description

load

Loads the data stored in the unused bits of the lock atomically.

lock

Acquire the lock, blocking until it is held.

lockAndLoad

Locks the lock and returns the data stored in the unused bits of the lock. This is useful when you want to use the unused bits of the lock to store data, in which case reading and locking should be done in one atomic operation.

store

Stores the data in the unused bits of the lock atomically. Since 2 bits are used by the lock, the most significant 2 bits of the provided value will be ignored.

try_lock

Try to acquire the lock without blocking.

unlock

Release the lock without modifying the stored data bits.

unlockAndStore

Unlocks the lock and stores the bits of the provided value into the data bits. Since 2 bits are used by the lock, the most significant 2 bits of the provided value will be ignored.

Protected Member Functions

Name

Description

heldBit

Mask for bit indicating that the flag is held.

lockSlowPath

Acquire the lock along the contended path, spinning then yielding.

unlockAndStoreWithModifier

Unlock the lock, applying a modifier to compute the stored word.

waitBit

Mask for bit indicating that there is a waiter that should be woken up.

Protected Static Member Functions

Name

Description

decodeDataFromByte

Decodes the value stored in the unused bits of the lock.

decodeDataFromWord

Decode the user data stored in the data bits of a lock word.

encodeDataToByte

Encodes the value for the unused bits of the lock.

encodeDataToWord

Encode user data into the data bits of a lock word, preserving lock bits.

Protected Data Members

Name

Description

lock_

The single byte holding both the lock bits and the packed data bits.

Protected Static Data Members

Name

Description

kDataBits

Mask selecting the bits available for user data.

kLockBits

Mask selecting the bits reserved for lock state.

kNumLockBits

Number of low bits reserved for lock state.

Template Parameters

Name

Description

MaxSpins

The maximum number of spins before yielding.

MaxYields

The maximum number of yields before parking.

Created with MrDocs