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