1 /* 2 * Copyright 2001-2011 Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _PTHREAD_H_ 6 #define _PTHREAD_H_ 7 8 9 #include <sched.h> 10 #include <stdint.h> 11 #include <sys/types.h> 12 #include <time.h> 13 14 15 #define PTHREAD_MUTEX_DEFAULT 0 16 #define PTHREAD_MUTEX_NORMAL 1 17 #define PTHREAD_MUTEX_ERRORCHECK 2 18 #define PTHREAD_MUTEX_RECURSIVE 3 19 20 #define PTHREAD_PROCESS_PRIVATE 0 21 #define PTHREAD_PROCESS_SHARED 1 22 23 /* 24 * Flags for threads and thread attributes. 25 */ 26 #define PTHREAD_DETACHED 0x1 27 #define PTHREAD_SCOPE_SYSTEM 0x2 28 #define PTHREAD_INHERIT_SCHED 0x4 29 #define PTHREAD_NOFLOAT 0x8 30 31 #define PTHREAD_CREATE_DETACHED PTHREAD_DETACHED 32 #define PTHREAD_CREATE_JOINABLE 0 33 #define PTHREAD_SCOPE_PROCESS 0 34 #define PTHREAD_EXPLICIT_SCHED 0 35 36 /* 37 * Flags for cancelling threads 38 */ 39 #define PTHREAD_CANCEL_ENABLE 0 40 #define PTHREAD_CANCEL_DISABLE 1 41 #define PTHREAD_CANCEL_DEFERRED 0 42 #define PTHREAD_CANCEL_ASYNCHRONOUS 2 43 #define PTHREAD_CANCELED ((void *) 1) 44 45 #define PTHREAD_ONCE_INIT { -1 } 46 47 #define PTHREAD_BARRIER_SERIAL_THREAD 1 48 49 #define PTHREAD_PRIO_NONE 0 50 #define PTHREAD_PRIO_INHERIT 1 51 #define PTHREAD_PRIO_PROTECT 2 52 53 54 /* private structure */ 55 struct __pthread_cleanup_handler { 56 struct __pthread_cleanup_handler *previous; 57 void (*function)(void *argument); 58 void *argument; 59 }; 60 61 #define pthread_cleanup_push(func, arg) \ 62 do { \ 63 struct __pthread_cleanup_handler __handler; \ 64 __handler.function = (func); \ 65 __handler.argument = (arg); \ 66 __pthread_cleanup_push_handler(&__handler); 67 68 #define pthread_cleanup_pop(execute) \ 69 if (execute) \ 70 __handler.function(__handler.argument); \ 71 __pthread_cleanup_pop_handler(); \ 72 } while (0) 73 74 75 #ifdef __cplusplus 76 extern "C" { 77 #endif 78 79 #define PTHREAD_MUTEX_INITIALIZER \ 80 { PTHREAD_MUTEX_DEFAULT, 0, -42, -1, 0 } 81 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER \ 82 { PTHREAD_MUTEX_RECURSIVE, 0, -42, -1, 0 } 83 #define PTHREAD_COND_INITIALIZER \ 84 { 0, -42, NULL, 0, 0 } 85 #define PTHREAD_RWLOCK_INITIALIZER \ 86 { 0, -1, {{0}} } 87 88 /* mutex functions */ 89 extern int pthread_mutex_destroy(pthread_mutex_t *mutex); 90 extern int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex, 91 int *_priorityCeiling); 92 extern int pthread_mutex_init(pthread_mutex_t *mutex, 93 const pthread_mutexattr_t *attr); 94 extern int pthread_mutex_lock(pthread_mutex_t *mutex); 95 extern int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, 96 int newPriorityCeiling, int *_oldPriorityCeiling); 97 extern int pthread_mutex_clocklock(pthread_mutex_t *mutex, 98 clockid_t clock_id, const struct timespec *abstime); 99 extern int pthread_mutex_timedlock(pthread_mutex_t *mutex, 100 const struct timespec *abstime); 101 extern int pthread_mutex_trylock(pthread_mutex_t *mutex); 102 extern int pthread_mutex_unlock(pthread_mutex_t *mutex); 103 104 /* mutex attribute functions */ 105 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *mutexAttr); 106 extern int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *mutexAttr, 107 int *_priorityCeiling); 108 extern int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *mutexAttr, 109 int *_protocol); 110 extern int pthread_mutexattr_getpshared(const pthread_mutexattr_t *mutexAttr, 111 int *_processShared); 112 extern int pthread_mutexattr_gettype(const pthread_mutexattr_t *mutexAttr, 113 int *_type); 114 extern int pthread_mutexattr_init(pthread_mutexattr_t *mutexAttr); 115 extern int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mutexAttr, 116 int priorityCeiling); 117 extern int pthread_mutexattr_setprotocol(pthread_mutexattr_t *mutexAttr, 118 int protocol); 119 extern int pthread_mutexattr_setpshared(pthread_mutexattr_t *mutexAttr, 120 int processShared); 121 extern int pthread_mutexattr_settype(pthread_mutexattr_t *mutexAttr, int type); 122 123 /* barrier functions */ 124 extern int pthread_barrier_init(pthread_barrier_t *barrier, 125 const pthread_barrierattr_t *attr, unsigned count); 126 extern int pthread_barrier_destroy(pthread_barrier_t *barrier); 127 extern int pthread_barrier_wait(pthread_barrier_t *barrier); 128 129 /* barrier attribute functions */ 130 extern int pthread_barrierattr_destroy(pthread_barrierattr_t *attr); 131 extern int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, 132 int *shared); 133 extern int pthread_barrierattr_init(pthread_barrierattr_t *attr); 134 extern int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, 135 int shared); 136 137 /* condition variable functions */ 138 extern int pthread_cond_destroy(pthread_cond_t *cond); 139 extern int pthread_cond_init(pthread_cond_t *cond, 140 const pthread_condattr_t *attr); 141 extern int pthread_cond_broadcast(pthread_cond_t *cond); 142 extern int pthread_cond_signal(pthread_cond_t *cond); 143 extern int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, 144 clockid_t clock_id, const struct timespec *abstime); 145 extern int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, 146 const struct timespec *abstime); 147 extern int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); 148 149 /* condition variable attribute functions */ 150 extern int pthread_condattr_destroy(pthread_condattr_t *condAttr); 151 extern int pthread_condattr_init(pthread_condattr_t *condAttr); 152 extern int pthread_condattr_getpshared(const pthread_condattr_t *condAttr, 153 int *processShared); 154 extern int pthread_condattr_setpshared(pthread_condattr_t *condAttr, 155 int processShared); 156 extern int pthread_condattr_getclock(const pthread_condattr_t *condAttr, 157 clockid_t *clockID); 158 extern int pthread_condattr_setclock(pthread_condattr_t *condAttr, 159 clockid_t clockID); 160 161 /* rwlock functions */ 162 extern int pthread_rwlock_init(pthread_rwlock_t *lock, 163 const pthread_rwlockattr_t *attr); 164 extern int pthread_rwlock_destroy(pthread_rwlock_t *lock); 165 extern int pthread_rwlock_rdlock(pthread_rwlock_t *lock); 166 extern int pthread_rwlock_tryrdlock(pthread_rwlock_t *lock); 167 extern int pthread_rwlock_clockrdlock(pthread_rwlock_t* rwlock, 168 clockid_t clock_id, const struct timespec* abstime); 169 extern int pthread_rwlock_timedrdlock(pthread_rwlock_t *lock, 170 const struct timespec *abstime); 171 extern int pthread_rwlock_wrlock(pthread_rwlock_t *lock); 172 extern int pthread_rwlock_trywrlock(pthread_rwlock_t *lock); 173 extern int pthread_rwlock_clockwrlock(pthread_rwlock_t* rwlock, 174 clockid_t clock_id, const struct timespec* abstime); 175 extern int pthread_rwlock_timedwrlock(pthread_rwlock_t *lock, 176 const struct timespec *abstime); 177 extern int pthread_rwlock_unlock(pthread_rwlock_t *lock); 178 179 /* rwlock attribute functions */ 180 extern int pthread_rwlockattr_init(pthread_rwlockattr_t *attr); 181 extern int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr); 182 extern int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, 183 int *shared); 184 extern int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, 185 int shared); 186 187 /* spinlock functions */ 188 extern int pthread_spin_init(pthread_spinlock_t* spinlock, int pshared); 189 extern int pthread_spin_destroy(pthread_spinlock_t* spinlock); 190 extern int pthread_spin_lock(pthread_spinlock_t* spinlock); 191 extern int pthread_spin_trylock(pthread_spinlock_t* spinlock); 192 extern int pthread_spin_unlock(pthread_spinlock_t* spinlock); 193 194 /* misc. functions */ 195 extern int pthread_atfork(void (*prepare)(void), void (*parent)(void), 196 void (*child)(void)); 197 extern int pthread_once(pthread_once_t *once_control, 198 void (*init_routine)(void)); 199 200 /* thread attributes functions */ 201 extern int pthread_attr_destroy(pthread_attr_t *attr); 202 extern int pthread_attr_init(pthread_attr_t *attr); 203 extern int pthread_attr_getdetachstate(const pthread_attr_t *attr, 204 int *detachstate); 205 extern int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); 206 extern int pthread_attr_getstacksize(const pthread_attr_t *attr, 207 size_t *stacksize); 208 extern int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize); 209 extern int pthread_attr_getscope(const pthread_attr_t *attr, 210 int *contentionScope); 211 extern int pthread_attr_setscope(pthread_attr_t *attr, int contentionScope); 212 213 extern int pthread_attr_getschedparam(const pthread_attr_t *attr, 214 struct sched_param *param); 215 extern int pthread_attr_setschedparam(pthread_attr_t *attr, 216 const struct sched_param *param); 217 218 extern int pthread_attr_getguardsize(const pthread_attr_t *attr, 219 size_t *guardsize); 220 extern int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize); 221 222 extern int pthread_attr_getstack(const pthread_attr_t *attr, 223 void **stackaddr, size_t *stacksize); 224 extern int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, 225 size_t stacksize); 226 227 #if 0 /* Unimplemented attribute functions: */ 228 229 /* [TPS] */ 230 extern int pthread_attr_getinheritsched(const pthread_attr_t *attr, 231 int *inheritsched); 232 extern int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched); 233 234 extern int pthread_attr_getschedpolicy(const pthread_attr_t *attr, 235 int *policy); 236 extern int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy); 237 238 #endif /* 0 */ 239 240 /* thread functions */ 241 extern int pthread_create(pthread_t *thread, const pthread_attr_t *attr, 242 void *(*start_routine)(void*), void *arg); 243 extern int pthread_detach(pthread_t thread); 244 extern int pthread_equal(pthread_t t1, pthread_t t2); 245 extern void pthread_exit(void *value_ptr) __attribute__ ((noreturn)); 246 extern int pthread_join(pthread_t thread, void **_value); 247 extern pthread_t pthread_self(void); 248 extern int pthread_getconcurrency(void); 249 extern int pthread_setconcurrency(int newLevel); 250 251 extern int pthread_cancel(pthread_t thread); 252 extern int pthread_setcancelstate(int state, int *_oldState); 253 extern int pthread_setcanceltype(int type, int *_oldType); 254 extern void pthread_testcancel(void); 255 256 extern int pthread_getschedparam(pthread_t thread, int *policy, 257 struct sched_param *param); 258 extern int pthread_setschedparam(pthread_t thread, int policy, 259 const struct sched_param *param); 260 261 /* thread specific data functions */ 262 extern int pthread_key_create(pthread_key_t *key, 263 void (*destructorFunc)(void*)); 264 extern int pthread_key_delete(pthread_key_t key); 265 extern void *pthread_getspecific(pthread_key_t key); 266 extern int pthread_setspecific(pthread_key_t key, const void *value); 267 268 /* private functions */ 269 extern void __pthread_cleanup_push_handler( 270 struct __pthread_cleanup_handler *handler); 271 extern struct __pthread_cleanup_handler *__pthread_cleanup_pop_handler(void); 272 273 #ifdef __cplusplus 274 } 275 #endif 276 277 #endif /* _PTHREAD_ */ 278