1 /* Linuxthreads - a simple clone()-based implementation of Posix        */
2 /* threads for Linux.                                                   */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr)              */
4 /*                                                                      */
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.               */
9 /*                                                                      */
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.                 */
14 
15 #ifndef _INTERNALS_H
16 #define _INTERNALS_H	1
17 
18 #include "uClibc-glue.h"
19 
20 /* Internal data structures */
21 
22 /* Includes */
23 
24 #include <limits.h>
25 //l4/#include <signal.h>
26 #include <stdbool.h>
27 #include <unistd.h>
28 //l4/#include <signal.h>
29 #include <bits/stackinfo.h>
30 //l4/#include <bits/sigcontextinfo.h>
31 #include <bits/pthreadtypes.h>
32 
33 #include <bits/libc-lock.h>
34 
35 #include <l4/sys/ipc.h>
36 
37 #ifdef USE_TLS
38 #include <tls.h>
39 #endif
40 #include "descr.h"
41 
42 #include "semaphore.h"
43 #include <pthread-functions.h>
44 
45 #ifndef THREAD_GETMEM
46 # define THREAD_GETMEM(descr, member) descr->member
47 #endif
48 #ifndef THREAD_GETMEM_NC
49 # define THREAD_GETMEM_NC(descr, member, idx) descr->member[idx]
50 #endif
51 #ifndef THREAD_SETMEM
52 # define THREAD_SETMEM(descr, member, value) descr->member = (value)
53 #endif
54 #ifndef THREAD_SETMEM_NC
55 # define THREAD_SETMEM_NC(descr, member, idx, value) descr->member[idx] = (value)
56 #endif
57 
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)
62 #else
63 # define LIBC_THREAD_GETMEM(descr, member) descr->member
64 # define LIBC_THREAD_SETMEM(descr, member, value) descr->member = (value)
65 #endif
66 
67 typedef void (*destr_function)(void *);
68 
69 struct pthread_key_struct {
70   int in_use;                   /* already allocated? */
71   destr_function destr;         /* destruction routine */
72 };
73 
74 #include <l4/sys/kdebug.h>
75 #define UNIMPL(x...) do { outstring("UNIMPL: " x "\n"); } while(0)
76 
77 
78 #define PTHREAD_START_ARGS_INITIALIZER(fct) \
79   { (void *(*) (void *)) fct, NULL }
80 
81 
82 /* The type of thread handles. */
83 typedef l4_utcb_t *pthread_handle;
84 
85 
86 /* The type of messages sent to the thread manager thread */
87 
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,
91     REQ_THREAD_EXIT
92 };
93 
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 */
102     } create;
103     struct {                    /* For REQ_FREE: */
104       pthread_t thread_id;      /*   identifier of thread to free */
105     } free;
106     struct {                    /* For REQ_PROCESS_EXIT: */
107       int code;                 /*   exit status */
108     } exit;
109     void * post;                /* For REQ_POST: the semaphore */
110     struct {			/* For REQ_FOR_EACH_THREAD: callback */
111       void (*fn)(void *, pthread_descr);
112       void *arg;
113     } for_each;
114   } req_args;
115 };
116 
117 
118 /* First free thread */
119 extern l4_utcb_t *__pthread_first_free_handle attribute_hidden;
120 
121 /* Descriptor of the main thread */
122 
123 extern pthread_descr __pthread_main_thread;
124 
125 /* File descriptor for sending requests to the thread manager.
126    Initially -1, meaning that __pthread_initialize_manager must be called. */
127 
128 extern l4_cap_idx_t __pthread_manager_request;
129 
130 /* Other end of the pipe for sending requests to the thread manager. */
131 
132 /* Maximum stack size.  */
133 extern size_t __pthread_max_stacksize;
134 
135 /* Default stack size for new threads.  */
136 extern size_t __pthread_default_stacksize;
137 
138 /* Pending request for a process-wide exit */
139 
140 extern int __pthread_exit_requested, __pthread_exit_code;
141 
142 /* Set to 1 by gdb if we're debugging */
143 
144 extern __volatile__ int __pthread_threads_debug;
145 
146 /* Globally enabled events.  */
147 //extern __volatile__ td_thr_events_t __pthread_threads_events;
148 
149 /* Pointer to descriptor of thread with last event.  */
150 //extern __volatile__ pthread_descr __pthread_last_event;
151 
152 /* Flag which tells whether we are executing on SMP kernel. */
153 extern int __pthread_smp_kernel;
154 
__pthread_send_manager_rq(struct pthread_request * r,int block)155 inline static void __pthread_send_manager_rq(struct pthread_request *r, int block)
156 {
157   if (l4_is_invalid_cap(__pthread_manager_request))
158     return;
159   __builtin_memcpy(l4_utcb_mr()->mr, r, sizeof(struct pthread_request));
160   l4_msgtag_t tag
161     = l4_msgtag(0, l4_bytes_to_mwords(sizeof(struct pthread_request)),
162                 0, L4_MSGTAG_SCHEDULE);
163   if (block)
164     l4_ipc_call(__pthread_manager_request, l4_utcb(), tag, L4_IPC_NEVER);
165   else
166     l4_ipc_send(__pthread_manager_request, l4_utcb(), tag, L4_IPC_NEVER);
167 }
168 
169 /* Return the handle corresponding to a thread id */
170 
thread_handle(pthread_t id)171 static __inline__ pthread_handle thread_handle(pthread_t id)
172 {
173   return (l4_utcb_t*)id; //&__pthread_handles[id % PTHREAD_THREADS_MAX];
174 }
175 
handle_to_descr(pthread_handle h)176 static inline pthread_descr handle_to_descr(pthread_handle h)
177 { return (pthread_descr)(l4_utcb_tcr_u(h)->user[0]); }
178 
handle_to_lock(pthread_handle h)179 static inline struct _pthread_fastlock *handle_to_lock(pthread_handle h)
180 { return (struct _pthread_fastlock *)(&l4_utcb_tcr_u(h)->user[1]); }
181 
182 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
183 
invalid_handle(pthread_handle h,pthread_t id)184 static __inline__ int invalid_handle(pthread_handle h, pthread_t id)
185 {
186   return h != id || handle_to_descr(h) == NULL
187     || handle_to_descr(h)->p_tid != id || handle_to_descr(h)->p_terminated;
188 }
189 
nonexisting_handle(pthread_handle h,pthread_t id)190 static __inline__ int nonexisting_handle(pthread_handle h, pthread_t id)
191 {
192   return handle_to_descr(h) == NULL || handle_to_descr(h)->p_tid != id;
193 }
194 
195 /* Fill in defaults left unspecified by pt-machine.h.  */
196 
197 /* We round up a value with page size. */
198 #ifndef page_roundup
199 #define page_roundup(v,p) ((((size_t) (v)) + (p) - 1) & ~((p) - 1))
200 #endif
201 
202 /* The page size we can get from the system.  This should likely not be
203    changed by the machine file but, you never know.  */
204 #ifndef PAGE_SIZE
205 #define PAGE_SIZE  (L4_PAGESIZE)
206 #endif
207 
208 /* The initial size of the thread stack.  Must be a multiple of PAGE_SIZE.  */
209 #ifndef INITIAL_STACK_SIZE
210 #define INITIAL_STACK_SIZE  (4 * PAGE_SIZE)
211 #endif
212 
213 /* Size of the thread manager stack. The "- 32" avoids wasting space
214    with some malloc() implementations. */
215 #ifndef THREAD_MANAGER_STACK_SIZE
216 #define THREAD_MANAGER_STACK_SIZE  (2 * PAGE_SIZE - 32)
217 #endif
218 
219 /* The base of the "array" of thread stacks.  The array will grow down from
220    here.  Defaults to the calculated bottom of the initial application
221    stack.  */
222 #ifndef THREAD_STACK_START_ADDRESS
223 #define THREAD_STACK_START_ADDRESS  __pthread_initial_thread_bos
224 #endif
225 
226 /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
227    architecture doesn't need a memory barrier instruction (e.g. Intel
228    x86).  Still we need the compiler to respect the barrier and emit
229    all outstanding operations which modify memory.  Some architectures
230    distinguish between full, read and write barriers.  */
231 
232 #ifndef MEMORY_BARRIER
233 #define MEMORY_BARRIER() __asm__ ("" : : : "memory")
234 #endif
235 #ifndef READ_MEMORY_BARRIER
236 #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
237 #endif
238 #ifndef WRITE_MEMORY_BARRIER
239 #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
240 #endif
241 
242 /* Max number of times we must spin on a spinlock calling sched_yield().
243    After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
244 
245 #ifndef MAX_SPIN_COUNT
246 #define MAX_SPIN_COUNT 50
247 #endif
248 
249 /* Max number of times the spinlock in the adaptive mutex implementation
250    spins actively on SMP systems.  */
251 
252 #ifndef MAX_ADAPTIVE_SPIN_COUNT
253 #define MAX_ADAPTIVE_SPIN_COUNT 100
254 #endif
255 
256 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
257    after MAX_SPIN_COUNT iterations of sched_yield().
258    With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
259    (Otherwise the kernel does busy-waiting for realtime threads,
260     giving other threads no chance to run.) */
261 
262 #ifndef SPIN_SLEEP_DURATION
263 #define SPIN_SLEEP_DURATION 2000001
264 #endif
265 
266 /* Defined and used in libc.so.  */
267 extern int __libc_multiple_threads L4_HIDDEN;
268 extern int __librt_multiple_threads;
269 
270 /* Debugging */
271 
272 #ifdef DEBUG
273 #include <assert.h>
274 #define ASSERT assert
275 #define MSG __pthread_message
276 #else
277 #define ASSERT(x)
278 #define MSG(msg,arg...)
279 #endif
280 
281 # define INIT_THREAD_SELF(descr, nr) do { l4_utcb_tcr()->user[0] = (l4_umword_t)descr; } while (0)
282 
283 
284 
285 /* Internal global functions */
286 __BEGIN_DECLS
287 extern int __pthread_l4_initialize_main_thread(pthread_descr th) attribute_hidden;
288 extern void __l4_add_utcbs(l4_addr_t start, l4_addr_t utcbs_end);
289 
290 
291 extern int __pthread_sched_idle_prio;
292 extern int __pthread_sched_other_prio;
293 extern int __pthread_sched_rr_prio_min;
294 extern int __pthread_sched_rr_prio_max;
295 
296 
297 extern void __pthread_cleanup_push(struct _pthread_cleanup_buffer * buffer,
298 			   void (*routine)(void *), void * arg);
299 extern void __pthread_cleanup_pop(struct _pthread_cleanup_buffer * buffer,
300 			  int execute);
301 extern void __pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer * buffer,
302 				  int execute);
303 extern void __pthread_cleanup_push_defer(struct _pthread_cleanup_buffer * buffer,
304 				 void (*routine)(void *), void * arg);
305 
306 extern void __pthread_do_exit (void *retval, char *currentframe)
307      __attribute__ ((__noreturn__));
308 extern void __pthread_destroy_specifics (void);
309 extern void __pthread_perform_cleanup (char *currentframe) internal_function;
310 extern void __pthread_init_max_stacksize (void);
311 extern int __pthread_initialize_manager (void);
312 extern void __pthread_message (const char * fmt, ...);
313 extern int __pthread_manager (void *reqfd);
314 extern int __pthread_start_manager (pthread_descr mgr) L4_HIDDEN;
315 extern int __pthread_manager_event (void *reqfd);
316 extern void __pthread_manager_sighandler (int sig);
317 extern void __pthread_reset_main_thread (void);
318 extern void __pthread_once_fork_prepare (void);
319 extern void __pthread_once_fork_parent (void);
320 extern void __pthread_once_fork_child (void);
321 extern void __flockfilelist (void);
322 extern void __funlockfilelist (void);
323 extern void __fresetlockfiles (void);
324 extern void __pthread_manager_adjust_prio (int thread_prio);
325 extern void __pthread_initialize_minimal (void);
326 
327 extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
328 					size_t __guardsize);
329 extern int __pthread_attr_getguardsize (const pthread_attr_t *__attr,
330 					size_t *__guardsize);
331 #if 0 /* uClibc: deprecated stuff disabled */
332 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
333 					void *__stackaddr);
334 extern int __pthread_attr_getstackaddr (const pthread_attr_t *__attr,
335 					void **__stackaddr);
336 #endif
337 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
338 					size_t __stacksize);
339 extern int __pthread_attr_getstacksize (const pthread_attr_t *__attr,
340 					size_t *__stacksize);
341 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
342 				    size_t __stacksize);
343 extern int __pthread_attr_getstack (const pthread_attr_t *__attr, void **__stackaddr,
344 				    size_t *__stacksize);
345 extern int __pthread_attr_destroy (pthread_attr_t *attr);
346 extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
347 					  int detachstate);
348 extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
349 					  int *detachstate);
350 extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
351 					 const struct sched_param *param);
352 extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
353 					 struct sched_param *param);
354 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
355 extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
356 					  int *policy);
357 extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
358 extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
359 					   int *inherit);
360 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
361 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
362 
363 extern int __pthread_getconcurrency (void);
364 extern int __pthread_setconcurrency (int __level);
365 extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
366 				      const struct timespec *__abstime);
367 extern int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
368 					   int *__pshared);
369 extern int __pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
370 					   int __pshared);
371 extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
372 					int *__kind);
373 extern void __pthread_kill_other_threads_np (void);
374 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
375 				 __const pthread_mutexattr_t *__mutex_attr);
376 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
377 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
378 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
379 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
380 
381 extern int __pthread_cond_init (pthread_cond_t *cond,
382 				const pthread_condattr_t *cond_attr);
383 extern int __pthread_cond_destroy (pthread_cond_t *cond);
384 extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
385 extern int __pthread_cond_timedwait (pthread_cond_t *cond,
386 				     pthread_mutex_t *mutex,
387 				     const struct timespec *abstime);
388 extern int __pthread_cond_signal (pthread_cond_t *cond);
389 extern int __pthread_cond_broadcast (pthread_cond_t *cond);
390 extern int __pthread_condattr_init (pthread_condattr_t *attr);
391 extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
392 extern pthread_t __pthread_self (void);
393 extern pthread_descr __pthread_thread_self (void);
394 extern pthread_descr __pthread_self_stack (void) L4_HIDDEN;
395 extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
396 extern void __pthread_exit (void *retval)
397 #if defined NOT_IN_libc && defined IS_IN_libpthread
398 	attribute_noreturn
399 #endif
400 	;
401 extern int __pthread_getschedparam (pthread_t thread, int *policy,
402 				    struct sched_param *param) __THROW;
403 extern int __pthread_setschedparam (pthread_t thread, int policy,
404 				    const struct sched_param *param) __THROW;
405 extern int __pthread_setcancelstate (int state, int * oldstate);
406 extern int __pthread_setcanceltype (int type, int * oldtype);
407 
408 extern int __pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
409 					 __const struct timespec *__restrict
410 					 __abstime);
411 extern int __pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
412 					 __const struct timespec *__restrict
413 					 __abstime);
414 extern int __pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
415 
416 extern int __pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
417 					     __restrict __attr,
418 					     int *__restrict __pshared);
419 
420 extern int __pthread_spin_lock (pthread_spinlock_t *__lock);
421 extern int __pthread_spin_trylock (pthread_spinlock_t *__lock);
422 extern int __pthread_spin_unlock (pthread_spinlock_t *__lock);
423 extern int __pthread_spin_init (pthread_spinlock_t *__lock, int __pshared);
424 extern int __pthread_spin_destroy (pthread_spinlock_t *__lock);
425 
426 /* Global pointers to old or new suspend functions */
427 
428 extern void (*__pthread_restart)(pthread_descr);
429 extern void (*__pthread_suspend)(pthread_descr);
430 extern int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *);
431 
432 /* Prototypes for some of the new semaphore functions.  */
433 extern int sem_post (sem_t * sem) __THROW;
434 extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value) __THROW;
435 extern int sem_wait (sem_t *__sem);
436 extern int sem_trywait (sem_t *__sem) __THROW;
437 extern int sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval) __THROW;
438 extern int sem_destroy (sem_t *__sem) __THROW;
439 
440 /* Prototypes for compatibility functions.  */
441 extern int __pthread_attr_init (pthread_attr_t *__attr);
442 extern int __pthread_create (pthread_t *__restrict __threadp,
443 				 const pthread_attr_t *__attr,
444 				 void *(*__start_routine) (void *),
445 				 void *__restrict __arg);
446 
447 /* The functions called the signal events.  */
448 extern void __linuxthreads_create_event (void);
449 extern void __linuxthreads_death_event (void);
450 extern void __linuxthreads_reap_event (void);
451 
452 /* This function is called to initialize the pthread library.  */
453 extern void __pthread_initialize (void);
454 
455 /* TSD.  */
456 extern int __pthread_internal_tsd_set (int key, const void * pointer);
457 extern void * __pthread_internal_tsd_get (int key);
458 extern void ** __attribute__ ((__const__))
459   __pthread_internal_tsd_address (int key);
460 #if 0
461 /* Sighandler wrappers.  */
462 extern void __pthread_sighandler(int signo, SIGCONTEXT ctx);
463 extern void __pthread_sighandler_rt(int signo, struct siginfo *si,
464 				    struct ucontext *uc);
465 extern void __pthread_null_sighandler(int sig);
466 extern int __pthread_sigaction (int sig, const struct sigaction *act,
467 				struct sigaction *oact);
468 extern int __pthread_sigwait (const sigset_t *set, int *sig);
469 extern int __pthread_raise (int sig);
470 #endif
471 /* Cancellation.  */
472 extern int __pthread_enable_asynccancel (void) L4_HIDDEN;
473 extern void __pthread_disable_asynccancel (int oldtype)
474   internal_function L4_HIDDEN;
475 
476 /* The two functions are in libc.so and not exported.  */
477 extern int __libc_enable_asynccancel (void) L4_HIDDEN;
478 extern void __libc_disable_asynccancel (int oldtype)
479   internal_function L4_HIDDEN;
480 
481 /* The two functions are in libc.so and are exported.  */
482 extern int __librt_enable_asynccancel (void);
483 extern void __librt_disable_asynccancel (int oldtype) internal_function;
484 
485 extern void __pthread_cleanup_upto (__jmp_buf target,
486 				    char *targetframe) L4_HIDDEN;
487 #if 0
488 extern pid_t __pthread_fork (struct fork_block *b) L4_HIDDEN;
489 #endif
490 
491 #define asm_handle(name) _asm_handle(name)
492 #define _asm_handle(name) #name
493 #define ASM_GLOBAL asm_handle(ASM_GLOBAL_DIRECTIVE)
494 #define ASM_CANCEL(name) asm_handle(C_SYMBOL_NAME(name))
495 
496 #if !defined NOT_IN_libc
497 # define LIBC_CANCEL_ASYNC() \
498   __libc_enable_asynccancel ()
499 # define LIBC_CANCEL_RESET(oldtype) \
500   __libc_disable_asynccancel (oldtype)
501 # define LIBC_CANCEL_HANDLED() \
502   __asm__ (ASM_GLOBAL " " ASM_CANCEL(__libc_enable_asynccancel)); \
503   __asm__ (ASM_GLOBAL " " ASM_CANCEL(__libc_disable_asynccancel))
504 #elif defined IS_IN_libpthread
505 # define LIBC_CANCEL_ASYNC() \
506   __pthread_enable_asynccancel ()
507 # define LIBC_CANCEL_RESET(oldtype) \
508   __pthread_disable_asynccancel (oldtype)
509 # define LIBC_CANCEL_HANDLED() \
510   __asm__ (ASM_GLOBAL " " ASM_CANCEL(__pthread_enable_asynccancel)); \
511   __asm__ (ASM_GLOBAL " " ASM_CANCEL(__pthread_disable_asynccancel))
512 #elif defined IS_IN_librt
513 # define LIBC_CANCEL_ASYNC() \
514   __librt_enable_asynccancel ()
515 # define LIBC_CANCEL_RESET(oldtype) \
516   __librt_disable_asynccancel (oldtype)
517 # define LIBC_CANCEL_HANDLED() \
518   __asm__ (ASM_GLOBAL " " ASM_CANCEL(__librt_enable_asynccancel)); \
519   __asm__ (ASM_GLOBAL " " ASM_CANCEL(__librt_disable_asynccancel))
520 #else
521 # define LIBC_CANCEL_ASYNC()    0 /* Just a dummy value.  */
522 # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it.  */
523 # define LIBC_CANCEL_HANDLED()	/* Nothing.  */
524 #endif
525 extern int * __libc_pthread_init (const struct pthread_functions *functions);
526 __END_DECLS
527 
528 
529 #ifndef USE_TLS
530 # define __manager_thread (&__pthread_manager_thread)
531 #else
532 # define __manager_thread __pthread_manager_threadp
533 #endif
534 
535 
536 static inline int __pthread_getprio(int policy, int prio);
__pthread_getprio(int policy,int prio)537 static inline int __pthread_getprio(int policy, int prio)
538 {
539     switch (policy)
540     {
541     case SCHED_OTHER:
542       prio = 0;
543       break;
544     case SCHED_IDLE:
545       prio = 0;
546       break;
547     case SCHED_RR:
548       prio -= __pthread_sched_rr_prio_min;
549       break;
550     case SCHED_L4:
551       break;
552     default:
553       return -1;
554     }
555 
556     return prio;
557 }
558 
559 static inline int __pthread_l4_getprio(int policy, int posix_prio);
__pthread_l4_getprio(int policy,int posix_prio)560 static inline int __pthread_l4_getprio(int policy, int posix_prio)
561 {
562   switch (policy)
563     {
564     case SCHED_OTHER:
565       return __pthread_sched_other_prio;
566     case SCHED_IDLE:
567       return __pthread_sched_idle_prio;
568     case SCHED_RR:
569       return __pthread_sched_rr_prio_min + posix_prio;
570     case SCHED_L4:
571       return posix_prio;
572     default:
573       return -1;
574     }
575 }
576 
577 
578 static inline pthread_descr
579 check_thread_self (void);
580 static inline pthread_descr
check_thread_self(void)581 check_thread_self (void)
582 {
583   pthread_descr self = thread_self ();
584 #if defined THREAD_SELF && defined INIT_THREAD_SELF
585   if (self == __manager_thread)
586     {
587       /* A new thread might get a cancel signal before it is fully
588 	 initialized, so that the thread register might still point to the
589 	 manager thread.  Double check that this is really the manager
590 	 thread.  */
591       self = handle_to_descr(l4_utcb());
592       if (self != __manager_thread)
593 	/* Oops, thread_self() isn't working yet..  */
594 	INIT_THREAD_SELF(self, self->p_nr);
595     }
596 #endif
597   return self;
598 }
599 
600 void __l4_kill_thread(l4_cap_idx_t cap);
601 
602 #endif /* internals.h */
603