]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/pthread_create.c
Update.
[thirdparty/glibc.git] / nptl / pthread_create.c
CommitLineData
3e4fc359 1/* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
76a50749
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
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
18 02111-1307 USA. */
19
20#include <errno.h>
21#include <stdbool.h>
22#include <stdlib.h>
23#include <string.h>
24#include "pthreadP.h"
25#include <hp-timing.h>
26#include <ldsodefs.h>
3e4fc359 27#include <atomic.h>
12d7ca07 28#include <libc-internal.h>
76a50749
UD
29
30#include <shlib-compat.h>
31
32
33/* Local function to start thread and handle cleanup. */
34static int start_thread (void *arg);
35/* Similar version used when debugging. */
36static int start_thread_debug (void *arg);
37
38
39/* Nozero if debugging mode is enabled. */
40int __pthread_debug;
41
42/* Globally enabled events. */
fa9a4ff0 43static td_thr_events_t __nptl_threads_events;
76a50749
UD
44
45/* Pointer to descriptor with the last event. */
fa9a4ff0 46static struct pthread *__nptl_last_event;
76a50749 47
47202270
UD
48/* Number of threads running. */
49unsigned int __nptl_nthreads = 1;
50
76a50749
UD
51
52/* Code to allocate and deallocate a stack. */
76a50749
UD
53#include "allocatestack.c"
54
55/* Code to create the thread. */
56#include "createthread.c"
57
58
59/* Table of the key information. */
fa9a4ff0 60struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX]
518b5308 61 __attribute__ ((nocommon));
415ef7d8 62hidden_data_def (__pthread_keys)
76a50749 63
fa9a4ff0
RM
64/* This is for libthread_db only. */
65const int __pthread_pthread_sizeof_descr = sizeof (struct pthread);
76a50749
UD
66
67struct pthread *
90491dc4 68internal_function
76a50749
UD
69__find_in_stack_list (pd)
70 struct pthread *pd;
71{
72 list_t *entry;
73 struct pthread *result = NULL;
74
75 lll_lock (stack_cache_lock);
76
77 list_for_each (entry, &stack_used)
78 {
79 struct pthread *curp;
80
d4f64e1a 81 curp = list_entry (entry, struct pthread, list);
76a50749
UD
82 if (curp == pd)
83 {
84 result = curp;
85 break;
86 }
87 }
88
89 if (result == NULL)
90 list_for_each (entry, &__stack_user)
91 {
92 struct pthread *curp;
93
d4f64e1a 94 curp = list_entry (entry, struct pthread, list);
76a50749
UD
95 if (curp == pd)
96 {
97 result = curp;
98 break;
99 }
100 }
101
102 lll_unlock (stack_cache_lock);
103
104 return result;
105}
106
107
108/* Deallocate POSIX thread-local-storage. */
109static void
ba25bb0f 110internal_function
877e51b2 111deallocate_tsd (void)
76a50749 112{
877e51b2
UD
113 struct pthread *self = THREAD_SELF;
114
76a50749
UD
115 /* Maybe no data was ever allocated. This happens often so we have
116 a flag for this. */
877e51b2 117 if (THREAD_GETMEM (self, specific_used))
76a50749
UD
118 {
119 size_t round;
6b4686a5 120 size_t cnt;
76a50749 121
6b4686a5
UD
122 round = 0;
123 do
76a50749 124 {
76a50749
UD
125 size_t idx;
126
c5acd3d7 127 /* So far no new nonzero data entry. */
877e51b2 128 THREAD_SETMEM (self, specific_used, false);
c5acd3d7 129
76a50749 130 for (cnt = idx = 0; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
6b4686a5
UD
131 {
132 struct pthread_key_data *level2;
133
877e51b2 134 level2 = THREAD_GETMEM_NC (self, specific, cnt);
6b4686a5
UD
135
136 if (level2 != NULL)
137 {
138 size_t inner;
139
140 for (inner = 0; inner < PTHREAD_KEY_2NDLEVEL_SIZE;
141 ++inner, ++idx)
142 {
143 void *data = level2[inner].data;
144
877e51b2
UD
145 if (data != NULL)
146 {
147 /* Always clear the data. */
148 level2[inner].data = NULL;
149
6b4686a5
UD
150 /* Make sure the data corresponds to a valid
151 key. This test fails if the key was
152 deallocated and also if it was
153 re-allocated. It is the user's
154 responsibility to free the memory in this
155 case. */
877e51b2
UD
156 if (level2[inner].seq
157 == __pthread_keys[idx].seq
158 /* It is not necessary to register a destructor
159 function. */
160 && __pthread_keys[idx].destr != NULL)
161 /* Call the user-provided destructor. */
162 __pthread_keys[idx].destr (data);
6b4686a5
UD
163 }
164 }
165 }
166 else
167 idx += PTHREAD_KEY_1STLEVEL_SIZE;
168 }
877e51b2
UD
169
170 if (THREAD_GETMEM (self, specific_used) == 0)
171 /* No data has been modified. */
172 goto just_free;
76a50749 173 }
877e51b2
UD
174 /* We only repeat the process a fixed number of times. */
175 while (__builtin_expect (++round < PTHREAD_DESTRUCTOR_ITERATIONS, 0));
76a50749 176
877e51b2
UD
177 /* Just clear the memory of the first block for reuse. */
178 memset (&THREAD_SELF->specific_1stblock, '\0',
179 sizeof (self->specific_1stblock));
6b4686a5 180
877e51b2 181 just_free:
6b4686a5
UD
182 /* Free the memory for the other blocks. */
183 for (cnt = 1; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
184 {
185 struct pthread_key_data *level2;
186
877e51b2 187 level2 = THREAD_GETMEM_NC (self, specific, cnt);
6b4686a5
UD
188 if (level2 != NULL)
189 {
190 /* The first block is allocated as part of the thread
191 descriptor. */
192 free (level2);
877e51b2 193 THREAD_SETMEM_NC (self, specific, cnt, NULL);
6b4686a5
UD
194 }
195 }
196
877e51b2 197 THREAD_SETMEM (self, specific_used, false);
76a50749
UD
198 }
199}
200
201
202/* Deallocate a thread's stack after optionally making sure the thread
203 descriptor is still valid. */
204void
90491dc4 205internal_function
76a50749
UD
206__free_tcb (struct pthread *pd)
207{
208 /* The thread is exiting now. */
ba25bb0f
UD
209 if (__builtin_expect (atomic_bit_test_set (&pd->cancelhandling,
210 TERMINATED_BIT) == 0, 1))
76a50749
UD
211 {
212 /* Remove the descriptor from the list. */
213 if (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
214 /* Something is really wrong. The descriptor for a still
215 running thread is gone. */
216 abort ();
217
76a50749
UD
218 /* Queue the stack memory block for reuse and exit the process. The
219 kernel will signal via writing to the address returned by
220 QUEUE-STACK when the stack is available. */
221 __deallocate_stack (pd);
222 }
223}
224
225
226static int
227start_thread (void *arg)
228{
47202270
UD
229 /* One more thread. */
230 atomic_increment (&__nptl_nthreads);
231
76a50749
UD
232 struct pthread *pd = (struct pthread *) arg;
233
234#if HP_TIMING_AVAIL
235 /* Remember the time when the thread was started. */
236 hp_timing_t now;
237 HP_TIMING_NOW (now);
238 THREAD_SETMEM (pd, cpuclock_offset, now);
239#endif
240
241 /* This is where the try/finally block should be created. For
242 compilers without that support we do use setjmp. */
877e51b2
UD
243 struct pthread_unwind_buf unwind_buf;
244
245 /* No previous handlers. */
246 unwind_buf.priv.data.prev = NULL;
247 unwind_buf.priv.data.cleanup = NULL;
248
249 int not_first_call;
250 not_first_call = setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf);
18d009ca 251 if (__builtin_expect (! not_first_call, 1))
76a50749 252 {
877e51b2
UD
253 /* Store the new cleanup handler info. */
254 THREAD_SETMEM (pd, cleanup_jmp_buf, &unwind_buf);
255
76a50749 256 /* Run the code the user provided. */
42c8f44c
UD
257#ifdef CALL_THREAD_FCT
258 THREAD_SETMEM (pd, result, CALL_THREAD_FCT (pd));
259#else
cc775edf 260 THREAD_SETMEM (pd, result, pd->start_routine (pd->arg));
42c8f44c 261#endif
76a50749
UD
262 }
263
6b4686a5 264 /* Run the destructor for the thread-local data. */
877e51b2 265 deallocate_tsd ();
6b4686a5 266
12d7ca07
RM
267 /* Clean up any state libc stored in thread-local variables. */
268 __libc_thread_freeres ();
76a50749 269
47202270
UD
270 /* If this is the last thread we terminate the process now. We
271 do not notify the debugger, it might just irritate it if there
272 is no thread left. */
729924a0 273 if (__builtin_expect (atomic_decrement_and_test (&__nptl_nthreads), 0))
47202270
UD
274 /* This was the last thread. */
275 exit (0);
276
76a50749
UD
277 /* Report the death of the thread if this is wanted. */
278 if (__builtin_expect (pd->report_events, 0))
279 {
280 /* See whether TD_DEATH is in any of the mask. */
281 const int idx = __td_eventword (TD_DEATH);
282 const uint32_t mask = __td_eventmask (TD_DEATH);
283
284 if ((mask & (__nptl_threads_events.event_bits[idx]
285 | pd->eventbuf.eventmask.event_bits[idx])) != 0)
286 {
287 /* Yep, we have to signal the death. Add the descriptor to
288 the list but only if it is not already on it. */
289 if (pd->nextevent == NULL)
290 {
291 pd->eventbuf.eventnum = TD_DEATH;
292 pd->eventbuf.eventdata = pd;
293
294 do
295 pd->nextevent = __nptl_last_event;
5a3ab2fc
UD
296 while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event,
297 pd, pd->nextevent));
76a50749
UD
298 }
299
300 /* Now call the function to signal the event. */
301 __nptl_death_event ();
302 }
303 }
304
6461e577
RM
305 /* The thread is exiting now. Don't set this bit until after we've hit
306 the event-reporting breakpoint, so that td_thr_get_info on us while at
307 the breakpoint reports TD_THR_RUN state rather than TD_THR_ZOMBIE. */
308 atomic_bit_set (&pd->cancelhandling, EXITING_BIT);
76a50749 309
76a50749
UD
310 /* If the thread is detached free the TCB. */
311 if (IS_DETACHED (pd))
312 /* Free the TCB. */
313 __free_tcb (pd);
314
315 /* We cannot call '_exit' here. '_exit' will terminate the process.
316
317 The 'exit' implementation in the kernel will signal when the
318 process is really dead since 'clone' got passed the CLONE_CLEARTID
319 flag. The 'tid' field in the TCB will be set to zero.
320
321 The exit code is zero since in case all threads exit by calling
322 'pthread_exit' the exit status must be 0 (zero). */
323 __exit_thread_inline (0);
324
325 /* NOTREACHED */
326 return 0;
327}
328
329
330/* Just list start_thread but we do some more things needed for a run
331 with a debugger attached. */
332static int
333start_thread_debug (void *arg)
334{
335 struct pthread *pd = (struct pthread *) arg;
336
337 /* Get the lock the parent locked to force synchronization. */
338 lll_lock (pd->lock);
339 /* And give it up right away. */
340 lll_unlock (pd->lock);
341
342 /* Now do the actual startup. */
343 return start_thread (arg);
344}
345
346
347/* Default thread attributes for the case when the user does not
348 provide any. */
349static const struct pthread_attr default_attr =
350 {
351 /* Just some value > 0 which gets rounded to the nearest page size. */
352 .guardsize = 1,
353 };
354
355
356int
357__pthread_create_2_1 (newthread, attr, start_routine, arg)
358 pthread_t *newthread;
359 const pthread_attr_t *attr;
360 void *(*start_routine) (void *);
361 void *arg;
362{
363 STACK_VARIABLES;
364 const struct pthread_attr *iattr;
365 struct pthread *pd;
366 int err;
367
368 iattr = (struct pthread_attr *) attr;
369 if (iattr == NULL)
370 /* Is this the best idea? On NUMA machines this could mean
371 accessing far-away memory. */
372 iattr = &default_attr;
373
374 err = ALLOCATE_STACK (iattr, &pd);
729924a0 375 if (__builtin_expect (err != 0, 0))
76a50749
UD
376 /* Something went wrong. Maybe a parameter of the attributes is
377 invalid or we could not allocate memory. */
378 return err;
379
380
381 /* Initialize the TCB. All initializations with zero should be
382 performed in 'get_cached_stack'. This way we avoid doing this if
383 the stack freshly allocated with 'mmap'. */
384
5d5d5969 385#ifdef TLS_TCB_AT_TP
76a50749 386 /* Reference to the TCB itself. */
55c11fbd 387 pd->header.self = pd;
76a50749 388
d4f64e1a 389 /* Self-reference for TLS. */
55c11fbd 390 pd->header.tcb = pd;
76a50749
UD
391#endif
392
393 /* Store the address of the start routine and the parameter. Since
394 we do not start the function directly the stillborn thread will
395 get the information from its thread descriptor. */
396 pd->start_routine = start_routine;
397 pd->arg = arg;
398
399 /* Copy the thread attribute flags. */
400 pd->flags = iattr->flags;
401
402 /* Initialize the field for the ID of the thread which is waiting
403 for us. This is a self-reference in case the thread is created
404 detached. */
405 pd->joinid = iattr->flags & ATTR_FLAG_DETACHSTATE ? pd : NULL;
406
407 /* The debug events are inherited from the parent. */
408 pd->eventbuf = THREAD_SELF->eventbuf;
409
410
411 /* Determine scheduling parameters for the thread.
412 XXX How to determine whether scheduling handling is needed? */
413 if (0 && attr != NULL)
414 {
415 if (iattr->flags & ATTR_FLAG_NOTINHERITSCHED)
416 {
417 /* Use the scheduling parameters the user provided. */
418 pd->schedpolicy = iattr->schedpolicy;
419 memcpy (&pd->schedparam, &iattr->schedparam,
420 sizeof (struct sched_param));
421 }
422 else
423 {
424 /* Just store the scheduling attributes of the parent. */
425 pd->schedpolicy = __sched_getscheduler (0);
426 __sched_getparam (0, &pd->schedparam);
427 }
428 }
429
430 /* Pass the descriptor to the caller. */
431 *newthread = (pthread_t) pd;
432
433 /* Start the thread. */
80f536db 434 err = create_thread (pd, iattr, STACK_VARIABLES_ARGS);
76a50749
UD
435 if (err != 0)
436 {
437 /* Something went wrong. Free the resources. */
438 __deallocate_stack (pd);
439 return err;
440 }
441
442 return 0;
443}
444versioned_symbol (libpthread, __pthread_create_2_1, pthread_create, GLIBC_2_1);
445
446
447#if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
448int
449__pthread_create_2_0 (newthread, attr, start_routine, arg)
450 pthread_t *newthread;
451 const pthread_attr_t *attr;
452 void *(*start_routine) (void *);
453 void *arg;
454{
455 /* The ATTR attribute is not really of type `pthread_attr_t *'. It has
456 the old size and access to the new members might crash the program.
457 We convert the struct now. */
458 struct pthread_attr new_attr;
459
460 if (attr != NULL)
461 {
462 struct pthread_attr *iattr = (struct pthread_attr *) attr;
463 size_t ps = __getpagesize ();
464
465 /* Copy values from the user-provided attributes. */
466 new_attr.schedparam = iattr->schedparam;
467 new_attr.schedpolicy = iattr->schedpolicy;
468 new_attr.flags = iattr->flags;
469
470 /* Fill in default values for the fields not present in the old
471 implementation. */
472 new_attr.guardsize = ps;
473 new_attr.stackaddr = NULL;
474 new_attr.stacksize = 0;
475
476 /* We will pass this value on to the real implementation. */
477 attr = (pthread_attr_t *) &new_attr;
478 }
479
480 return __pthread_create_2_1 (newthread, attr, start_routine, arg);
481}
482compat_symbol (libpthread, __pthread_create_2_0, pthread_create,
483 GLIBC_2_0);
484#endif