1 /* Copyright (C) 2002, 2003, 2004, 2005 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
25 #include <hp-timing.h>
28 #include <libc-internal.h>
32 /* Local function to start thread and handle cleanup. */
33 static int start_thread (void *arg);
36 /* Nozero if debugging mode is enabled. */
39 /* Globally enabled events. */
40 static td_thr_events_t __nptl_threads_events;
42 /* Pointer to descriptor with the last event. */
43 static struct pthread *__nptl_last_event;
45 /* Number of threads running. */
46 unsigned int __nptl_nthreads = 1;
49 /* Code to allocate and deallocate a stack. */
50 #include "allocatestack.c"
52 /* Code to create the thread. */
53 #include "createthread.c"
58 __find_in_stack_list (pd)
62 struct pthread *result = NULL;
64 lll_lock (stack_cache_lock);
66 list_for_each (entry, &stack_used)
70 curp = list_entry (entry, struct pthread, list);
79 list_for_each (entry, &__stack_user)
83 curp = list_entry (entry, struct pthread, list);
91 lll_unlock (stack_cache_lock);
97 /* Deallocate POSIX thread-local-storage. */
100 __nptl_deallocate_tsd (void)
102 struct pthread *self = THREAD_SELF;
104 /* Maybe no data was ever allocated. This happens often so we have
106 if (THREAD_GETMEM (self, specific_used))
116 /* So far no new nonzero data entry. */
117 THREAD_SETMEM (self, specific_used, false);
119 for (cnt = idx = 0; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
121 struct pthread_key_data *level2;
123 level2 = THREAD_GETMEM_NC (self, specific, cnt);
129 for (inner = 0; inner < PTHREAD_KEY_2NDLEVEL_SIZE;
132 void *data = level2[inner].data;
136 /* Always clear the data. */
137 level2[inner].data = NULL;
139 /* Make sure the data corresponds to a valid
140 key. This test fails if the key was
141 deallocated and also if it was
142 re-allocated. It is the user's
143 responsibility to free the memory in this
145 if (level2[inner].seq
146 == __pthread_keys[idx].seq
147 /* It is not necessary to register a destructor
149 && __pthread_keys[idx].destr != NULL)
150 /* Call the user-provided destructor. */
151 __pthread_keys[idx].destr (data);
156 idx += PTHREAD_KEY_1STLEVEL_SIZE;
159 if (THREAD_GETMEM (self, specific_used) == 0)
160 /* No data has been modified. */
163 /* We only repeat the process a fixed number of times. */
164 while (__builtin_expect (++round < PTHREAD_DESTRUCTOR_ITERATIONS, 0));
166 /* Just clear the memory of the first block for reuse. */
167 memset (&THREAD_SELF->specific_1stblock, '\0',
168 sizeof (self->specific_1stblock));
171 /* Free the memory for the other blocks. */
172 for (cnt = 1; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
174 struct pthread_key_data *level2;
176 level2 = THREAD_GETMEM_NC (self, specific, cnt);
179 /* The first block is allocated as part of the thread
182 THREAD_SETMEM_NC (self, specific, cnt, NULL);
186 THREAD_SETMEM (self, specific_used, false);
191 /* Deallocate a thread's stack after optionally making sure the thread
192 descriptor is still valid. */
195 __free_tcb (struct pthread *pd)
197 /* The thread is exiting now. */
198 if (__builtin_expect (atomic_bit_test_set (&pd->cancelhandling,
199 TERMINATED_BIT) == 0, 1))
201 /* Remove the descriptor from the list. */
202 if (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
203 /* Something is really wrong. The descriptor for a still
204 running thread is gone. */
207 /* Queue the stack memory block for reuse and exit the process. The
208 kernel will signal via writing to the address returned by
209 QUEUE-STACK when the stack is available. */
210 __deallocate_stack (pd);
216 start_thread (void *arg)
218 struct pthread *pd = (struct pthread *) arg;
221 /* Remember the time when the thread was started. */
224 THREAD_SETMEM (pd, cpuclock_offset, now);
227 /* Initialize resolver state pointer. */
230 /* This is where the try/finally block should be created. For
231 compilers without that support we do use setjmp. */
232 struct pthread_unwind_buf unwind_buf;
234 /* No previous handlers. */
235 unwind_buf.priv.data.prev = NULL;
236 unwind_buf.priv.data.cleanup = NULL;
239 not_first_call = setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf);
240 if (__builtin_expect (! not_first_call, 1))
242 /* Store the new cleanup handler info. */
243 THREAD_SETMEM (pd, cleanup_jmp_buf, &unwind_buf);
245 if (__builtin_expect (pd->stopped_start, 0))
247 int oldtype = CANCEL_ASYNC ();
249 /* Get the lock the parent locked to force synchronization. */
251 /* And give it up right away. */
252 lll_unlock (pd->lock);
254 CANCEL_RESET (oldtype);
257 /* Run the code the user provided. */
258 #ifdef CALL_THREAD_FCT
259 THREAD_SETMEM (pd, result, CALL_THREAD_FCT (pd));
261 THREAD_SETMEM (pd, result, pd->start_routine (pd->arg));
265 /* Run the destructor for the thread-local data. */
266 __nptl_deallocate_tsd ();
268 /* If this is the last thread we terminate the process now. We
269 do not notify the debugger, it might just irritate it if there
270 is no thread left. */
271 if (__builtin_expect (atomic_decrement_and_test (&__nptl_nthreads), 0))
272 /* This was the last thread. */
275 /* Report the death of the thread if this is wanted. */
276 if (__builtin_expect (pd->report_events, 0))
278 /* See whether TD_DEATH is in any of the mask. */
279 const int idx = __td_eventword (TD_DEATH);
280 const uint32_t mask = __td_eventmask (TD_DEATH);
282 if ((mask & (__nptl_threads_events.event_bits[idx]
283 | pd->eventbuf.eventmask.event_bits[idx])) != 0)
285 /* Yep, we have to signal the death. Add the descriptor to
286 the list but only if it is not already on it. */
287 if (pd->nextevent == NULL)
289 pd->eventbuf.eventnum = TD_DEATH;
290 pd->eventbuf.eventdata = pd;
293 pd->nextevent = __nptl_last_event;
294 while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event,
298 /* Now call the function to signal the event. */
299 __nptl_death_event ();
303 /* The thread is exiting now. Don't set this bit until after we've hit
304 the event-reporting breakpoint, so that td_thr_get_info on us while at
305 the breakpoint reports TD_THR_RUN state rather than TD_THR_ZOMBIE. */
306 atomic_bit_set (&pd->cancelhandling, EXITING_BIT);
308 /* If the thread is detached free the TCB. */
309 if (IS_DETACHED (pd))
313 /* We cannot call '_exit' here. '_exit' will terminate the process.
315 The 'exit' implementation in the kernel will signal when the
316 process is really dead since 'clone' got passed the CLONE_CLEARTID
317 flag. The 'tid' field in the TCB will be set to zero.
319 The exit code is zero since in case all threads exit by calling
320 'pthread_exit' the exit status must be 0 (zero). */
321 __exit_thread_inline (0);
328 /* Default thread attributes for the case when the user does not
330 static const struct pthread_attr default_attr =
332 /* Just some value > 0 which gets rounded to the nearest page size. */
338 __pthread_create_2_1 (newthread, attr, start_routine, arg)
339 pthread_t *newthread;
340 const pthread_attr_t *attr;
341 void *(*start_routine) (void *);
346 const struct pthread_attr *iattr = (struct pthread_attr *) attr;
348 /* Is this the best idea? On NUMA machines this could mean
349 accessing far-away memory. */
350 iattr = &default_attr;
352 struct pthread *pd = 0;
353 int err = ALLOCATE_STACK (iattr, &pd);
354 if (__builtin_expect (err != 0, 0))
355 /* Something went wrong. Maybe a parameter of the attributes is
356 invalid or we could not allocate memory. */
360 /* Initialize the TCB. All initializations with zero should be
361 performed in 'get_cached_stack'. This way we avoid doing this if
362 the stack freshly allocated with 'mmap'. */
365 /* Reference to the TCB itself. */
366 pd->header.self = pd;
368 /* Self-reference for TLS. */
372 /* Store the address of the start routine and the parameter. Since
373 we do not start the function directly the stillborn thread will
374 get the information from its thread descriptor. */
375 pd->start_routine = start_routine;
378 /* Copy the thread attribute flags. */
379 struct pthread *self = THREAD_SELF;
380 pd->flags = ((iattr->flags & ~(ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
381 | (self->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)));
383 /* Initialize the field for the ID of the thread which is waiting
384 for us. This is a self-reference in case the thread is created
386 pd->joinid = iattr->flags & ATTR_FLAG_DETACHSTATE ? pd : NULL;
388 /* The debug events are inherited from the parent. */
389 pd->eventbuf = self->eventbuf;
392 /* Copy the parent's scheduling parameters. The flags will say what
393 is valid and what is not. */
394 pd->schedpolicy = self->schedpolicy;
395 pd->schedparam = self->schedparam;
397 /* Copy the stack guard canary. */
398 #ifdef THREAD_COPY_STACK_GUARD
399 THREAD_COPY_STACK_GUARD (pd);
402 /* Determine scheduling parameters for the thread. */
404 && __builtin_expect ((iattr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0, 0)
405 && (iattr->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)) != 0)
407 INTERNAL_SYSCALL_DECL (scerr);
409 /* Use the scheduling parameters the user provided. */
410 if (iattr->flags & ATTR_FLAG_POLICY_SET)
411 pd->schedpolicy = iattr->schedpolicy;
412 else if ((pd->flags & ATTR_FLAG_POLICY_SET) == 0)
414 pd->schedpolicy = INTERNAL_SYSCALL (sched_getscheduler, scerr, 1, 0);
415 pd->flags |= ATTR_FLAG_POLICY_SET;
418 if (iattr->flags & ATTR_FLAG_SCHED_SET)
419 memcpy (&pd->schedparam, &iattr->schedparam,
420 sizeof (struct sched_param));
421 else if ((pd->flags & ATTR_FLAG_SCHED_SET) == 0)
423 INTERNAL_SYSCALL (sched_getparam, scerr, 2, 0, &pd->schedparam);
424 pd->flags |= ATTR_FLAG_SCHED_SET;
427 /* Check for valid priorities. */
428 int minprio = INTERNAL_SYSCALL (sched_get_priority_min, scerr, 1,
430 int maxprio = INTERNAL_SYSCALL (sched_get_priority_max, scerr, 1,
432 if (pd->schedparam.sched_priority < minprio
433 || pd->schedparam.sched_priority > maxprio)
440 /* Pass the descriptor to the caller. */
441 *newthread = (pthread_t) pd;
443 /* Remember whether the thread is detached or not. In case of an
444 error we have to free the stacks of non-detached stillborn
446 bool is_detached = IS_DETACHED (pd);
448 /* Start the thread. */
449 err = create_thread (pd, iattr, STACK_VARIABLES_ARGS);
452 /* Something went wrong. Free the resources. */
456 __deallocate_stack (pd);
463 weak_alias(__pthread_create_2_1, pthread_create)
465 /* Information for libthread_db. */
467 #include "../nptl_db/db_info.c"
469 /* If pthread_create is present, libgcc_eh.a and libsupc++.a expects some other POSIX thread
470 functions to be present as well. */
471 PTHREAD_STATIC_FN_REQUIRE (pthread_mutex_lock)
472 PTHREAD_STATIC_FN_REQUIRE (pthread_mutex_unlock)
474 PTHREAD_STATIC_FN_REQUIRE (pthread_once)
475 PTHREAD_STATIC_FN_REQUIRE (pthread_cancel)
477 PTHREAD_STATIC_FN_REQUIRE (pthread_key_create)
478 PTHREAD_STATIC_FN_REQUIRE (pthread_setspecific)
479 PTHREAD_STATIC_FN_REQUIRE (pthread_getspecific)
481 /* UCLIBC_MUTEX_xxx macros expects to have these as well */
482 PTHREAD_STATIC_FN_REQUIRE (pthread_mutex_init)
483 PTHREAD_STATIC_FN_REQUIRE (_pthread_cleanup_push_defer)
484 PTHREAD_STATIC_FN_REQUIRE (_pthread_cleanup_pop_restore)