threads.h raw

   1  #ifndef _THREADS_H
   2  #define _THREADS_H
   3  
   4  #include <features.h>
   5  #include <time.h>
   6  
   7  #ifdef __cplusplus
   8  extern "C" {
   9  typedef unsigned long thrd_t;
  10  #else
  11  typedef struct __pthread *thrd_t;
  12  #define thread_local _Thread_local
  13  #endif
  14  
  15  typedef int once_flag;
  16  typedef unsigned tss_t;
  17  typedef int (*thrd_start_t)(void *);
  18  typedef void (*tss_dtor_t)(void *);
  19  
  20  #define __NEED_cnd_t
  21  #define __NEED_mtx_t
  22  
  23  #include <bits/alltypes.h>
  24  
  25  #define TSS_DTOR_ITERATIONS 4
  26  
  27  enum {
  28  	thrd_success  = 0,
  29  	thrd_busy     = 1,
  30  	thrd_error    = 2,
  31  	thrd_nomem    = 3,
  32  	thrd_timedout = 4,
  33  };
  34  
  35  enum {
  36  	mtx_plain     = 0,
  37  	mtx_recursive = 1,
  38  	mtx_timed     = 2,
  39  };
  40  
  41  #define ONCE_FLAG_INIT 0
  42  
  43  int thrd_create(thrd_t *, thrd_start_t, void *);
  44  _Noreturn void thrd_exit(int);
  45  
  46  int thrd_detach(thrd_t);
  47  int thrd_join(thrd_t, int *);
  48  
  49  int thrd_sleep(const struct timespec *, struct timespec *);
  50  void thrd_yield(void);
  51  
  52  thrd_t thrd_current(void);
  53  int thrd_equal(thrd_t, thrd_t);
  54  #ifndef __cplusplus
  55  #define thrd_equal(A, B) ((A) == (B))
  56  #endif
  57  
  58  void call_once(once_flag *, void (*)(void));
  59  
  60  int mtx_init(mtx_t *, int);
  61  void mtx_destroy(mtx_t *);
  62  
  63  int mtx_lock(mtx_t *);
  64  int mtx_timedlock(mtx_t *__restrict, const struct timespec *__restrict);
  65  int mtx_trylock(mtx_t *);
  66  int mtx_unlock(mtx_t *);
  67  
  68  int cnd_init(cnd_t *);
  69  void cnd_destroy(cnd_t *);
  70  
  71  int cnd_broadcast(cnd_t *);
  72  int cnd_signal(cnd_t *);
  73  
  74  int cnd_timedwait(cnd_t *__restrict, mtx_t *__restrict, const struct timespec *__restrict);
  75  int cnd_wait(cnd_t *, mtx_t *);
  76  
  77  int tss_create(tss_t *, tss_dtor_t);
  78  void tss_delete(tss_t);
  79  
  80  int tss_set(tss_t, void *);
  81  void *tss_get(tss_t);
  82  
  83  #if _REDIR_TIME64
  84  __REDIR(thrd_sleep, __thrd_sleep_time64);
  85  __REDIR(mtx_timedlock, __mtx_timedlock_time64);
  86  __REDIR(cnd_timedwait, __cnd_timedwait_time64);
  87  #endif
  88  
  89  #ifdef __cplusplus
  90  }
  91  #endif
  92  
  93  #endif
  94