1 | /* |
---|
2 | * pthread.h - pthread API |
---|
3 | * |
---|
4 | * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless |
---|
5 | * Copyright (c) 2011,2012 UPMC Sorbonne Universites |
---|
6 | * |
---|
7 | * This file is part of ALMOS. |
---|
8 | * |
---|
9 | * ALMOS is free software; you can redistribute it and/or modify it |
---|
10 | * under the terms of the GNU General Public License as published by |
---|
11 | * the Free Software Foundation; version 2.0 of the License. |
---|
12 | * |
---|
13 | * ALMOS is distributed in the hope that it will be useful, but |
---|
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | * General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with ALMOS; if not, write to the Free Software Foundation, |
---|
20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef _PTHREAD_H_ |
---|
24 | #define _PTHREAD_H_ |
---|
25 | |
---|
26 | #include <sys/types.h> |
---|
27 | #include <sched.h> |
---|
28 | #include <sys/list.h> |
---|
29 | #include <semaphore.h> |
---|
30 | |
---|
31 | /* START COPYING FROM KERNEL HEADER */ |
---|
32 | #define __PT_ATTR_DEFAULT 0x000 |
---|
33 | #define __PT_ATTR_DETACH 0x001 /* for compatiblity */ |
---|
34 | #define __PT_FORK_WILL_EXEC 0x001 /* for compatiblity */ |
---|
35 | #define __PT_FORK_USE_TARGET_CPU 0x002 /* for compatiblity */ |
---|
36 | #define __PT_ATTR_LEGACY_MASK 0x003 /* TODO: remove legacy attr issue*/ |
---|
37 | |
---|
38 | #define __PT_FORK_USE_AFFINITY 0x004 |
---|
39 | #define __PT_ATTR_MEM_PRIO 0x008 |
---|
40 | #define __PT_ATTR_INTERLEAVE_SEQ 0x010 |
---|
41 | #define __PT_ATTR_INTERLEAVE_ALL 0x020 |
---|
42 | #define __PT_ATTR_AUTO_MGRT 0x040 |
---|
43 | #define __PT_ATTR_AUTO_NXTT 0x080 |
---|
44 | #define __PT_ATTR_MEM_CID_RR 0x100 |
---|
45 | |
---|
46 | typedef struct |
---|
47 | { |
---|
48 | uint_t key; |
---|
49 | uint_t flags; |
---|
50 | uint_t sched_policy; |
---|
51 | uint_t inheritsched; |
---|
52 | void *stack_addr; |
---|
53 | size_t stack_size; |
---|
54 | void *entry_func; |
---|
55 | void *exit_func; |
---|
56 | void *arg1; |
---|
57 | void *arg2; |
---|
58 | void *sigreturn_func; |
---|
59 | void *sigstack_addr; |
---|
60 | size_t sigstack_size; |
---|
61 | struct sched_param sched_param; |
---|
62 | sint_t cid; |
---|
63 | sint_t cpu_lid; |
---|
64 | sint_t cpu_gid; |
---|
65 | uint_t tid; // kernel cookie can be used in debuggin userland |
---|
66 | pid_t pid; |
---|
67 | } pthread_attr_t; |
---|
68 | /* END COPYING FROM KERNEL HEADER */ |
---|
69 | |
---|
70 | /* Pthread related constants */ |
---|
71 | #define PTHREAD_THREADS_MAX 1024 |
---|
72 | #define PTHREAD_STACK_MIN 2*4096 |
---|
73 | #define PTHREAD_CREATE_DETACHED 1 |
---|
74 | #define PTHREAD_CREATE_JOINABLE 0 |
---|
75 | #define PTHREAD_EXPLICIT_SCHED 0 |
---|
76 | #define PTHREAD_INHERIT_SCHED 1 |
---|
77 | #define PTHREAD_SCOPE_SYSTEM 0 |
---|
78 | #define PTHREAD_SCOPE_PROCESS 1 |
---|
79 | #define PTHREAD_PROCESS_PRIVATE 0 |
---|
80 | #define PTHREAD_PROCESS_SHARED 1 |
---|
81 | #define SEM_VALUE_MAX PTHREAD_THREADS_MAX |
---|
82 | |
---|
83 | struct __shared_s |
---|
84 | { |
---|
85 | volatile uint_t mailbox __CACHELINE; |
---|
86 | struct list_entry list; |
---|
87 | int tid; |
---|
88 | void *arg; |
---|
89 | }; |
---|
90 | |
---|
91 | #define PTHREAD_KEYS_MAX 64 |
---|
92 | #define PTHREAD_DESTRUCTOR_ITERATIONS 32 |
---|
93 | |
---|
94 | enum{ |
---|
95 | __PT_TLS_SHARED = 0, |
---|
96 | __PT_TLS_ERRNO, |
---|
97 | __PT_TLS_COND_WAIT, |
---|
98 | __PT_TLS_LOCAL_HEAP, |
---|
99 | __PT_TLS_FORK_FLAGS, |
---|
100 | __PT_TLS_FORK_CPUID, |
---|
101 | __PT_TLS_VALUES_NR |
---|
102 | }; |
---|
103 | |
---|
104 | #define __pthread_tls_set(tls,index,val) |
---|
105 | #define __pthread_tls_get(tls,index) |
---|
106 | #define __pthread_tls_getlocation(tls,index) |
---|
107 | |
---|
108 | typedef struct __pthread_tls_s |
---|
109 | { |
---|
110 | int signature; |
---|
111 | pthread_attr_t attr; |
---|
112 | void *values_tbl[PTHREAD_KEYS_MAX]; |
---|
113 | uint_t tls_tbl[__PT_TLS_VALUES_NR]; |
---|
114 | }__pthread_tls_t; |
---|
115 | |
---|
116 | void __pthread_init(void); |
---|
117 | void __pthread_tls_init(__pthread_tls_t *tls); |
---|
118 | void __pthread_tls_destroy(void); |
---|
119 | void __pthread_keys_init(void); |
---|
120 | void __pthread_keys_destroy(void); |
---|
121 | void __pthread_barrier_init(void); |
---|
122 | |
---|
123 | #define __pthread_tls_seterrno(tls,errno) |
---|
124 | |
---|
125 | #define __PTHREAD_OBJECT_CREATED 0xA5B5 |
---|
126 | #define __PTHREAD_OBJECT_DESTROYED 0xB0A0B0A0 |
---|
127 | #define __PTHREAD_OBJECT_BUSY 0x5A5A5A5A |
---|
128 | #define __PTHREAD_OBJECT_FREE 0xC0A5C0A5 |
---|
129 | |
---|
130 | typedef unsigned long pthread_t; |
---|
131 | typedef unsigned long pthread_rwlock_t; |
---|
132 | typedef unsigned long pthread_rwlockattr_t; |
---|
133 | typedef unsigned long pthread_key_t; |
---|
134 | |
---|
135 | typedef struct |
---|
136 | { |
---|
137 | uint_t val __CACHELINE; |
---|
138 | }pthread_spinlock_t; |
---|
139 | |
---|
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 |
---|
144 | |
---|
145 | typedef struct |
---|
146 | { |
---|
147 | int scope; |
---|
148 | uint_t type; |
---|
149 | uint_t cntr; |
---|
150 | }pthread_mutexattr_t; |
---|
151 | |
---|
152 | typedef struct |
---|
153 | { |
---|
154 | sem_t sem; |
---|
155 | pthread_spinlock_t lock; |
---|
156 | volatile uint_t value __CACHELINE; |
---|
157 | uint_t waiting __CACHELINE; |
---|
158 | struct list_entry queue __CACHELINE; |
---|
159 | pthread_mutexattr_t attr __CACHELINE; |
---|
160 | }pthread_mutex_t; |
---|
161 | |
---|
162 | #define __MUTEX_INITIALIZER(_t) \ |
---|
163 | { \ |
---|
164 | .sem = 0, \ |
---|
165 | .lock = {.val = __PTHREAD_OBJECT_FREE}, \ |
---|
166 | .value = __PTHREAD_OBJECT_FREE, \ |
---|
167 | .waiting = 0, \ |
---|
168 | .queue = {.next = 0, .pred = 0}, \ |
---|
169 | .attr = {.type = (_t), .scope = PTHREAD_PROCESS_PRIVATE, .cntr = 0}\ |
---|
170 | } |
---|
171 | |
---|
172 | #define PTHREAD_MUTEX_INITIALIZER __MUTEX_INITIALIZER(PTHREAD_MUTEX_DEFAULT) |
---|
173 | #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER __MUTEX_INITIALIZER(PTHREAD_MUTEX_RECURSIVE) |
---|
174 | #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER __MUTEX_INITIALIZER(PTHREAD_MUTEX_ERRORCHECK) |
---|
175 | |
---|
176 | typedef struct |
---|
177 | { |
---|
178 | int scope; |
---|
179 | void *sysid; |
---|
180 | pthread_spinlock_t lock; |
---|
181 | uint_t count __CACHELINE; |
---|
182 | struct list_entry queue __CACHELINE; |
---|
183 | }pthread_cond_t; |
---|
184 | |
---|
185 | #define PTHREAD_COND_INITIALIZER \ |
---|
186 | { \ |
---|
187 | .scope = PTHREAD_PROCESS_PRIVATE, \ |
---|
188 | .lock = {.val = __PTHREAD_OBJECT_FREE}, \ |
---|
189 | .count = 0, \ |
---|
190 | .queue = {.next = 0, .pred = 0} \ |
---|
191 | } |
---|
192 | |
---|
193 | typedef struct |
---|
194 | { |
---|
195 | int lock; |
---|
196 | int state; |
---|
197 | }pthread_once_t; |
---|
198 | |
---|
199 | #define PTHREAD_BARRIER_SERIAL_THREAD 1 |
---|
200 | |
---|
201 | typedef struct |
---|
202 | { |
---|
203 | sint_t scope; |
---|
204 | }pthread_barrierattr_t; |
---|
205 | |
---|
206 | typedef struct |
---|
207 | { |
---|
208 | sint_t scope; |
---|
209 | }pthread_condattr_t; |
---|
210 | |
---|
211 | typedef struct |
---|
212 | { |
---|
213 | int scope; |
---|
214 | void *sysid; |
---|
215 | |
---|
216 | __cacheline_t cntr; |
---|
217 | __cacheline_t count; |
---|
218 | |
---|
219 | volatile __cacheline_t state[2]; |
---|
220 | |
---|
221 | union { |
---|
222 | uint_t phase; |
---|
223 | __cacheline_t pading; |
---|
224 | }; |
---|
225 | }pthread_barrier_t; |
---|
226 | |
---|
227 | #define PTHREAD_ONCE_INIT {.lock = 0, .state = 0} |
---|
228 | |
---|
229 | /** Pthread Attribute Modifiers */ |
---|
230 | int pthread_attr_init (pthread_attr_t *attr); |
---|
231 | int pthread_attr_destroy(pthread_attr_t *attr); |
---|
232 | int pthread_attr_setstacksize(pthread_attr_t *attr, unsigned int stacksize); |
---|
233 | int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize); |
---|
234 | int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize); |
---|
235 | int pthread_attr_getstack(pthread_attr_t *attr, void **stackaddr, size_t *stacksize); |
---|
236 | int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); |
---|
237 | int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate); |
---|
238 | int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy); |
---|
239 | int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy); |
---|
240 | int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param); |
---|
241 | int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param); |
---|
242 | int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit); |
---|
243 | int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit); |
---|
244 | int pthread_attr_setscope(pthread_attr_t *attr, int scope); |
---|
245 | |
---|
246 | /** Thread Management */ |
---|
247 | void pthread_exit(void *retval); |
---|
248 | int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); |
---|
249 | int pthread_join(pthread_t th, void **thread_return); |
---|
250 | int pthread_detach(pthread_t thread); |
---|
251 | int pthread_equal(pthread_t thread1,pthread_t thread2); |
---|
252 | int pthread_once(pthread_once_t *once_control, void (*init_routine) (void)); |
---|
253 | void pthread_yield(void); |
---|
254 | pthread_t pthread_self(void); |
---|
255 | |
---|
256 | /** Thread specific data */ |
---|
257 | int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)); |
---|
258 | int pthread_key_delete(pthread_key_t key); |
---|
259 | int pthread_setspecific(pthread_key_t key, const void *value); |
---|
260 | void *pthread_getspecific(pthread_key_t key); |
---|
261 | |
---|
262 | /** SpinLock Sync Object */ |
---|
263 | int pthread_spin_init(pthread_spinlock_t *lock,int pshared); |
---|
264 | int pthread_spin_lock(pthread_spinlock_t *lock); |
---|
265 | int pthread_spin_trylock(pthread_spinlock_t *lock); |
---|
266 | int pthread_spin_unlock(pthread_spinlock_t *lock); |
---|
267 | int pthread_spin_destroy(pthread_spinlock_t *lock); |
---|
268 | |
---|
269 | /** Mutex Sync Object */ |
---|
270 | int pthread_mutexattr_destroy(pthread_mutexattr_t *attr); |
---|
271 | int pthread_mutexattr_init(pthread_mutexattr_t *attr); |
---|
272 | int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared); |
---|
273 | int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared); |
---|
274 | int pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type); |
---|
275 | int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type); |
---|
276 | int pthread_mutex_init (pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); |
---|
277 | int pthread_mutex_lock(pthread_mutex_t *mutex); |
---|
278 | int pthread_mutex_trylock(pthread_mutex_t *mutex); |
---|
279 | int pthread_mutex_unlock(pthread_mutex_t *mutex); |
---|
280 | int pthread_mutex_destroy(pthread_mutex_t *mutex); |
---|
281 | |
---|
282 | /** Condition Variable Sync Object */ |
---|
283 | int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr); |
---|
284 | int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared); |
---|
285 | int pthread_condattr_getpshared(pthread_condattr_t *attr, int *pshared); |
---|
286 | int pthread_cond_signal(pthread_cond_t *cond); |
---|
287 | int pthread_cond_broadcast(pthread_cond_t *cond); |
---|
288 | int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); |
---|
289 | int pthread_cond_destroy(pthread_cond_t *cond); |
---|
290 | |
---|
291 | /** Rread/Write Lock Sync Object */ |
---|
292 | int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr); |
---|
293 | int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock); |
---|
294 | int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock); |
---|
295 | int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock); |
---|
296 | int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock); |
---|
297 | int pthread_rwlock_unlock(pthread_rwlock_t *rwlock); |
---|
298 | int pthread_rwlock_destroy(pthread_rwlock_t *rwlock); |
---|
299 | |
---|
300 | /** Barrier Sync Object */ |
---|
301 | int pthread_barrierattr_destroy(pthread_barrierattr_t *attr); |
---|
302 | int pthread_barrierattr_init(pthread_barrierattr_t *attr); |
---|
303 | int pthread_barrierattr_getpshared(pthread_barrierattr_t *attr, int *scope); |
---|
304 | int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int scope); |
---|
305 | int pthread_barrier_init (pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned count); |
---|
306 | int pthread_barrier_wait (pthread_barrier_t *barrier); |
---|
307 | int pthread_barrier_destroy(pthread_barrier_t *barrier); |
---|
308 | |
---|
309 | |
---|
310 | /** POSIX-Like, Not Protable, Additional Operations */ |
---|
311 | #define PT_TRACE_OFF 0 |
---|
312 | #define PT_TRACE_ON 1 |
---|
313 | #define PT_SHOW_STATE 2 |
---|
314 | |
---|
315 | #define PT_FORK_DEFAULT __PT_ATTR_DEFAULT |
---|
316 | #define PT_FORK_WILL_EXEC __PT_FORK_WILL_EXEC |
---|
317 | #define PT_FORK_TARGET_CPU __PT_FORK_USE_TARGET_CPU |
---|
318 | #define PT_FORK_SET_AFFINITY __PT_FORK_USE_AFFINITY |
---|
319 | #define PT_ATTR_MEM_PRIO __PT_ATTR_MEM_PRIO |
---|
320 | #define PT_ATTR_AUTO_MGRT __PT_ATTR_AUTO_MGRT |
---|
321 | #define PT_ATTR_AUTO_NXTT __PT_ATTR_AUTO_NXTT |
---|
322 | #define PT_ATTR_INTERLEAVE_SEQ __PT_ATTR_INTERLEAVE_SEQ |
---|
323 | #define PT_ATTR_INTERLEAVE_ALL __PT_ATTR_INTERLEAVE_ALL |
---|
324 | #define PT_ATTR_MEM_CID_RR __PT_ATTR_MEM_CID_RR |
---|
325 | |
---|
326 | |
---|
327 | int pthread_migrate_np(pthread_attr_t *attr); |
---|
328 | int pthread_profiling_np(int cmd, pid_t pid, pthread_t tid); |
---|
329 | int pthread_attr_setflags_np(pthread_attr_t *attr, unsigned int flags, unsigned int *old); |
---|
330 | int pthread_attr_setcpuid_np(pthread_attr_t *attr, int cpu_id, int *old_cpu_id); |
---|
331 | int pthread_attr_getcpuid_np(int *cpu_id); |
---|
332 | int pthread_attr_setforkinfo_np(int flags); |
---|
333 | int pthread_attr_setforkcpuid_np(int cpu_id); |
---|
334 | int pthread_attr_getforkcpuid_np(int *cpu_id); |
---|
335 | |
---|
336 | |
---|
337 | extern pthread_mutex_t __printf_lock; |
---|
338 | extern uint_t ___dmsg_lock; |
---|
339 | extern uint_t ___dmsg_ok; |
---|
340 | |
---|
341 | #define printf_r(...) \ |
---|
342 | do{ \ |
---|
343 | pthread_mutex_lock(&__printf_lock); \ |
---|
344 | printf(__VA_ARGS__); \ |
---|
345 | pthread_mutex_unlock(&__printf_lock); \ |
---|
346 | }while(0) |
---|
347 | |
---|
348 | #define fprintf_r(x,...) \ |
---|
349 | do{ \ |
---|
350 | pthread_mutex_lock(&__printf_lock); \ |
---|
351 | fprintf((x), __VA_ARGS__); \ |
---|
352 | pthread_mutex_unlock(&__printf_lock); \ |
---|
353 | }while(0) |
---|
354 | |
---|
355 | #define dmsg_r(x,...) \ |
---|
356 | do{ \ |
---|
357 | if(___dmsg_ok){ \ |
---|
358 | cpu_spinlock_lock(&___dmsg_lock); \ |
---|
359 | fprintf((x), __VA_ARGS__); \ |
---|
360 | cpu_spinlock_unlock(&___dmsg_lock); \ |
---|
361 | } \ |
---|
362 | }while(0) |
---|
363 | |
---|
364 | #define dmsg(x,...) \ |
---|
365 | do{ \ |
---|
366 | if(___dmsg_ok){ \ |
---|
367 | fprintf((x), __VA_ARGS__); \ |
---|
368 | } \ |
---|
369 | }while(0) |
---|
370 | |
---|
371 | |
---|
372 | //////////////////////////////////////////////////////////////////// |
---|
373 | // Private Section // |
---|
374 | //////////////////////////////////////////////////////////////////// |
---|
375 | #undef __pthread_tls_set |
---|
376 | #define __pthread_tls_set(_tls,_indx,_val) do{(_tls)->tls_tbl[(_indx)] = (uint_t)(_val);}while(0) |
---|
377 | |
---|
378 | #undef __pthread_tls_get |
---|
379 | #define __pthread_tls_get(_tls,_indx) ((_tls)->tls_tbl[(_indx)]) |
---|
380 | |
---|
381 | #undef __pthread_tls_getlocation |
---|
382 | #define __pthread_tls_getlocation(_tls,_indx) (&(_tls)->tls_tbl[(_indx)]) |
---|
383 | |
---|
384 | #endif /* _PTHREAD_H_ */ |
---|