1 /* Copyright (C) 2002, 2003 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"
58 /* Table of the key information. */
59 struct pthread_key_struct __pthread_keys
[PTHREAD_KEYS_MAX
]
60 __attribute__ ((nocommon
));
61 hidden_data_def (__pthread_keys
)
63 /* This is for libthread_db only. */
64 const int __pthread_pthread_sizeof_descr
= sizeof (struct pthread
);
68 __find_in_stack_list (pd
)
72 struct pthread
*result
= NULL
;
74 lll_lock (stack_cache_lock
);
76 list_for_each (entry
, &stack_used
)
80 curp
= list_entry (entry
, struct pthread
, list
);
89 list_for_each (entry
, &__stack_user
)
93 curp
= list_entry (entry
, struct pthread
, list
);
101 lll_unlock (stack_cache_lock
);
107 /* Deallocate POSIX thread-local-storage. */
110 deallocate_tsd (void)
112 struct pthread
*self
= THREAD_SELF
;
114 /* Maybe no data was ever allocated. This happens often so we have
116 if (THREAD_GETMEM (self
, specific_used
))
126 /* So far no new nonzero data entry. */
127 THREAD_SETMEM (self
, specific_used
, false);
129 for (cnt
= idx
= 0; cnt
< PTHREAD_KEY_1STLEVEL_SIZE
; ++cnt
)
131 struct pthread_key_data
*level2
;
133 level2
= THREAD_GETMEM_NC (self
, specific
, cnt
);
139 for (inner
= 0; inner
< PTHREAD_KEY_2NDLEVEL_SIZE
;
142 void *data
= level2
[inner
].data
;
146 /* Always clear the data. */
147 level2
[inner
].data
= NULL
;
149 /* Make sure the data corresponds to a valid
150 key. This test fails if the key was
151 deallocated and also if it was
152 re-allocated. It is the user's
153 responsibility to free the memory in this
155 if (level2
[inner
].seq
156 == __pthread_keys
[idx
].seq
157 /* It is not necessary to register a destructor
159 && __pthread_keys
[idx
].destr
!= NULL
)
160 /* Call the user-provided destructor. */
161 __pthread_keys
[idx
].destr (data
);
166 idx
+= PTHREAD_KEY_1STLEVEL_SIZE
;
169 if (THREAD_GETMEM (self
, specific_used
) == 0)
170 /* No data has been modified. */
173 /* We only repeat the process a fixed number of times. */
174 while (__builtin_expect (++round
< PTHREAD_DESTRUCTOR_ITERATIONS
, 0));
176 /* Just clear the memory of the first block for reuse. */
177 memset (&THREAD_SELF
->specific_1stblock
, '\0',
178 sizeof (self
->specific_1stblock
));
181 /* Free the memory for the other blocks. */
182 for (cnt
= 1; cnt
< PTHREAD_KEY_1STLEVEL_SIZE
; ++cnt
)
184 struct pthread_key_data
*level2
;
186 level2
= THREAD_GETMEM_NC (self
, specific
, cnt
);
189 /* The first block is allocated as part of the thread
192 THREAD_SETMEM_NC (self
, specific
, cnt
, NULL
);
196 THREAD_SETMEM (self
, specific_used
, false);
201 /* Deallocate a thread's stack after optionally making sure the thread
202 descriptor is still valid. */
205 __free_tcb (struct pthread
*pd
)
207 /* The thread is exiting now. */
208 if (__builtin_expect (atomic_bit_test_set (&pd
->cancelhandling
,
209 TERMINATED_BIT
) == 0, 1))
211 /* Remove the descriptor from the list. */
212 if (DEBUGGING_P
&& __find_in_stack_list (pd
) == NULL
)
213 /* Something is really wrong. The descriptor for a still
214 running thread is gone. */
217 /* Queue the stack memory block for reuse and exit the process. The
218 kernel will signal via writing to the address returned by
219 QUEUE-STACK when the stack is available. */
220 __deallocate_stack (pd
);
226 start_thread (void *arg
)
228 /* One more thread. */
229 atomic_increment (&__nptl_nthreads
);
231 struct pthread
*pd
= (struct pthread
*) arg
;
233 #ifndef __ASSUME_CLONE_STOPPED
234 /* Get the lock the parent locked to force synchronization. */
236 /* And give it up right away. */
237 lll_unlock (pd
->lock
);
241 /* Remember the time when the thread was started. */
244 THREAD_SETMEM (pd
, cpuclock_offset
, now
);
247 /* Initialize resolver state pointer. */
250 /* This is where the try/finally block should be created. For
251 compilers without that support we do use setjmp. */
252 struct pthread_unwind_buf unwind_buf
;
254 /* No previous handlers. */
255 unwind_buf
.priv
.data
.prev
= NULL
;
256 unwind_buf
.priv
.data
.cleanup
= NULL
;
259 not_first_call
= setjmp ((struct __jmp_buf_tag
*) unwind_buf
.cancel_jmp_buf
);
260 if (__builtin_expect (! not_first_call
, 1))
262 /* Store the new cleanup handler info. */
263 THREAD_SETMEM (pd
, cleanup_jmp_buf
, &unwind_buf
);
265 /* Run the code the user provided. */
266 #ifdef CALL_THREAD_FCT
267 THREAD_SETMEM (pd
, result
, CALL_THREAD_FCT (pd
));
269 THREAD_SETMEM (pd
, result
, pd
->start_routine (pd
->arg
));
273 /* Run the destructor for the thread-local data. */
276 /* Clean up any state libc stored in thread-local variables. */
277 __libc_thread_freeres ();
279 /* If this is the last thread we terminate the process now. We
280 do not notify the debugger, it might just irritate it if there
281 is no thread left. */
282 if (__builtin_expect (atomic_decrement_and_test (&__nptl_nthreads
), 0))
283 /* This was the last thread. */
286 /* Report the death of the thread if this is wanted. */
287 if (__builtin_expect (pd
->report_events
, 0))
289 /* See whether TD_DEATH is in any of the mask. */
290 const int idx
= __td_eventword (TD_DEATH
);
291 const uint32_t mask
= __td_eventmask (TD_DEATH
);
293 if ((mask
& (__nptl_threads_events
.event_bits
[idx
]
294 | pd
->eventbuf
.eventmask
.event_bits
[idx
])) != 0)
296 /* Yep, we have to signal the death. Add the descriptor to
297 the list but only if it is not already on it. */
298 if (pd
->nextevent
== NULL
)
300 pd
->eventbuf
.eventnum
= TD_DEATH
;
301 pd
->eventbuf
.eventdata
= pd
;
304 pd
->nextevent
= __nptl_last_event
;
305 while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event
,
309 /* Now call the function to signal the event. */
310 __nptl_death_event ();
314 /* The thread is exiting now. Don't set this bit until after we've hit
315 the event-reporting breakpoint, so that td_thr_get_info on us while at
316 the breakpoint reports TD_THR_RUN state rather than TD_THR_ZOMBIE. */
317 atomic_bit_set (&pd
->cancelhandling
, EXITING_BIT
);
319 /* If the thread is detached free the TCB. */
320 if (IS_DETACHED (pd
))
324 /* We cannot call '_exit' here. '_exit' will terminate the process.
326 The 'exit' implementation in the kernel will signal when the
327 process is really dead since 'clone' got passed the CLONE_CLEARTID
328 flag. The 'tid' field in the TCB will be set to zero.
330 The exit code is zero since in case all threads exit by calling
331 'pthread_exit' the exit status must be 0 (zero). */
332 __exit_thread_inline (0);
339 /* Default thread attributes for the case when the user does not
341 static const struct pthread_attr default_attr
=
343 /* Just some value > 0 which gets rounded to the nearest page size. */
349 __pthread_create_2_1 (newthread
, attr
, start_routine
, arg
)
350 pthread_t
*newthread
;
351 const pthread_attr_t
*attr
;
352 void *(*start_routine
) (void *);
356 const struct pthread_attr
*iattr
;
360 iattr
= (struct pthread_attr
*) attr
;
362 /* Is this the best idea? On NUMA machines this could mean
363 accessing far-away memory. */
364 iattr
= &default_attr
;
366 err
= ALLOCATE_STACK (iattr
, &pd
);
367 if (__builtin_expect (err
!= 0, 0))
368 /* Something went wrong. Maybe a parameter of the attributes is
369 invalid or we could not allocate memory. */
373 /* Initialize the TCB. All initializations with zero should be
374 performed in 'get_cached_stack'. This way we avoid doing this if
375 the stack freshly allocated with 'mmap'. */
378 /* Reference to the TCB itself. */
379 pd
->header
.self
= pd
;
381 /* Self-reference for TLS. */
385 /* Store the address of the start routine and the parameter. Since
386 we do not start the function directly the stillborn thread will
387 get the information from its thread descriptor. */
388 pd
->start_routine
= start_routine
;
391 /* Copy the thread attribute flags. */
392 pd
->flags
= iattr
->flags
;
394 /* Initialize the field for the ID of the thread which is waiting
395 for us. This is a self-reference in case the thread is created
397 pd
->joinid
= iattr
->flags
& ATTR_FLAG_DETACHSTATE
? pd
: NULL
;
399 /* The debug events are inherited from the parent. */
400 pd
->eventbuf
= THREAD_SELF
->eventbuf
;
403 /* Determine scheduling parameters for the thread.
404 XXX How to determine whether scheduling handling is needed? */
405 if (0 && attr
!= NULL
)
407 if (iattr
->flags
& ATTR_FLAG_NOTINHERITSCHED
)
409 /* Use the scheduling parameters the user provided. */
410 pd
->schedpolicy
= iattr
->schedpolicy
;
411 memcpy (&pd
->schedparam
, &iattr
->schedparam
,
412 sizeof (struct sched_param
));
416 /* Just store the scheduling attributes of the parent. */
417 pd
->schedpolicy
= __sched_getscheduler (0);
418 __sched_getparam (0, &pd
->schedparam
);
422 /* Pass the descriptor to the caller. */
423 *newthread
= (pthread_t
) pd
;
425 /* Start the thread. */
426 err
= create_thread (pd
, iattr
, STACK_VARIABLES_ARGS
);
429 /* Something went wrong. Free the resources. */
430 __deallocate_stack (pd
);
436 versioned_symbol (libpthread
, __pthread_create_2_1
, pthread_create
, GLIBC_2_1
);
439 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
441 __pthread_create_2_0 (newthread
, attr
, start_routine
, arg
)
442 pthread_t
*newthread
;
443 const pthread_attr_t
*attr
;
444 void *(*start_routine
) (void *);
447 /* The ATTR attribute is not really of type `pthread_attr_t *'. It has
448 the old size and access to the new members might crash the program.
449 We convert the struct now. */
450 struct pthread_attr new_attr
;
454 struct pthread_attr
*iattr
= (struct pthread_attr
*) attr
;
455 size_t ps
= __getpagesize ();
457 /* Copy values from the user-provided attributes. */
458 new_attr
.schedparam
= iattr
->schedparam
;
459 new_attr
.schedpolicy
= iattr
->schedpolicy
;
460 new_attr
.flags
= iattr
->flags
;
462 /* Fill in default values for the fields not present in the old
464 new_attr
.guardsize
= ps
;
465 new_attr
.stackaddr
= NULL
;
466 new_attr
.stacksize
= 0;
467 new_attr
.cpuset
= NULL
;
469 /* We will pass this value on to the real implementation. */
470 attr
= (pthread_attr_t
*) &new_attr
;
473 return __pthread_create_2_1 (newthread
, attr
, start_routine
, arg
);
475 compat_symbol (libpthread
, __pthread_create_2_0
, pthread_create
,