145b820ddSAxel Dörfler /* 224df6592SIngo Weinhold * Copyright 2001-2011 Haiku, Inc. All Rights Reserved. 347a21c5cSAugustin Cavalier * Distributed under the terms of the MIT License. 445b820ddSAxel Dörfler */ 5f924ff50SAxel Dörfler #ifndef _PTHREAD_H_ 6f924ff50SAxel Dörfler #define _PTHREAD_H_ 7f924ff50SAxel Dörfler 8945fb7e3SAxel Dörfler 92c9d7792SJérôme Duval #include <sched.h> 10370602bfSIngo Weinhold #include <stdint.h> 1124df6592SIngo Weinhold #include <sys/types.h> 12f924ff50SAxel Dörfler #include <time.h> 13f924ff50SAxel Dörfler 14945fb7e3SAxel Dörfler 1502e6d76cSIngo Weinhold #define PTHREAD_MUTEX_DEFAULT 0 1602e6d76cSIngo Weinhold #define PTHREAD_MUTEX_NORMAL 1 1702e6d76cSIngo Weinhold #define PTHREAD_MUTEX_ERRORCHECK 2 1802e6d76cSIngo Weinhold #define PTHREAD_MUTEX_RECURSIVE 3 19f924ff50SAxel Dörfler 2002e6d76cSIngo Weinhold #define PTHREAD_PROCESS_PRIVATE 0 2102e6d76cSIngo Weinhold #define PTHREAD_PROCESS_SHARED 1 22f924ff50SAxel Dörfler 23a00ed9faSJérôme Duval /* 24a00ed9faSJérôme Duval * Flags for threads and thread attributes. 25a00ed9faSJérôme Duval */ 26a00ed9faSJérôme Duval #define PTHREAD_DETACHED 0x1 27a00ed9faSJérôme Duval #define PTHREAD_SCOPE_SYSTEM 0x2 28a00ed9faSJérôme Duval #define PTHREAD_INHERIT_SCHED 0x4 29a00ed9faSJérôme Duval #define PTHREAD_NOFLOAT 0x8 30f924ff50SAxel Dörfler 31a00ed9faSJérôme Duval #define PTHREAD_CREATE_DETACHED PTHREAD_DETACHED 32a00ed9faSJérôme Duval #define PTHREAD_CREATE_JOINABLE 0 33a00ed9faSJérôme Duval #define PTHREAD_SCOPE_PROCESS 0 34a00ed9faSJérôme Duval #define PTHREAD_EXPLICIT_SCHED 0 35f924ff50SAxel Dörfler 36a00ed9faSJérôme Duval /* 37a00ed9faSJérôme Duval * Flags for cancelling threads 38a00ed9faSJérôme Duval */ 39a00ed9faSJérôme Duval #define PTHREAD_CANCEL_ENABLE 0 40a00ed9faSJérôme Duval #define PTHREAD_CANCEL_DISABLE 1 41a00ed9faSJérôme Duval #define PTHREAD_CANCEL_DEFERRED 0 42a00ed9faSJérôme Duval #define PTHREAD_CANCEL_ASYNCHRONOUS 2 43a00ed9faSJérôme Duval #define PTHREAD_CANCELED ((void *) 1) 44a00ed9faSJérôme Duval 453ca1072dSIngo Weinhold #define PTHREAD_ONCE_INIT { -1 } 46a00ed9faSJérôme Duval 47*b24cc7caSAugustin Cavalier #define PTHREAD_BARRIER_SERIAL_THREAD 1 48*b24cc7caSAugustin Cavalier 49a00ed9faSJérôme Duval #define PTHREAD_PRIO_NONE 0 50a00ed9faSJérôme Duval #define PTHREAD_PRIO_INHERIT 1 51a00ed9faSJérôme Duval #define PTHREAD_PRIO_PROTECT 2 52f924ff50SAxel Dörfler 53799ab823SJérôme Duval 54a635399bSAxel Dörfler /* private structure */ 55a635399bSAxel Dörfler struct __pthread_cleanup_handler { 56a635399bSAxel Dörfler struct __pthread_cleanup_handler *previous; 57a635399bSAxel Dörfler void (*function)(void *argument); 58a635399bSAxel Dörfler void *argument; 59a635399bSAxel Dörfler }; 60a635399bSAxel Dörfler 61a635399bSAxel Dörfler #define pthread_cleanup_push(func, arg) \ 62a635399bSAxel Dörfler do { \ 63a635399bSAxel Dörfler struct __pthread_cleanup_handler __handler; \ 64a635399bSAxel Dörfler __handler.function = (func); \ 65a635399bSAxel Dörfler __handler.argument = (arg); \ 66a635399bSAxel Dörfler __pthread_cleanup_push_handler(&__handler); 67a635399bSAxel Dörfler 68a635399bSAxel Dörfler #define pthread_cleanup_pop(execute) \ 69a635399bSAxel Dörfler if (execute) \ 70a635399bSAxel Dörfler __handler.function(__handler.argument); \ 71a635399bSAxel Dörfler __pthread_cleanup_pop_handler(); \ 72a635399bSAxel Dörfler } while (0) 73a635399bSAxel Dörfler 74a635399bSAxel Dörfler 7545b820ddSAxel Dörfler #ifdef __cplusplus 7645b820ddSAxel Dörfler extern "C" { 7745b820ddSAxel Dörfler #endif 7845b820ddSAxel Dörfler 793ca1072dSIngo Weinhold #define PTHREAD_MUTEX_INITIALIZER \ 803ca1072dSIngo Weinhold { PTHREAD_MUTEX_DEFAULT, 0, -42, -1, 0 } 81f924ff50SAxel Dörfler #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER \ 823ca1072dSIngo Weinhold { PTHREAD_MUTEX_RECURSIVE, 0, -42, -1, 0 } 83a8af2b6dSIngo Weinhold #define PTHREAD_COND_INITIALIZER \ 84a8af2b6dSIngo Weinhold { 0, -42, NULL, 0, 0 } 856c9e0126SJérôme Duval #define PTHREAD_RWLOCK_INITIALIZER \ 866c9e0126SJérôme Duval { 0, -1, {{0}} } 87550a30a0SRyan Leavengood 88f924ff50SAxel Dörfler /* mutex functions */ 89f924ff50SAxel Dörfler extern int pthread_mutex_destroy(pthread_mutex_t *mutex); 900bec83a8SJérôme Duval extern int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex, 91945fb7e3SAxel Dörfler int *_priorityCeiling); 92945fb7e3SAxel Dörfler extern int pthread_mutex_init(pthread_mutex_t *mutex, 93945fb7e3SAxel Dörfler const pthread_mutexattr_t *attr); 94f924ff50SAxel Dörfler extern int pthread_mutex_lock(pthread_mutex_t *mutex); 95945fb7e3SAxel Dörfler extern int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, 96945fb7e3SAxel Dörfler int newPriorityCeiling, int *_oldPriorityCeiling); 97e41d4bd1SJérôme Duval extern int pthread_mutex_clocklock(pthread_mutex_t *mutex, 98e41d4bd1SJérôme Duval clockid_t clock_id, const struct timespec *abstime); 99945fb7e3SAxel Dörfler extern int pthread_mutex_timedlock(pthread_mutex_t *mutex, 100e41d4bd1SJérôme Duval const struct timespec *abstime); 101f924ff50SAxel Dörfler extern int pthread_mutex_trylock(pthread_mutex_t *mutex); 102f924ff50SAxel Dörfler extern int pthread_mutex_unlock(pthread_mutex_t *mutex); 103f924ff50SAxel Dörfler 104f924ff50SAxel Dörfler /* mutex attribute functions */ 105f924ff50SAxel Dörfler extern int pthread_mutexattr_destroy(pthread_mutexattr_t *mutexAttr); 1060bec83a8SJérôme Duval extern int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *mutexAttr, 107945fb7e3SAxel Dörfler int *_priorityCeiling); 1080bec83a8SJérôme Duval extern int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *mutexAttr, 109945fb7e3SAxel Dörfler int *_protocol); 1100bec83a8SJérôme Duval extern int pthread_mutexattr_getpshared(const pthread_mutexattr_t *mutexAttr, 111945fb7e3SAxel Dörfler int *_processShared); 1120bec83a8SJérôme Duval extern int pthread_mutexattr_gettype(const pthread_mutexattr_t *mutexAttr, 113945fb7e3SAxel Dörfler int *_type); 114f924ff50SAxel Dörfler extern int pthread_mutexattr_init(pthread_mutexattr_t *mutexAttr); 115945fb7e3SAxel Dörfler extern int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mutexAttr, 116945fb7e3SAxel Dörfler int priorityCeiling); 117945fb7e3SAxel Dörfler extern int pthread_mutexattr_setprotocol(pthread_mutexattr_t *mutexAttr, 118945fb7e3SAxel Dörfler int protocol); 119945fb7e3SAxel Dörfler extern int pthread_mutexattr_setpshared(pthread_mutexattr_t *mutexAttr, 120945fb7e3SAxel Dörfler int processShared); 121f924ff50SAxel Dörfler extern int pthread_mutexattr_settype(pthread_mutexattr_t *mutexAttr, int type); 122f924ff50SAxel Dörfler 1230e0f49e7SDmytro Shynkevych /* barrier functions */ 1240e0f49e7SDmytro Shynkevych extern int pthread_barrier_init(pthread_barrier_t *barrier, 1250e0f49e7SDmytro Shynkevych const pthread_barrierattr_t *attr, unsigned count); 1260e0f49e7SDmytro Shynkevych extern int pthread_barrier_destroy(pthread_barrier_t *barrier); 1270e0f49e7SDmytro Shynkevych extern int pthread_barrier_wait(pthread_barrier_t *barrier); 1280e0f49e7SDmytro Shynkevych 1290e0f49e7SDmytro Shynkevych /* barrier attribute functions */ 1300e0f49e7SDmytro Shynkevych extern int pthread_barrierattr_destroy(pthread_barrierattr_t *attr); 1310e0f49e7SDmytro Shynkevych extern int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, 1320e0f49e7SDmytro Shynkevych int *shared); 1330e0f49e7SDmytro Shynkevych extern int pthread_barrierattr_init(pthread_barrierattr_t *attr); 1340e0f49e7SDmytro Shynkevych extern int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, 1350e0f49e7SDmytro Shynkevych int shared); 1360e0f49e7SDmytro Shynkevych 137550a30a0SRyan Leavengood /* condition variable functions */ 138550a30a0SRyan Leavengood extern int pthread_cond_destroy(pthread_cond_t *cond); 139945fb7e3SAxel Dörfler extern int pthread_cond_init(pthread_cond_t *cond, 140945fb7e3SAxel Dörfler const pthread_condattr_t *attr); 141550a30a0SRyan Leavengood extern int pthread_cond_broadcast(pthread_cond_t *cond); 142550a30a0SRyan Leavengood extern int pthread_cond_signal(pthread_cond_t *cond); 143ceb7b761SJérôme Duval extern int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, 144ceb7b761SJérôme Duval clockid_t clock_id, const struct timespec *abstime); 145550a30a0SRyan Leavengood extern int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, 146550a30a0SRyan Leavengood const struct timespec *abstime); 147550a30a0SRyan Leavengood extern int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); 148550a30a0SRyan Leavengood 149550a30a0SRyan Leavengood /* condition variable attribute functions */ 150550a30a0SRyan Leavengood extern int pthread_condattr_destroy(pthread_condattr_t *condAttr); 151550a30a0SRyan Leavengood extern int pthread_condattr_init(pthread_condattr_t *condAttr); 152945fb7e3SAxel Dörfler extern int pthread_condattr_getpshared(const pthread_condattr_t *condAttr, 153945fb7e3SAxel Dörfler int *processShared); 154945fb7e3SAxel Dörfler extern int pthread_condattr_setpshared(pthread_condattr_t *condAttr, 155945fb7e3SAxel Dörfler int processShared); 15610b4fed2SHamish Morrison extern int pthread_condattr_getclock(const pthread_condattr_t *condAttr, 15710b4fed2SHamish Morrison clockid_t *clockID); 15810b4fed2SHamish Morrison extern int pthread_condattr_setclock(pthread_condattr_t *condAttr, 15910b4fed2SHamish Morrison clockid_t clockID); 160550a30a0SRyan Leavengood 161370602bfSIngo Weinhold /* rwlock functions */ 162370602bfSIngo Weinhold extern int pthread_rwlock_init(pthread_rwlock_t *lock, 163370602bfSIngo Weinhold const pthread_rwlockattr_t *attr); 164370602bfSIngo Weinhold extern int pthread_rwlock_destroy(pthread_rwlock_t *lock); 165370602bfSIngo Weinhold extern int pthread_rwlock_rdlock(pthread_rwlock_t *lock); 166370602bfSIngo Weinhold extern int pthread_rwlock_tryrdlock(pthread_rwlock_t *lock); 167d96e266bSJérôme Duval extern int pthread_rwlock_clockrdlock(pthread_rwlock_t* rwlock, 168d96e266bSJérôme Duval clockid_t clock_id, const struct timespec* abstime); 169370602bfSIngo Weinhold extern int pthread_rwlock_timedrdlock(pthread_rwlock_t *lock, 170d96e266bSJérôme Duval const struct timespec *abstime); 171370602bfSIngo Weinhold extern int pthread_rwlock_wrlock(pthread_rwlock_t *lock); 172370602bfSIngo Weinhold extern int pthread_rwlock_trywrlock(pthread_rwlock_t *lock); 173d96e266bSJérôme Duval extern int pthread_rwlock_clockwrlock(pthread_rwlock_t* rwlock, 174d96e266bSJérôme Duval clockid_t clock_id, const struct timespec* abstime); 175370602bfSIngo Weinhold extern int pthread_rwlock_timedwrlock(pthread_rwlock_t *lock, 176d96e266bSJérôme Duval const struct timespec *abstime); 177370602bfSIngo Weinhold extern int pthread_rwlock_unlock(pthread_rwlock_t *lock); 178370602bfSIngo Weinhold 179370602bfSIngo Weinhold /* rwlock attribute functions */ 180370602bfSIngo Weinhold extern int pthread_rwlockattr_init(pthread_rwlockattr_t *attr); 181370602bfSIngo Weinhold extern int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr); 182370602bfSIngo Weinhold extern int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, 183370602bfSIngo Weinhold int *shared); 184370602bfSIngo Weinhold extern int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, 185370602bfSIngo Weinhold int shared); 186370602bfSIngo Weinhold 187ef1716f8SIngo Weinhold /* spinlock functions */ 188ef1716f8SIngo Weinhold extern int pthread_spin_init(pthread_spinlock_t* spinlock, int pshared); 189ef1716f8SIngo Weinhold extern int pthread_spin_destroy(pthread_spinlock_t* spinlock); 190ef1716f8SIngo Weinhold extern int pthread_spin_lock(pthread_spinlock_t* spinlock); 191ef1716f8SIngo Weinhold extern int pthread_spin_trylock(pthread_spinlock_t* spinlock); 192ef1716f8SIngo Weinhold extern int pthread_spin_unlock(pthread_spinlock_t* spinlock); 193ef1716f8SIngo Weinhold 19445b820ddSAxel Dörfler /* misc. functions */ 195945fb7e3SAxel Dörfler extern int pthread_atfork(void (*prepare)(void), void (*parent)(void), 196945fb7e3SAxel Dörfler void (*child)(void)); 1973ca1072dSIngo Weinhold extern int pthread_once(pthread_once_t *once_control, 1983ca1072dSIngo Weinhold void (*init_routine)(void)); 19945b820ddSAxel Dörfler 200a00ed9faSJérôme Duval /* thread attributes functions */ 201a00ed9faSJérôme Duval extern int pthread_attr_destroy(pthread_attr_t *attr); 202a00ed9faSJérôme Duval extern int pthread_attr_init(pthread_attr_t *attr); 203945fb7e3SAxel Dörfler extern int pthread_attr_getdetachstate(const pthread_attr_t *attr, 204945fb7e3SAxel Dörfler int *detachstate); 205a00ed9faSJérôme Duval extern int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); 206f7cc12b3SIngo Weinhold extern int pthread_attr_getstacksize(const pthread_attr_t *attr, 207f7cc12b3SIngo Weinhold size_t *stacksize); 208f7cc12b3SIngo Weinhold extern int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize); 209a411c76cSAxel Dörfler extern int pthread_attr_getscope(const pthread_attr_t *attr, 210a411c76cSAxel Dörfler int *contentionScope); 211a411c76cSAxel Dörfler extern int pthread_attr_setscope(pthread_attr_t *attr, int contentionScope); 212f7cc12b3SIngo Weinhold 213f7cc12b3SIngo Weinhold extern int pthread_attr_getschedparam(const pthread_attr_t *attr, 214f7cc12b3SIngo Weinhold struct sched_param *param); 215f7cc12b3SIngo Weinhold extern int pthread_attr_setschedparam(pthread_attr_t *attr, 216f7cc12b3SIngo Weinhold const struct sched_param *param); 217f7cc12b3SIngo Weinhold 218d1f280c8SHamish Morrison extern int pthread_attr_getguardsize(const pthread_attr_t *attr, 219d1f280c8SHamish Morrison size_t *guardsize); 220d1f280c8SHamish Morrison extern int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize); 221d1f280c8SHamish Morrison 222901c3d44SJérôme Duval extern int pthread_attr_getstack(const pthread_attr_t *attr, 223901c3d44SJérôme Duval void **stackaddr, size_t *stacksize); 224901c3d44SJérôme Duval extern int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, 225901c3d44SJérôme Duval size_t stacksize); 226901c3d44SJérôme Duval 227f386c591SJérôme Duval #if 0 /* Unimplemented attribute functions: */ 228f386c591SJérôme Duval 229b9320128SIngo Weinhold /* [TPS] */ 230f7cc12b3SIngo Weinhold extern int pthread_attr_getinheritsched(const pthread_attr_t *attr, 231f7cc12b3SIngo Weinhold int *inheritsched); 232f7cc12b3SIngo Weinhold extern int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched); 233f7cc12b3SIngo Weinhold 234f7cc12b3SIngo Weinhold extern int pthread_attr_getschedpolicy(const pthread_attr_t *attr, 235f7cc12b3SIngo Weinhold int *policy); 236f7cc12b3SIngo Weinhold extern int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy); 237f7cc12b3SIngo Weinhold 238f7cc12b3SIngo Weinhold #endif /* 0 */ 239f7cc12b3SIngo Weinhold 240c80e6107SAxel Dörfler /* thread functions */ 241a00ed9faSJérôme Duval extern int pthread_create(pthread_t *thread, const pthread_attr_t *attr, 242a00ed9faSJérôme Duval void *(*start_routine)(void*), void *arg); 243a00ed9faSJérôme Duval extern int pthread_detach(pthread_t thread); 244a00ed9faSJérôme Duval extern int pthread_equal(pthread_t t1, pthread_t t2); 24530749200SLeorize extern void pthread_exit(void *value_ptr) __attribute__ ((noreturn)); 246c80e6107SAxel Dörfler extern int pthread_join(pthread_t thread, void **_value); 247a00ed9faSJérôme Duval extern pthread_t pthread_self(void); 248b9fe86d2SAxel Dörfler extern int pthread_getconcurrency(void); 249b9fe86d2SAxel Dörfler extern int pthread_setconcurrency(int newLevel); 250ad6ada0bSJérôme Duval 251c80e6107SAxel Dörfler extern int pthread_cancel(pthread_t thread); 252c80e6107SAxel Dörfler extern int pthread_setcancelstate(int state, int *_oldState); 253c80e6107SAxel Dörfler extern int pthread_setcanceltype(int type, int *_oldType); 254c80e6107SAxel Dörfler extern void pthread_testcancel(void); 255c80e6107SAxel Dörfler 25666204ee8SJérôme Duval extern int pthread_getschedparam(pthread_t thread, int *policy, 25766204ee8SJérôme Duval struct sched_param *param); 25866204ee8SJérôme Duval extern int pthread_setschedparam(pthread_t thread, int policy, 25966204ee8SJérôme Duval const struct sched_param *param); 26066204ee8SJérôme Duval 2611f11178fSJérôme Duval /* thread specific data functions */ 262945fb7e3SAxel Dörfler extern int pthread_key_create(pthread_key_t *key, 263945fb7e3SAxel Dörfler void (*destructorFunc)(void*)); 2641f11178fSJérôme Duval extern int pthread_key_delete(pthread_key_t key); 2651f11178fSJérôme Duval extern void *pthread_getspecific(pthread_key_t key); 2661f11178fSJérôme Duval extern int pthread_setspecific(pthread_key_t key, const void *value); 2671f11178fSJérôme Duval 268a635399bSAxel Dörfler /* private functions */ 269a635399bSAxel Dörfler extern void __pthread_cleanup_push_handler( 270a635399bSAxel Dörfler struct __pthread_cleanup_handler *handler); 271a635399bSAxel Dörfler extern struct __pthread_cleanup_handler *__pthread_cleanup_pop_handler(void); 272a635399bSAxel Dörfler 27345b820ddSAxel Dörfler #ifdef __cplusplus 27445b820ddSAxel Dörfler } 27545b820ddSAxel Dörfler #endif 27645b820ddSAxel Dörfler 277f924ff50SAxel Dörfler #endif /* _PTHREAD_ */ 278