TinyCThread
1.1
|
#include <time.h>
#include <pthread.h>
Go to the source code of this file.
Macros | |
#define | TINYCTHREAD_VERSION_MAJOR 1 |
TinyCThread version (major number). More... | |
#define | TINYCTHREAD_VERSION_MINOR 2 |
TinyCThread version (minor number). More... | |
#define | TINYCTHREAD_VERSION (TINYCTHREAD_VERSION_MAJOR * 100 + TINYCTHREAD_VERSION_MINOR) |
TinyCThread version (full version). More... | |
#define | _Thread_local |
Thread local storage keyword. More... | |
#define | thrd_error 0 |
The requested operation failed. | |
#define | thrd_success 1 |
The requested operation succeeded. | |
#define | thrd_timedout 2 |
The time specified in the call was reached without acquiring the requested resource. | |
#define | thrd_busy 3 |
The requested operation failed because a tesource requested by a test and return function is already in use. | |
#define | thrd_nomem 4 |
The requested operation failed because it was unable to allocate memory. | |
#define | call_once(flag, func) pthread_once(flag,func) |
Invoke a callback exactly once. More... | |
Typedefs | |
typedef int(* | thrd_start_t )(void *arg) |
Thread start function. More... | |
typedef void(* | tss_dtor_t )(void *val) |
Destructor function for a thread-specific storage. More... | |
Functions | |
int | mtx_init (mtx_t *mtx, int type) |
Create a mutex object. More... | |
void | mtx_destroy (mtx_t *mtx) |
Release any resources used by the given mutex. More... | |
int | mtx_lock (mtx_t *mtx) |
Lock the given mutex. More... | |
int | mtx_timedlock (mtx_t *mtx, const struct timespec *ts) |
NOT YET IMPLEMENTED. | |
int | mtx_trylock (mtx_t *mtx) |
Try to lock the given mutex. More... | |
int | mtx_unlock (mtx_t *mtx) |
Unlock the given mutex. More... | |
int | cnd_init (cnd_t *cond) |
Create a condition variable object. More... | |
void | cnd_destroy (cnd_t *cond) |
Release any resources used by the given condition variable. More... | |
int | cnd_signal (cnd_t *cond) |
Signal a condition variable. More... | |
int | cnd_broadcast (cnd_t *cond) |
Broadcast a condition variable. More... | |
int | cnd_wait (cnd_t *cond, mtx_t *mtx) |
Wait for a condition variable to become signaled. More... | |
int | cnd_timedwait (cnd_t *cond, mtx_t *mtx, const struct timespec *ts) |
Wait for a condition variable to become signaled. More... | |
int | thrd_create (thrd_t *thr, thrd_start_t func, void *arg) |
Create a new thread. More... | |
thrd_t | thrd_current (void) |
Identify the calling thread. More... | |
int | thrd_detach (thrd_t thr) |
Dispose of any resources allocated to the thread when that thread exits. More... | |
int | thrd_equal (thrd_t thr0, thrd_t thr1) |
Compare two thread identifiers. More... | |
void | thrd_exit (int res) TTHREAD_NORETURN |
Terminate execution of the calling thread. More... | |
int | thrd_join (thrd_t thr, int *res) |
Wait for a thread to terminate. More... | |
int | thrd_sleep (const struct timespec *duration, struct timespec *remaining) |
Put the calling thread to sleep. More... | |
void | thrd_yield (void) |
Yield execution to another thread. More... | |
int | tss_create (tss_t *key, tss_dtor_t dtor) |
Create a thread-specific storage. More... | |
void | tss_delete (tss_t key) |
Delete a thread-specific storage. More... | |
void * | tss_get (tss_t key) |
Get the value for a thread-specific storage. More... | |
int | tss_set (tss_t key, void *val) |
Set the value for a thread-specific storage. More... | |
#define _Thread_local |
Thread local storage keyword.
A variable that is declared with the _Thread_local
keyword makes the value of the variable local to each thread (known as thread-local storage, or TLS). Example usage:
_Thread_local
keyword is a macro that maps to the corresponding compiler directive (e.g. __declspec(thread)
). #define call_once | ( | flag, | |
func | |||
) | pthread_once(flag,func) |
Invoke a callback exactly once.
flag | Flag used to ensure the callback is invoked exactly once. |
func | Callback to invoke. |
#define TINYCTHREAD_VERSION (TINYCTHREAD_VERSION_MAJOR * 100 + TINYCTHREAD_VERSION_MINOR) |
TinyCThread version (full version).
#define TINYCTHREAD_VERSION_MAJOR 1 |
TinyCThread version (major number).
#define TINYCTHREAD_VERSION_MINOR 2 |
TinyCThread version (minor number).
typedef int(* thrd_start_t)(void *arg) |
Thread start function.
Any thread that is started with the thrd_create() function must be started through a function of this type.
arg | The thread argument (the arg argument of the corresponding thrd_create() call). |
typedef void(* tss_dtor_t)(void *val) |
Destructor function for a thread-specific storage.
val | The value of the destructed thread-specific storage. |
int cnd_broadcast | ( | cnd_t * | cond | ) |
Broadcast a condition variable.
Unblocks all of the threads that are blocked on the given condition variable at the time of the call. If no threads are blocked on the condition variable at the time of the call, the function does nothing and return success.
cond | A condition variable object. |
void cnd_destroy | ( | cnd_t * | cond | ) |
Release any resources used by the given condition variable.
cond | A condition variable object. |
int cnd_init | ( | cnd_t * | cond | ) |
Create a condition variable object.
cond | A condition variable object. |
int cnd_signal | ( | cnd_t * | cond | ) |
Signal a condition variable.
Unblocks one of the threads that are blocked on the given condition variable at the time of the call. If no threads are blocked on the condition variable at the time of the call, the function does nothing and return success.
cond | A condition variable object. |
int cnd_timedwait | ( | cnd_t * | cond, |
mtx_t * | mtx, | ||
const struct timespec * | ts | ||
) |
Wait for a condition variable to become signaled.
The function atomically unlocks the given mutex and endeavors to block until the given condition variable is signaled by a call to cnd_signal or to cnd_broadcast, or until after the specified time. When the calling thread becomes unblocked it locks the mutex before it returns.
cond | A condition variable object. |
mtx | A mutex object. |
xt | A point in time at which the request will time out (absolute time). |
int cnd_wait | ( | cnd_t * | cond, |
mtx_t * | mtx | ||
) |
Wait for a condition variable to become signaled.
The function atomically unlocks the given mutex and endeavors to block until the given condition variable is signaled by a call to cnd_signal or to cnd_broadcast. When the calling thread becomes unblocked it locks the mutex before it returns.
cond | A condition variable object. |
mtx | A mutex object. |
void mtx_destroy | ( | mtx_t * | mtx | ) |
Release any resources used by the given mutex.
mtx | A mutex object. |
int mtx_init | ( | mtx_t * | mtx, |
int | type | ||
) |
Create a mutex object.
mtx | A mutex object. |
type | Bit-mask that must have one of the following six values:
|
int mtx_lock | ( | mtx_t * | mtx | ) |
Lock the given mutex.
Blocks until the given mutex can be locked. If the mutex is non-recursive, and the calling thread already has a lock on the mutex, this call will block forever.
mtx | A mutex object. |
int mtx_trylock | ( | mtx_t * | mtx | ) |
Try to lock the given mutex.
The specified mutex shall support either test and return or timeout. If the mutex is already locked, the function returns without blocking.
mtx | A mutex object. |
int mtx_unlock | ( | mtx_t * | mtx | ) |
Unlock the given mutex.
mtx | A mutex object. |
int thrd_create | ( | thrd_t * | thr, |
thrd_start_t | func, | ||
void * | arg | ||
) |
Create a new thread.
thr | Identifier of the newly created thread. |
func | A function pointer to the function that will be executed in the new thread. |
arg | An argument to the thread function. |
thrd_t thrd_current | ( | void | ) |
Identify the calling thread.
int thrd_detach | ( | thrd_t | thr | ) |
Dispose of any resources allocated to the thread when that thread exits.
int thrd_equal | ( | thrd_t | thr0, |
thrd_t | thr1 | ||
) |
Compare two thread identifiers.
The function determines if two thread identifiers refer to the same thread.
void thrd_exit | ( | int | res | ) |
Terminate execution of the calling thread.
res | Result code of the calling thread. |
int thrd_join | ( | thrd_t | thr, |
int * | res | ||
) |
Wait for a thread to terminate.
The function joins the given thread with the current thread by blocking until the other thread has terminated.
thr | The thread to join with. |
res | If this pointer is not NULL, the function will store the result code of the given thread in the integer pointed to by res . |
int thrd_sleep | ( | const struct timespec * | duration, |
struct timespec * | remaining | ||
) |
Put the calling thread to sleep.
Suspend execution of the calling thread.
duration | Interval to sleep for |
remaining | If non-NULL, this parameter will hold the remaining time until time_point upon return. This will typically be zero, but if the thread was woken up by a signal that is not ignored before duration was reached remaining will hold a positive time. |
void thrd_yield | ( | void | ) |
Yield execution to another thread.
Permit other threads to run, even if the current thread would ordinarily continue to run.
int tss_create | ( | tss_t * | key, |
tss_dtor_t | dtor | ||
) |
Create a thread-specific storage.
key | The unique key identifier that will be set if the function is successful. |
dtor | Destructor function. This can be NULL. |
dtor
will definitely be called when appropriate for threads created with thrd_create. It will be called for other threads in most cases, the possible exception being for DLLs loaded with LoadLibraryEx. In order to be certain, you should use thrd_create whenever possible. void tss_delete | ( | tss_t | key | ) |
Delete a thread-specific storage.
The function releases any resources used by the given thread-specific storage.
key | The key that shall be deleted. |
void* tss_get | ( | tss_t | key | ) |
Get the value for a thread-specific storage.
key | The thread-specific storage identifier. |
int tss_set | ( | tss_t | key, |
void * | val | ||
) |
Set the value for a thread-specific storage.
key | The thread-specific storage identifier. |
val | The value of the thread-specific storage to set for the current thread. |