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>
31 #include <shlib-compat.h>
34 /* Local function to start thread and handle cleanup. */
35 static int start_thread (void *arg
);
38 /* Nozero if debugging mode is enabled. */
41 /* Globally enabled events. */
42 static td_thr_events_t __nptl_threads_events
;
44 /* Pointer to descriptor with the last event. */
45 static struct pthread
*__nptl_last_event
;
47 /* Number of threads running. */
48 unsigned int __nptl_nthreads
= 1;
51 /* Code to allocate and deallocate a stack. */
52 #include "allocatestack.c"
54 /* Code to create the thread. */
55 #include "createthread.c"
60 __find_in_stack_list (pd
)
64 struct pthread
*result
= NULL
;
66 lll_lock (stack_cache_lock
);
68 list_for_each (entry
, &stack_used
)
72 curp
= list_entry (entry
, struct pthread
, list
);
81 list_for_each (entry
, &__stack_user
)
85 curp
= list_entry (entry
, struct pthread
, list
);
93 lll_unlock (stack_cache_lock
);
99 /* Deallocate POSIX thread-local-storage. */
102 __nptl_deallocate_tsd (void)
104 struct pthread
*self
= THREAD_SELF
;
106 /* Maybe no data was ever allocated. This happens often so we have
108 if (THREAD_GETMEM (self
, specific_used
))
118 /* So far no new nonzero data entry. */
119 THREAD_SETMEM (self
, specific_used
, false);
121 for (cnt
= idx
= 0; cnt
< PTHREAD_KEY_1STLEVEL_SIZE
; ++cnt
)
123 struct pthread_key_data
*level2
;
125 level2
= THREAD_GETMEM_NC (self
, specific
, cnt
);
131 for (inner
= 0; inner
< PTHREAD_KEY_2NDLEVEL_SIZE
;
134 void *data
= level2
[inner
].data
;
138 /* Always clear the data. */
139 level2
[inner
].data
= NULL
;
141 /* Make sure the data corresponds to a valid
142 key. This test fails if the key was
143 deallocated and also if it was
144 re-allocated. It is the user's
145 responsibility to free the memory in this
147 if (level2
[inner
].seq
148 == __pthread_keys
[idx
].seq
149 /* It is not necessary to register a destructor
151 && __pthread_keys
[idx
].destr
!= NULL
)
152 /* Call the user-provided destructor. */
153 __pthread_keys
[idx
].destr (data
);
158 idx
+= PTHREAD_KEY_1STLEVEL_SIZE
;
161 if (THREAD_GETMEM (self
, specific_used
) == 0)
162 /* No data has been modified. */
165 /* We only repeat the process a fixed number of times. */
166 while (__builtin_expect (++round
< PTHREAD_DESTRUCTOR_ITERATIONS
, 0));
168 /* Just clear the memory of the first block for reuse. */
169 memset (&THREAD_SELF
->specific_1stblock
, '\0',
170 sizeof (self
->specific_1stblock
));
173 /* Free the memory for the other blocks. */
174 for (cnt
= 1; cnt
< PTHREAD_KEY_1STLEVEL_SIZE
; ++cnt
)
176 struct pthread_key_data
*level2
;
178 level2
= THREAD_GETMEM_NC (self
, specific
, cnt
);
181 /* The first block is allocated as part of the thread
184 THREAD_SETMEM_NC (self
, specific
, cnt
, NULL
);
188 THREAD_SETMEM (self
, specific_used
, false);
193 /* Deallocate a thread's stack after optionally making sure the thread
194 descriptor is still valid. */
197 __free_tcb (struct pthread
*pd
)
199 /* The thread is exiting now. */
200 if (__builtin_expect (atomic_bit_test_set (&pd
->cancelhandling
,
201 TERMINATED_BIT
) == 0, 1))
203 /* Remove the descriptor from the list. */
204 if (DEBUGGING_P
&& __find_in_stack_list (pd
) == NULL
)
205 /* Something is really wrong. The descriptor for a still
206 running thread is gone. */
209 /* Queue the stack memory block for reuse and exit the process. The
210 kernel will signal via writing to the address returned by
211 QUEUE-STACK when the stack is available. */
212 __deallocate_stack (pd
);
218 start_thread (void *arg
)
220 struct pthread
*pd
= (struct pthread
*) arg
;
223 /* Remember the time when the thread was started. */
226 THREAD_SETMEM (pd
, cpuclock_offset
, now
);
229 /* Initialize resolver state pointer. */
232 /* This is where the try/finally block should be created. For
233 compilers without that support we do use setjmp. */
234 struct pthread_unwind_buf unwind_buf
;
236 /* No previous handlers. */
237 unwind_buf
.priv
.data
.prev
= NULL
;
238 unwind_buf
.priv
.data
.cleanup
= NULL
;
241 not_first_call
= setjmp ((struct __jmp_buf_tag
*) unwind_buf
.cancel_jmp_buf
);
242 if (__builtin_expect (! not_first_call
, 1))
244 /* Store the new cleanup handler info. */
245 THREAD_SETMEM (pd
, cleanup_jmp_buf
, &unwind_buf
);
247 if (__builtin_expect (pd
->stopped_start
, 0))
249 int oldtype
= CANCEL_ASYNC ();
251 /* Get the lock the parent locked to force synchronization. */
253 /* And give it up right away. */
254 lll_unlock (pd
->lock
);
256 CANCEL_RESET (oldtype
);
259 /* Run the code the user provided. */
260 #ifdef CALL_THREAD_FCT
261 THREAD_SETMEM (pd
, result
, CALL_THREAD_FCT (pd
));
263 THREAD_SETMEM (pd
, result
, pd
->start_routine (pd
->arg
));
267 /* Run the destructor for the thread-local data. */
268 __nptl_deallocate_tsd ();
270 /* Clean up any state libc stored in thread-local variables. */
271 __libc_thread_freeres ();
273 /* If this is the last thread we terminate the process now. We
274 do not notify the debugger, it might just irritate it if there
275 is no thread left. */
276 if (__builtin_expect (atomic_decrement_and_test (&__nptl_nthreads
), 0))
277 /* This was the last thread. */
280 /* Report the death of the thread if this is wanted. */
281 if (__builtin_expect (pd
->report_events
, 0))
283 /* See whether TD_DEATH is in any of the mask. */
284 const int idx
= __td_eventword (TD_DEATH
);
285 const uint32_t mask
= __td_eventmask (TD_DEATH
);
287 if ((mask
& (__nptl_threads_events
.event_bits
[idx
]
288 | pd
->eventbuf
.eventmask
.event_bits
[idx
])) != 0)
290 /* Yep, we have to signal the death. Add the descriptor to
291 the list but only if it is not already on it. */
292 if (pd
->nextevent
== NULL
)
294 pd
->eventbuf
.eventnum
= TD_DEATH
;
295 pd
->eventbuf
.eventdata
= pd
;
298 pd
->nextevent
= __nptl_last_event
;
299 while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event
,
303 /* Now call the function to signal the event. */
304 __nptl_death_event ();
308 /* The thread is exiting now. Don't set this bit until after we've hit
309 the event-reporting breakpoint, so that td_thr_get_info on us while at
310 the breakpoint reports TD_THR_RUN state rather than TD_THR_ZOMBIE. */
311 atomic_bit_set (&pd
->cancelhandling
, EXITING_BIT
);
313 /* If the thread is detached free the TCB. */
314 if (IS_DETACHED (pd
))
317 else if (__builtin_expect (pd
->cancelhandling
& SETXID_BITMASK
, 0))
319 /* Some other thread might call any of the setXid functions and expect
320 us to reply. In this case wait until we did that. */
322 lll_futex_wait (&pd
->setxid_futex
, 0);
323 while (pd
->cancelhandling
& SETXID_BITMASK
);
325 /* Reset the value so that the stack can be reused. */
326 pd
->setxid_futex
= 0;
329 /* We cannot call '_exit' here. '_exit' will terminate the process.
331 The 'exit' implementation in the kernel will signal when the
332 process is really dead since 'clone' got passed the CLONE_CLEARTID
333 flag. The 'tid' field in the TCB will be set to zero.
335 The exit code is zero since in case all threads exit by calling
336 'pthread_exit' the exit status must be 0 (zero). */
337 __exit_thread_inline (0);
344 /* Default thread attributes for the case when the user does not
346 static const struct pthread_attr default_attr
=
348 /* Just some value > 0 which gets rounded to the nearest page size. */
354 __pthread_create_2_1 (newthread
, attr
, start_routine
, arg
)
355 pthread_t
*newthread
;
356 const pthread_attr_t
*attr
;
357 void *(*start_routine
) (void *);
362 const struct pthread_attr
*iattr
= (struct pthread_attr
*) attr
;
364 /* Is this the best idea? On NUMA machines this could mean
365 accessing far-away memory. */
366 iattr
= &default_attr
;
368 struct pthread
*pd
= NULL
;
369 int err
= ALLOCATE_STACK (iattr
, &pd
);
370 if (__builtin_expect (err
!= 0, 0))
371 /* Something went wrong. Maybe a parameter of the attributes is
372 invalid or we could not allocate memory. */
376 /* Initialize the TCB. All initializations with zero should be
377 performed in 'get_cached_stack'. This way we avoid doing this if
378 the stack freshly allocated with 'mmap'. */
381 /* Reference to the TCB itself. */
382 pd
->header
.self
= pd
;
384 /* Self-reference for TLS. */
388 /* Store the address of the start routine and the parameter. Since
389 we do not start the function directly the stillborn thread will
390 get the information from its thread descriptor. */
391 pd
->start_routine
= start_routine
;
394 /* Copy the thread attribute flags. */
395 struct pthread
*self
= THREAD_SELF
;
396 pd
->flags
= ((iattr
->flags
& ~(ATTR_FLAG_SCHED_SET
| ATTR_FLAG_POLICY_SET
))
397 | (self
->flags
& (ATTR_FLAG_SCHED_SET
| ATTR_FLAG_POLICY_SET
)));
399 /* Initialize the field for the ID of the thread which is waiting
400 for us. This is a self-reference in case the thread is created
402 pd
->joinid
= iattr
->flags
& ATTR_FLAG_DETACHSTATE
? pd
: NULL
;
404 /* The debug events are inherited from the parent. */
405 pd
->eventbuf
= self
->eventbuf
;
408 /* Copy the parent's scheduling parameters. The flags will say what
409 is valid and what is not. */
410 pd
->schedpolicy
= self
->schedpolicy
;
411 pd
->schedparam
= self
->schedparam
;
413 /* Copy the stack guard canary. */
414 #ifdef THREAD_COPY_STACK_GUARD
415 THREAD_COPY_STACK_GUARD (pd
);
418 /* Determine scheduling parameters for the thread. */
420 && __builtin_expect ((iattr
->flags
& ATTR_FLAG_NOTINHERITSCHED
) != 0, 0)
421 && (iattr
->flags
& (ATTR_FLAG_SCHED_SET
| ATTR_FLAG_POLICY_SET
)) != 0)
423 INTERNAL_SYSCALL_DECL (scerr
);
425 /* Use the scheduling parameters the user provided. */
426 if (iattr
->flags
& ATTR_FLAG_POLICY_SET
)
427 pd
->schedpolicy
= iattr
->schedpolicy
;
428 else if ((pd
->flags
& ATTR_FLAG_POLICY_SET
) == 0)
430 pd
->schedpolicy
= INTERNAL_SYSCALL (sched_getscheduler
, scerr
, 1, 0);
431 pd
->flags
|= ATTR_FLAG_POLICY_SET
;
434 if (iattr
->flags
& ATTR_FLAG_SCHED_SET
)
435 memcpy (&pd
->schedparam
, &iattr
->schedparam
,
436 sizeof (struct sched_param
));
437 else if ((pd
->flags
& ATTR_FLAG_SCHED_SET
) == 0)
439 INTERNAL_SYSCALL (sched_getparam
, scerr
, 2, 0, &pd
->schedparam
);
440 pd
->flags
|= ATTR_FLAG_SCHED_SET
;
443 /* Check for valid priorities. */
444 int minprio
= INTERNAL_SYSCALL (sched_get_priority_min
, scerr
, 1,
446 int maxprio
= INTERNAL_SYSCALL (sched_get_priority_max
, scerr
, 1,
448 if (pd
->schedparam
.sched_priority
< minprio
449 || pd
->schedparam
.sched_priority
> maxprio
)
456 /* Pass the descriptor to the caller. */
457 *newthread
= (pthread_t
) pd
;
459 /* Remember whether the thread is detached or not. In case of an
460 error we have to free the stacks of non-detached stillborn
462 bool is_detached
= IS_DETACHED (pd
);
464 /* Start the thread. */
465 err
= create_thread (pd
, iattr
, STACK_VARIABLES_ARGS
);
468 /* Something went wrong. Free the resources. */
472 __deallocate_stack (pd
);
479 versioned_symbol (libpthread
, __pthread_create_2_1
, pthread_create
, GLIBC_2_1
);
482 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
484 __pthread_create_2_0 (newthread
, attr
, start_routine
, arg
)
485 pthread_t
*newthread
;
486 const pthread_attr_t
*attr
;
487 void *(*start_routine
) (void *);
490 /* The ATTR attribute is not really of type `pthread_attr_t *'. It has
491 the old size and access to the new members might crash the program.
492 We convert the struct now. */
493 struct pthread_attr new_attr
;
497 struct pthread_attr
*iattr
= (struct pthread_attr
*) attr
;
498 size_t ps
= __getpagesize ();
500 /* Copy values from the user-provided attributes. */
501 new_attr
.schedparam
= iattr
->schedparam
;
502 new_attr
.schedpolicy
= iattr
->schedpolicy
;
503 new_attr
.flags
= iattr
->flags
;
505 /* Fill in default values for the fields not present in the old
507 new_attr
.guardsize
= ps
;
508 new_attr
.stackaddr
= NULL
;
509 new_attr
.stacksize
= 0;
510 new_attr
.cpuset
= NULL
;
512 /* We will pass this value on to the real implementation. */
513 attr
= (pthread_attr_t
*) &new_attr
;
516 return __pthread_create_2_1 (newthread
, attr
, start_routine
, arg
);
518 compat_symbol (libpthread
, __pthread_create_2_0
, pthread_create
,
522 /* Information for libthread_db. */
524 #include "../nptl_db/db_info.c"
526 /* If pthread_create is present, libgcc_eh.a and libsupc++.a expects some other POSIX thread
527 functions to be present as well. */
528 PTHREAD_STATIC_FN_REQUIRE (pthread_mutex_lock
)
529 PTHREAD_STATIC_FN_REQUIRE (pthread_mutex_unlock
)
531 PTHREAD_STATIC_FN_REQUIRE (pthread_once
)
532 PTHREAD_STATIC_FN_REQUIRE (pthread_cancel
)
534 PTHREAD_STATIC_FN_REQUIRE (pthread_key_create
)
535 PTHREAD_STATIC_FN_REQUIRE (pthread_setspecific
)
536 PTHREAD_STATIC_FN_REQUIRE (pthread_getspecific
)