1 /* 2 * Copyright 2001-2008, Haiku Inc. All Rights Reserved. 3 * Distributed under the terms of the Haiku License. 4 */ 5 #ifndef _PTHREAD_H_ 6 #define _PTHREAD_H_ 7 8 #include <time.h> 9 10 typedef int pthread_t; 11 typedef struct _pthread_attr *pthread_attr_t; 12 typedef struct _pthread_mutex *pthread_mutex_t; 13 typedef struct _pthread_mutexattr *pthread_mutexattr_t; 14 typedef struct _pthread_cond *pthread_cond_t; 15 typedef struct _pthread_condattr *pthread_condattr_t; 16 typedef int pthread_key_t; 17 typedef struct _pthread_once pthread_once_t; 18 typedef struct _pthread_rwlock *pthread_rwlock_t; 19 typedef struct _pthread_rwlockattr *pthread_rwlockattr_t; 20 typedef struct _pthread_barrier *pthread_barrier_t; 21 typedef struct _pthread_barrierattr *pthread_barrierattr_t; 22 typedef struct _pthread_spinlock *pthread_spinlock_t; 23 24 struct _pthread_once { 25 int state; 26 pthread_mutex_t mutex; 27 }; 28 29 30 enum pthread_mutex_type { 31 PTHREAD_MUTEX_DEFAULT, 32 PTHREAD_MUTEX_NORMAL, 33 PTHREAD_MUTEX_ERRORCHECK, 34 PTHREAD_MUTEX_RECURSIVE, 35 }; 36 37 enum pthread_process_shared { 38 PTHREAD_PROCESS_PRIVATE, 39 PTHREAD_PROCESS_SHARED, 40 }; 41 42 /* 43 * Flags for threads and thread attributes. 44 */ 45 #define PTHREAD_DETACHED 0x1 46 #define PTHREAD_SCOPE_SYSTEM 0x2 47 #define PTHREAD_INHERIT_SCHED 0x4 48 #define PTHREAD_NOFLOAT 0x8 49 50 #define PTHREAD_CREATE_DETACHED PTHREAD_DETACHED 51 #define PTHREAD_CREATE_JOINABLE 0 52 #define PTHREAD_SCOPE_PROCESS 0 53 #define PTHREAD_EXPLICIT_SCHED 0 54 55 /* 56 * Flags for cancelling threads 57 */ 58 #define PTHREAD_CANCEL_ENABLE 0 59 #define PTHREAD_CANCEL_DISABLE 1 60 #define PTHREAD_CANCEL_DEFERRED 0 61 #define PTHREAD_CANCEL_ASYNCHRONOUS 2 62 #define PTHREAD_CANCELED ((void *) 1) 63 64 #define PTHREAD_NEEDS_INIT 0 65 #define PTHREAD_DONE_INIT 1 66 #define PTHREAD_ONCE_INIT { PTHREAD_NEEDS_INIT, NULL } 67 68 #define PTHREAD_BARRIER_SERIAL_THREAD -1 69 #define PTHREAD_PRIO_NONE 0 70 #define PTHREAD_PRIO_INHERIT 1 71 #define PTHREAD_PRIO_PROTECT 2 72 73 //extern pthread_mutexattr_t pthread_mutexattr_default; 74 75 /* private structure */ 76 struct __pthread_cleanup_handler { 77 struct __pthread_cleanup_handler *previous; 78 void (*function)(void *argument); 79 void *argument; 80 }; 81 82 #define pthread_cleanup_push(func, arg) \ 83 do { \ 84 struct __pthread_cleanup_handler __handler; \ 85 __handler.function = (func); \ 86 __handler.argument = (arg); \ 87 __pthread_cleanup_push_handler(&__handler); 88 89 #define pthread_cleanup_pop(execute) \ 90 if (execute) \ 91 __handler.function(__handler.argument); \ 92 __pthread_cleanup_pop_handler(); \ 93 } while (0) 94 95 96 #ifdef __cplusplus 97 extern "C" { 98 #endif 99 100 extern pthread_mutex_t _pthread_mutex_static_initializer(void); 101 extern pthread_mutex_t _pthread_recursive_mutex_static_initializer(void); 102 #define PTHREAD_MUTEX_INITIALIZER NULL 103 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER \ 104 pthread_recursive_mutex_static_initializer(); 105 106 extern pthread_cond_t _pthread_cond_static_initializer(void); 107 #define PTHREAD_COND_INITIALIZER _pthread_cond_static_initializer(); 108 109 /* mutex functions */ 110 extern int pthread_mutex_destroy(pthread_mutex_t *mutex); 111 extern int pthread_mutex_getprioceiling(pthread_mutex_t *mutex, int *_priorityCeiling); 112 extern int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); 113 extern int pthread_mutex_lock(pthread_mutex_t *mutex); 114 extern int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int newPriorityCeiling, 115 int *_oldPriorityCeiling); 116 extern int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *spec); 117 extern int pthread_mutex_trylock(pthread_mutex_t *mutex); 118 extern int pthread_mutex_unlock(pthread_mutex_t *mutex); 119 120 /* mutex attribute functions */ 121 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *mutexAttr); 122 extern int pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mutexAttr, int *_priorityCeiling); 123 extern int pthread_mutexattr_getprotocol(pthread_mutexattr_t *mutexAttr, int *_protocol); 124 extern int pthread_mutexattr_getpshared(pthread_mutexattr_t *mutexAttr, int *_processShared); 125 extern int pthread_mutexattr_gettype(pthread_mutexattr_t *mutexAttr, int *_type); 126 extern int pthread_mutexattr_init(pthread_mutexattr_t *mutexAttr); 127 extern int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mutexAttr, int priorityCeiling); 128 extern int pthread_mutexattr_setprotocol(pthread_mutexattr_t *mutexAttr, int protocol); 129 extern int pthread_mutexattr_setpshared(pthread_mutexattr_t *mutexAttr, int processShared); 130 extern int pthread_mutexattr_settype(pthread_mutexattr_t *mutexAttr, int type); 131 132 /* condition variable functions */ 133 extern int pthread_cond_destroy(pthread_cond_t *cond); 134 extern int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); 135 extern int pthread_cond_broadcast(pthread_cond_t *cond); 136 extern int pthread_cond_signal(pthread_cond_t *cond); 137 extern int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, 138 const struct timespec *abstime); 139 extern int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); 140 141 /* condition variable attribute functions */ 142 extern int pthread_condattr_destroy(pthread_condattr_t *condAttr); 143 extern int pthread_condattr_init(pthread_condattr_t *condAttr); 144 extern int pthread_condattr_getpshared(const pthread_condattr_t *condAttr, int *processShared); 145 extern int pthread_condattr_setpshared(pthread_condattr_t *condAttr, int processShared); 146 147 /* misc. functions */ 148 extern int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void)); 149 extern int pthread_once(pthread_once_t *once_control, void (*init_routine)()); 150 151 /* thread attributes functions */ 152 extern int pthread_attr_destroy(pthread_attr_t *attr); 153 extern int pthread_attr_init(pthread_attr_t *attr); 154 extern int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate); 155 extern int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); 156 157 /* thread functions */ 158 extern int pthread_create(pthread_t *thread, const pthread_attr_t *attr, 159 void *(*start_routine)(void*), void *arg); 160 extern int pthread_detach(pthread_t thread); 161 extern int pthread_equal(pthread_t t1, pthread_t t2); 162 extern void pthread_exit(void *value_ptr); 163 extern int pthread_join(pthread_t thread, void **_value); 164 extern pthread_t pthread_self(void); 165 extern int pthread_kill(pthread_t thread, int sig); 166 extern int pthread_getconcurrency(void); 167 extern int pthread_setconcurrency(int newLevel); 168 169 extern int pthread_cancel(pthread_t thread); 170 extern int pthread_setcancelstate(int state, int *_oldState); 171 extern int pthread_setcanceltype(int type, int *_oldType); 172 extern void pthread_testcancel(void); 173 174 /* thread specific data functions */ 175 extern int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)); 176 extern int pthread_key_delete(pthread_key_t key); 177 extern void *pthread_getspecific(pthread_key_t key); 178 extern int pthread_setspecific(pthread_key_t key, const void *value); 179 180 /* private functions */ 181 extern void __pthread_cleanup_push_handler( 182 struct __pthread_cleanup_handler *handler); 183 extern struct __pthread_cleanup_handler *__pthread_cleanup_pop_handler(void); 184 185 #ifdef __cplusplus 186 } 187 #endif 188 189 #endif /* _PTHREAD_ */ 190