#ifndef _THREADS_H #define _THREADS_H /* This one is explicitly allowed to be included. */ #include #ifdef __cplusplus extern "C" { #endif #define __NEED_cnd_t #define __NEED_mtx_t /* Until we come up with a better naming scheme, we need to expose some pthread types. */ #define __NEED_pthread_cond_t #define __NEED_pthread_mutex_t #define __NEED_pthread_once_t #define __NEED_pthread_t #define __NEED_pthread_key_t #include #include typedef struct __mtx_on_heap __mtx_t; void __mtx_unref(__mtx_t *) __attribute__((nonnull(1))); __mtx_t* __mtx_getref(mtx_t *) __attribute__((nonnull(1))); int __mtx_lock(__mtx_t *) __attribute__((nonnull(1))); int __mtx_trylock(__mtx_t *) __attribute__((nonnull(1))); int __mtx_unlock(__mtx_t *) __attribute__((nonnull(1))); int __mtx_timedlock(__mtx_t *restrict, const struct timespec *restrict) __attribute__((nonnull(1))); typedef struct __cnd_on_heap __cnd_t; void __cnd_unref(__cnd_t *) __attribute__((nonnull(1))); __cnd_t* __cnd_getref(cnd_t *, __mtx_t *, int *) __attribute__((nonnull(1,3))); int __cnd_wait(__cnd_t *restrict, __mtx_t *restrict) __attribute__((nonnull(1,2))); typedef pthread_t thrd_t; typedef pthread_key_t tss_t; typedef int (*thrd_start_t)(void*); typedef void (*tss_dtor_t)(void*); typedef struct once_flag once_flag; /* Just have a copy of the definition for the moment. This has to be replaced by sharing that declaration in alltypes.h */ struct __once_cb { void (*__f)(void *); void *__x; struct __ptcb *__next; }; struct once_flag { int __cntrl; int __waiters; struct __once_cb __cb; }; #ifndef __THRD_EXPERIMENTAL # define __THRD_EXPERIMENTAL 1 #endif /* The following list of 9 integer constants makes up for the binary compatibility of this C thread implementation. You must never link code against versions of the C library that do not agree upon these ABI parameters. Additionally this implementation assumes that the 5 types have the same size across C libraries and that these types can be initialized by the default initializer. The values for the 9 parameters are not fixed for now. Depending on the choices of other implementations and the evolution of the C standard they may still change. This should happen rarely, but you have to consider the C thread feature to be experimental until then, and be prepared to recompile your binary when linking against a different implementation or a new version. The macro __THRD_EXPERIMENTAL will be defined as long as we consider this ABI to be unstable. This comes with some link time checks and an overhead of some bytes. At your own risk you may switch this check off by defining __THRD_EXPERIMENTAL macro to 0. */ #define __THRD_SUCCESS 0 #define __THRD_BUSY EBUSY #define __THRD_ERROR EINVAL #define __THRD_NOMEM ENOMEM #define __THRD_TIMEDOUT ETIMEDOUT #define __MTX_PLAIN 0 #define __MTX_RECURSIVE 1 #define __MTX_TIMED 2 #define TSS_DTOR_ITERATIONS 4 #define __THRD_CONCAT10_(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) \ _0 ## _ ## _1 ## _ ## _2 ## _ ## _3 ## _ ## _4 ## _ ## _5 ## _ ## _6 ## _ ## _7 ## _ ## _8 ## _ ## _9 #define __THRD_CONCAT10(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) \ __THRD_CONCAT10_(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) #define __THRD_ABI \ __THRD_CONCAT10(__thrd_abi, \ __THRD_SUCCESS, \ __THRD_BUSY, \ __THRD_ERROR, \ __THRD_NOMEM, \ __THRD_TIMEDOUT, \ __MTX_PLAIN, \ __MTX_RECURSIVE, \ __MTX_TIMED, \ TSS_DTOR_ITERATIONS \ ) #define __THRD_SHFT(X, Y) (((X) << 3) ^ (Y)) enum { __thrd_abi = __THRD_SHFT(sizeof(cnd_t), __THRD_SHFT(sizeof(mtx_t), __THRD_SHFT(sizeof(once_flag), __THRD_SHFT(sizeof(thrd_t), sizeof(tss_t))))), }; extern unsigned const __THRD_ABI; #define __THRD_ABI_CHECK (1/(__THRD_ABI == __thrd_abi)) #if __THRD_EXPERIMENTAL # define __THRD_ABI_MARK __attribute__((used)) static unsigned const*const __thrd_abi_mark = &__THRD_ABI #else # define __THRD_ABI_MARK typedef unsigned __thrd_abi_dummy_type #endif enum { thrd_success = __THRD_SUCCESS, thrd_busy = __THRD_BUSY, thrd_error = __THRD_ERROR, thrd_nomem = __THRD_NOMEM, thrd_timedout = __THRD_TIMEDOUT, }; enum { mtx_plain = __MTX_PLAIN, mtx_recursive = __MTX_RECURSIVE, // all mutexes are timed, here. so this is a no-op mtx_timed = __MTX_TIMED, }; #define ONCE_FLAG_INIT { ._cntrl = 0, } #define thread_local _Thread_local int thrd_create(thrd_t *, thrd_start_t, void *); _Noreturn void thrd_exit(int); int thrd_detach(thrd_t); int thrd_join(thrd_t, int *); int thrd_sleep(const struct timespec *, struct timespec *); void thrd_yield(void); thrd_t thrd_current(void); int thrd_equal(thrd_t, thrd_t); #define thrd_equal(A, B) ((A) == (B)) void call_once(once_flag *, void (*)(void)); int mtx_init(mtx_t *, int); void mtx_destroy(mtx_t *); int mtx_lock(mtx_t *); int mtx_timedlock(mtx_t *restrict, const struct timespec *restrict); int mtx_trylock(mtx_t *); int mtx_unlock(mtx_t *); int cnd_init(cnd_t *); void cnd_destroy(cnd_t *); int cnd_broadcast(cnd_t *); int cnd_signal(cnd_t *); int cnd_timedwait(cnd_t *restrict, mtx_t *restrict, const struct timespec *restrict); int cnd_wait(cnd_t *, mtx_t *); int tss_create(tss_t *, tss_dtor_t); void tss_delete(tss_t key); int tss_set(tss_t, void *); void *tss_get(tss_t); #ifdef __cplusplus } #endif #endif