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 /* Adaptive mutex definitions. */
49 #ifndef MAX_ADAPTIVE_COUNT
50 # define MAX_ADAPTIVE_COUNT 100
54 /* Internal variables. */
57 /* Default stack size. */
58 extern size_t __default_stacksize attribute_hidden
;
60 /* Size and alignment of static TLS block. */
61 extern size_t __static_tls_size attribute_hidden
;
62 extern size_t __static_tls_align_m1 attribute_hidden
;
64 /* Flag whether the machine is SMP or not. */
65 extern int __is_smp attribute_hidden
;
67 /* Thread descriptor handling. */
68 extern list_t __stack_user
;
69 hidden_proto (__stack_user
)
71 /* Attribute handling. */
72 extern struct pthread_attr
*__attr_list attribute_hidden
;
73 extern lll_lock_t __attr_list_lock attribute_hidden
;
75 /* First available RT signal. */
76 extern int __current_sigrtmin attribute_hidden
;
77 /* Last available RT signal. */
78 extern int __current_sigrtmax attribute_hidden
;
80 /* Concurrency handling. */
81 extern int __concurrency_level attribute_hidden
;
83 /* Thread-local data key handling. */
84 extern struct pthread_key_struct __pthread_keys
[PTHREAD_KEYS_MAX
];
85 hidden_proto (__pthread_keys
)
87 /* Number of threads running. */
88 extern unsigned int __nptl_nthreads attribute_hidden
;
90 /* The library can run in debugging mode where it performs a lot more
92 extern int __pthread_debug attribute_hidden
;
93 /** For now disable debugging support. */
95 # define DEBUGGING_P __builtin_expect (__pthread_debug, 0)
96 # define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
97 # define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd)
99 # define DEBUGGING_P 0
100 /* Simplified test. This will not catch all invalid descriptors but
101 is better than nothing. And if the test triggers the thread
102 descriptor is guaranteed to be invalid. */
103 # define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0)
104 # define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0)
108 /* Cancellation test. */
109 #define CANCELLATION_P(self) \
111 int cancelhandling = THREAD_GETMEM (self, cancelhandling); \
112 if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \
114 THREAD_SETMEM (self, result, PTHREAD_CANCELED); \
120 extern void __pthread_unwind (__pthread_unwind_buf_t
*__buf
)
121 __cleanup_fct_attribute
__attribute ((__noreturn__
))
122 #if !defined SHARED && !defined IS_IN_libpthread
126 extern void __pthread_unwind_next (__pthread_unwind_buf_t
*__buf
)
127 __cleanup_fct_attribute
__attribute ((__noreturn__
))
132 extern void __pthread_register_cancel (__pthread_unwind_buf_t
*__buf
)
133 __cleanup_fct_attribute
;
134 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t
*__buf
)
135 __cleanup_fct_attribute
;
136 #if defined NOT_IN_libc && defined IS_IN_libpthread
137 hidden_proto (__pthread_unwind
)
138 hidden_proto (__pthread_unwind_next
)
139 hidden_proto (__pthread_register_cancel
)
140 hidden_proto (__pthread_unregister_cancel
)
142 extern void attribute_hidden
pthread_cancel_init (void);
147 /* Called when a thread reacts on a cancellation request. */
149 __attribute ((noreturn
, always_inline
))
152 struct pthread
*self
= THREAD_SELF
;
154 /* Make sure we get no more cancellations. */
155 THREAD_ATOMIC_BIT_SET (self
, cancelhandling
, EXITING_BIT
);
157 __pthread_unwind ((__pthread_unwind_buf_t
*)
158 THREAD_GETMEM (self
, cleanup_jmp_buf
));
162 /* Set cancellation mode to asynchronous. */
163 #define CANCEL_ASYNC() \
164 __pthread_enable_asynccancel ()
165 /* Reset to previous cancellation mode. */
166 #define CANCEL_RESET(oldtype) \
167 __pthread_disable_asynccancel (oldtype)
169 #if !defined NOT_IN_libc
170 /* Same as CANCEL_ASYNC, but for use in libc.so. */
171 # define LIBC_CANCEL_ASYNC() \
172 __libc_enable_asynccancel ()
173 /* Same as CANCEL_RESET, but for use in libc.so. */
174 # define LIBC_CANCEL_RESET(oldtype) \
175 __libc_disable_asynccancel (oldtype)
176 # define LIBC_CANCEL_HANDLED() \
177 __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \
178 __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel")
179 #elif defined NOT_IN_libc && defined IS_IN_libpthread
180 # define LIBC_CANCEL_ASYNC() CANCEL_ASYNC ()
181 # define LIBC_CANCEL_RESET(val) CANCEL_RESET (val)
182 # define LIBC_CANCEL_HANDLED() \
183 __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \
184 __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel")
185 #elif defined NOT_IN_libc && defined IS_IN_librt
186 # define LIBC_CANCEL_ASYNC() \
187 __librt_enable_asynccancel ()
188 # define LIBC_CANCEL_RESET(val) \
189 __librt_disable_asynccancel (val)
190 # define LIBC_CANCEL_HANDLED() \
191 __asm (".globl " __SYMBOL_PREFIX "__librt_enable_asynccancel"); \
192 __asm (".globl " __SYMBOL_PREFIX "__librt_disable_asynccancel")
194 # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
195 # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
196 # define LIBC_CANCEL_HANDLED() /* Nothing. */
199 /* The signal used for asynchronous cancelation. */
200 #define SIGCANCEL __SIGRTMIN
203 /* Signal needed for the kernel-supported POSIX timer implementation.
204 We can reuse the cancellation signal since we can distinguish
205 cancellation from timer expirations. */
206 #define SIGTIMER SIGCANCEL
209 /* Internal prototypes. */
211 /* Thread list handling. */
212 extern struct pthread
*__find_in_stack_list (struct pthread
*pd
)
213 attribute_hidden internal_function
;
215 /* Deallocate a thread's stack after optionally making sure the thread
216 descriptor is still valid. */
217 extern void __free_tcb (struct pthread
*pd
) attribute_hidden internal_function
;
219 /* Free allocated stack. */
220 extern void __deallocate_stack (struct pthread
*pd
)
221 attribute_hidden internal_function
;
223 /* Mark all the stacks except for the current one as available. This
224 function also re-initializes the lock for the stack cache. */
225 extern void __reclaim_stacks (void) attribute_hidden
;
227 /* Make all threads's stacks executable. */
228 extern int __make_stacks_executable (void **stack_endp
)
229 internal_function attribute_hidden
;
231 /* longjmp handling. */
232 extern void __pthread_cleanup_upto (__jmp_buf target
, char *targetframe
);
233 #if defined NOT_IN_libc && defined IS_IN_libpthread
234 hidden_proto (__pthread_cleanup_upto
)
238 /* Functions with versioned interfaces. */
239 extern int __pthread_create_2_1 (pthread_t
*newthread
,
240 const pthread_attr_t
*attr
,
241 void *(*start_routine
) (void *), void *arg
);
242 extern int __pthread_create_2_0 (pthread_t
*newthread
,
243 const pthread_attr_t
*attr
,
244 void *(*start_routine
) (void *), void *arg
);
245 extern int __pthread_attr_init_2_1 (pthread_attr_t
*attr
);
246 extern int __pthread_attr_init_2_0 (pthread_attr_t
*attr
);
249 /* Event handlers for libthread_db interface. */
250 extern void __nptl_create_event (void);
251 extern void __nptl_death_event (void);
252 hidden_proto (__nptl_create_event
)
253 hidden_proto (__nptl_death_event
)
255 /* Register the generation counter in the libpthread with the libc. */
256 #ifdef TLS_MULTIPLE_THREADS_IN_TCB
257 extern void __libc_pthread_init (unsigned long int *ptr
,
258 void (*reclaim
) (void),
259 const struct pthread_functions
*functions
)
262 extern int *__libc_pthread_init (unsigned long int *ptr
,
263 void (*reclaim
) (void),
264 const struct pthread_functions
*functions
)
267 /* Variable set to a nonzero value if more than one thread runs or ran. */
268 extern int __pthread_multiple_threads attribute_hidden
;
269 /* Pointer to the corresponding variable in libc. */
270 extern int *__libc_multiple_threads_ptr attribute_hidden
;
273 /* Find a thread given its TID. */
274 extern struct pthread
*__find_thread_by_id (pid_t tid
) attribute_hidden
;
276 extern void __pthread_init_static_tls (struct link_map
*) attribute_hidden
;
279 /* Namespace save aliases. */
280 extern int __pthread_getschedparam (pthread_t thread_id
, int *policy
,
281 struct sched_param
*param
);
282 extern int __pthread_setschedparam (pthread_t thread_id
, int policy
,
283 const struct sched_param
*param
);
284 extern int __pthread_setcancelstate (int state
, int *oldstate
);
285 extern int __pthread_mutex_init (pthread_mutex_t
*__mutex
,
286 __const pthread_mutexattr_t
*__mutexattr
);
287 extern int __pthread_mutex_init_internal (pthread_mutex_t
*__mutex
,
288 __const pthread_mutexattr_t
*__mutexattr
)
290 extern int __pthread_mutex_destroy (pthread_mutex_t
*__mutex
);
291 extern int __pthread_mutex_destroy_internal (pthread_mutex_t
*__mutex
)
293 extern int __pthread_mutex_trylock (pthread_mutex_t
*_mutex
);
294 extern int __pthread_mutex_lock (pthread_mutex_t
*__mutex
);
295 extern int __pthread_mutex_lock_internal (pthread_mutex_t
*__mutex
)
297 extern int __pthread_mutex_cond_lock (pthread_mutex_t
*__mutex
)
298 attribute_hidden internal_function
;
299 extern int __pthread_mutex_unlock (pthread_mutex_t
*__mutex
);
300 extern int __pthread_mutex_unlock_internal (pthread_mutex_t
*__mutex
)
302 extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t
*__mutex
,
304 attribute_hidden internal_function
;
305 extern int __pthread_mutexattr_init (pthread_mutexattr_t
*attr
);
306 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t
*attr
);
307 extern int __pthread_mutexattr_settype (pthread_mutexattr_t
*attr
, int kind
);
308 extern int __pthread_attr_destroy (pthread_attr_t
*attr
);
309 extern int __pthread_attr_getdetachstate (const pthread_attr_t
*attr
,
311 extern int __pthread_attr_setdetachstate (pthread_attr_t
*attr
,
313 extern int __pthread_attr_getinheritsched (const pthread_attr_t
*attr
,
315 extern int __pthread_attr_setinheritsched (pthread_attr_t
*attr
, int inherit
);
316 extern int __pthread_attr_getschedparam (const pthread_attr_t
*attr
,
317 struct sched_param
*param
);
318 extern int __pthread_attr_setschedparam (pthread_attr_t
*attr
,
319 const struct sched_param
*param
);
320 extern int __pthread_attr_getschedpolicy (const pthread_attr_t
*attr
,
322 extern int __pthread_attr_setschedpolicy (pthread_attr_t
*attr
, int policy
);
323 extern int __pthread_attr_getscope (const pthread_attr_t
*attr
, int *scope
);
324 extern int __pthread_attr_setscope (pthread_attr_t
*attr
, int scope
);
325 extern int __pthread_attr_getstackaddr (__const pthread_attr_t
*__restrict
326 __attr
, void **__restrict __stackaddr
);
327 extern int __pthread_attr_setstackaddr (pthread_attr_t
*__attr
,
329 extern int __pthread_attr_getstacksize (__const pthread_attr_t
*__restrict
331 size_t *__restrict __stacksize
);
332 extern int __pthread_attr_setstacksize (pthread_attr_t
*__attr
,
334 extern int __pthread_attr_getstack (__const pthread_attr_t
*__restrict __attr
,
335 void **__restrict __stackaddr
,
336 size_t *__restrict __stacksize
);
337 extern int __pthread_attr_setstack (pthread_attr_t
*__attr
, void *__stackaddr
,
339 extern int __pthread_rwlock_init (pthread_rwlock_t
*__restrict __rwlock
,
340 __const pthread_rwlockattr_t
*__restrict
342 extern int __pthread_rwlock_destroy (pthread_rwlock_t
*__rwlock
);
343 extern int __pthread_rwlock_rdlock (pthread_rwlock_t
*__rwlock
);
344 extern int __pthread_rwlock_rdlock_internal (pthread_rwlock_t
*__rwlock
);
345 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t
*__rwlock
);
346 extern int __pthread_rwlock_wrlock (pthread_rwlock_t
*__rwlock
);
347 extern int __pthread_rwlock_wrlock_internal (pthread_rwlock_t
*__rwlock
);
348 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t
*__rwlock
);
349 extern int __pthread_rwlock_unlock (pthread_rwlock_t
*__rwlock
);
350 extern int __pthread_rwlock_unlock_internal (pthread_rwlock_t
*__rwlock
);
351 extern int __pthread_cond_broadcast (pthread_cond_t
*cond
);
352 extern int __pthread_cond_destroy (pthread_cond_t
*cond
);
353 extern int __pthread_cond_init (pthread_cond_t
*cond
,
354 const pthread_condattr_t
*cond_attr
);
355 extern int __pthread_cond_signal (pthread_cond_t
*cond
);
356 extern int __pthread_cond_wait (pthread_cond_t
*cond
, pthread_mutex_t
*mutex
);
357 extern int __pthread_cond_timedwait (pthread_cond_t
*cond
,
358 pthread_mutex_t
*mutex
,
359 const struct timespec
*abstime
);
360 extern int __pthread_condattr_destroy (pthread_condattr_t
*attr
);
361 extern int __pthread_condattr_init (pthread_condattr_t
*attr
);
362 extern int __pthread_key_create (pthread_key_t
*key
, void (*destr
) (void *));
363 extern int __pthread_key_create_internal (pthread_key_t
*key
,
364 void (*destr
) (void *));
365 extern void *__pthread_getspecific (pthread_key_t key
);
366 extern void *__pthread_getspecific_internal (pthread_key_t key
);
367 extern int __pthread_setspecific (pthread_key_t key
, const void *value
);
368 extern int __pthread_setspecific_internal (pthread_key_t key
,
370 extern int __pthread_once (pthread_once_t
*once_control
,
371 void (*init_routine
) (void));
372 extern int __pthread_once_internal (pthread_once_t
*once_control
,
373 void (*init_routine
) (void));
374 extern int __pthread_atfork (void (*prepare
) (void), void (*parent
) (void),
375 void (*child
) (void));
376 extern pthread_t
__pthread_self (void);
377 extern int __pthread_equal (pthread_t thread1
, pthread_t thread2
);
378 extern int __pthread_kill (pthread_t threadid
, int signo
);
379 extern void __pthread_exit (void *value
);
380 extern int __pthread_setcanceltype (int type
, int *oldtype
);
381 extern int __pthread_enable_asynccancel (void) attribute_hidden
;
382 extern void __pthread_disable_asynccancel (int oldtype
)
383 internal_function attribute_hidden
;
385 extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t
*cond
);
386 extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t
*cond
);
387 extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t
*cond
,
388 const pthread_condattr_t
*cond_attr
);
389 extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t
*cond
);
390 extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t
*cond
,
391 pthread_mutex_t
*mutex
,
392 const struct timespec
*abstime
);
393 extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t
*cond
,
394 pthread_mutex_t
*mutex
);
396 extern int __pthread_getaffinity_np (pthread_t th
, size_t cpusetsize
,
399 /* The two functions are in libc.so and not exported. */
400 extern int __libc_enable_asynccancel (void) attribute_hidden
;
401 extern void __libc_disable_asynccancel (int oldtype
)
402 internal_function attribute_hidden
;
405 /* The two functions are in librt.so and not exported. */
406 extern int __librt_enable_asynccancel (void) attribute_hidden
;
407 extern void __librt_disable_asynccancel (int oldtype
)
408 internal_function attribute_hidden
;
410 #ifdef IS_IN_libpthread
411 /* Special versions which use non-exported functions. */
412 extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer
*buffer
,
413 void (*routine
) (void *), void *arg
)
415 # undef pthread_cleanup_push
416 # define pthread_cleanup_push(routine,arg) \
417 { struct _pthread_cleanup_buffer _buffer; \
418 __pthread_cleanup_push (&_buffer, (routine), (arg));
420 extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer
*buffer
,
421 int execute
) attribute_hidden
;
422 # undef pthread_cleanup_pop
423 # define pthread_cleanup_pop(execute) \
424 __pthread_cleanup_pop (&_buffer, (execute)); }
427 extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer
*buffer
,
428 void (*routine
) (void *), void *arg
);
429 extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer
*buffer
,
432 /* Old cleanup interfaces, still used in libc.so. */
433 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer
*buffer
,
434 void (*routine
) (void *), void *arg
);
435 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer
*buffer
,
437 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer
*buffer
,
438 void (*routine
) (void *), void *arg
);
439 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer
*buffer
,
442 extern void __nptl_deallocate_tsd (void) attribute_hidden
;
444 #endif /* pthreadP.h */