KallistiOS git master
Independent SDK for the Sega Dreamcast
|
C11 Threading APIs More...
Files | |
file | threads.h |
C11 Threading API. | |
Macros | |
#define | ONCE_FLAG_INIT KTHREAD_ONCE_INIT |
Macro to initialize a once_flag object. | |
#define | TSS_DTOR_ITERATIONS 1 |
Maximum number of iterations over TSS destructors. | |
Typedefs | |
typedef kthread_once_t | once_flag |
Object type backing call_once. | |
typedef mutex_t | mtx_t |
C11 mutual exclusion lock type. | |
typedef condvar_t | cnd_t |
C11 condition variable type. | |
typedef kthread_t * | thrd_t |
C11 thread identifier type. | |
typedef int(* | thrd_start_t) (void *) |
C11 thread start function type. | |
typedef kthread_key_t | tss_t |
C11 thread-specific storage type. | |
typedef void(* | tss_dtor_t) (void *) |
C11 thread-specific storage destructor type. | |
Functions | |
void | call_once (once_flag *flag, void(*func)(void)) |
Call a function one time, no matter how many threads try. | |
void | mtx_destroy (mtx_t *mtx) |
Deinitialize a mutex lock. | |
int | mtx_init (mtx_t *mtx, int type) |
Initialize a mutex lock. | |
int | mtx_lock (mtx_t *mtx) |
Lock a mutex lock. | |
int | mtx_timedlock (mtx_t *__RESTRICT mtx, const struct timespec *__RESTRICT ts) |
Lock a mutex lock with a timeout. | |
int | mtx_trylock (mtx_t *mtx) |
Attempt to acquire a mutex lock. | |
int | mtx_unlock (mtx_t *mtx) |
Unlock a previously acquired lock. | |
int | cnd_broadcast (cnd_t *cond) |
Broadcast to all threads locked on a condition variable. | |
void | cnd_destroy (cnd_t *cond) |
Deinitialize a condition variable. | |
int | cnd_init (cnd_t *cond) |
Initialize a condition variable. | |
int | cnd_signal (cnd_t *cond) |
Signal one thread locked on a condition variable. | |
int | cnd_timedwait (cnd_t *__RESTRICT cond, mtx_t *__RESTRICT mtx, const struct timespec *__RESTRICT ts) |
Wait on a condition variable (with a timeout). | |
int | cnd_wait (cnd_t *cond, mtx_t *mtx) |
Wait on a condition variable. | |
int | thrd_create (thrd_t *thr, thrd_start_t func, void *arg) |
Create and start a new thread. | |
thrd_t | thrd_current (void) |
Return the identifier of the currently running thread. | |
int | thrd_detach (thrd_t thr) |
Detach a running thread. | |
int | thrd_equal (thrd_t thr0, thrd_t thr1) |
Compare two threads for equality. | |
_Noreturn void | thrd_exit (int res) |
Terminate the current thread immediately. | |
int | thrd_join (thrd_t thr, int *res) |
Join a running thread. | |
int | thrd_sleep (const struct timespec *duration, struct timespec *remaining) |
Put the currently running thread to sleep. | |
void | thrd_yield (void) |
Yield the current thread's timeslice. | |
int | tss_create (tss_t *key, tss_dtor_t dtor) |
Create a thread-specific storage pointer. | |
void | tss_delete (tss_t key) |
Free resources associated with a thread-specific storage key. | |
void * | tss_get (tss_t key) |
Retrieve the value associated with a thread-specific storage key. | |
int | tss_set (tss_t key, void *val) |
Set the value associated with a thread-specific storage key. | |
Return values | |
C11 Thread function return values Most of the C11 thread-related functions that return a result code return one of these. | |
#define | thrd_success 0 |
Success. | |
#define | thrd_error -1 |
Uncategorized error. | |
#define | thrd_timedout -2 |
Time out error. | |
#define | thrd_busy -3 |
Resource busy. | |
#define | thrd_nomem -4 |
Out of memory. | |
Mutex types | |
C11 mutual exclusion lock types These are the possible types of mutex locks that C11 allows for. Note that mtx_plain or mtx_recursive can be ORed with mtx_timed as well. | |
#define | mtx_plain (1 << 0) |
Plain mutex. | |
#define | mtx_recursive (1 << 1) |
Recursive mutex. | |
#define | mtx_timed (1 << 2) |
Mutex supporting the mtx_timedlock function. | |
C11 Threading APIs
#define mtx_plain (1 << 0) |
Plain mutex.
#define mtx_recursive (1 << 1) |
Recursive mutex.
#define mtx_timed (1 << 2) |
Mutex supporting the mtx_timedlock function.
#define ONCE_FLAG_INIT KTHREAD_ONCE_INIT |
Macro to initialize a once_flag object.
#define thrd_busy -3 |
Resource busy.
#define thrd_error -1 |
Uncategorized error.
#define thrd_nomem -4 |
Out of memory.
#define thrd_success 0 |
Success.
#define thrd_timedout -2 |
Time out error.
#define TSS_DTOR_ITERATIONS 1 |
Maximum number of iterations over TSS destructors.
This macro defines the maximum number of iterations that will be performed over the destructors for thread-specific storage objects when a thread terminates.
C11 condition variable type.
This type holds an identifier for a condition variable object that is to be used with C11 threading support.
C11 mutual exclusion lock type.
This type holds an identifier for a mutual exclusion (mutex) lock to be used with C11 threading support.
typedef kthread_once_t once_flag |
Object type backing call_once.
This object type holds a flag that is used by the call_once function to call a function one time. It should always be initialized with the ONCE_FLAG_INIT macro.
typedef int(* thrd_start_t) (void *) |
C11 thread start function type.
This is a function pointer type representing a function used to begin a thread. The thread exits when the function returns or calls thrd_exit().
C11 thread identifier type.
This type holds an identifier for a C11 thread.
typedef void(* tss_dtor_t) (void *) |
C11 thread-specific storage destructor type.
This is a function pointer type which describes a destructor for a thread-specific storage object.
typedef kthread_key_t tss_t |
C11 thread-specific storage type.
This type holds a thread-specific storage identifier, which allows a value to be associated with it for each and every thread running.
|
extern |
Call a function one time, no matter how many threads try.
This function uses the once_flag object passed in to ensure that a given function is called exactly once, regardless of how many threads attempt to call through the once_flag.
flag | The once_flag to run against. |
func | The function to call. |
|
extern |
Broadcast to all threads locked on a condition variable.
This function wakes all threads that are blocked on the condition variable cond at the time of the call. If no threads are currently blocked on cond, this call does nothing.
cond | The condition variable to signal. |
thrd_success | On success. |
thrd_error | If the request cannot be honored. |
|
extern |
Deinitialize a condition variable.
This function cleans up all resources associated with the given condition variable. You must ensure that no threads are currently blocked on the condition variable before calling this function.
cond | The condition variable to deinitialize. |
|
extern |
Initialize a condition variable.
This function initializes the specified condition variable for use.
cond | The condition variable to signal. |
thrd_success | On success. |
thrd_nomem | If memory cannot be allocated for the new condition variable. |
thrd_error | If the request cannot be honored for some other reason. |
|
extern |
Signal one thread locked on a condition variable.
This function wakes one thread that is blocked on the condition variable cond at the time of the call. If no threads are currently blocked on cond, this call does nothing.
cond | The condition variable to signal. |
thrd_success | On success. |
thrd_error | If the request cannot be honored. |
|
extern |
Wait on a condition variable (with a timeout).
This function puts the calling thread to sleep until either the condition variable is signaled or the timeout specified expires, whichever happens first. The specified mutex must be held by the calling thread when calling this function and will be held by the thread again when it is unblocked.
cond | The condition variable to wait on. |
mtx | The mutex associated with the condition variable. |
ts | The time to wait before timing out. |
thrd_success | On success. |
thrd_timedout | If the timeout was reached before the condition variable was signaled. |
thrd_error | If the request cannot be honored for some other reason. |
Wait on a condition variable.
This function puts the calling thread to sleep until the condition variable is signaled. The specified mutex must be held by the calling thread when calling this function and will be held by the thread again when it is unblocked.
cond | The condition variable to wait on. |
mtx | The mutex associated with the condition variable. |
thrd_success | On success. |
thrd_error | If the request cannot be honored. |
|
extern |
Deinitialize a mutex lock.
This function deinitializes a mutex lock that was previously created with mtx_init().
mtx | The mutex to deinitialize. |
|
extern |
Initialize a mutex lock.
This function initializes a mutex lock of the given type for later use to protect critical sections of code.
mtx | The mutex to initialize. |
type | The type of mutex desired |
thrd_success | On success. |
thrd_error | If the request could not be honored. |
|
extern |
Lock a mutex lock.
This function locks the specified mutex, preventing any other threads from obtaining the same lock.
This function will block until the lock can be obtained.
mtx | The mutex to lock. |
thrd_success | On success. |
thrd_error | If the request could not be honored. |
|
extern |
Lock a mutex lock with a timeout.
This function locks the specified mutex, assuming that the lock can be obtained in the time period specified.
This function will block until the lock can be obtained or the timeout expires.
mtx | The mutex to lock. |
ts | The amount of time to wait before timing out. |
thrd_success | On success. |
thrd_error | If the request could not be honored for some other reason than a timeout. |
thrd_timedout | If the timeout specified passes without obtaining the lock. |
|
extern |
Attempt to acquire a mutex lock.
This function attempts to acquire the specified mutex and will not block if it cannot be obtained.
mtx | The mutex to lock. |
thrd_success | On success. |
thrd_busy | If the lock is already locked by a thread. |
thrd_error | If the request could not be honored for some other reason. |
|
extern |
Unlock a previously acquired lock.
This function releases the specified mutex lock, allowing other threads to acquire it.
mtx | The mutex to unlock. |
thrd_success | On success. |
thrd_error | If the request cannot be honored. |
|
extern |
Create and start a new thread.
This function creates a new thread, calling the function specified. The thread is immediately added to the runnable queue of the scheduler and can start at any moment after that. The thread ends when either the function specified returns or when the thread calls thrd_exit().
thr | Storage for the thread identifier. |
func | The function to call in the new thread. |
arg | Argument to pass to the function called. |
thrd_success | On success. |
thrd_nomem | If memory cannot be allocated to satisfy the request. |
thrd_error | If the request cannot be honored for some other reason. |
|
extern |
Return the identifier of the currently running thread.
|
extern |
Detach a running thread.
This function detaches a thread, which informs the kernel that any resources associated with the thread should be freed immediately when it terminates.
thr | The thread to detach. |
thrd_success | On success. |
thrd_error | If the request cannot be honored. |
Compare two threads for equality.
This function checks the two two thread identifiers passed in to see if they refer to the same thread.
thr0 | The first thread to compare. |
thr1 | The second thread to compare. |
|
extern |
Terminate the current thread immediately.
This function terminates the calling thread immediately, setting the return value of the thread to the value specified.
res | The return value of the thread. |
|
extern |
Join a running thread.
This function joins the current thread with the specified thread, blocking until that thread has terminated.
thr | The thread to join with. |
res | Pointer to storage for the result code of the other thread. Set to NULL if you don't care about the result value. |
thrd_success | On success. |
thrd_error | If the request cannot be honored. |
|
extern |
Put the currently running thread to sleep.
This function puts the currently running thread to sleep for the specified duration of time, returning any left over time (if interrupted by a signal, for instance) in the second parameter.
duration | The amount of time to sleep. |
remaining | Any remaining time from the duration that the thread did not sleep for. |
|
extern |
Yield the current thread's timeslice.
This function immediately pauses the current thread's execution and switches to another thread in the ready queue (if there are any threads ready to execute).
|
extern |
Create a thread-specific storage pointer.
This function creates a thread-specific storage pointer and associates the destructor function supplied with it. After creating the pointer, each thread may associate a piece of data with the key.
key | The key to initialize. |
dtor | The destructor to associate with the key. |
thrd_success | On success. |
thrd_error | On failure. |
|
extern |
Free resources associated with a thread-specific storage key.
This function releases any resources used by the thread-specific storage key specified. Note that this DOES NOT call any destructors.
key | The key to deinitialize. |
|
extern |
Retrieve the value associated with a thread-specific storage key.
This function retrieves the value associated with the specified thread-specific storage key and returns it to the caller. If no value has been set in the current thread, NULL is returned.
key | The key to look up the value associated with. |
|
extern |
Set the value associated with a thread-specific storage key.
This function sets the value to be associated with the specified thread-specific storage key, overwriting any previous keys. Note that this DOES NOT call any destructors.
key | The key to set the value for. |
val | The value to set. |
thrd_success | On success. |
thrd_error | If the request cannot be honored. |