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) descr->member
52 # define THREAD_SETMEM(descr, member, value) descr->member = (value)
54 #ifndef THREAD_SETMEM_NC
55 # define THREAD_SETMEM_NC(descr, member, value) descr->member = (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\n"); /*printf("UNIMPLEMENTED:%s:%s %s:", __FILE__, __LINE__, __func__);*/ }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
93 struct pthread_request {
94 pthread_descr req_thread; /* Thread doing the request */
95 enum pthread_request_rq req_kind;
96 union { /* Arguments for request */
97 struct { /* For REQ_CREATE: */
98 const pthread_attr_t * attr; /* thread attributes */
99 void * (*fn)(void *); /* start function */
100 void * arg; /* argument to start function */
102 struct { /* For REQ_FREE: */
103 pthread_t thread_id; /* identifier of thread to free */
105 struct { /* For REQ_PROCESS_EXIT: */
106 int code; /* exit status */
108 void * post; /* For REQ_POST: the semaphore */
109 struct { /* For REQ_FOR_EACH_THREAD: callback */
110 void (*fn)(void *, pthread_descr);
117 /* First free thread */
118 extern l4_utcb_t *__pthread_first_free_handle attribute_hidden;
120 /* Descriptor of the main thread */
122 extern pthread_descr __pthread_main_thread;
124 /* File descriptor for sending requests to the thread manager.
125 Initially -1, meaning that __pthread_initialize_manager must be called. */
127 extern l4_cap_idx_t __pthread_manager_request;
129 /* Other end of the pipe for sending requests to the thread manager. */
131 /* Maximum stack size. */
132 extern size_t __pthread_max_stacksize;
134 /* Default stack size for new threads. */
135 extern size_t __pthread_default_stacksize;
137 /* Pending request for a process-wide exit */
139 extern int __pthread_exit_requested, __pthread_exit_code;
141 /* Set to 1 by gdb if we're debugging */
143 extern __volatile__ int __pthread_threads_debug;
145 /* Globally enabled events. */
146 //extern __volatile__ td_thr_events_t __pthread_threads_events;
148 /* Pointer to descriptor of thread with last event. */
149 //extern __volatile__ pthread_descr __pthread_last_event;
151 /* Flag which tells whether we are executing on SMP kernel. */
152 extern int __pthread_smp_kernel;
154 inline static void __pthread_send_manager_rq(struct pthread_request *r, int block)
156 if (l4_is_invalid_cap(__pthread_manager_request))
158 __builtin_memcpy(l4_utcb_mr()->mr, r, sizeof(struct pthread_request));
159 l4_msgtag_t tag = l4_msgtag(0,
160 (sizeof(struct pthread_request) + sizeof(l4_umword_t) - 1) / sizeof(l4_umword_t),
163 l4_ipc_call(__pthread_manager_request, l4_utcb(), tag, L4_IPC_NEVER);
165 l4_ipc_send(__pthread_manager_request, l4_utcb(), tag, L4_IPC_NEVER);
168 /* Return the handle corresponding to a thread id */
170 static __inline__ pthread_handle thread_handle(pthread_t id)
172 return (l4_utcb_t*)id; //&__pthread_handles[id % PTHREAD_THREADS_MAX];
175 static inline pthread_descr handle_to_descr(pthread_handle h)
176 { return (pthread_descr)(l4_utcb_tcr_u(h)->user[0]); }
178 static inline struct _pthread_fastlock *handle_to_lock(pthread_handle h)
179 { return (struct _pthread_fastlock *)(&l4_utcb_tcr_u(h)->user[1]); }
181 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
183 static __inline__ int invalid_handle(pthread_handle h, pthread_t id)
185 return h != id || handle_to_descr(h) == NULL
186 || handle_to_descr(h)->p_tid != id || handle_to_descr(h)->p_terminated;
189 static __inline__ int nonexisting_handle(pthread_handle h, pthread_t id)
191 return handle_to_descr(h) == NULL || handle_to_descr(h)->p_tid != id;
194 /* Fill in defaults left unspecified by pt-machine.h. */
196 /* We round up a value with page size. */
198 #define page_roundup(v,p) ((((size_t) (v)) + (p) - 1) & ~((p) - 1))
201 /* The page size we can get from the system. This should likely not be
202 changed by the machine file but, you never know. */
204 #define PAGE_SIZE (L4_PAGESIZE)
207 /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
208 #ifndef INITIAL_STACK_SIZE
209 #define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
212 /* Size of the thread manager stack. The "- 32" avoids wasting space
213 with some malloc() implementations. */
214 #ifndef THREAD_MANAGER_STACK_SIZE
215 #define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
218 /* The base of the "array" of thread stacks. The array will grow down from
219 here. Defaults to the calculated bottom of the initial application
221 #ifndef THREAD_STACK_START_ADDRESS
222 #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
225 /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
226 architecture doesn't need a memory barrier instruction (e.g. Intel
227 x86). Still we need the compiler to respect the barrier and emit
228 all outstanding operations which modify memory. Some architectures
229 distinguish between full, read and write barriers. */
231 #ifndef MEMORY_BARRIER
232 #define MEMORY_BARRIER() __asm__ ("" : : : "memory")
234 #ifndef READ_MEMORY_BARRIER
235 #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
237 #ifndef WRITE_MEMORY_BARRIER
238 #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
241 /* Max number of times we must spin on a spinlock calling sched_yield().
242 After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
244 #ifndef MAX_SPIN_COUNT
245 #define MAX_SPIN_COUNT 50
248 /* Max number of times the spinlock in the adaptive mutex implementation
249 spins actively on SMP systems. */
251 #ifndef MAX_ADAPTIVE_SPIN_COUNT
252 #define MAX_ADAPTIVE_SPIN_COUNT 100
255 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
256 after MAX_SPIN_COUNT iterations of sched_yield().
257 With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
258 (Otherwise the kernel does busy-waiting for realtime threads,
259 giving other threads no chance to run.) */
261 #ifndef SPIN_SLEEP_DURATION
262 #define SPIN_SLEEP_DURATION 2000001
265 /* Defined and used in libc.so. */
266 extern int __libc_multiple_threads L4_HIDDEN;
267 extern int __librt_multiple_threads;
273 #define ASSERT assert
274 #define MSG __pthread_message
277 #define MSG(msg,arg...)
280 # define INIT_THREAD_SELF(descr, nr) do { l4_utcb_tcr()->user[0] = (l4_umword_t)descr; } while (0)
284 /* Internal global functions */
286 extern int __pthread_l4_initialize_main_thread(pthread_descr th) attribute_hidden;
289 extern int __pthread_sched_idle_prio;
290 extern int __pthread_sched_other_prio;
291 extern int __pthread_sched_rr_prio_min;
292 extern int __pthread_sched_rr_prio_max;
295 extern void __pthread_do_exit (void *retval, char *currentframe)
296 __attribute__ ((__noreturn__));
297 extern void __pthread_destroy_specifics (void);
298 extern void __pthread_perform_cleanup (char *currentframe);
299 extern void __pthread_init_max_stacksize (void);
300 extern int __pthread_initialize_manager (void);
301 extern void __pthread_message (const char * fmt, ...);
302 extern int __pthread_manager (void *reqfd);
303 extern int __pthread_start_manager (pthread_descr mgr) L4_HIDDEN;
304 extern int __pthread_manager_event (void *reqfd);
305 extern void __pthread_manager_sighandler (int sig);
306 extern void __pthread_reset_main_thread (void);
307 extern void __pthread_once_fork_prepare (void);
308 extern void __pthread_once_fork_parent (void);
309 extern void __pthread_once_fork_child (void);
310 extern void __flockfilelist (void);
311 extern void __funlockfilelist (void);
312 extern void __fresetlockfiles (void);
313 extern void __pthread_manager_adjust_prio (int thread_prio);
314 extern void __pthread_initialize_minimal (void);
316 extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
318 extern int __pthread_attr_getguardsize (const pthread_attr_t *__attr,
319 size_t *__guardsize);
320 #if 0 /* uClibc: deprecated stuff disabled */
321 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
323 extern int __pthread_attr_getstackaddr (const pthread_attr_t *__attr,
326 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
328 extern int __pthread_attr_getstacksize (const pthread_attr_t *__attr,
329 size_t *__stacksize);
330 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
332 extern int __pthread_attr_getstack (const pthread_attr_t *__attr, void **__stackaddr,
333 size_t *__stacksize);
334 extern int __pthread_attr_destroy (pthread_attr_t *attr);
335 extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
337 extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
339 extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
340 const struct sched_param *param);
341 extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
342 struct sched_param *param);
343 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
344 extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
346 extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
347 extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
349 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
350 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
352 extern int __pthread_getconcurrency (void);
353 extern int __pthread_setconcurrency (int __level);
354 extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
355 const struct timespec *__abstime);
356 extern int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
358 extern int __pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
360 extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
362 extern void __pthread_kill_other_threads_np (void);
363 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
364 __const pthread_mutexattr_t *__mutex_attr);
365 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
366 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
367 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
368 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
370 extern int __pthread_cond_init (pthread_cond_t *cond,
371 const pthread_condattr_t *cond_attr);
372 extern int __pthread_cond_destroy (pthread_cond_t *cond);
373 extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
374 extern int __pthread_cond_timedwait (pthread_cond_t *cond,
375 pthread_mutex_t *mutex,
376 const struct timespec *abstime);
377 extern int __pthread_cond_signal (pthread_cond_t *cond);
378 extern int __pthread_cond_broadcast (pthread_cond_t *cond);
379 extern int __pthread_condattr_init (pthread_condattr_t *attr);
380 extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
381 extern pthread_t __pthread_self (void);
382 extern pthread_descr __pthread_thread_self (void);
383 extern pthread_descr __pthread_self_stack (void) L4_HIDDEN;
384 extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
385 extern void __pthread_exit (void *retval)
386 #if defined NOT_IN_libc && defined IS_IN_libpthread
390 extern int __pthread_getschedparam (pthread_t thread, int *policy,
391 struct sched_param *param) __THROW;
392 extern int __pthread_setschedparam (pthread_t thread, int policy,
393 const struct sched_param *param) __THROW;
394 extern int __pthread_setcancelstate (int state, int * oldstate);
395 extern int __pthread_setcanceltype (int type, int * oldtype);
397 extern int __pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
398 __const struct timespec *__restrict
400 extern int __pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
401 __const struct timespec *__restrict
403 extern int __pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
405 extern int __pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
407 int *__restrict __pshared);
409 extern int __pthread_spin_lock (pthread_spinlock_t *__lock);
410 extern int __pthread_spin_trylock (pthread_spinlock_t *__lock);
411 extern int __pthread_spin_unlock (pthread_spinlock_t *__lock);
412 extern int __pthread_spin_init (pthread_spinlock_t *__lock, int __pshared);
413 extern int __pthread_spin_destroy (pthread_spinlock_t *__lock);
415 /* Global pointers to old or new suspend functions */
417 extern void (*__pthread_restart)(pthread_descr);
418 extern void (*__pthread_suspend)(pthread_descr);
419 extern int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *);
421 /* Prototypes for some of the new semaphore functions. */
422 extern int sem_post (sem_t * sem) __THROW;
423 extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value) __THROW;
424 extern int sem_wait (sem_t *__sem);
425 extern int sem_trywait (sem_t *__sem) __THROW;
426 extern int sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval) __THROW;
427 extern int sem_destroy (sem_t *__sem) __THROW;
429 /* Prototypes for compatibility functions. */
430 extern int __pthread_attr_init (pthread_attr_t *__attr);
431 extern int __pthread_create (pthread_t *__restrict __threadp,
432 const pthread_attr_t *__attr,
433 void *(*__start_routine) (void *),
434 void *__restrict __arg);
436 /* The functions called the signal events. */
437 extern void __linuxthreads_create_event (void);
438 extern void __linuxthreads_death_event (void);
439 extern void __linuxthreads_reap_event (void);
441 /* This function is called to initialize the pthread library. */
442 extern void __pthread_initialize (void);
445 extern int __pthread_internal_tsd_set (int key, const void * pointer);
446 extern void * __pthread_internal_tsd_get (int key);
447 extern void ** __attribute__ ((__const__))
448 __pthread_internal_tsd_address (int key);
450 /* Sighandler wrappers. */
451 extern void __pthread_sighandler(int signo, SIGCONTEXT ctx);
452 extern void __pthread_sighandler_rt(int signo, struct siginfo *si,
453 struct ucontext *uc);
454 extern void __pthread_null_sighandler(int sig);
455 extern int __pthread_sigaction (int sig, const struct sigaction *act,
456 struct sigaction *oact);
457 extern int __pthread_sigwait (const sigset_t *set, int *sig);
458 extern int __pthread_raise (int sig);
461 extern int __pthread_enable_asynccancel (void) L4_HIDDEN;
462 extern void __pthread_disable_asynccancel (int oldtype)
463 internal_function L4_HIDDEN;
465 /* The two functions are in libc.so and not exported. */
466 extern int __libc_enable_asynccancel (void) L4_HIDDEN;
467 extern void __libc_disable_asynccancel (int oldtype)
468 internal_function L4_HIDDEN;
470 /* The two functions are in libc.so and are exported. */
471 extern int __librt_enable_asynccancel (void);
472 extern void __librt_disable_asynccancel (int oldtype) internal_function;
474 extern void __pthread_cleanup_upto (__jmp_buf target,
475 char *targetframe) L4_HIDDEN;
477 extern pid_t __pthread_fork (struct fork_block *b) L4_HIDDEN;
480 #define asm_handle(name) _asm_handle(name)
481 #define _asm_handle(name) #name
482 #define ASM_GLOBAL asm_handle(ASM_GLOBAL_DIRECTIVE)
483 #define ASM_CANCEL(name) asm_handle(C_SYMBOL_NAME(name))
485 #if !defined NOT_IN_libc
486 # define LIBC_CANCEL_ASYNC() \
487 __libc_enable_asynccancel ()
488 # define LIBC_CANCEL_RESET(oldtype) \
489 __libc_disable_asynccancel (oldtype)
490 # define LIBC_CANCEL_HANDLED() \
491 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__libc_enable_asynccancel)); \
492 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__libc_disable_asynccancel))
493 #elif defined IS_IN_libpthread
494 # define LIBC_CANCEL_ASYNC() \
495 __pthread_enable_asynccancel ()
496 # define LIBC_CANCEL_RESET(oldtype) \
497 __pthread_disable_asynccancel (oldtype)
498 # define LIBC_CANCEL_HANDLED() \
499 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__pthread_enable_asynccancel)); \
500 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__pthread_disable_asynccancel))
501 #elif defined IS_IN_librt
502 # define LIBC_CANCEL_ASYNC() \
503 __librt_enable_asynccancel ()
504 # define LIBC_CANCEL_RESET(oldtype) \
505 __librt_disable_asynccancel (oldtype)
506 # define LIBC_CANCEL_HANDLED() \
507 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__librt_enable_asynccancel)); \
508 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__librt_disable_asynccancel))
510 # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
511 # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
512 # define LIBC_CANCEL_HANDLED() /* Nothing. */
514 extern int * __libc_pthread_init (const struct pthread_functions *functions);
519 # define __manager_thread (&__pthread_manager_thread)
521 # define __manager_thread __pthread_manager_threadp
525 static inline int __pthread_getprio(int policy, int prio);
526 static inline int __pthread_getprio(int policy, int prio)
537 prio -= __pthread_sched_rr_prio_min;
548 static inline int __pthread_l4_getprio(int policy, int posix_prio);
549 static inline int __pthread_l4_getprio(int policy, int posix_prio)
554 return __pthread_sched_other_prio;
556 return __pthread_sched_idle_prio;
558 return __pthread_sched_rr_prio_min + posix_prio;
567 static inline pthread_descr
568 check_thread_self (void);
569 static inline pthread_descr
570 check_thread_self (void)
572 pthread_descr self = thread_self ();
573 #if defined THREAD_SELF && defined INIT_THREAD_SELF
574 if (self == __manager_thread)
576 /* A new thread might get a cancel signal before it is fully
577 initialized, so that the thread register might still point to the
578 manager thread. Double check that this is really the manager
580 self = __pthread_self_stack();
581 if (self != __manager_thread)
582 /* Oops, thread_self() isn't working yet.. */
583 INIT_THREAD_SELF(self, self->p_nr);
589 #endif /* internals.h */