1 /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <sys/syscall.h>
29 #include <lowlevellock.h>
30 #include <stackinfo.h>
31 #include <internaltypes.h>
32 #include <pthread-functions.h>
36 /* Atomic operations on TLS memory. */
37 #ifndef THREAD_ATOMIC_CMPXCHG_VAL
38 # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, new, old) \
39 atomic_compare_and_exchange_val_acq (&(descr)->member, new, old)
42 #ifndef THREAD_ATOMIC_BIT_SET
43 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
44 atomic_bit_set (&(descr)->member, bit)
48 /* Internal variables. */
51 /* Default stack size. */
52 extern size_t __default_stacksize attribute_hidden
;
54 /* Size and alignment of static TLS block. */
55 extern size_t __static_tls_size attribute_hidden
;
56 extern size_t __static_tls_align_m1 attribute_hidden
;
58 /* Thread descriptor handling. */
59 extern list_t __stack_user
;
60 hidden_proto (__stack_user
)
62 /* Attribute handling. */
63 extern struct pthread_attr
*__attr_list attribute_hidden
;
64 extern lll_lock_t __attr_list_lock attribute_hidden
;
66 /* First available RT signal. */
67 extern int __current_sigrtmin attribute_hidden
;
68 /* Last available RT signal. */
69 extern int __current_sigrtmax attribute_hidden
;
71 /* Concurrency handling. */
72 extern int __concurrency_level attribute_hidden
;
74 /* Thread-local data key handling. */
75 extern struct pthread_key_struct __pthread_keys
[PTHREAD_KEYS_MAX
];
76 hidden_proto (__pthread_keys
)
78 /* Number of threads running. */
79 extern unsigned int __nptl_nthreads attribute_hidden
;
81 /* The library can run in debugging mode where it performs a lot more
83 extern int __pthread_debug attribute_hidden
;
84 /** For now disable debugging support. */
86 # define DEBUGGING_P __builtin_expect (__pthread_debug, 0)
87 # define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
88 # define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd)
90 # define DEBUGGING_P 0
91 /* Simplified test. This will not catch all invalid descriptors but
92 is better than nothing. And if the test triggers the thread
93 descriptor is guaranteed to be invalid. */
94 # define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0)
95 # define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0)
99 /* Cancellation test. */
100 #define CANCELLATION_P(self) \
102 int cancelhandling = THREAD_GETMEM (self, cancelhandling); \
103 if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \
105 THREAD_SETMEM (self, result, PTHREAD_CANCELED); \
111 extern void __pthread_unwind (__pthread_unwind_buf_t
*__buf
)
112 __cleanup_fct_attribute
__attribute ((__noreturn__
))
113 #if !defined SHARED && !defined IS_IN_libpthread
117 extern void __pthread_unwind_next (__pthread_unwind_buf_t
*__buf
)
118 __cleanup_fct_attribute
__attribute ((__noreturn__
))
123 extern void __pthread_register_cancel (__pthread_unwind_buf_t
*__buf
)
124 __cleanup_fct_attribute
;
125 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t
*__buf
)
126 __cleanup_fct_attribute
;
127 #if defined NOT_IN_libc && defined IS_IN_libpthread
128 hidden_proto (__pthread_unwind
)
129 hidden_proto (__pthread_unwind_next
)
130 hidden_proto (__pthread_register_cancel
)
131 hidden_proto (__pthread_unregister_cancel
)
133 extern void attribute_hidden
pthread_cancel_init (void);
138 /* Called when a thread reacts on a cancellation request. */
140 __attribute ((noreturn
, always_inline
))
143 struct pthread
*self
= THREAD_SELF
;
145 /* Make sure we get no more cancellations. */
146 THREAD_ATOMIC_BIT_SET (self
, cancelhandling
, EXITING_BIT
);
148 __pthread_unwind ((__pthread_unwind_buf_t
*)
149 THREAD_GETMEM (self
, cleanup_jmp_buf
));
153 /* Set cancellation mode to asynchronous. */
154 #define CANCEL_ASYNC() \
155 __pthread_enable_asynccancel ()
156 /* Reset to previous cancellation mode. */
157 #define CANCEL_RESET(oldtype) \
158 __pthread_disable_asynccancel (oldtype)
160 #if !defined NOT_IN_libc
161 /* Same as CANCEL_ASYNC, but for use in libc.so. */
162 # define LIBC_CANCEL_ASYNC() \
163 __libc_enable_asynccancel ()
164 /* Same as CANCEL_RESET, but for use in libc.so. */
165 # define LIBC_CANCEL_RESET(oldtype) \
166 __libc_disable_asynccancel (oldtype)
167 # define LIBC_CANCEL_HANDLED() \
168 __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \
169 __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel")
170 #elif defined NOT_IN_libc && defined IS_IN_libpthread
171 # define LIBC_CANCEL_ASYNC() CANCEL_ASYNC ()
172 # define LIBC_CANCEL_RESET(val) CANCEL_RESET (val)
173 # define LIBC_CANCEL_HANDLED() \
174 __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \
175 __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel")
176 #elif defined NOT_IN_libc && defined IS_IN_librt
177 # define LIBC_CANCEL_ASYNC() \
178 __librt_enable_asynccancel ()
179 # define LIBC_CANCEL_RESET(val) \
180 __librt_disable_asynccancel (val)
181 # define LIBC_CANCEL_HANDLED() \
182 __asm (".globl " __SYMBOL_PREFIX "__librt_enable_asynccancel"); \
183 __asm (".globl " __SYMBOL_PREFIX "__librt_disable_asynccancel")
185 # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
186 # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
187 # define LIBC_CANCEL_HANDLED() /* Nothing. */
190 /* The signal used for asynchronous cancelation. */
191 #define SIGCANCEL __SIGRTMIN
194 /* Signal needed for the kernel-supported POSIX timer implementation.
195 We can reuse the cancellation signal since we can distinguish
196 cancellation from timer expirations. */
197 #define SIGTIMER SIGCANCEL
200 /* Internal prototypes. */
202 /* Thread list handling. */
203 extern struct pthread
*__find_in_stack_list (struct pthread
*pd
)
204 attribute_hidden internal_function
;
206 /* Deallocate a thread's stack after optionally making sure the thread
207 descriptor is still valid. */
208 extern void __free_tcb (struct pthread
*pd
) attribute_hidden internal_function
;
210 /* Free allocated stack. */
211 extern void __deallocate_stack (struct pthread
*pd
)
212 attribute_hidden internal_function
;
214 /* Mark all the stacks except for the current one as available. This
215 function also re-initializes the lock for the stack cache. */
216 extern void __reclaim_stacks (void) attribute_hidden
;
218 /* Make all threads's stacks executable. */
219 extern int __make_stacks_executable (void **stack_endp
)
220 internal_function attribute_hidden
;
222 /* longjmp handling. */
223 extern void __pthread_cleanup_upto (__jmp_buf target
, char *targetframe
);
224 #if defined NOT_IN_libc && defined IS_IN_libpthread
225 hidden_proto (__pthread_cleanup_upto
)
229 /* Functions with versioned interfaces. */
230 extern int __pthread_create_2_1 (pthread_t
*newthread
,
231 const pthread_attr_t
*attr
,
232 void *(*start_routine
) (void *), void *arg
);
233 extern int __pthread_create_2_0 (pthread_t
*newthread
,
234 const pthread_attr_t
*attr
,
235 void *(*start_routine
) (void *), void *arg
);
236 extern int __pthread_attr_init_2_1 (pthread_attr_t
*attr
);
237 extern int __pthread_attr_init_2_0 (pthread_attr_t
*attr
);
240 /* Event handlers for libthread_db interface. */
241 extern void __nptl_create_event (void);
242 extern void __nptl_death_event (void);
243 hidden_proto (__nptl_create_event
)
244 hidden_proto (__nptl_death_event
)
246 /* Register the generation counter in the libpthread with the libc. */
247 #ifdef TLS_MULTIPLE_THREADS_IN_TCB
248 extern void __libc_pthread_init (unsigned long int *ptr
,
249 void (*reclaim
) (void),
250 const struct pthread_functions
*functions
)
253 extern int *__libc_pthread_init (unsigned long int *ptr
,
254 void (*reclaim
) (void),
255 const struct pthread_functions
*functions
)
258 /* Variable set to a nonzero value if more than one thread runs or ran. */
259 extern int __pthread_multiple_threads attribute_hidden
;
260 /* Pointer to the corresponding variable in libc. */
261 extern int *__libc_multiple_threads_ptr attribute_hidden
;
264 /* Find a thread given its TID. */
265 extern struct pthread
*__find_thread_by_id (pid_t tid
) attribute_hidden
;
267 extern void __pthread_init_static_tls (struct link_map
*) attribute_hidden
;
270 /* Namespace save aliases. */
271 extern int __pthread_getschedparam (pthread_t thread_id
, int *policy
,
272 struct sched_param
*param
);
273 extern int __pthread_setschedparam (pthread_t thread_id
, int policy
,
274 const struct sched_param
*param
);
275 extern int __pthread_setcancelstate (int state
, int *oldstate
);
276 extern int __pthread_mutex_init (pthread_mutex_t
*__mutex
,
277 __const pthread_mutexattr_t
*__mutexattr
);
278 extern int __pthread_mutex_init_internal (pthread_mutex_t
*__mutex
,
279 __const pthread_mutexattr_t
*__mutexattr
)
281 extern int __pthread_mutex_destroy (pthread_mutex_t
*__mutex
);
282 extern int __pthread_mutex_destroy_internal (pthread_mutex_t
*__mutex
)
284 extern int __pthread_mutex_trylock (pthread_mutex_t
*_mutex
);
285 extern int __pthread_mutex_lock (pthread_mutex_t
*__mutex
);
286 extern int __pthread_mutex_lock_internal (pthread_mutex_t
*__mutex
)
288 extern int __pthread_mutex_cond_lock (pthread_mutex_t
*__mutex
)
289 attribute_hidden internal_function
;
290 extern int __pthread_mutex_unlock (pthread_mutex_t
*__mutex
);
291 extern int __pthread_mutex_unlock_internal (pthread_mutex_t
*__mutex
)
293 extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t
*__mutex
,
295 attribute_hidden internal_function
;
296 extern int __pthread_mutexattr_init (pthread_mutexattr_t
*attr
);
297 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t
*attr
);
298 extern int __pthread_mutexattr_settype (pthread_mutexattr_t
*attr
, int kind
);
299 extern int __pthread_attr_destroy (pthread_attr_t
*attr
);
300 extern int __pthread_attr_getdetachstate (const pthread_attr_t
*attr
,
302 extern int __pthread_attr_setdetachstate (pthread_attr_t
*attr
,
304 extern int __pthread_attr_getinheritsched (const pthread_attr_t
*attr
,
306 extern int __pthread_attr_setinheritsched (pthread_attr_t
*attr
, int inherit
);
307 extern int __pthread_attr_getschedparam (const pthread_attr_t
*attr
,
308 struct sched_param
*param
);
309 extern int __pthread_attr_setschedparam (pthread_attr_t
*attr
,
310 const struct sched_param
*param
);
311 extern int __pthread_attr_getschedpolicy (const pthread_attr_t
*attr
,
313 extern int __pthread_attr_setschedpolicy (pthread_attr_t
*attr
, int policy
);
314 extern int __pthread_attr_getscope (const pthread_attr_t
*attr
, int *scope
);
315 extern int __pthread_attr_setscope (pthread_attr_t
*attr
, int scope
);
316 extern int __pthread_attr_getstackaddr (__const pthread_attr_t
*__restrict
317 __attr
, void **__restrict __stackaddr
);
318 extern int __pthread_attr_setstackaddr (pthread_attr_t
*__attr
,
320 extern int __pthread_attr_getstacksize (__const pthread_attr_t
*__restrict
322 size_t *__restrict __stacksize
);
323 extern int __pthread_attr_setstacksize (pthread_attr_t
*__attr
,
325 extern int __pthread_attr_getstack (__const pthread_attr_t
*__restrict __attr
,
326 void **__restrict __stackaddr
,
327 size_t *__restrict __stacksize
);
328 extern int __pthread_attr_setstack (pthread_attr_t
*__attr
, void *__stackaddr
,
330 extern int __pthread_rwlock_init (pthread_rwlock_t
*__restrict __rwlock
,
331 __const pthread_rwlockattr_t
*__restrict
333 extern int __pthread_rwlock_destroy (pthread_rwlock_t
*__rwlock
);
334 extern int __pthread_rwlock_rdlock (pthread_rwlock_t
*__rwlock
);
335 extern int __pthread_rwlock_rdlock_internal (pthread_rwlock_t
*__rwlock
);
336 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t
*__rwlock
);
337 extern int __pthread_rwlock_wrlock (pthread_rwlock_t
*__rwlock
);
338 extern int __pthread_rwlock_wrlock_internal (pthread_rwlock_t
*__rwlock
);
339 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t
*__rwlock
);
340 extern int __pthread_rwlock_unlock (pthread_rwlock_t
*__rwlock
);
341 extern int __pthread_rwlock_unlock_internal (pthread_rwlock_t
*__rwlock
);
342 extern int __pthread_cond_broadcast (pthread_cond_t
*cond
);
343 extern int __pthread_cond_destroy (pthread_cond_t
*cond
);
344 extern int __pthread_cond_init (pthread_cond_t
*cond
,
345 const pthread_condattr_t
*cond_attr
);
346 extern int __pthread_cond_signal (pthread_cond_t
*cond
);
347 extern int __pthread_cond_wait (pthread_cond_t
*cond
, pthread_mutex_t
*mutex
);
348 extern int __pthread_cond_timedwait (pthread_cond_t
*cond
,
349 pthread_mutex_t
*mutex
,
350 const struct timespec
*abstime
);
351 extern int __pthread_condattr_destroy (pthread_condattr_t
*attr
);
352 extern int __pthread_condattr_init (pthread_condattr_t
*attr
);
353 extern int __pthread_key_create (pthread_key_t
*key
, void (*destr
) (void *));
354 extern int __pthread_key_create_internal (pthread_key_t
*key
,
355 void (*destr
) (void *));
356 extern void *__pthread_getspecific (pthread_key_t key
);
357 extern void *__pthread_getspecific_internal (pthread_key_t key
);
358 extern int __pthread_setspecific (pthread_key_t key
, const void *value
);
359 extern int __pthread_setspecific_internal (pthread_key_t key
,
361 extern int __pthread_once (pthread_once_t
*once_control
,
362 void (*init_routine
) (void));
363 extern int __pthread_once_internal (pthread_once_t
*once_control
,
364 void (*init_routine
) (void));
365 extern int __pthread_atfork (void (*prepare
) (void), void (*parent
) (void),
366 void (*child
) (void));
367 extern pthread_t
__pthread_self (void);
368 extern int __pthread_equal (pthread_t thread1
, pthread_t thread2
);
369 extern int __pthread_kill (pthread_t threadid
, int signo
);
370 extern void __pthread_exit (void *value
);
371 extern int __pthread_setcanceltype (int type
, int *oldtype
);
372 extern int __pthread_enable_asynccancel (void) attribute_hidden
;
373 extern void __pthread_disable_asynccancel (int oldtype
)
374 internal_function attribute_hidden
;
376 extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t
*cond
);
377 extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t
*cond
);
378 extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t
*cond
,
379 const pthread_condattr_t
*cond_attr
);
380 extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t
*cond
);
381 extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t
*cond
,
382 pthread_mutex_t
*mutex
,
383 const struct timespec
*abstime
);
384 extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t
*cond
,
385 pthread_mutex_t
*mutex
);
387 extern int __pthread_getaffinity_np (pthread_t th
, size_t cpusetsize
,
390 /* The two functions are in libc.so and not exported. */
391 extern int __libc_enable_asynccancel (void) attribute_hidden
;
392 extern void __libc_disable_asynccancel (int oldtype
)
393 internal_function attribute_hidden
;
396 /* The two functions are in librt.so and not exported. */
397 extern int __librt_enable_asynccancel (void) attribute_hidden
;
398 extern void __librt_disable_asynccancel (int oldtype
)
399 internal_function attribute_hidden
;
401 #ifdef IS_IN_libpthread
402 /* Special versions which use non-exported functions. */
403 extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer
*buffer
,
404 void (*routine
) (void *), void *arg
)
406 # undef pthread_cleanup_push
407 # define pthread_cleanup_push(routine,arg) \
408 { struct _pthread_cleanup_buffer _buffer; \
409 __pthread_cleanup_push (&_buffer, (routine), (arg));
411 extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer
*buffer
,
412 int execute
) attribute_hidden
;
413 # undef pthread_cleanup_pop
414 # define pthread_cleanup_pop(execute) \
415 __pthread_cleanup_pop (&_buffer, (execute)); }
418 extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer
*buffer
,
419 void (*routine
) (void *), void *arg
);
420 extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer
*buffer
,
423 /* Old cleanup interfaces, still used in libc.so. */
424 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer
*buffer
,
425 void (*routine
) (void *), void *arg
);
426 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer
*buffer
,
428 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer
*buffer
,
429 void (*routine
) (void *), void *arg
);
430 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer
*buffer
,
433 #endif /* pthreadP.h */