1 /* 2 * Copyright (C) 2015-2021 Alibaba Group Holding Limited 3 */ 4 5 #ifndef _PTHREAD_H 6 #define _PTHREAD_H 7 8 #include <stdint.h> 9 #include <stddef.h> 10 #include <sys/timespec.h> 11 #include <sched.h> 12 13 #ifdef __cplusplus 14 extern "C" { 15 #endif 16 17 #define PTHREAD_CREATE_JOINABLE 0 18 #define PTHREAD_CREATE_DETACHED 1 19 20 #define PTHREAD_MUTEX_NORMAL 0 21 #define PTHREAD_MUTEX_DEFAULT 1 22 #define PTHREAD_MUTEX_RECURSIVE 1 23 #define PTHREAD_MUTEX_ERRORCHECK 2 24 25 #define PTHREAD_PRIO_NONE 0 26 #define PTHREAD_PRIO_INHERIT 1 27 #define PTHREAD_PRIO_PROTECT 2 28 29 #define PTHREAD_INHERIT_SCHED 0 30 #define PTHREAD_EXPLICIT_SCHED 1 31 32 #define PTHREAD_SCOPE_SYSTEM 0 33 #define PTHREAD_SCOPE_PROCESS 1 34 35 #define PTHREAD_PROCESS_PRIVATE 0 36 #define PTHREAD_PROCESS_SHARED 1 37 38 #define PTHREAD_CANCEL_ENABLE 0 39 #define PTHREAD_CANCEL_DISABLE 1 40 #define PTHREAD_CANCEL_MASKED 2 41 #define PTHREAD_CANCEL_DEFERRED 0 42 #define PTHREAD_CANCEL_ASYNCHRONOUS 1 43 #define PTHREAD_CANCELED ((void *)-1) 44 45 #define PTHREAD_DYN_INIT 0X01 46 #define PTHREAD_STATIC_INIT 0X02 47 #define PTHREAD_MUTEX_INITIALIZER {PTHREAD_STATIC_INIT, NULL, {0}} 48 #define PTHREAD_COND_INITIALIZER {PTHREAD_STATIC_INIT, 0, 0, NULL, NULL, NULL, {0}} 49 #define PTHREAD_ONCE_INIT 0 50 51 typedef void * pthread_t; 52 typedef int pthread_once_t; 53 54 typedef struct pthread_attr { 55 uint8_t flag; 56 void *stackaddr; /* the start addr of the stack of the pthead */ 57 size_t stacksize; /* the size of the stack of the pthead */ 58 size_t guardsize; /* guardsize is set to protect the stack, not supported */ 59 int contentionscope; /* the scope of contention, only PTHREAD_SCOPE_SYSTEM is supported */ 60 int inheritsched; /* when set to PTHREAD_INHERIT_SCHED, specifies that the thread scheduling attributes 61 shall be inherited from the creating thread, and the scheduling attributes in this 62 attr argument shall be ignored */ 63 int schedpolicy; /* the sched policy of the thread */ 64 int detachstate; /* when set to PTHREAD_CREATE_JOINABLE, thread will not end untill the creating thread end */ 65 int sched_priority; /* The thread scheduling priority */ 66 uint64_t sched_slice; /* The time slice in SCHED_RR mode (ms) */ 67 } pthread_attr_t; 68 69 typedef struct pthread_mutexattr { 70 uint8_t flag; 71 int type; 72 int protocol; 73 int prioceiling; 74 int pshared; 75 } pthread_mutexattr_t; 76 77 typedef struct pthread_mutex { 78 uint8_t flag; 79 void *mutex; 80 pthread_mutexattr_t attr; 81 } pthread_mutex_t; 82 83 typedef struct pthread_condattr { 84 uint8_t flag; 85 int pshared; /* allow this to be shared amongst processes */ 86 clock_t clock; /* specifiy clock for timeouts */ 87 } pthread_condattr_t; 88 89 typedef struct pthread_cond { 90 uint8_t flag; 91 int waiting; 92 int signals; 93 void *lock; 94 void *wait_sem; 95 void *wait_done; 96 pthread_condattr_t attr; 97 } pthread_cond_t; 98 99 /********* Thread Specific Data *********/ 100 typedef int pthread_key_t; 101 102 int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void)); 103 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, 104 void *(*start_routine)(void *), void *arg); 105 void pthread_exit(void *value_ptr); 106 int pthread_detach(pthread_t thread); 107 int pthread_join(pthread_t thread, void **retval); 108 int pthread_cancel(pthread_t thread); 109 void pthread_testcancel(void); 110 int pthread_setcancelstate(int state, int *oldstate); 111 int pthread_setcanceltype(int type, int *oldtype); 112 int pthread_kill(pthread_t thread, int sig); 113 int pthread_equal(pthread_t t1, pthread_t t2); 114 int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *pParam); 115 void pthread_cleanup_pop(int execute); 116 void pthread_cleanup_push(void (*routine)(void *), void *arg); 117 int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); 118 pthread_t pthread_self(void); 119 int pthread_getcpuclockid(pthread_t thread_id, clockid_t *clock_id); 120 int pthread_setconcurrency(int new_level); 121 int pthread_getconcurrency(void); 122 int pthread_setschedprio(pthread_t thread, int prio); 123 int pthread_setname_np(pthread_t thread, const char *name); 124 int pthread_timedjoin_np(pthread_t thread, void **retval, const struct timespec *abstime); 125 126 int pthread_attr_init(pthread_attr_t *attr); 127 int pthread_attr_destroy(pthread_attr_t *attr); 128 int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); 129 int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate); 130 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy); 131 int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy); 132 int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param); 133 int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param); 134 int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize); 135 int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize); 136 int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr); 137 int pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr); 138 int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize); 139 int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t *stacksize); 140 int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched); 141 int pthread_attr_getinheritsched(const pthread_attr_t *__restrict attr, int *__restrict inheritsched); 142 int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize); 143 int pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize); 144 int pthread_attr_setscope(pthread_attr_t *attr, int scope); 145 int pthread_attr_getscope(const pthread_attr_t *attr, int *scope); 146 147 int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); 148 int pthread_mutex_destroy(pthread_mutex_t *mutex); 149 int pthread_mutex_lock(pthread_mutex_t *mutex); 150 int pthread_mutex_unlock(pthread_mutex_t *mutex); 151 int pthread_mutex_trylock(pthread_mutex_t *mutex); 152 int pthread_mutex_getprioceiling(const pthread_mutex_t *__restrict mutex, int *__restrict prioceiling); 153 int pthread_mutex_setprioceiling(pthread_mutex_t *__restrict mutex, int prioceiling, int *__restrict old_ceiling); 154 155 int pthread_mutexattr_init(pthread_mutexattr_t *attr); 156 int pthread_mutexattr_destroy(pthread_mutexattr_t *attr); 157 int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type); 158 int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type); 159 int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared); 160 int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared); 161 int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol); 162 int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *__restrict attr, int *__restrict protocol); 163 int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *__restrict attr, int *__restrict prioceiling); 164 int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling); 165 166 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); 167 int pthread_cond_destroy(pthread_cond_t *cond); 168 int pthread_cond_broadcast(pthread_cond_t *cond); 169 int pthread_cond_signal(pthread_cond_t *cond); 170 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); 171 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime); 172 173 int pthread_condattr_init(pthread_condattr_t *attr); 174 int pthread_condattr_destroy(pthread_condattr_t *attr); 175 int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock); 176 177 int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)); 178 int pthread_setspecific(pthread_key_t key, const void *value); 179 void* pthread_getspecific(pthread_key_t key); 180 int pthread_key_delete(pthread_key_t key); 181 182 #ifdef __cplusplus 183 } 184 #endif 185 #endif /* _PTHREAD_H */ 186