compiler/pthread: port latest version
[AROS.git] / compiler / pthread / pthread.h
blob9678fd4068ee527cd7914e95e4570ab053f7ec90
1 /*
2 Copyright (C) 2014 Szilard Biro
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
21 #ifndef PTHREAD_H
22 #define PTHREAD_H
24 #include <sys/types.h>
25 #include <sys/time.h>
26 #include <errno.h>
27 #include <exec/types.h>
28 #include <exec/semaphores.h>
29 #include <sched.h>
32 // POSIX options
35 #undef _POSIX_THREADS
36 #define _POSIX_THREADS
37 #undef _POSIX_READER_WRITER_LOCKS
38 #define _POSIX_READER_WRITER_LOCKS
39 #undef _POSIX_SPIN_LOCKS
40 #define _POSIX_SPIN_LOCKS
41 #undef _POSIX_THREAD_ATTR_STACKSIZE
42 #define _POSIX_THREAD_ATTR_STACKSIZE
43 #undef _POSIX_THREAD_PRIORITY_SCHEDULING
44 #define _POSIX_THREAD_PRIORITY_SCHEDULING
47 // POSIX limits
50 #define PTHREAD_KEYS_MAX 64
51 #define PTHREAD_STACK_MIN 40960
52 #define PTHREAD_THREADS_MAX 2019
53 #define PTHREAD_DESTRUCTOR_ITERATIONS 4
56 // POSIX pthread types
59 typedef unsigned int pthread_t;
60 typedef unsigned int pthread_key_t;
63 // POSIX thread attribute values
66 #define PTHREAD_CREATE_JOINABLE 0
67 #define PTHREAD_CREATE_DETACHED 1
69 #define PTHREAD_INHERIT_SCHED 0
70 #define PTHREAD_EXPLICIT_SCHED 1
72 #define PTHREAD_SCOPE_PROCESS 0
73 #define PTHREAD_SCOPE_SYSTEM 1
75 #define PTHREAD_CANCEL_ENABLE 0
76 #define PTHREAD_CANCEL_DISABLE 1
78 #define PTHREAD_CANCEL_ASYNCHRONOUS 0
79 #define PTHREAD_CANCEL_DEFERRED 1
81 #define PTHREAD_PROCESS_PRIVATE 0
82 #define PTHREAD_PROCESS_SHARED 1
85 // Threads
88 struct pthread_attr
90 void *stackaddr;
91 size_t stacksize;
92 int detachstate;
93 struct sched_param param;
94 int inheritsched;
95 int contentionscope;
98 typedef struct pthread_attr pthread_attr_t;
100 #define PTHREAD_CANCELED ((void *)-1)
103 // Once key
106 struct pthread_once
108 volatile int done;
109 int started;
110 int lock;
113 typedef struct pthread_once pthread_once_t;
115 #define PTHREAD_ONCE_INIT {0, -1, 0}
118 // Mutex
121 #define PTHREAD_MUTEX_NORMAL 0
122 #define PTHREAD_MUTEX_RECURSIVE 1
123 #define PTHREAD_MUTEX_ERRORCHECK 2
124 #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
126 struct pthread_mutexattr
128 int pshared;
129 int kind;
132 typedef struct pthread_mutexattr pthread_mutexattr_t;
134 struct pthread_mutex
136 int kind;
137 struct SignalSemaphore semaphore;
138 int incond;
141 typedef struct pthread_mutex pthread_mutex_t;
143 #ifndef __AROS__
144 #define NULL_MINLIST {0, 0, 0}
145 #else
146 #define NULL_MINLIST {0, 0, {0}}
147 #endif
148 #define NULL_MINNODE {0, 0}
149 #define NULL_NODE {0, 0, 0, 0, 0}
150 #define NULL_SEMAPHOREREQUEST {NULL_MINNODE, 0}
151 #define NULL_SEMAPHORE {NULL_NODE, 0, NULL_MINLIST, NULL_SEMAPHOREREQUEST, 0, 0}
153 #define PTHREAD_MUTEX_INITIALIZER {PTHREAD_MUTEX_NORMAL, NULL_SEMAPHORE}
154 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER {PTHREAD_MUTEX_RECURSIVE, NULL_SEMAPHORE}
155 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER {PTHREAD_MUTEX_ERRORCHECK, NULL_SEMAPHORE}
158 // Condition variables
161 struct pthread_condattr
163 int pshared;
166 typedef struct pthread_condattr pthread_condattr_t;
168 struct pthread_cond
170 int pad1;
171 struct SignalSemaphore semaphore;
172 struct MinList waiters;
175 typedef struct pthread_cond pthread_cond_t;
177 #define PTHREAD_COND_INITIALIZER {0, NULL_SEMAPHORE, NULL_MINLIST}
180 // Barriers
183 #define PTHREAD_BARRIER_SERIAL_THREAD 1
185 struct pthread_barrierattr
187 int pshared;
190 typedef struct pthread_barrierattr pthread_barrierattr_t;
192 struct pthread_barrier
194 unsigned int curr_height;
195 unsigned int total_height;
196 pthread_cond_t breeched;
197 pthread_mutex_t lock;
200 typedef struct pthread_barrier pthread_barrier_t;
203 // Read-write locks
206 struct pthread_rwlockattr
208 int pshared;
211 typedef struct pthread_rwlockattr pthread_rwlockattr_t;
213 struct pthread_rwlock
215 struct SignalSemaphore semaphore;
218 typedef struct pthread_rwlock pthread_rwlock_t;
220 #define PTHREAD_RWLOCK_INITIALIZER {NULL_SEMAPHORE}
223 // Spinlocks
226 typedef int pthread_spinlock_t;
228 #define PTHREAD_SPINLOCK_INITIALIZER 0
231 // POSIX thread routines
234 #ifdef __cplusplus
235 extern "C" {
236 #endif
239 // Thread attribute functions
242 int pthread_attr_init(pthread_attr_t *attr);
243 int pthread_attr_destroy(pthread_attr_t *attr);
244 int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
245 int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
246 int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t *stacksize);
247 int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize);
248 int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
249 int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
250 int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param);
251 int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);
252 int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy);
253 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
254 int pthread_attr_getinheritsched(pthread_attr_t *attr, int *inheritsched);
255 int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
256 int pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope);
257 int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope);
260 // Thread functions
263 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start)(void *), void *arg);
264 int pthread_detach(pthread_t thread);
265 int pthread_equal(pthread_t t1, pthread_t t2);
266 void pthread_exit(void *value_ptr);
267 int pthread_join(pthread_t thread, void **value_ptr);
268 pthread_t pthread_self(void);
269 int pthread_cancel(pthread_t thread);
270 int pthread_setcancelstate(int state, int *oldstate);
271 int pthread_setcanceltype(int type, int *oldtype);
272 void pthread_testcancel(void);
273 int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
276 // Scheduling functions
279 int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param);
280 int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param);
281 int pthread_setconcurrency(int level);
282 int pthread_getconcurrency(void);
285 // Thread specific data functions
288 int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
289 int pthread_key_delete(pthread_key_t key);
290 int pthread_setspecific(pthread_key_t key, const void *value);
291 void *pthread_getspecific(pthread_key_t key);
294 // Mutex attribute functions
297 int pthread_mutexattr_init(pthread_mutexattr_t *attr);
298 int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
299 int pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr, int *pshared);
300 int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
301 int pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *kind);
302 int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind);
305 // Mutex functions
308 int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
309 int pthread_mutex_destroy(pthread_mutex_t *mutex);
310 int pthread_mutex_lock(pthread_mutex_t *mutex);
311 int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
312 int pthread_mutex_trylock(pthread_mutex_t *mutex);
313 int pthread_mutex_unlock(pthread_mutex_t *mutex);
316 // Condition variable attribute functions
319 int pthread_condattr_init(pthread_condattr_t *attr);
320 int pthread_condattr_destroy(pthread_condattr_t *attr);
321 int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared);
322 int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared);
325 // Condition variable functions
328 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
329 int pthread_cond_destroy(pthread_cond_t *cond);
330 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
331 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);
332 int pthread_cond_signal(pthread_cond_t *cond);
333 int pthread_cond_broadcast(pthread_cond_t *cond);
336 // Barrier attribute functions
339 int pthread_barrierattr_init(pthread_barrierattr_t *attr);
340 int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);
341 int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, int *pshared);
342 int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared);
345 // Barrier functions
348 int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count);
349 int pthread_barrier_destroy(pthread_barrier_t *barrier);
350 int pthread_barrier_wait(pthread_barrier_t *barrier);
353 // Read-write lock attribute functions
356 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);
357 int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
358 int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, int *pshared);
359 int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared);
362 // Read-write lock functions
365 int pthread_rwlock_init(pthread_rwlock_t *lock, const pthread_rwlockattr_t *attr);
366 int pthread_rwlock_destroy(pthread_rwlock_t *lock);
367 int pthread_rwlock_tryrdlock(pthread_rwlock_t *lock);
368 int pthread_rwlock_trywrlock(pthread_rwlock_t *lock);
369 int pthread_rwlock_rdlock(pthread_rwlock_t *lock);
370 int pthread_rwlock_timedrdlock(pthread_rwlock_t *lock, const struct timespec *abstime);
371 int pthread_rwlock_wrlock(pthread_rwlock_t *lock);
372 int pthread_rwlock_timedwrlock(pthread_rwlock_t *lock, const struct timespec *abstime);
373 int pthread_rwlock_unlock(pthread_rwlock_t *lock);
376 // Spinlock functions
379 int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
380 int pthread_spin_destroy(pthread_spinlock_t *lock);
381 int pthread_spin_lock(pthread_spinlock_t *lock);
382 int pthread_spin_trylock(pthread_spinlock_t *lock);
383 int pthread_spin_unlock(pthread_spinlock_t *lock);
386 // Non-portable functions
389 int pthread_setname_np(pthread_t thread, const char *name);
390 int pthread_getname_np(pthread_t thread, char *name, size_t len);
391 int pthread_cond_timedwait_relative_np(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *reltime);
394 // Cancellation cleanup
397 void pthread_cleanup_push(void (*routine)(void *), void *arg);
398 void pthread_cleanup_pop(int execute);
401 // Signalling
404 int pthread_kill(pthread_t thread, int sig);
407 // Wrap cancellation points
410 #ifdef _UNISTD_H_
411 #define close(...) (pthread_testcancel(), close(__VA_ARGS__))
412 #define fsync(...) (pthread_testcancel(), fsync(__VA_ARGS__))
413 #define read(...) (pthread_testcancel(), read(__VA_ARGS__))
414 #ifdef __MORPHOS__
415 #define select(...) (pthread_testcancel(), select(__VA_ARGS__))
416 #endif
417 #define sleep(...) (pthread_testcancel(), sleep(__VA_ARGS__))
418 #define usleep(...) (pthread_testcancel(), usleep(__VA_ARGS__))
419 #endif
421 #ifdef _FCNTL_H_
422 #define creat(...) (pthread_testcancel(), creat(__VA_ARGS__))
423 #define fcntl(...) (pthread_testcancel(), fcntl(__VA_ARGS__))
424 #define open(...) (pthread_testcancel(), open(__VA_ARGS__))
425 #endif
427 #ifdef _TIME_H_
428 #define nanosleep(...) (pthread_testcancel(), nanosleep(__VA_ARGS__))
429 #endif
431 #ifdef _SYS_UIO_H_
432 #define readv(...) (pthread_testcancel(), readv(__VA_ARGS__))
433 #endif
435 #ifdef _STDLIB_H_
436 #define system(...) (pthread_testcancel(), system(__VA_ARGS__))
437 #endif
439 #if defined(CLIB_BSDSOCKET_PROTOS_H) || defined(CLIB_SOCKET_PROTOS_H)
440 #define accept(...) (pthread_testcancel(), accept(__VA_ARGS__))
441 #define connect(...) (pthread_testcancel(), connect(__VA_ARGS__))
442 #define CloseSocket(...) (pthread_testcancel(), CloseSocket(__VA_ARGS__))
443 #define recv(...) (pthread_testcancel(), recv(__VA_ARGS__))
444 #define recvfrom(...) (pthread_testcancel(), recvfrom(__VA_ARGS__))
445 #define recvmsg(...) (pthread_testcancel(), recvmsg(__VA_ARGS__))
446 #ifdef __AROS__
447 #define select(...) (pthread_testcancel(), select(__VA_ARGS__))
448 #endif
449 #define send(...) (pthread_testcancel(), send(__VA_ARGS__))
450 #define sendmsg(...) (pthread_testcancel(), sendmsg(__VA_ARGS__))
451 #define sendto(...) (pthread_testcancel(), sendto(__VA_ARGS__))
452 #endif
454 #ifdef __cplusplus
456 #endif
458 #endif