Overview
This section explains how to create and manage Signals.
A Signal is a Kernel-Level object used to notify one or multiple waiting Threads.
It internally contains a Value which is either fplSignalValue_Set or fplSignalValue_Unset.
When this value gets changed, all Threads which waits on that Signal will wakeup.
They can be shared across process boundaries and may be used as standalone locks to shared data, but the number of Signals is limited by the OS that can be allocated at a time.
Initialize a Signal
Call fplSignalInit() with a pointer to fplSignalHandle as an argument, to initialize a Signal.
Also, you need to specify if the Signal starts as fplSignalValue_Set or fplSignalValue_Unset as a second argument.
When you are done with that Signal, you need to call fplSignalDestroy() to release its internal resources.
}
fpl_platform_api bool fplSignalInit(fplSignalHandle *signal, const fplSignalValue initialValue)
Initializes the given signal.
fpl_platform_api void fplSignalDestroy(fplSignalHandle *signal)
Releases the given signal and clears the structure to zero.
@ fplSignalValue_Unset
Value is unset.
The signal handle structure.
Waiting for Signal
Wait for a single Signal to be set
Call fplSignalWaitForOne() with a pointer to fplSignalHandle and a fplTimeoutValue argument, to let the current thread wait until the Signal is set.
fpl_platform_api bool fplSignalWaitForOne(fplSignalHandle *signal, const fplTimeoutValue timeout)
Waits until the given signal is waked up.
#define FPL_TIMEOUT_INFINITE
Infinite timeout constant.
Wait for any Signal to be set
Call fplSignalWaitForAny() to let the current thread wait until at least one Signal is set.
Example, default signal handle array:
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.
Example, custom struct with stored signal handle:
typedef struct MyCustomStruct {
int a;
void *ptr;
short b;
} MyCustomStruct;
MyCustomStruct *customSignals;
Wait for all Signals to be set
Call fplSignalWaitForAll() to let the current thread wait until all Signals are set.
Example, default signal handle array:
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 waked up.
Example, custom struct with stored signal handle:
typedef struct MyCustomStruct {
int a;
void *ptr;
short b;
} MyCustomStruct;
MyCustomStruct *customSignals;
Setting a Signal
Call fplSignalSet() with a pointer to fplSignalHandle as an argument, to set a Signal and wakeup all waiting Threads.
- Note
- Unlike fplConditionVariable , setting a Signal is not Thread-Safe so you should ensure that only one thread at a time will set it!
fpl_platform_api bool fplMutexUnlock(fplMutexHandle *mutex)
Unlocks the given mutex.
fpl_platform_api bool fplSignalSet(fplSignalHandle *signal)
Sets the signal and wakes up the given signal.
fpl_platform_api bool fplMutexLock(fplMutexHandle *mutex)
Locks the given mutex and blocks any other threads.
Resetting a Signal
Call fplSignalReset() with a pointer to fplSignalHandle as an argument, to reset a Signal.
- Note
- Unlike fplConditionVariable , resetting a Signal is not Thread-Safe so you should ensure that only one thread at a time will set it!
fpl_platform_api bool fplSignalReset(fplSignalHandle *signal)
Resets the signal.