1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
5 /* This program is free software; you can redistribute it and/or */
6 /* modify it under the terms of the GNU Library General Public License */
7 /* as published by the Free Software Foundation; either version 2 */
8 /* of the License, or (at your option) any later version. */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU Library General Public License for more details. */
16 #define _INTERNALS_H 1
18 #include "uClibc-glue.h"
20 /* Internal data structures */
25 //l4/#include <signal.h>
28 //l4/#include <signal.h>
29 #include <bits/stackinfo.h>
30 //l4/#include <bits/sigcontextinfo.h>
31 #include <bits/pthreadtypes.h>
33 #include <bits/libc-lock.h>
35 #include <l4/sys/ipc.h>
42 #include "semaphore.h"
43 #include <pthread-functions.h>
46 # define THREAD_GETMEM(descr, member) descr->member
48 #ifndef THREAD_GETMEM_NC
49 # define THREAD_GETMEM_NC(descr, member, idx) descr->member[idx]
52 # define THREAD_SETMEM(descr, member, value) descr->member = (value)
54 #ifndef THREAD_SETMEM_NC
55 # define THREAD_SETMEM_NC(descr, member, idx, value) descr->member[idx] = (value)
58 #if !defined NOT_IN_libc
59 # define LIBC_THREAD_GETMEM(descr, member) THREAD_GETMEM (descr, member)
60 # define LIBC_THREAD_SETMEM(descr, member, value) \
61 THREAD_SETMEM (descr, member, value)
63 # define LIBC_THREAD_GETMEM(descr, member) descr->member
64 # define LIBC_THREAD_SETMEM(descr, member, value) descr->member = (value)
67 typedef void (*destr_function)(void *);
69 struct pthread_key_struct {
70 int in_use; /* already allocated? */
71 destr_function destr; /* destruction routine */
74 #include <l4/sys/kdebug.h>
75 #define UNIMPL(x...) do { outstring("UNIMPL: " x "\n"); } while(0)
78 #define PTHREAD_START_ARGS_INITIALIZER(fct) \
79 { (void *(*) (void *)) fct, NULL }
82 /* The type of thread handles. */
83 typedef l4_utcb_t *pthread_handle;
86 /* The type of messages sent to the thread manager thread */
88 enum pthread_request_rq { /* Request kind */
89 REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
90 REQ_POST, REQ_DEBUG, REQ_KICK, REQ_FOR_EACH_THREAD,
94 struct pthread_request {
95 pthread_descr req_thread; /* Thread doing the request */
96 enum pthread_request_rq req_kind;
97 union { /* Arguments for request */
98 struct { /* For REQ_CREATE: */
99 const pthread_attr_t * attr; /* thread attributes */
100 void * (*fn)(void *); /* start function */
101 void * arg; /* argument to start function */
103 struct { /* For REQ_FREE: */
104 pthread_t thread_id; /* identifier of thread to free */
106 struct { /* For REQ_PROCESS_EXIT: */
107 int code; /* exit status */
109 void * post; /* For REQ_POST: the semaphore */
110 struct { /* For REQ_FOR_EACH_THREAD: callback */
111 void (*fn)(void *, pthread_descr);
118 /* First free thread */
119 extern l4_utcb_t *__pthread_first_free_handle attribute_hidden;
121 /* Descriptor of the main thread */
123 extern pthread_descr __pthread_main_thread;
125 /* File descriptor for sending requests to the thread manager.
126 Initially -1, meaning that __pthread_initialize_manager must be called. */
128 extern l4_cap_idx_t __pthread_manager_request;
130 /* Other end of the pipe for sending requests to the thread manager. */
132 /* Maximum stack size. */
133 extern size_t __pthread_max_stacksize;
135 /* Default stack size for new threads. */
136 extern size_t __pthread_default_stacksize;
138 /* Pending request for a process-wide exit */
140 extern int __pthread_exit_requested, __pthread_exit_code;
142 /* Set to 1 by gdb if we're debugging */
144 extern __volatile__ int __pthread_threads_debug;
146 /* Globally enabled events. */
147 //extern __volatile__ td_thr_events_t __pthread_threads_events;
149 /* Pointer to descriptor of thread with last event. */
150 //extern __volatile__ pthread_descr __pthread_last_event;
152 /* Flag which tells whether we are executing on SMP kernel. */
153 extern int __pthread_smp_kernel;
155 inline static void __pthread_send_manager_rq(struct pthread_request *r, int block)
157 if (l4_is_invalid_cap(__pthread_manager_request))
159 __builtin_memcpy(l4_utcb_mr()->mr, r, sizeof(struct pthread_request));
162 (sizeof(struct pthread_request) + sizeof(l4_umword_t) - 1) / sizeof(l4_umword_t),
163 0, L4_MSGTAG_SCHEDULE);
165 l4_ipc_call(__pthread_manager_request, l4_utcb(), tag, L4_IPC_NEVER);
167 l4_ipc_send(__pthread_manager_request, l4_utcb(), tag, L4_IPC_NEVER);
170 /* Return the handle corresponding to a thread id */
172 static __inline__ pthread_handle thread_handle(pthread_t id)
174 return (l4_utcb_t*)id; //&__pthread_handles[id % PTHREAD_THREADS_MAX];
177 static inline pthread_descr handle_to_descr(pthread_handle h)
178 { return (pthread_descr)(l4_utcb_tcr_u(h)->user[0]); }
180 static inline struct _pthread_fastlock *handle_to_lock(pthread_handle h)
181 { return (struct _pthread_fastlock *)(&l4_utcb_tcr_u(h)->user[1]); }
183 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
185 static __inline__ int invalid_handle(pthread_handle h, pthread_t id)
187 return h != id || handle_to_descr(h) == NULL
188 || handle_to_descr(h)->p_tid != id || handle_to_descr(h)->p_terminated;
191 static __inline__ int nonexisting_handle(pthread_handle h, pthread_t id)
193 return handle_to_descr(h) == NULL || handle_to_descr(h)->p_tid != id;
196 /* Fill in defaults left unspecified by pt-machine.h. */
198 /* We round up a value with page size. */
200 #define page_roundup(v,p) ((((size_t) (v)) + (p) - 1) & ~((p) - 1))
203 /* The page size we can get from the system. This should likely not be
204 changed by the machine file but, you never know. */
206 #define PAGE_SIZE (L4_PAGESIZE)
209 /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
210 #ifndef INITIAL_STACK_SIZE
211 #define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
214 /* Size of the thread manager stack. The "- 32" avoids wasting space
215 with some malloc() implementations. */
216 #ifndef THREAD_MANAGER_STACK_SIZE
217 #define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
220 /* The base of the "array" of thread stacks. The array will grow down from
221 here. Defaults to the calculated bottom of the initial application
223 #ifndef THREAD_STACK_START_ADDRESS
224 #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
227 /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
228 architecture doesn't need a memory barrier instruction (e.g. Intel
229 x86). Still we need the compiler to respect the barrier and emit
230 all outstanding operations which modify memory. Some architectures
231 distinguish between full, read and write barriers. */
233 #ifndef MEMORY_BARRIER
234 #define MEMORY_BARRIER() __asm__ ("" : : : "memory")
236 #ifndef READ_MEMORY_BARRIER
237 #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
239 #ifndef WRITE_MEMORY_BARRIER
240 #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
243 /* Max number of times we must spin on a spinlock calling sched_yield().
244 After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
246 #ifndef MAX_SPIN_COUNT
247 #define MAX_SPIN_COUNT 50
250 /* Max number of times the spinlock in the adaptive mutex implementation
251 spins actively on SMP systems. */
253 #ifndef MAX_ADAPTIVE_SPIN_COUNT
254 #define MAX_ADAPTIVE_SPIN_COUNT 100
257 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
258 after MAX_SPIN_COUNT iterations of sched_yield().
259 With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
260 (Otherwise the kernel does busy-waiting for realtime threads,
261 giving other threads no chance to run.) */
263 #ifndef SPIN_SLEEP_DURATION
264 #define SPIN_SLEEP_DURATION 2000001
267 /* Defined and used in libc.so. */
268 extern int __libc_multiple_threads L4_HIDDEN;
269 extern int __librt_multiple_threads;
275 #define ASSERT assert
276 #define MSG __pthread_message
279 #define MSG(msg,arg...)
282 # define INIT_THREAD_SELF(descr, nr) do { l4_utcb_tcr()->user[0] = (l4_umword_t)descr; } while (0)
286 /* Internal global functions */
288 extern int __pthread_l4_initialize_main_thread(pthread_descr th) attribute_hidden;
289 extern void __l4_add_utcbs(l4_addr_t start, l4_addr_t utcbs_end);
292 extern int __pthread_sched_idle_prio;
293 extern int __pthread_sched_other_prio;
294 extern int __pthread_sched_rr_prio_min;
295 extern int __pthread_sched_rr_prio_max;
298 extern void __pthread_do_exit (void *retval, char *currentframe)
299 __attribute__ ((__noreturn__));
300 extern void __pthread_destroy_specifics (void);
301 extern void __pthread_perform_cleanup (char *currentframe);
302 extern void __pthread_init_max_stacksize (void);
303 extern int __pthread_initialize_manager (void);
304 extern void __pthread_message (const char * fmt, ...);
305 extern int __pthread_manager (void *reqfd);
306 extern int __pthread_start_manager (pthread_descr mgr) L4_HIDDEN;
307 extern int __pthread_manager_event (void *reqfd);
308 extern void __pthread_manager_sighandler (int sig);
309 extern void __pthread_reset_main_thread (void);
310 extern void __pthread_once_fork_prepare (void);
311 extern void __pthread_once_fork_parent (void);
312 extern void __pthread_once_fork_child (void);
313 extern void __flockfilelist (void);
314 extern void __funlockfilelist (void);
315 extern void __fresetlockfiles (void);
316 extern void __pthread_manager_adjust_prio (int thread_prio);
317 extern void __pthread_initialize_minimal (void);
319 extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
321 extern int __pthread_attr_getguardsize (const pthread_attr_t *__attr,
322 size_t *__guardsize);
323 #if 0 /* uClibc: deprecated stuff disabled */
324 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
326 extern int __pthread_attr_getstackaddr (const pthread_attr_t *__attr,
329 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
331 extern int __pthread_attr_getstacksize (const pthread_attr_t *__attr,
332 size_t *__stacksize);
333 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
335 extern int __pthread_attr_getstack (const pthread_attr_t *__attr, void **__stackaddr,
336 size_t *__stacksize);
337 extern int __pthread_attr_destroy (pthread_attr_t *attr);
338 extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
340 extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
342 extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
343 const struct sched_param *param);
344 extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
345 struct sched_param *param);
346 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
347 extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
349 extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
350 extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
352 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
353 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
355 extern int __pthread_getconcurrency (void);
356 extern int __pthread_setconcurrency (int __level);
357 extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
358 const struct timespec *__abstime);
359 extern int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
361 extern int __pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
363 extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
365 extern void __pthread_kill_other_threads_np (void);
366 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
367 __const pthread_mutexattr_t *__mutex_attr);
368 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
369 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
370 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
371 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
373 extern int __pthread_cond_init (pthread_cond_t *cond,
374 const pthread_condattr_t *cond_attr);
375 extern int __pthread_cond_destroy (pthread_cond_t *cond);
376 extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
377 extern int __pthread_cond_timedwait (pthread_cond_t *cond,
378 pthread_mutex_t *mutex,
379 const struct timespec *abstime);
380 extern int __pthread_cond_signal (pthread_cond_t *cond);
381 extern int __pthread_cond_broadcast (pthread_cond_t *cond);
382 extern int __pthread_condattr_init (pthread_condattr_t *attr);
383 extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
384 extern pthread_t __pthread_self (void);
385 extern pthread_descr __pthread_thread_self (void);
386 extern pthread_descr __pthread_self_stack (void) L4_HIDDEN;
387 extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
388 extern void __pthread_exit (void *retval)
389 #if defined NOT_IN_libc && defined IS_IN_libpthread
393 extern int __pthread_getschedparam (pthread_t thread, int *policy,
394 struct sched_param *param) __THROW;
395 extern int __pthread_setschedparam (pthread_t thread, int policy,
396 const struct sched_param *param) __THROW;
397 extern int __pthread_setcancelstate (int state, int * oldstate);
398 extern int __pthread_setcanceltype (int type, int * oldtype);
400 extern int __pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
401 __const struct timespec *__restrict
403 extern int __pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
404 __const struct timespec *__restrict
406 extern int __pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
408 extern int __pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
410 int *__restrict __pshared);
412 extern int __pthread_spin_lock (pthread_spinlock_t *__lock);
413 extern int __pthread_spin_trylock (pthread_spinlock_t *__lock);
414 extern int __pthread_spin_unlock (pthread_spinlock_t *__lock);
415 extern int __pthread_spin_init (pthread_spinlock_t *__lock, int __pshared);
416 extern int __pthread_spin_destroy (pthread_spinlock_t *__lock);
418 /* Global pointers to old or new suspend functions */
420 extern void (*__pthread_restart)(pthread_descr);
421 extern void (*__pthread_suspend)(pthread_descr);
422 extern int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *);
424 /* Prototypes for some of the new semaphore functions. */
425 extern int sem_post (sem_t * sem) __THROW;
426 extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value) __THROW;
427 extern int sem_wait (sem_t *__sem);
428 extern int sem_trywait (sem_t *__sem) __THROW;
429 extern int sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval) __THROW;
430 extern int sem_destroy (sem_t *__sem) __THROW;
432 /* Prototypes for compatibility functions. */
433 extern int __pthread_attr_init (pthread_attr_t *__attr);
434 extern int __pthread_create (pthread_t *__restrict __threadp,
435 const pthread_attr_t *__attr,
436 void *(*__start_routine) (void *),
437 void *__restrict __arg);
439 /* The functions called the signal events. */
440 extern void __linuxthreads_create_event (void);
441 extern void __linuxthreads_death_event (void);
442 extern void __linuxthreads_reap_event (void);
444 /* This function is called to initialize the pthread library. */
445 extern void __pthread_initialize (void);
448 extern int __pthread_internal_tsd_set (int key, const void * pointer);
449 extern void * __pthread_internal_tsd_get (int key);
450 extern void ** __attribute__ ((__const__))
451 __pthread_internal_tsd_address (int key);
453 /* Sighandler wrappers. */
454 extern void __pthread_sighandler(int signo, SIGCONTEXT ctx);
455 extern void __pthread_sighandler_rt(int signo, struct siginfo *si,
456 struct ucontext *uc);
457 extern void __pthread_null_sighandler(int sig);
458 extern int __pthread_sigaction (int sig, const struct sigaction *act,
459 struct sigaction *oact);
460 extern int __pthread_sigwait (const sigset_t *set, int *sig);
461 extern int __pthread_raise (int sig);
464 extern int __pthread_enable_asynccancel (void) L4_HIDDEN;
465 extern void __pthread_disable_asynccancel (int oldtype)
466 internal_function L4_HIDDEN;
468 /* The two functions are in libc.so and not exported. */
469 extern int __libc_enable_asynccancel (void) L4_HIDDEN;
470 extern void __libc_disable_asynccancel (int oldtype)
471 internal_function L4_HIDDEN;
473 /* The two functions are in libc.so and are exported. */
474 extern int __librt_enable_asynccancel (void);
475 extern void __librt_disable_asynccancel (int oldtype) internal_function;
477 extern void __pthread_cleanup_upto (__jmp_buf target,
478 char *targetframe) L4_HIDDEN;
480 extern pid_t __pthread_fork (struct fork_block *b) L4_HIDDEN;
483 #define asm_handle(name) _asm_handle(name)
484 #define _asm_handle(name) #name
485 #define ASM_GLOBAL asm_handle(ASM_GLOBAL_DIRECTIVE)
486 #define ASM_CANCEL(name) asm_handle(C_SYMBOL_NAME(name))
488 #if !defined NOT_IN_libc
489 # define LIBC_CANCEL_ASYNC() \
490 __libc_enable_asynccancel ()
491 # define LIBC_CANCEL_RESET(oldtype) \
492 __libc_disable_asynccancel (oldtype)
493 # define LIBC_CANCEL_HANDLED() \
494 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__libc_enable_asynccancel)); \
495 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__libc_disable_asynccancel))
496 #elif defined IS_IN_libpthread
497 # define LIBC_CANCEL_ASYNC() \
498 __pthread_enable_asynccancel ()
499 # define LIBC_CANCEL_RESET(oldtype) \
500 __pthread_disable_asynccancel (oldtype)
501 # define LIBC_CANCEL_HANDLED() \
502 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__pthread_enable_asynccancel)); \
503 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__pthread_disable_asynccancel))
504 #elif defined IS_IN_librt
505 # define LIBC_CANCEL_ASYNC() \
506 __librt_enable_asynccancel ()
507 # define LIBC_CANCEL_RESET(oldtype) \
508 __librt_disable_asynccancel (oldtype)
509 # define LIBC_CANCEL_HANDLED() \
510 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__librt_enable_asynccancel)); \
511 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__librt_disable_asynccancel))
513 # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
514 # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
515 # define LIBC_CANCEL_HANDLED() /* Nothing. */
517 extern int * __libc_pthread_init (const struct pthread_functions *functions);
522 # define __manager_thread (&__pthread_manager_thread)
524 # define __manager_thread __pthread_manager_threadp
528 static inline int __pthread_getprio(int policy, int prio);
529 static inline int __pthread_getprio(int policy, int prio)
540 prio -= __pthread_sched_rr_prio_min;
551 static inline int __pthread_l4_getprio(int policy, int posix_prio);
552 static inline int __pthread_l4_getprio(int policy, int posix_prio)
557 return __pthread_sched_other_prio;
559 return __pthread_sched_idle_prio;
561 return __pthread_sched_rr_prio_min + posix_prio;
570 static inline pthread_descr
571 check_thread_self (void);
572 static inline pthread_descr
573 check_thread_self (void)
575 pthread_descr self = thread_self ();
576 #if defined THREAD_SELF && defined INIT_THREAD_SELF
577 if (self == __manager_thread)
579 /* A new thread might get a cancel signal before it is fully
580 initialized, so that the thread register might still point to the
581 manager thread. Double check that this is really the manager
583 self = handle_to_descr(l4_utcb());
584 if (self != __manager_thread)
585 /* Oops, thread_self() isn't working yet.. */
586 INIT_THREAD_SELF(self, self->p_nr);
592 #endif /* internals.h */