Asynchronous Thread and Mutex Management Functions. More...
Go to the source code of this file.
Macros | |
| #define | QSC_ASYNC_PARALLEL_MAX 128ULL |
| The maximum number of threads that can be launched in parallel for a parallel for loop. | |
Functions | |
| QSC_EXPORT_API bool | qsc_async_atomic_bool_load (volatile bool *target) |
| Atomically load the current value of a boolean. | |
| QSC_EXPORT_API void | qsc_async_atomic_bool_store (volatile bool *target, bool value) |
| Atomically store a value into a boolean. | |
| QSC_EXPORT_API bool | qsc_async_atomic_bool_exchange (volatile bool *target, bool value) |
| Atomically swap a boolean with a new value. | |
| QSC_EXPORT_API bool | qsc_async_atomic_bool_compare_exchange (volatile bool *target, bool expected, bool desired) |
| Atomically compare a boolean and conditionally store a new value. | |
| QSC_EXPORT_API int32_t | qsc_async_atomic_int32_load (volatile int32_t *target) |
| Atomically load an int32_t value. | |
| QSC_EXPORT_API void | qsc_async_atomic_int32_store (volatile int32_t *target, int32_t value) |
| Atomically store an int32_t value. | |
| QSC_EXPORT_API int32_t | qsc_async_atomic_int32_exchange (volatile int32_t *target, int32_t value) |
| Atomically exchange an int32_t value. | |
| QSC_EXPORT_API bool | qsc_async_atomic_int32_compare_exchange (volatile int32_t *target, int32_t expected, int32_t desired) |
| Atomically compare and conditionally exchange an int32_t value. | |
| QSC_EXPORT_API int32_t | qsc_async_atomic_int32_add (volatile int32_t *target, int32_t value) |
| Atomically add a value to an int32_t. | |
| QSC_EXPORT_API int32_t | qsc_async_atomic_int32_subtract (volatile int32_t *target, int32_t value) |
| Atomically subtract a value from an int32_t. | |
| QSC_EXPORT_API int32_t | qsc_async_atomic_int32_increment (volatile int32_t *target) |
| Atomically increment an int32_t by one. | |
| QSC_EXPORT_API int32_t | qsc_async_atomic_int32_decrement (volatile int32_t *target) |
| Atomically decrement an int32_t by one. | |
| QSC_EXPORT_API void | qsc_async_launch_thread (void(*func)(void *), void *state) |
| Launch a function on a new thread. | |
| QSC_EXPORT_API void | qsc_async_launch_parallel_threads (void(*func)(void *), size_t count,...) |
| Launch multiple threads in parallel using variadic arguments. | |
| QSC_EXPORT_API qsc_mutex | qsc_async_mutex_create (void) |
| Create a mutex. | |
| QSC_EXPORT_API bool | qsc_async_mutex_destroy (qsc_mutex mtx) |
| Destroy a mutex. | |
| QSC_EXPORT_API void | qsc_async_mutex_lock (qsc_mutex mtx) |
| Lock a mutex. | |
| QSC_EXPORT_API qsc_mutex | qsc_async_mutex_lock_ex (void) |
| Create and lock a mutex. | |
| QSC_EXPORT_API void | qsc_async_mutex_unlock (qsc_mutex mtx) |
| Unlock a mutex. | |
| QSC_EXPORT_API void | qsc_async_mutex_unlock_ex (qsc_mutex mtx) |
| Unlock and destroy a mutex. | |
| QSC_EXPORT_API size_t | qsc_async_processor_count (void) |
| Get the number of processor cores available. | |
| QSC_EXPORT_API qsc_thread | qsc_async_thread_create (void(*func)(void *), void *state) |
| Create a thread with one parameter. | |
| QSC_EXPORT_API qsc_thread | qsc_async_thread_create_ex (void(*func)(void **), void **args) |
| Create a thread with multiple parameters. | |
| QSC_EXPORT_API qsc_thread | qsc_async_thread_create_noargs (void(*func)(void)) |
| Create a thread with no arguments parameter. | |
| QSC_EXPORT_API int32_t | qsc_async_thread_resume (qsc_thread handle) |
| Resume a suspended thread. | |
| QSC_EXPORT_API void | qsc_async_thread_sleep (uint32_t msec) |
| Suspend the calling thread for a specified number of milliseconds. | |
| QSC_EXPORT_API int32_t | qsc_async_thread_suspend (qsc_thread handle) |
| Suspend a thread. | |
| QSC_EXPORT_API bool | qsc_async_thread_terminate (qsc_thread handle) |
| Terminate a thread. | |
| QSC_EXPORT_API void | qsc_async_thread_wait (qsc_thread handle) |
| Wait for a thread to complete execution. | |
| QSC_EXPORT_API void | qsc_async_thread_wait_time (qsc_thread handle, uint32_t msec) |
| Wait for a thread to complete execution with a timeout. | |
| QSC_EXPORT_API void | qsc_async_thread_wait_all (qsc_thread *handles, size_t count) |
| Wait for an array of threads to complete execution. | |
Asynchronous Thread and Mutex Management Functions.
This header defines the public API for asynchronous thread management and mutex operations. It provides functions for launching threads (both individually and in parallel), creating and managing mutexes for thread synchronization, and waiting on thread execution. The API supports both Windows and POSIX threading models.
| QSC_EXPORT_API bool qsc_async_atomic_bool_compare_exchange | ( | volatile bool * | target, |
| bool | expected, | ||
| bool | desired ) |
Atomically compare a boolean and conditionally store a new value.
Reads target and compares it to expected. If they are equal, desired is written to target and the function returns true. If they differ, the target is left unchanged and the function returns false. The comparison and the conditional store are performed as a single indivisible operation.
| target | [volatile bool*] Pointer to the boolean to test and update. Must not be NULL. |
| expected | [bool] The value the caller expects target to currently hold. |
| desired | [bool] The value to store if the comparison succeeds. |
target did not equal expected. | QSC_EXPORT_API bool qsc_async_atomic_bool_exchange | ( | volatile bool * | target, |
| bool | value ) |
Atomically swap a boolean with a new value.
Stores value into target and returns the value that target held immediately before the swap. The read and the write occur as a single indivisible operation; no other thread can observe an intermediate state.
| target | [volatile bool*] Pointer to the boolean to swap. Must not be NULL. |
| value | [bool] The new value to store. |
target before the swap. | QSC_EXPORT_API bool qsc_async_atomic_bool_load | ( | volatile bool * | target | ) |
Atomically load the current value of a boolean.
Reads the value at target using a sequentially consistent atomic load. The operation is equivalent to acquiring the value without the possibility of observing a partial write from a concurrent store.
| target | [volatile bool*] Pointer to the boolean to read. Must not be NULL. |
target at the time of the load. | QSC_EXPORT_API void qsc_async_atomic_bool_store | ( | volatile bool * | target, |
| bool | value ) |
Atomically store a value into a boolean.
Writes value to target using a sequentially consistent atomic store. Any thread that subsequently calls qsc_async_atomic_bool_load on the same target is guaranteed to observe value or any later write.
| target | [volatile bool*] Pointer to the boolean to write. Must not be NULL. |
| value | [bool] The value to store. |
| QSC_EXPORT_API int32_t qsc_async_atomic_int32_add | ( | volatile int32_t * | target, |
| int32_t | value ) |
Atomically add a value to an int32_t.
Adds value to *target and returns the value AFTER the addition.
| target | [volatile int32_t*] Pointer to the atomic integer. |
| value | [int32_t] The amount to add. |
| QSC_EXPORT_API bool qsc_async_atomic_int32_compare_exchange | ( | volatile int32_t * | target, |
| int32_t | expected, | ||
| int32_t | desired ) |
Atomically compare and conditionally exchange an int32_t value.
If *target equals expected, stores desired into *target. The comparison and store occur as a single atomic operation.
| target | [volatile int32_t*] Pointer to the atomic integer. |
| expected | [int32_t] The value the caller expects *target to hold. |
| desired | [int32_t] The value to store if the comparison succeeds. |
| QSC_EXPORT_API int32_t qsc_async_atomic_int32_decrement | ( | volatile int32_t * | target | ) |
Atomically decrement an int32_t by one.
Decrements *target by one and returns the value AFTER the decrement.
| target | [volatile int32_t*] Pointer to the atomic integer. |
| QSC_EXPORT_API int32_t qsc_async_atomic_int32_exchange | ( | volatile int32_t * | target, |
| int32_t | value ) |
Atomically exchange an int32_t value.
Writes value to *target and returns the previous value.
| target | [volatile int32_t*] Pointer to the atomic integer. |
| value | [int32_t] The new value to store. |
| QSC_EXPORT_API int32_t qsc_async_atomic_int32_increment | ( | volatile int32_t * | target | ) |
Atomically increment an int32_t by one.
Increments *target by one and returns the value AFTER the increment.
| target | [volatile int32_t*] Pointer to the atomic integer. |
| QSC_EXPORT_API int32_t qsc_async_atomic_int32_load | ( | volatile int32_t * | target | ) |
Atomically load an int32_t value.
Reads the current value of *target with sequential consistency.
| target | [volatile int32_t*] Pointer to the atomic integer. |
| QSC_EXPORT_API void qsc_async_atomic_int32_store | ( | volatile int32_t * | target, |
| int32_t | value ) |
Atomically store an int32_t value.
Writes value to *target with sequential consistency.
| target | [volatile int32_t*] Pointer to the atomic integer. |
| value | [int32_t] The value to store. |
| QSC_EXPORT_API int32_t qsc_async_atomic_int32_subtract | ( | volatile int32_t * | target, |
| int32_t | value ) |
Atomically subtract a value from an int32_t.
Subtracts value from *target and returns the value AFTER the subtraction.
| target | [volatile int32_t*] Pointer to the atomic integer. |
| value | [int32_t] The amount to subtract. |
| QSC_EXPORT_API void qsc_async_launch_parallel_threads | ( | void(* | func )(void *), |
| size_t | count, | ||
| ... ) |
Launch multiple threads in parallel using variadic arguments.
Spawns several threads, each executing the provided function with its respective argument.
| func | [void (*)(void*)] Pointer to the function to execute. |
| count | [size_t] The number of threads (and corresponding arguments) to launch. |
| ... | [variadic] Variadic arguments representing the state for each thread. |
| QSC_EXPORT_API void qsc_async_launch_thread | ( | void(* | func )(void *), |
| void * | state ) |
Launch a function on a new thread.
Spawns a new thread to execute the provided function with a single argument.
| func | [void (*)(void*)] Pointer to the function to execute. |
| state | [void*] Pointer to the argument to pass to the function. |
| QSC_EXPORT_API qsc_mutex qsc_async_mutex_create | ( | void | ) |
Create a mutex.
Creates a new mutex object for synchronizing threads.
| QSC_EXPORT_API bool qsc_async_mutex_destroy | ( | qsc_mutex | mtx | ) |
Destroy a mutex.
Destroys the specified mutex object.
| mtx | [qsc_mutex] The mutex handle to destroy. |
| QSC_EXPORT_API void qsc_async_mutex_lock | ( | qsc_mutex | mtx | ) |
Lock a mutex.
Blocks until the specified mutex is acquired.
| mtx | [qsc_mutex] The mutex to lock. |
| QSC_EXPORT_API qsc_mutex qsc_async_mutex_lock_ex | ( | void | ) |
Create and lock a mutex.
Creates a mutex, locks it immediately, and returns the locked mutex.
| QSC_EXPORT_API void qsc_async_mutex_unlock | ( | qsc_mutex | mtx | ) |
Unlock a mutex.
Unlocks the specified mutex.
| mtx | [qsc_mutex] The mutex to unlock. |
| QSC_EXPORT_API void qsc_async_mutex_unlock_ex | ( | qsc_mutex | mtx | ) |
Unlock and destroy a mutex.
Unlocks the specified mutex and then destroys it.
| mtx | [qsc_mutex] The mutex to unlock and destroy. |
| QSC_EXPORT_API size_t qsc_async_processor_count | ( | void | ) |
Get the number of processor cores available.
Retrieves the number of CPU cores (including hyper-threads) available on the system.
| QSC_EXPORT_API qsc_thread qsc_async_thread_create | ( | void(* | func )(void *), |
| void * | state ) |
Create a thread with one parameter.
Creates a new thread that executes the specified function with a single argument.
| func | [void (*)(void*)] Pointer to the function to execute in the new thread. |
| state | [void*] Pointer to the argument to pass to the thread function. |
| QSC_EXPORT_API qsc_thread qsc_async_thread_create_ex | ( | void(* | func )(void **), |
| void ** | args ) |
Create a thread with multiple parameters.
Creates a new thread that executes the specified function with multiple arguments.
| func | [void (*)(void**)] Pointer to the function to execute in the new thread. |
| args | [void**] An array of pointers to the arguments. |
| QSC_EXPORT_API qsc_thread qsc_async_thread_create_noargs | ( | void(* | func )(void) | ) |
Create a thread with no arguments parameter.
Creates a new thread that executes the specified function with multiple arguments.
| func | [void (*)(void**)] Pointer to the function to execute in the new thread. |
| QSC_EXPORT_API int32_t qsc_async_thread_resume | ( | qsc_thread | handle | ) |
Resume a suspended thread.
Resumes execution of a thread that has been suspended.
| handle | [qsc_thread] The thread handle to resume. |
| QSC_EXPORT_API void qsc_async_thread_sleep | ( | uint32_t | msec | ) |
Suspend the calling thread for a specified number of milliseconds.
Suspends execution of the calling thread for the given duration.
| msec | [uint32_t] The number of milliseconds to sleep. |
| QSC_EXPORT_API int32_t qsc_async_thread_suspend | ( | qsc_thread | handle | ) |
Suspend a thread.
Suspends the execution of the specified thread.
| handle | [qsc_thread] The thread handle to suspend. |
| QSC_EXPORT_API bool qsc_async_thread_terminate | ( | qsc_thread | handle | ) |
Terminate a thread.
Terminates the specified thread. On Windows, this may terminate the calling thread.
| handle | [qsc_thread] The thread handle to terminate. |
| QSC_EXPORT_API void qsc_async_thread_wait | ( | qsc_thread | handle | ) |
Wait for a thread to complete execution.
Blocks until the specified thread has finished executing.
| handle | [qsc_thread] The thread handle to wait on. |
| QSC_EXPORT_API void qsc_async_thread_wait_all | ( | qsc_thread * | handles, |
| size_t | count ) |
Wait for an array of threads to complete execution.
Blocks until all threads in the provided array have finished executing.
| handles | [qsc_thread*] An array of thread handles. |
| count | [size_t] The number of threads in the array. |
| QSC_EXPORT_API void qsc_async_thread_wait_time | ( | qsc_thread | handle, |
| uint32_t | msec ) |
Wait for a thread to complete execution with a timeout.
Blocks until the specified thread has finished executing or the timeout expires.
| handle | [qsc_thread] The thread handle to wait on. |
| msec | [uint32_t] The maximum number of milliseconds to wait. |