- Wrap change of entries in an InitChange/ExitChange pair (without this,
[AROS.git] / compiler / pthread / pthread.h
blob67318ed71ae406b2ddd5ed88edf8ed77225a9d89
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 // Basic types
35 /*#ifndef _TIMESPEC_DEFINED
36 #define _TIMESPEC_DEFINED
37 struct timespec
39 long tv_sec;
40 long tv_nsec;
42 #endif*/
44 #ifndef _SCHED_PARAM_DEFINED
45 #define _SCHED_PARAM_DEFINED
46 struct sched_param
48 int sched_priority;
50 #endif
52 #ifndef SCHED_RR
53 #define SCHED_RR 0
54 #endif
57 // POSIX options
60 #define _POSIX_THREADS
61 #define _POSIX_READER_WRITER_LOCKS
62 #define _POSIX_SPIN_LOCKS
63 #define _POSIX_BARRIERS
64 #define _POSIX_THREAD_SAFE_FUNCTIONS
65 #define _POSIX_THREAD_ATTR_STACKSIZE
66 #define _POSIX_THREAD_PRIORITY_SCHEDULING
69 // POSIX limits
72 #define PTHREAD_KEYS_MAX 64
73 #define PTHREAD_STACK_MIN 40960
74 #define PTHREAD_THREADS_MAX 2019
77 // POSIX pthread types
80 typedef unsigned int pthread_t;
81 typedef unsigned int pthread_key_t;
84 // POSIX thread attribute values
87 #define PTHREAD_CREATE_JOINABLE 0
88 #define PTHREAD_CREATE_DETACHED 1
90 #define PTHREAD_INHERIT_SCHED 0
91 #define PTHREAD_EXPLICIT_SCHED 1
93 #define PTHREAD_SCOPE_PROCESS 0
94 #define PTHREAD_SCOPE_SYSTEM 1
96 #define PTHREAD_CANCEL_ENABLE 0
97 #define PTHREAD_CANCEL_DISABLE 1
99 #define PTHREAD_CANCEL_ASYNCHRONOUS 0
100 #define PTHREAD_CANCEL_DEFERRED 1
102 #define PTHREAD_PROCESS_PRIVATE 0
103 #define PTHREAD_PROCESS_SHARED 1
106 // Threads
109 struct pthread_attr
111 void *stackaddr;
112 size_t stacksize;
113 int detachstate;
114 struct sched_param param;
115 int inheritsched;
116 int contentionscope;
119 typedef struct pthread_attr pthread_attr_t;
122 // Once key
125 struct pthread_once
127 volatile int done; // Indicates if user function executed
128 int started; // First thread to increment this value
129 // to zero executes the user function
132 typedef struct pthread_once pthread_once_t;
134 #define PTHREAD_ONCE_INIT {0, -1}
137 // Mutex
140 #define PTHREAD_MUTEX_NORMAL 0
141 #define PTHREAD_MUTEX_RECURSIVE 1
142 //#define PTHREAD_MUTEX_ERRORCHECK 2
143 #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
145 struct pthread_mutexattr
147 int pshared;
148 int kind;
151 typedef struct pthread_mutexattr pthread_mutexattr_t;
153 struct pthread_mutex
155 int kind;
156 struct SignalSemaphore semaphore;
159 typedef struct pthread_mutex pthread_mutex_t;
161 #define NULL_MINLIST {0, 0, {0}}
162 #define NULL_MINNODE {0, 0}
163 #define NULL_NODE {0, 0, 0, 0, 0}
164 #define NULL_SEMAPHOREREQUEST {NULL_MINNODE, 0}
165 #define NULL_SEMAPHORE {NULL_NODE, 0, NULL_MINLIST, NULL_SEMAPHOREREQUEST, 0, 0}
167 #define PTHREAD_MUTEX_INITIALIZER {PTHREAD_MUTEX_NORMAL, NULL_SEMAPHORE}
168 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER {PTHREAD_MUTEX_RECURSIVE, NULL_SEMAPHORE}
171 // Condition variables
174 struct pthread_condattr
176 int pshared;
179 typedef struct pthread_condattr pthread_condattr_t;
181 struct pthread_cond
183 int waiting;
184 struct SignalSemaphore semaphore;
185 struct MinList waiters;
188 typedef struct pthread_cond pthread_cond_t;
190 #define PTHREAD_COND_INITIALIZER {0, NULL_SEMAPHORE, NULL_MINLIST}
193 // POSIX thread routines
196 #ifdef __cplusplus
197 extern "C" {
198 #endif
201 // Thread attribute functions
204 int pthread_attr_init(pthread_attr_t *attr);
205 int pthread_attr_destroy(pthread_attr_t *attr);
206 int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
207 int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
208 int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t *stacksize);
209 int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize);
210 int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
211 int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
212 int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param);
213 int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);
214 int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy);
215 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
216 int pthread_attr_getinheritsched(pthread_attr_t *attr, int *inheritsched);
217 int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
218 int pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope);
219 int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope);
222 // Thread functions
225 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start)(void *), void *arg);
226 int pthread_detach(pthread_t thread);
227 int pthread_equal(pthread_t t1, pthread_t t2);
228 void pthread_exit(void *value_ptr);
229 int pthread_join(pthread_t thread, void **value_ptr);
230 pthread_t pthread_self(void);
231 int pthread_cancel(pthread_t thread);
232 int pthread_setcancelstate(int state, int *oldstate);
233 int pthread_setcanceltype(int type, int *oldtype);
234 void pthread_testcancel(void);
235 int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
238 // Scheduling functions
241 int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param);
242 int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param);
243 int pthread_setconcurrency(int level);
244 int pthread_getconcurrency(void);
247 // Thread specific data functions
250 int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
251 int pthread_key_delete(pthread_key_t key);
252 int pthread_setspecific(pthread_key_t key, const void *value);
253 void *pthread_getspecific(pthread_key_t key);
256 // Mutex attribute functions
259 int pthread_mutexattr_init(pthread_mutexattr_t *attr);
260 int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
261 int pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr, int *pshared);
262 int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
263 int pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *kind);
264 int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind);
267 // Mutex functions
270 int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
271 int pthread_mutex_destroy(pthread_mutex_t *mutex);
272 int pthread_mutex_lock(pthread_mutex_t *mutex);
273 int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
274 int pthread_mutex_trylock(pthread_mutex_t *mutex);
275 int pthread_mutex_unlock(pthread_mutex_t *mutex);
278 // Condition variable functions
281 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
282 int pthread_cond_destroy(pthread_cond_t *cond);
283 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
284 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);
285 int pthread_cond_signal(pthread_cond_t *cond);
286 int pthread_cond_broadcast(pthread_cond_t *cond);
289 // NP
292 int pthread_setname_np(pthread_t thread, const char *name);
293 int pthread_getname_np(pthread_t thread, char *name, size_t len);
296 // Cancellation cleanup
299 void pthread_cleanup_push(void (*routine)(void *), void *arg);
300 void pthread_cleanup_pop(int execute);
303 // Signalling
306 int pthread_kill(pthread_t thread, int sig);
308 #ifdef __cplusplus
310 #endif
312 #endif