Mstdlib-1.24.0

Data Structures

struct  M_thread_spinlock_t
 

Macros

#define M_THREAD_SPINLOCK_STATIC_INITIALIZER   { 0, 0, 0 }
 

Functions

void M_thread_spinlock_lock (M_thread_spinlock_t *spinlock)
 
void M_thread_spinlock_unlock (M_thread_spinlock_t *spinlock)
 

Detailed Description

Spinlocks


Data Structure Documentation

◆ M_thread_spinlock_t

struct M_thread_spinlock_t

Public struct for spinlocks, so static initializers can be used

Data Fields
volatile M_uint32 current
volatile M_uint32 queue
M_threadid_t threadid

Macro Definition Documentation

◆ M_THREAD_SPINLOCK_STATIC_INITIALIZER

#define M_THREAD_SPINLOCK_STATIC_INITIALIZER   { 0, 0, 0 }

Static initializer for spinlocks

Function Documentation

◆ M_thread_spinlock_lock()

void M_thread_spinlock_lock ( M_thread_spinlock_t spinlock)

Lock a spinlock.

A spinlock is similar in usage to a mutex, but should NOT be used in place of a mutex. When in doubt, use a mutex instead, a spinlock is almost always the wrong thing to use. Spinlocks can be used protect areas of memory that are very unlikely to have high contention and should only be held for very short durations, or when the act of initializing a mutex might itself cause a race condition (such as during an initialization procedure as mutexes do not support static initializers).

When lock contention occurs on a spinlock, it will spin, consuming CPU, waiting for the lock to be released. Spinlocks are purely implemented in userland using atomics. The implementation uses 'tickets' to try to guarantee lock order in a first-come first-served manner, and has rudimentary backoff logic to attempt to reduce resource consumption during periods of high lock contention.

A spinlock variable must have been initialized using M_THREAD_SPINLOCK_STATIC_INITIALIZER and passed by reference into this function. There is no initialization or destruction function.

Parameters
[in]spinlockSpinlock initialized via M_THREAD_SPINLOCK_STATIC_INITIALIZER and passed by reference.

◆ M_thread_spinlock_unlock()

void M_thread_spinlock_unlock ( M_thread_spinlock_t spinlock)

Unlock a spinlock

See M_thread_spinlock_lock() for more information.

Parameters
[in]spinlockSpinlock initialized via M_THREAD_SPINLOCK_STATIC_INITIALIZER and passed by reference.