source: trunk/sys/libpthread/include/pthread.h @ 122

Last change on this file since 122 was 1, checked in by alain, 7 years ago

First import

File size: 12.3 KB
Line 
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
46typedef 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
83struct __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
94enum{
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
108typedef 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
116void __pthread_init(void);
117void __pthread_tls_init(__pthread_tls_t *tls);
118void __pthread_tls_destroy(void);
119void __pthread_keys_init(void);
120void __pthread_keys_destroy(void);
121void __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
130typedef unsigned long pthread_t;
131typedef unsigned long pthread_rwlock_t;
132typedef unsigned long pthread_rwlockattr_t;
133typedef unsigned long pthread_key_t;
134
135typedef 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
145typedef struct
146{
147        int  scope;
148        uint_t  type;
149        uint_t  cntr;
150}pthread_mutexattr_t;
151
152typedef 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
176typedef 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
193typedef struct
194{
195        int lock;
196        int state;
197}pthread_once_t;
198
199#define PTHREAD_BARRIER_SERIAL_THREAD 1
200
201typedef struct
202{
203        sint_t scope;
204}pthread_barrierattr_t;
205
206typedef struct
207{
208        sint_t scope;
209}pthread_condattr_t;
210
211typedef 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 */
230int  pthread_attr_init (pthread_attr_t *attr);
231int  pthread_attr_destroy(pthread_attr_t *attr);
232int  pthread_attr_setstacksize(pthread_attr_t *attr, unsigned int stacksize);
233int  pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize);
234int  pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize);
235int  pthread_attr_getstack(pthread_attr_t *attr, void **stackaddr, size_t *stacksize);
236int  pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
237int  pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
238int  pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
239int  pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
240int  pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);
241int  pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param);
242int  pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit);
243int  pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit);
244int  pthread_attr_setscope(pthread_attr_t *attr, int scope);
245
246/** Thread Management */
247void pthread_exit(void *retval);
248int  pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
249int  pthread_join(pthread_t th, void **thread_return);
250int  pthread_detach(pthread_t thread);
251int  pthread_equal(pthread_t thread1,pthread_t thread2);
252int  pthread_once(pthread_once_t *once_control, void (*init_routine) (void));
253void pthread_yield(void);
254pthread_t pthread_self(void);
255
256/** Thread specific data */
257int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));
258int pthread_key_delete(pthread_key_t key);
259int pthread_setspecific(pthread_key_t key, const void *value);
260void *pthread_getspecific(pthread_key_t key);
261
262/** SpinLock Sync Object */
263int  pthread_spin_init(pthread_spinlock_t *lock,int pshared);
264int  pthread_spin_lock(pthread_spinlock_t *lock);
265int  pthread_spin_trylock(pthread_spinlock_t *lock);
266int  pthread_spin_unlock(pthread_spinlock_t *lock);
267int  pthread_spin_destroy(pthread_spinlock_t *lock);
268
269/** Mutex Sync Object */
270int  pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
271int  pthread_mutexattr_init(pthread_mutexattr_t *attr);
272int  pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
273int  pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared);
274int  pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type);
275int  pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
276int  pthread_mutex_init (pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
277int  pthread_mutex_lock(pthread_mutex_t *mutex);
278int  pthread_mutex_trylock(pthread_mutex_t *mutex);
279int  pthread_mutex_unlock(pthread_mutex_t *mutex);
280int  pthread_mutex_destroy(pthread_mutex_t *mutex);
281
282/** Condition Variable Sync Object */
283int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);
284int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared);
285int pthread_condattr_getpshared(pthread_condattr_t *attr, int *pshared);
286int pthread_cond_signal(pthread_cond_t *cond);
287int pthread_cond_broadcast(pthread_cond_t *cond);
288int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
289int pthread_cond_destroy(pthread_cond_t *cond);
290
291/** Rread/Write Lock Sync Object */
292int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
293int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
294int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
295int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
296int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
297int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
298int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
299
300/** Barrier Sync Object */
301int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);
302int pthread_barrierattr_init(pthread_barrierattr_t *attr);
303int pthread_barrierattr_getpshared(pthread_barrierattr_t *attr, int *scope);
304int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int scope);
305int pthread_barrier_init (pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned count);
306int pthread_barrier_wait (pthread_barrier_t *barrier);
307int 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
327int pthread_migrate_np(pthread_attr_t *attr);
328int pthread_profiling_np(int cmd, pid_t pid, pthread_t tid);
329int pthread_attr_setflags_np(pthread_attr_t *attr, unsigned int flags, unsigned int *old);
330int pthread_attr_setcpuid_np(pthread_attr_t *attr, int cpu_id, int *old_cpu_id);
331int pthread_attr_getcpuid_np(int *cpu_id);
332int pthread_attr_setforkinfo_np(int flags);
333int pthread_attr_setforkcpuid_np(int cpu_id);
334int pthread_attr_getforkcpuid_np(int *cpu_id);
335
336
337extern pthread_mutex_t __printf_lock;
338extern uint_t ___dmsg_lock;
339extern 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_ */
Note: See TracBrowser for help on using the repository browser.