]> pd.if.org Git - pdclib/blob - includes/threads.h
PDCLib includes with quotes, not <>.
[pdclib] / includes / threads.h
1 /* Threads <threads.h>
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #ifndef _PDCLIB_THREADS_H
8 #define _PDCLIB_THREADS_H _PDCLIB_THREADS_H
9 #include "_PDCLIB_int.h"
10 #include "_PDCLIB_threadconfig.h"
11
12 #include <time.h>
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 #define thread_local _Thread_local
19
20 typedef _PDCLIB_once_flag once_flag;
21
22 enum
23 {
24     mtx_plain       = 0,
25     mtx_recursive   = (1 << 0),
26     mtx_timed       = (1 << 1),
27
28     _PDCLIB_mtx_valid_mask = mtx_recursive | mtx_timed
29 };
30
31 enum
32 {
33     thrd_success    = 0,
34     thrd_timeout    = 1,
35     thrd_busy       = 2,
36     thrd_error      = 3,
37     thrd_nomem      = 4,
38 };
39
40 #define ONCE_FLAG_INIT _PDCLIB_ONCE_FLAG_INIT
41 #ifdef _PDCLIB_ONCE_FLAG_IS_DONE
42 static inline void call_once( once_flag * flag, void (*func)( void ) )
43 {
44     if ( ! _PDCLIB_ONCE_FLAG_IS_DONE( flag ) )
45     {
46         _PDCLIB_call_once( flag, func );
47     }
48 }
49 #else
50 void call_once( once_flag * flag, void (*func)( void ) );
51 #endif
52
53 #ifdef _PDCLIB_MTX_T
54 typedef _PDCLIB_MTX_T mtx_t;
55 void mtx_destroy( mtx_t * mtx ) _PDCLIB_nothrow;
56 int mtx_init( mtx_t * mtx, int type ) _PDCLIB_nothrow;
57 int mtx_lock( mtx_t * mtx ) _PDCLIB_nothrow;
58 int mtx_timedlock( mtx_t * _PDCLIB_restrict mtx, const struct timespec * _PDCLIB_restrict ts ) _PDCLIB_nothrow;
59 int mtx_trylock( mtx_t * mtx ) _PDCLIB_nothrow;
60 int mtx_unlock( mtx_t * mtx ) _PDCLIB_nothrow;
61 #endif
62
63 #ifdef _PDCLIB_CND_T
64 typedef _PDCLIB_CND_T cnd_t;
65 int cnd_broadcast( cnd_t * cond ) _PDCLIB_nothrow;
66 void cnd_destroy( cnd_t * cond ) _PDCLIB_nothrow;
67 int cnd_init( cnd_t * cond ) _PDCLIB_nothrow;
68 int cnd_signal( cnd_t * cond ) _PDCLIB_nothrow;
69 int cnd_timedwait( cnd_t *_PDCLIB_restrict cond, mtx_t * _PDCLIB_restrict mtx, const struct timespec * _PDCLIB_restrict ts ) _PDCLIB_nothrow;
70 int cnd_wait( cnd_t * cond, mtx_t * mtx ) _PDCLIB_nothrow;
71 #endif
72
73 #ifdef _PDCLIB_THRD_T
74 #define _PDCLIB_THRD_HAVE_MISC
75 typedef _PDCLIB_THRD_T thrd_t;
76 typedef int (*thrd_start_t)( void * );
77
78 int thrd_create( thrd_t * thr, thrd_start_t func, void * arg ) _PDCLIB_nothrow;
79 thrd_t thrd_current( void ) _PDCLIB_nothrow;
80 int thrd_detach( thrd_t thr ) _PDCLIB_nothrow;
81 int thrd_equal( thrd_t thr0, thrd_t thr1 ) _PDCLIB_nothrow;
82
83 /* Not nothrow: systems may use exceptions at thread exit */
84 _PDCLIB_noreturn void thrd_exit( int res );
85 /* Not nothrow: systems may potentially propogate exceptions out of thrd_join? */
86 int thrd_join( thrd_t thr, int * res );
87 #endif
88
89 #ifdef _PDCLIB_THRD_HAVE_MISC
90 int thrd_sleep( const struct timespec * duration, struct timespec * remaining ) _PDCLIB_nothrow;
91 void thrd_yield( void ) _PDCLIB_nothrow;
92 #endif
93
94 /* The behaviour of tss_t is woefully underspecified in the C11 standard. In
95    particular, it never specifies where/when/<b>if</b> destructors are called.
96
97    In lieu of any clarification, we assume the behaviour of POSIX pthread_key_t
98 */
99
100 #ifdef _PDCLIB_TSS_T
101 #define TSS_DTOR_ITERATIONS _PDCLIB_TSS_DTOR_ITERATIONS
102
103 typedef _PDCLIB_TSS_T tss_t;
104 typedef void (*tss_dtor_t)( void * );
105
106 int tss_create( tss_t * key, tss_dtor_t dtor ) _PDCLIB_nothrow;
107 void tss_delete( tss_t key ) _PDCLIB_nothrow;
108 void * tss_get( tss_t key ) _PDCLIB_nothrow;
109 int tss_set( tss_t key, void * val ) _PDCLIB_nothrow;
110 #endif
111
112 #ifdef __cplusplus
113 }
114 #endif
115
116 #endif