KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches

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_tthrd_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.
 

Detailed Description

C11 Threading APIs

Macro Definition Documentation

◆ mtx_plain

#define mtx_plain   (1 << 0)

Plain mutex.

◆ mtx_recursive

#define mtx_recursive   (1 << 1)

Recursive mutex.

◆ mtx_timed

#define mtx_timed   (1 << 2)

Mutex supporting the mtx_timedlock function.

◆ ONCE_FLAG_INIT

#define ONCE_FLAG_INIT   KTHREAD_ONCE_INIT

Macro to initialize a once_flag object.

◆ thrd_busy

#define thrd_busy   -3

Resource busy.

◆ thrd_error

#define thrd_error   -1

Uncategorized error.

◆ thrd_nomem

#define thrd_nomem   -4

Out of memory.

◆ thrd_success

#define thrd_success   0

Success.

◆ thrd_timedout

#define thrd_timedout   -2

Time out error.

◆ TSS_DTOR_ITERATIONS

#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.

Typedef Documentation

◆ cnd_t

typedef condvar_t cnd_t

C11 condition variable type.

This type holds an identifier for a condition variable object that is to be used with C11 threading support.

◆ mtx_t

typedef mutex_t mtx_t

C11 mutual exclusion lock type.

This type holds an identifier for a mutual exclusion (mutex) lock to be used with C11 threading support.

◆ 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.

◆ thrd_start_t

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().

◆ thrd_t

typedef kthread_t* thrd_t

C11 thread identifier type.

This type holds an identifier for a C11 thread.

◆ tss_dtor_t

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.

◆ 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.

Function Documentation

◆ call_once()

void call_once ( once_flag * flag,
void(* func )(void) )
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.

Parameters
flagThe once_flag to run against.
funcThe function to call.

◆ cnd_broadcast()

int cnd_broadcast ( cnd_t * cond)
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.

Parameters
condThe condition variable to signal.
Return values
thrd_successOn success.
thrd_errorIf the request cannot be honored.

◆ cnd_destroy()

void cnd_destroy ( cnd_t * cond)
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.

Parameters
condThe condition variable to deinitialize.
Note
Deinitializing a condition variable that is currently being waited on by threads results in undefined behavior.

◆ cnd_init()

int cnd_init ( cnd_t * cond)
extern

Initialize a condition variable.

This function initializes the specified condition variable for use.

Parameters
condThe condition variable to signal.
Return values
thrd_successOn success.
thrd_nomemIf memory cannot be allocated for the new condition variable.
thrd_errorIf the request cannot be honored for some other reason.

◆ cnd_signal()

int cnd_signal ( cnd_t * cond)
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.

Parameters
condThe condition variable to signal.
Return values
thrd_successOn success.
thrd_errorIf the request cannot be honored.

◆ cnd_timedwait()

int cnd_timedwait ( cnd_t *__RESTRICT cond,
mtx_t *__RESTRICT mtx,
const struct timespec *__RESTRICT ts )
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.

Parameters
condThe condition variable to wait on.
mtxThe mutex associated with the condition variable.
tsThe time to wait before timing out.
Return values
thrd_successOn success.
thrd_timedoutIf the timeout was reached before the condition variable was signaled.
thrd_errorIf the request cannot be honored for some other reason.
Note
Calling this function in an interrupt will result in an error being returned.
Although timeouts are specified in seconds and nanoseconds, the timeout will be rounded up to the nearest millisecond.

◆ cnd_wait()

int cnd_wait ( cnd_t * cond,
mtx_t * mtx )
extern

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.

Parameters
condThe condition variable to wait on.
mtxThe mutex associated with the condition variable.
Return values
thrd_successOn success.
thrd_errorIf the request cannot be honored.
Note
Calling this function in an interrupt will result in an error being returned.

◆ mtx_destroy()

void mtx_destroy ( mtx_t * mtx)
extern

Deinitialize a mutex lock.

This function deinitializes a mutex lock that was previously created with mtx_init().

Parameters
mtxThe mutex to deinitialize.

◆ mtx_init()

int mtx_init ( mtx_t * mtx,
int type )
extern

Initialize a mutex lock.

This function initializes a mutex lock of the given type for later use to protect critical sections of code.

Parameters
mtxThe mutex to initialize.
typeThe type of mutex desired
Return values
thrd_successOn success.
thrd_errorIf the request could not be honored.

◆ mtx_lock()

int mtx_lock ( mtx_t * mtx)
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.

Parameters
mtxThe mutex to lock.
Return values
thrd_successOn success.
thrd_errorIf the request could not be honored.
Note
Calling this function in an interrupt will result in an error being returned.

◆ mtx_timedlock()

int mtx_timedlock ( mtx_t *__RESTRICT mtx,
const struct timespec *__RESTRICT ts )
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.

Parameters
mtxThe mutex to lock.
tsThe amount of time to wait before timing out.
Return values
thrd_successOn success.
thrd_errorIf the request could not be honored for some other reason than a timeout.
thrd_timedoutIf the timeout specified passes without obtaining the lock.
Note
Calling this function in an interrupt will result in an error being returned.
Although timeouts are specified in seconds and nanoseconds, the timeout will be rounded up to the nearest millisecond.

◆ mtx_trylock()

int mtx_trylock ( mtx_t * mtx)
extern

Attempt to acquire a mutex lock.

This function attempts to acquire the specified mutex and will not block if it cannot be obtained.

Parameters
mtxThe mutex to lock.
Return values
thrd_successOn success.
thrd_busyIf the lock is already locked by a thread.
thrd_errorIf the request could not be honored for some other reason.
Note
This function is safe to call in an interrupt.
Always check the return value to ensure that the lock was obtained.

◆ mtx_unlock()

int mtx_unlock ( mtx_t * mtx)
extern

Unlock a previously acquired lock.

This function releases the specified mutex lock, allowing other threads to acquire it.

Parameters
mtxThe mutex to unlock.
Return values
thrd_successOn success.
thrd_errorIf the request cannot be honored.
Note
Unlocking a mutex that was not previously locked by the calling thread results in undefined behavior.

◆ thrd_create()

int thrd_create ( thrd_t * thr,
thrd_start_t func,
void * arg )
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().

Parameters
thrStorage for the thread identifier.
funcThe function to call in the new thread.
argArgument to pass to the function called.
Return values
thrd_successOn success.
thrd_nomemIf memory cannot be allocated to satisfy the request.
thrd_errorIf the request cannot be honored for some other reason.
Note
All threads created are joinable threads by default. That means that in order to free all resources at thread termination, the thread must be joined with the thrd_join() function or detached at some point with thrd_detach().

◆ thrd_current()

thrd_t thrd_current ( void )
extern

Return the identifier of the currently running thread.

Returns
The current thread's ID.

◆ thrd_detach()

int thrd_detach ( thrd_t thr)
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.

Parameters
thrThe thread to detach.
Return values
thrd_successOn success.
thrd_errorIf the request cannot be honored.
Note
Detaching an already detached thread has no effect.
Detaching a thread that has been joined with another thread results in undefined behavior.

◆ thrd_equal()

int thrd_equal ( thrd_t thr0,
thrd_t thr1 )
extern

Compare two threads for equality.

This function checks the two two thread identifiers passed in to see if they refer to the same thread.

Parameters
thr0The first thread to compare.
thr1The second thread to compare.
Returns
0 if the threads are not equal, nonzero if the threads are equal.

◆ thrd_exit()

_Noreturn void thrd_exit ( int res)
extern

Terminate the current thread immediately.

This function terminates the calling thread immediately, setting the return value of the thread to the value specified.

Parameters
resThe return value of the thread.
Note
This function will not return.

◆ thrd_join()

int thrd_join ( thrd_t thr,
int * res )
extern

Join a running thread.

This function joins the current thread with the specified thread, blocking until that thread has terminated.

Parameters
thrThe thread to join with.
resPointer to storage for the result code of the other thread. Set to NULL if you don't care about the result value.
Return values
thrd_successOn success.
thrd_errorIf the request cannot be honored.
Note
Joining with a previously detached thread results in undefined behavior.
Joining with a thread that has already been joined to another thread results in undefined behavior.
Calling this function in an interrupt will result in an error being returned.

◆ thrd_sleep()

int thrd_sleep ( const struct timespec * duration,
struct timespec * remaining )
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.

Parameters
durationThe amount of time to sleep.
remainingAny remaining time from the duration that the thread did not sleep for.
Returns
0 if the requested time elapsed, a negative value otherwise.
Note
Although the duration is expressed in seconds and nanoseconds, all sleeping is done in millisecond increments. The value specified will be rounded up if it is not an even number of milliseconds.
KOS does not support signals, so remaining will only ever have a value after the function if there is some sort of error.
Calling this function in an interrupt will result in an error being returned.

◆ thrd_yield()

void thrd_yield ( void )
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).

Note
Calling this function in an interrupt will not have any effect.

◆ tss_create()

int tss_create ( tss_t * key,
tss_dtor_t dtor )
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.

Parameters
keyThe key to initialize.
dtorThe destructor to associate with the key.
Return values
thrd_successOn success.
thrd_errorOn failure.

◆ tss_delete()

void tss_delete ( tss_t key)
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.

Parameters
keyThe key to deinitialize.

◆ tss_get()

void * tss_get ( tss_t key)
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.

Parameters
keyThe key to look up the value associated with.
Returns
The value associated with the key.

◆ tss_set()

int tss_set ( tss_t key,
void * val )
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.

Parameters
keyThe key to set the value for.
valThe value to set.
Return values
thrd_successOn success.
thrd_errorIf the request cannot be honored.