Skip to content

Threading and synchronizations routines

Finalspace edited this page May 29, 2026 · 1 revision

This category contains functions/types for dealing with concurrent programming, such as threads, mutexes, conditions, etc. More...

Data Structures

Type Name
struct fplConditionVariable
Stores the condition variable structure. More...
union fplInternalConditionVariable
Stores the internal condition variable for any platform. More...
union fplInternalMutexHandle
Stores the internal mutex handle for any platform. More...
union fplInternalSemaphoreHandle
Stores the internal semaphore handle for any platform. More...
union fplInternalSignalHandle
Stores the internal signal handle for any platform. More...
union fplInternalThreadHandle
Stores the thread handle for any platform. More...
struct fplMutexHandle
Stores the mutex handle structure. More...
struct fplSemaphoreHandle
Stores the semaphore handle structure. More...
struct fplSignalHandle
Stores the signal handle structure. More...
struct fplThreadHandle
Stores the thread handle structure. More...
struct fplThreadParameters
Stores creation parameters for fplThreadCreateWithParameters(). More...

Typedefs

Type Name
typedef void fpl_run_thread_callback(const fplThreadHandle *thread, void *data)
A function definition for a callback to execute user code inside another thread.
typedef uint32_t fplThreadState
A type definition that maps fplThreadStates into a 32-bit integer for atomic storage.

Enumerations

Type Name
enum fplSignalValue { fplSignalValue_Unset , fplSignalValue_Set }
An enumeration of signal values. More...
enum fplThreadPriority {
fplThreadPriority_Unknown , fplThreadPriority_Idle , fplThreadPriority_Low , fplThreadPriority_Normal ,
fplThreadPriority_High , fplThreadPriority_RealTime , fplThreadPriority_Lowest , fplThreadPriority_Highest ,
fplThreadPriority_First , fplThreadPriority_Last
}
Defines all possible thread priorities. More...
enum fplThreadStates { fplThreadState_Stopped , fplThreadState_Starting , fplThreadState_Running , fplThreadState_Stopping }
An enumeration of thread states. More...

Functions

Type Name
fpl_platform_api bool fplConditionBroadcast (fplConditionVariable *condition)
Wakes up all threads that wait on the given condition.
fpl_platform_api void fplConditionDestroy (fplConditionVariable *condition)
Releases the given condition and clears the structure to zero.
fpl_platform_api bool fplConditionInit (fplConditionVariable *condition)
Initializes the given condition.
fpl_platform_api bool fplConditionSignal (fplConditionVariable *condition)
Wakes up one thread that waits on the given condition.
fpl_platform_api bool fplConditionWait (fplConditionVariable *condition, fplMutexHandle *mutex, const fplTimeoutValue timeout)
Sleeps on the given condition and releases the mutex when done.
fpl_common_api size_t fplGetAvailableThreadCount (void)
Gets the number of available threads.
fpl_platform_api uint64_t fplGetCurrentThreadId (void)
Gets the thread id for the current thread.
fpl_common_api const fplThreadHandle * fplGetMainThread (void)
Gets the thread handle for the main thread.
fpl_platform_api fplThreadPriority fplGetThreadPriority (fplThreadHandle *thread)
Retrieves the current thread priority from the OS for the given thread handle.
fpl_common_api fplThreadState fplGetThreadState (fplThreadHandle *thread)
Gets the current thread state for the given thread.
fpl_common_api size_t fplGetUsedThreadCount (void)
Gets the number of used/active threads.
fpl_platform_api void fplMutexDestroy (fplMutexHandle *mutex)
Releases the given mutex and clears the structure to zero.
fpl_platform_api bool fplMutexInit (fplMutexHandle *mutex)
Initializes the given mutex.
fpl_platform_api bool fplMutexLock (fplMutexHandle *mutex)
Locks the given mutex and blocks any other threads.
fpl_platform_api bool fplMutexTryLock (fplMutexHandle *mutex)
Tries to lock the given mutex without blocking other threads.
fpl_platform_api bool fplMutexUnlock (fplMutexHandle *mutex)
Unlocks the given mutex.
fpl_platform_api void fplSemaphoreDestroy (fplSemaphoreHandle *semaphore)
Releases the internal semaphore resources.
fpl_platform_api bool fplSemaphoreInit (fplSemaphoreHandle *semaphore, const uint32_t initialValue)
Initializes the semaphore with the given initial value.
fpl_platform_api bool fplSemaphoreRelease (fplSemaphoreHandle *semaphore)
Increments the semaphore value by one.
fpl_platform_api bool fplSemaphoreTryWait (fplSemaphoreHandle *semaphore)
Tries to wait for the semaphore until it gets signaled or returns immediately.
fpl_platform_api int32_t fplSemaphoreValue (fplSemaphoreHandle *semaphore)
Gets the current semaphore value.
fpl_platform_api bool fplSemaphoreWait (fplSemaphoreHandle *semaphore, const fplTimeoutValue timeout)
Waits for the semaphore until it gets signaled or the timeout has been reached.
fpl_platform_api bool fplSetThreadPriority (fplThreadHandle *thread, const fplThreadPriority newPriority)
Changes the thread priority to the given one, for the given thread handle.
fpl_platform_api void fplSignalDestroy (fplSignalHandle *signal)
Releases the given signal and clears the structure to zero.
fpl_platform_api bool fplSignalInit (fplSignalHandle *signal, const fplSignalValue initialValue)
Initializes the given signal.
fpl_platform_api bool fplSignalReset (fplSignalHandle *signal)
Resets the signal.
fpl_platform_api bool fplSignalSet (fplSignalHandle *signal)
Sets the signal and wakes up the given signal.
fpl_platform_api bool fplSignalWaitForAll (fplSignalHandle **signals, const size_t count, const size_t stride, const fplTimeoutValue timeout)
Waits until all the given signals are woken up.
fpl_platform_api bool fplSignalWaitForAny (fplSignalHandle **signals, const size_t count, const size_t stride, const fplTimeoutValue timeout)
Waits until any of the given signals wakes up or the timeout has been reached.
fpl_platform_api bool fplSignalWaitForOne (fplSignalHandle *signal, const fplTimeoutValue timeout)
Waits until the given signal is woken up.
fpl_platform_api fplThreadHandle * fplThreadCreate (fpl_run_thread_callback *runFunc, void *data)
Creates and starts a thread and returns the handle to it.
fpl_platform_api fplThreadHandle * fplThreadCreateWithParameters (fplThreadParameters *parameters)
Creates and starts a thread from the specified parameters and returns the handle to it.
fpl_platform_api void fplThreadSleep (const uint32_t milliseconds)
Lets the current thread sleep for the given amount of milliseconds.
fpl_platform_api bool fplThreadTerminate (fplThreadHandle *thread)
Forces the given thread to stop and release all underlying resources.
fpl_platform_api bool fplThreadWaitForAll (fplThreadHandle **threads, const size_t count, const size_t stride, const fplTimeoutValue timeout)
Waits until all given threads are done running or the given timeout has been reached.
fpl_platform_api bool fplThreadWaitForAny (fplThreadHandle **threads, const size_t count, const size_t stride, const fplTimeoutValue timeout)
Waits until one of the given threads is done running or the given timeout has been reached.
fpl_platform_api bool fplThreadWaitForOne (fplThreadHandle *thread, const fplTimeoutValue timeout)
Waits until the given thread is done running or the given timeout has been reached.
fpl_platform_api bool fplThreadYield (void)
Lets the current thread yield execution to another thread that is ready to run on this core.

Detailed Description

This category contains functions/types for dealing with concurrent programming, such as threads, mutexes, conditions, etc.

Typedef Documentation

fpl_run_thread_callback

typedef void fpl_run_thread_callback(const fplThreadHandle *thread, void *data)

A function definition for a callback to execute user code inside another thread.

Parameters

Direction Parameter Description
[in] thread Reference to the thread handle fplThreadHandle.
[in] data Reference to the user data pointer.

Definition at line 7374 of file final_platform_layer.h.

fplThreadState

typedef uint32_t fplThreadState

A type definition that maps fplThreadStates into a 32-bit integer for atomic storage.

Definition at line 7334 of file final_platform_layer.h.

Enumeration Type Documentation

fplSignalValue

enum fplSignalValue

An enumeration of signal values.

Name Description
fplSignalValue_Unset Value is unset.
fplSignalValue_Set Value is set.

Definition at line 7537 of file final_platform_layer.h.

fplThreadPriority

enum fplThreadPriority

Defines all possible thread priorities.

Name Description
fplThreadPriority_Unknown Unknown priority.
fplThreadPriority_Idle Idle priority (Only when nothing is going on).
fplThreadPriority_Low Low priority.
fplThreadPriority_Normal Normal priority.
fplThreadPriority_High High priority.
fplThreadPriority_RealTime Realtime priority (Time critical).
fplThreadPriority_Lowest Lowest fplThreadPriority.
fplThreadPriority_Highest Highest fplThreadPriority.
fplThreadPriority_First First fplThreadPriority.
fplThreadPriority_Last Last fplThreadPriority.

Definition at line 7340 of file final_platform_layer.h.

fplThreadStates

enum fplThreadStates

An enumeration of thread states.

Name Description
fplThreadState_Stopped Thread is stopped.
fplThreadState_Starting Thread is being started.
fplThreadState_Running Thread is still running.
fplThreadState_Stopping Thread is being stopped.

Definition at line 7319 of file final_platform_layer.h.

Function Documentation

fplConditionBroadcast()

fpl_platform_api bool fplConditionBroadcast ( fplConditionVariable * condition)

Wakes up all threads that wait on the given condition.

Parameters

Direction Parameter Description
[in,out] condition Reference to the condition variable structure fplConditionVariable.

Returns: Returns true when the function succeeds, false otherwise.

See also: Send a Condition-Variable Broadcast to all waiting Threads

fplConditionDestroy()

fpl_platform_api void fplConditionDestroy ( fplConditionVariable * condition)

Releases the given condition and clears the structure to zero.

Parameters

Direction Parameter Description
[in,out] condition Reference to the condition variable structure fplConditionVariable.

See also: Initialize a Condition-Variable

fplConditionInit()

fpl_platform_api bool fplConditionInit ( fplConditionVariable * condition)

Initializes the given condition.

Parameters

Direction Parameter Description
[in,out] condition Reference to the condition variable structure fplConditionVariable.

Returns: Returns true when initialization was successful, false otherwise.

Note: Use

fplConditionDestroy() when you are done with this Condition Variable to release its resources.

See also: Initialize a Condition-Variable

fplConditionSignal()

fpl_platform_api bool fplConditionSignal ( fplConditionVariable * condition)

Wakes up one thread that waits on the given condition.

Parameters

Direction Parameter Description
[in,out] condition Reference to the condition variable structure fplConditionVariable.

Returns: Returns true when the function succeeds, false otherwise.

See also: Send a Signal to a Condition-Variable to one waiting Thread

fplConditionWait()

fpl_platform_api bool fplConditionWait ( fplConditionVariable * condition, fplMutexHandle * mutex, const fplTimeoutValue timeout )

Sleeps on the given condition and releases the mutex when done.

Parameters

Direction Parameter Description
[in,out] condition Reference to the condition variable structure.
[in,out] mutex Reference to the mutex handle structure fplConditionVariable.
[in] timeout The number of milliseconds to wait. When this is set to FPL_TIMEOUT_INFINITE, it will wait indefinitely.

Returns: Returns true when the function succeeds, false otherwise.

See also: Waiting on a Condition-Variable

fplGetAvailableThreadCount()

fpl_common_api size_t fplGetAvailableThreadCount ( void )

Gets the number of available threads.

Returns: Returns the number of available threads.

fplGetCurrentThreadId()

fpl_platform_api uint64_t fplGetCurrentThreadId ( void )

Gets the thread id for the current thread.

Returns: Returns the thread id for the current thread.

fplGetMainThread()

fpl_common_api const fplThreadHandle * fplGetMainThread ( void )

Gets the thread handle for the main thread.

Returns: Returns the immutable pointer to the thread handle.

fplGetThreadPriority()

fpl_platform_api fplThreadPriority fplGetThreadPriority ( fplThreadHandle * thread)

Retrieves the current thread priority from the OS for the given thread handle.

Parameters

Direction Parameter Description
[in] thread Reference to the thread handle structure.

Returns: Returns the current thread priority.

fplGetThreadState()

fpl_common_api fplThreadState fplGetThreadState ( fplThreadHandle * thread)

Gets the current thread state for the given thread.

Parameters

Direction Parameter Description
[in] thread Reference to the thread handle.

Returns: Returns the current thread state for the given thread.

See also: Query the Thread State

fplGetUsedThreadCount()

fpl_common_api size_t fplGetUsedThreadCount ( void )

Gets the number of used/active threads.

Returns: Returns the number of used/active threads.

fplMutexDestroy()

fpl_platform_api void fplMutexDestroy ( fplMutexHandle * mutex)

Releases the given mutex and clears the structure to zero.

Parameters

Direction Parameter Description
[in,out] mutex Reference to the mutex handle structure fplMutexHandle.

See also: Initialize a Mutex

fplMutexInit()

fpl_platform_api bool fplMutexInit ( fplMutexHandle * mutex)

Initializes the given mutex.

Parameters

Direction Parameter Description
[in,out] mutex Reference to the mutex handle structure fplMutexHandle.

Returns: Returns true when the mutex was initialized, false otherwise.

Note: Use

fplMutexDestroy() when you are done with this mutex.

See also: Initialize a Mutex

fplMutexLock()

fpl_platform_api bool fplMutexLock ( fplMutexHandle * mutex)

Locks the given mutex and blocks any other threads.

Parameters

Direction Parameter Description
[in,out] mutex Reference to the mutex handle structure fplMutexHandle.

Returns: Returns true when the mutex was locked, false otherwise.

See also: Locking a Mutex

fplMutexTryLock()

fpl_platform_api bool fplMutexTryLock ( fplMutexHandle * mutex)

Tries to lock the given mutex without blocking other threads.

Parameters

Direction Parameter Description
[in,out] mutex Reference to the mutex handle structure fplMutexHandle.

Returns: Returns true when the mutex was locked, false otherwise.

See also: Trying to lock a Mutex

fplMutexUnlock()

fpl_platform_api bool fplMutexUnlock ( fplMutexHandle * mutex)

Unlocks the given mutex.

Parameters

Direction Parameter Description
[in,out] mutex Reference to the mutex handle structure fplMutexHandle.

Returns: Returns true when the mutex was unlocked, false otherwise.

See also: Unlocking a Mutex

fplSemaphoreDestroy()

fpl_platform_api void fplSemaphoreDestroy ( fplSemaphoreHandle * semaphore)

Releases the internal semaphore resources.

Parameters

Direction Parameter Description
[in,out] semaphore Reference to the semaphore handle structure fplSemaphoreHandle.

Warning: Do not call this when a thread is still waiting on this semaphore.

See also: Initialize a Semaphore

fplSemaphoreInit()

fpl_platform_api bool fplSemaphoreInit ( fplSemaphoreHandle * semaphore, const uint32_t initialValue )

Initializes the semaphore with the given initial value.

Parameters

Direction Parameter Description
[in,out] semaphore Reference to the semaphore handle structure fplSemaphoreHandle.
[in] initialValue The initial value.

Returns: Returns true when the semaphore was initialized, false otherwise.

See also: Initialize a Semaphore

fplSemaphoreRelease()

fpl_platform_api bool fplSemaphoreRelease ( fplSemaphoreHandle * semaphore)

Increments the semaphore value by one.

Parameters

Direction Parameter Description
[in,out] semaphore Reference to the semaphore handle structure fplSemaphoreHandle.

Returns: Returns true when the semaphore was incremented, false otherwise.

See also: Releasing the Semaphore

fplSemaphoreTryWait()

fpl_platform_api bool fplSemaphoreTryWait ( fplSemaphoreHandle * semaphore)

Tries to wait for the semaphore until it gets signaled or returns immediately.

Parameters

Direction Parameter Description
[in,out] semaphore Reference to the semaphore handle structure fplSemaphoreHandle.

Returns: Returns true when the semaphore got signaled, false otherwise.

Note: When a semaphore gets signaled, the semaphore value is decreased by one.

See also: Trying to wait on a Semaphore

fplSemaphoreValue()

fpl_platform_api int32_t fplSemaphoreValue ( fplSemaphoreHandle * semaphore)

Gets the current semaphore value.

Parameters

Direction Parameter Description
[in,out] semaphore Reference to the semaphore handle structure fplSemaphoreHandle.

Returns: Returns the current semaphore value.

See also: Reading the Value from the Semaphore

fplSemaphoreWait()

fpl_platform_api bool fplSemaphoreWait ( fplSemaphoreHandle * semaphore, const fplTimeoutValue timeout )

Waits for the semaphore until it gets signaled or the timeout has been reached.

Parameters

Direction Parameter Description
[in,out] semaphore Reference to the semaphore handle structure fplSemaphoreHandle.
[in] timeout The number of milliseconds to wait. When this is set to FPL_TIMEOUT_INFINITE, it will wait indefinitely.

Returns: Returns true when the semaphore got signaled, false otherwise.

Note: When a semaphore gets signaled, the semaphore value is decreased by one.

See also: Waiting/Locking a Semaphore

fplSetThreadPriority()

fpl_platform_api bool fplSetThreadPriority ( fplThreadHandle * thread, const fplThreadPriority newPriority )

Changes the thread priority to the given one, for the given thread handle.

Parameters

Direction Parameter Description
[in] thread Reference to the thread handle structure.
[in] newPriority The new thread priority for the given thread.

Returns: Returns true when the priority was changed, false otherwise.

fplSignalDestroy()

fpl_platform_api void fplSignalDestroy ( fplSignalHandle * signal)

Releases the given signal and clears the structure to zero.

Parameters

Direction Parameter Description
[in,out] signal Reference to the signal handle structure fplSignalHandle.

See also: Initialize a Signal

fplSignalInit()

fpl_platform_api bool fplSignalInit ( fplSignalHandle * signal, const fplSignalValue initialValue )

Initializes the given signal.

Parameters

Direction Parameter Description
[in,out] signal Reference to the signal handle structure fplSignalHandle.
[in] initialValue The initial value the signal is set to.

Returns: Returns true when initialization was successful, false otherwise.

Note: Use

fplSignalDestroy() when you are done with this Signal to release it.

See also: Initialize a Signal

fplSignalReset()

fpl_platform_api bool fplSignalReset ( fplSignalHandle * signal)

Resets the signal.

Parameters

Direction Parameter Description
[in,out] signal Reference to the signal handle structure fplSignalHandle.

Returns: Returns true when the signal was reset, false otherwise.

See also: Resetting a Signal

fplSignalSet()

fpl_platform_api bool fplSignalSet ( fplSignalHandle * signal)

Sets the signal and wakes up the given signal.

Parameters

Direction Parameter Description
[in,out] signal Reference to the signal handle structure fplSignalHandle.

Returns: Returns true when the signal was set and broadcasted, false otherwise.

See also: Setting a Signal

fplSignalWaitForAll()

fpl_platform_api bool fplSignalWaitForAll ( fplSignalHandle ** signals, const size_t count, const size_t stride, const fplTimeoutValue timeout )

Waits until all the given signals are woken up.

Parameters

Direction Parameter Description
[in,out] signals Reference to the first signal handle structure fplSignalHandle.
[in] count The number of signals.
[in] stride The size in bytes to the next signal handle. When this is set to zero, the array default is used.
[in] timeout The number of milliseconds to wait. When this is set to FPL_TIMEOUT_INFINITE, it will wait indefinitely.

Returns: Returns true when all signals woke up or the timeout has been reached, false otherwise.

See also: Wait for all Signals to be set

fplSignalWaitForAny()

fpl_platform_api bool fplSignalWaitForAny ( fplSignalHandle ** signals, const size_t count, const size_t stride, const fplTimeoutValue timeout )

Waits until any of the given signals wakes up or the timeout has been reached.

Parameters

Direction Parameter Description
[in,out] signals Reference to the first signal handle structure fplSignalHandle.
[in] count The number of signals.
[in] stride The size in bytes to the next signal handle. When this is set to zero, the array default is used.
[in] timeout The number of milliseconds to wait. When this is set to FPL_TIMEOUT_INFINITE, it will wait indefinitely.

Returns: Returns true when any of the signals woke up or the timeout has been reached, false otherwise.

See also: Wait for any Signal to be set

fplSignalWaitForOne()

fpl_platform_api bool fplSignalWaitForOne ( fplSignalHandle * signal, const fplTimeoutValue timeout )

Waits until the given signal is woken up.

Parameters

Direction Parameter Description
[in,out] signal Reference to the signal handle structure fplSignalHandle.
[in] timeout The number of milliseconds to wait. When this is set to FPL_TIMEOUT_INFINITE, it will wait indefinitely.

Returns: Returns true when the signal woke up or the timeout has been reached, false otherwise.

See also: Wait for a single Signal to be set

fplThreadCreate()

fpl_platform_api fplThreadHandle * fplThreadCreate ( fpl_run_thread_callback * runFunc, void * data )

Creates and starts a thread and returns the handle to it.

Parameters

Direction Parameter Description
[in] runFunc Reference to the run thread callback fpl_run_thread_callback.
[in] data Reference to the user data pointer passed to the execution function callback.

Returns: Returns a pointer to the thread handle structure or null when the limit of active threads has been reached.

Warning: Do not free this thread context directly!

Note: The resources are automatically cleaned up when the thread terminates.

See also: Creating a Thread

fplThreadCreateWithParameters()

fpl_platform_api fplThreadHandle * fplThreadCreateWithParameters ( fplThreadParameters * parameters)

Creates and starts a thread from the specified parameters and returns the handle to it.

Parameters

Direction Parameter Description
[in] parameters Reference to the thread parameters fplThreadParameters.

Returns: Returns a pointer to the thread handle structure or null when the limit of active threads has been reached.

Warning: Do not free this thread context directly!

Note: The resources are automatically cleaned up when the thread terminates.

fplThreadSleep()

fpl_platform_api void fplThreadSleep ( const uint32_t milliseconds)

Lets the current thread sleep for the given amount of milliseconds.

Parameters

Direction Parameter Description
[in] milliseconds Number of milliseconds to sleep.

Note: There is no guarantee that the OS sleeps for the exact amount of milliseconds! This can vary based on the OS scheduler's granularity.

fplThreadTerminate()

fpl_platform_api bool fplThreadTerminate ( fplThreadHandle * thread)

Forces the given thread to stop and release all underlying resources.

Parameters

Direction Parameter Description
[in] thread Reference to the thread handle structure.

Returns: Returns true when the thread was terminated, false otherwise.

Warning: Do not free the given thread context manually!

Note: This thread context may get re-used for another thread in the future.

See also: Terminate a thread

fplThreadWaitForAll()

fpl_platform_api bool fplThreadWaitForAll ( fplThreadHandle ** threads, const size_t count, const size_t stride, const fplTimeoutValue timeout )

Waits until all given threads are done running or the given timeout has been reached.

Parameters

Direction Parameter Description
[in] threads Reference to the first thread handle pointer.
[in] count The number of threads.
[in] stride The size in bytes to the next thread handle. When this is set to zero, the array default is used.
[in] timeout The number of milliseconds to wait. When this is set to FPL_TIMEOUT_INFINITE, it will wait indefinitely.

Returns: Returns true when all threads complete or when the timeout has been reached, false otherwise.

See also: Wait for all Threads to Exit

fplThreadWaitForAny()

fpl_platform_api bool fplThreadWaitForAny ( fplThreadHandle ** threads, const size_t count, const size_t stride, const fplTimeoutValue timeout )

Waits until one of the given threads is done running or the given timeout has been reached.

Parameters

Direction Parameter Description
[in] threads Reference to the first thread handle fplThreadHandle.
[in] count The number of threads.
[in] stride The size in bytes to the next thread handle. When this is set to zero, the size of the thread handle is used.
[in] timeout The number of milliseconds to wait. When this is set to FPL_TIMEOUT_INFINITE, it will wait indefinitely.

Returns: Returns true when one thread completes or when the timeout has been reached, false otherwise.

See also: Wait for any Thread to Exit

fplThreadWaitForOne()

fpl_platform_api bool fplThreadWaitForOne ( fplThreadHandle * thread, const fplTimeoutValue timeout )

Waits until the given thread is done running or the given timeout has been reached.

Parameters

Direction Parameter Description
[in] thread Reference to the thread handle structure.
[in] timeout The number of milliseconds to wait. When this is set to FPL_TIMEOUT_INFINITE, it will wait indefinitely.

Returns: Returns true when the thread completes or when the timeout has been reached, false otherwise.

See also: Wait for a single Thread to Exit

fplThreadYield()

fpl_platform_api bool fplThreadYield ( void )

Lets the current thread yield execution to another thread that is ready to run on this core.

Returns: Returns true when the function succeeds, false otherwise.

Final Platform Layer

Pages

Topics

Data Structures

Clone this wiki locally