]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/pthread_create.c
Update.
[thirdparty/glibc.git] / nptl / pthread_create.c
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.
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>
27 #include <atomic.h>
28 #include <libc-internal.h>
29
30 #include <shlib-compat.h>
31
32
33 /* Local function to start thread and handle cleanup. */
34 static int start_thread (void *arg);
35 /* Similar version used when debugging. */
36 static int start_thread_debug (void *arg);
37
38
39 /* Nozero if debugging mode is enabled. */
40 int __pthread_debug;
41
42 /* Globally enabled events. */
43 static td_thr_events_t __nptl_threads_events;
44
45 /* Pointer to descriptor with the last event. */
46 static struct pthread *__nptl_last_event;
47
48 /* Number of threads running. */
49 unsigned int __nptl_nthreads = 1;
50
51
52 /* Code to allocate and deallocate a stack. */
53 #include "allocatestack.c"
54
55 /* Code to create the thread. */
56 #include "createthread.c"
57
58
59 /* Table of the key information. */
60 struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX]
61 __attribute__ ((nocommon));
62 hidden_def (__pthread_keys)
63
64 /* This is for libthread_db only. */
65 const int __pthread_pthread_sizeof_descr = sizeof (struct pthread);
66
67 struct pthread *
68 internal_function
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
81 curp = list_entry (entry, struct pthread, list);
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
94 curp = list_entry (entry, struct pthread, list);
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. */
109 static void
110 internal_function
111 deallocate_tsd (struct pthread *pd)
112 {
113 /* Maybe no data was ever allocated. This happens often so we have
114 a flag for this. */
115 if (pd->specific_used)
116 {
117 size_t round;
118 bool found_nonzero;
119
120 for (round = 0, found_nonzero = true;
121 found_nonzero && round < PTHREAD_DESTRUCTOR_ITERATIONS;
122 ++round)
123 {
124 size_t cnt;
125 size_t idx;
126
127 /* So far no new nonzero data entry. */
128 found_nonzero = false;
129
130 for (cnt = idx = 0; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
131 if (pd->specific[cnt] != NULL)
132 {
133 size_t inner;
134
135 for (inner = 0; inner < PTHREAD_KEY_2NDLEVEL_SIZE;
136 ++inner, ++idx)
137 {
138 void *data = pd->specific[cnt][inner].data;
139
140 if (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
146 case. */
147 && (pd->specific[cnt][inner].seq
148 == __pthread_keys[idx].seq)
149 /* It is not necessary to register a destructor
150 function. */
151 && __pthread_keys[idx].destr != NULL)
152 {
153 pd->specific[cnt][inner].data = NULL;
154 __pthread_keys[idx].destr (data);
155 found_nonzero = true;
156 }
157 }
158
159 if (cnt != 0)
160 {
161 /* The first block is allocated as part of the thread
162 descriptor. */
163 free (pd->specific[cnt]);
164 pd->specific[cnt] = NULL;
165 }
166 else
167 /* Clear the memory of the first block for reuse. */
168 memset (&pd->specific_1stblock, '\0',
169 sizeof (pd->specific_1stblock));
170 }
171 else
172 idx += PTHREAD_KEY_1STLEVEL_SIZE;
173 }
174
175 pd->specific_used = false;
176 }
177 }
178
179
180 /* Deallocate a thread's stack after optionally making sure the thread
181 descriptor is still valid. */
182 void
183 internal_function
184 __free_tcb (struct pthread *pd)
185 {
186 /* The thread is exiting now. */
187 if (__builtin_expect (atomic_bit_test_set (&pd->cancelhandling,
188 TERMINATED_BIT) == 0, 1))
189 {
190 /* Remove the descriptor from the list. */
191 if (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
192 /* Something is really wrong. The descriptor for a still
193 running thread is gone. */
194 abort ();
195
196 /* Run the destructor for the thread-local data. */
197 deallocate_tsd (pd);
198
199 /* Queue the stack memory block for reuse and exit the process. The
200 kernel will signal via writing to the address returned by
201 QUEUE-STACK when the stack is available. */
202 __deallocate_stack (pd);
203 }
204 }
205
206
207 static int
208 start_thread (void *arg)
209 {
210 /* One more thread. */
211 atomic_increment (&__nptl_nthreads);
212
213 struct pthread *pd = (struct pthread *) arg;
214
215 #if HP_TIMING_AVAIL
216 /* Remember the time when the thread was started. */
217 hp_timing_t now;
218 HP_TIMING_NOW (now);
219 THREAD_SETMEM (pd, cpuclock_offset, now);
220 #endif
221
222 /* This is where the try/finally block should be created. For
223 compilers without that support we do use setjmp. */
224 if (__builtin_expect (setjmp (pd->cancelbuf) == 0, 1))
225 {
226 /* Run the code the user provided. */
227 THREAD_SETMEM (pd, result, pd->start_routine (pd->arg));
228 }
229
230 /* Clean up any state libc stored in thread-local variables. */
231 __libc_thread_freeres ();
232
233 /* If this is the last thread we terminate the process now. We
234 do not notify the debugger, it might just irritate it if there
235 is no thread left. */
236 if (__builtin_expect (atomic_decrement_and_test (&__nptl_nthreads), 0))
237 /* This was the last thread. */
238 exit (0);
239
240 /* Report the death of the thread if this is wanted. */
241 if (__builtin_expect (pd->report_events, 0))
242 {
243 /* See whether TD_DEATH is in any of the mask. */
244 const int idx = __td_eventword (TD_DEATH);
245 const uint32_t mask = __td_eventmask (TD_DEATH);
246
247 if ((mask & (__nptl_threads_events.event_bits[idx]
248 | pd->eventbuf.eventmask.event_bits[idx])) != 0)
249 {
250 /* Yep, we have to signal the death. Add the descriptor to
251 the list but only if it is not already on it. */
252 if (pd->nextevent == NULL)
253 {
254 pd->eventbuf.eventnum = TD_DEATH;
255 pd->eventbuf.eventdata = pd;
256
257 do
258 pd->nextevent = __nptl_last_event;
259 while (atomic_compare_and_exchange_acq (&__nptl_last_event, pd,
260 pd->nextevent) != 0);
261 }
262
263 /* Now call the function to signal the event. */
264 __nptl_death_event ();
265 }
266 }
267
268 /* The thread is exiting now. Don't set this bit until after we've hit
269 the event-reporting breakpoint, so that td_thr_get_info on us while at
270 the breakpoint reports TD_THR_RUN state rather than TD_THR_ZOMBIE. */
271 atomic_bit_set (&pd->cancelhandling, EXITING_BIT);
272
273 /* If the thread is detached free the TCB. */
274 if (IS_DETACHED (pd))
275 /* Free the TCB. */
276 __free_tcb (pd);
277
278 /* We cannot call '_exit' here. '_exit' will terminate the process.
279
280 The 'exit' implementation in the kernel will signal when the
281 process is really dead since 'clone' got passed the CLONE_CLEARTID
282 flag. The 'tid' field in the TCB will be set to zero.
283
284 The exit code is zero since in case all threads exit by calling
285 'pthread_exit' the exit status must be 0 (zero). */
286 __exit_thread_inline (0);
287
288 /* NOTREACHED */
289 return 0;
290 }
291
292
293 /* Just list start_thread but we do some more things needed for a run
294 with a debugger attached. */
295 static int
296 start_thread_debug (void *arg)
297 {
298 struct pthread *pd = (struct pthread *) arg;
299
300 /* Get the lock the parent locked to force synchronization. */
301 lll_lock (pd->lock);
302 /* And give it up right away. */
303 lll_unlock (pd->lock);
304
305 /* Now do the actual startup. */
306 return start_thread (arg);
307 }
308
309
310 /* Default thread attributes for the case when the user does not
311 provide any. */
312 static const struct pthread_attr default_attr =
313 {
314 /* Just some value > 0 which gets rounded to the nearest page size. */
315 .guardsize = 1,
316 };
317
318
319 int
320 __pthread_create_2_1 (newthread, attr, start_routine, arg)
321 pthread_t *newthread;
322 const pthread_attr_t *attr;
323 void *(*start_routine) (void *);
324 void *arg;
325 {
326 STACK_VARIABLES;
327 const struct pthread_attr *iattr;
328 struct pthread *pd;
329 int err;
330
331 iattr = (struct pthread_attr *) attr;
332 if (iattr == NULL)
333 /* Is this the best idea? On NUMA machines this could mean
334 accessing far-away memory. */
335 iattr = &default_attr;
336
337 err = ALLOCATE_STACK (iattr, &pd);
338 if (__builtin_expect (err != 0, 0))
339 /* Something went wrong. Maybe a parameter of the attributes is
340 invalid or we could not allocate memory. */
341 return err;
342
343
344 /* Initialize the TCB. All initializations with zero should be
345 performed in 'get_cached_stack'. This way we avoid doing this if
346 the stack freshly allocated with 'mmap'. */
347
348 #ifdef TLS_TCB_AT_TP
349 /* Reference to the TCB itself. */
350 pd->self = pd;
351
352 /* Self-reference for TLS. */
353 pd->tcb = pd;
354 #endif
355
356 /* Store the address of the start routine and the parameter. Since
357 we do not start the function directly the stillborn thread will
358 get the information from its thread descriptor. */
359 pd->start_routine = start_routine;
360 pd->arg = arg;
361
362 /* Copy the thread attribute flags. */
363 pd->flags = iattr->flags;
364
365 /* Initialize the field for the ID of the thread which is waiting
366 for us. This is a self-reference in case the thread is created
367 detached. */
368 pd->joinid = iattr->flags & ATTR_FLAG_DETACHSTATE ? pd : NULL;
369
370 /* The debug events are inherited from the parent. */
371 pd->eventbuf = THREAD_SELF->eventbuf;
372
373
374 /* Determine scheduling parameters for the thread.
375 XXX How to determine whether scheduling handling is needed? */
376 if (0 && attr != NULL)
377 {
378 if (iattr->flags & ATTR_FLAG_NOTINHERITSCHED)
379 {
380 /* Use the scheduling parameters the user provided. */
381 pd->schedpolicy = iattr->schedpolicy;
382 memcpy (&pd->schedparam, &iattr->schedparam,
383 sizeof (struct sched_param));
384 }
385 else
386 {
387 /* Just store the scheduling attributes of the parent. */
388 pd->schedpolicy = __sched_getscheduler (0);
389 __sched_getparam (0, &pd->schedparam);
390 }
391 }
392
393 /* Pass the descriptor to the caller. */
394 *newthread = (pthread_t) pd;
395
396 /* Start the thread. */
397 err = create_thread (pd, STACK_VARIABLES_ARGS);
398 if (err != 0)
399 {
400 /* Something went wrong. Free the resources. */
401 __deallocate_stack (pd);
402 return err;
403 }
404
405 return 0;
406 }
407 versioned_symbol (libpthread, __pthread_create_2_1, pthread_create, GLIBC_2_1);
408
409
410 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
411 int
412 __pthread_create_2_0 (newthread, attr, start_routine, arg)
413 pthread_t *newthread;
414 const pthread_attr_t *attr;
415 void *(*start_routine) (void *);
416 void *arg;
417 {
418 /* The ATTR attribute is not really of type `pthread_attr_t *'. It has
419 the old size and access to the new members might crash the program.
420 We convert the struct now. */
421 struct pthread_attr new_attr;
422
423 if (attr != NULL)
424 {
425 struct pthread_attr *iattr = (struct pthread_attr *) attr;
426 size_t ps = __getpagesize ();
427
428 /* Copy values from the user-provided attributes. */
429 new_attr.schedparam = iattr->schedparam;
430 new_attr.schedpolicy = iattr->schedpolicy;
431 new_attr.flags = iattr->flags;
432
433 /* Fill in default values for the fields not present in the old
434 implementation. */
435 new_attr.guardsize = ps;
436 new_attr.stackaddr = NULL;
437 new_attr.stacksize = 0;
438
439 /* We will pass this value on to the real implementation. */
440 attr = (pthread_attr_t *) &new_attr;
441 }
442
443 return __pthread_create_2_1 (newthread, attr, start_routine, arg);
444 }
445 compat_symbol (libpthread, __pthread_create_2_0, pthread_create,
446 GLIBC_2_0);
447 #endif