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>
30 #include <shlib-compat.h>
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
);
39 /* Nozero if debugging mode is enabled. */
42 /* Globally enabled events. */
43 static td_thr_events_t __nptl_threads_events
;
45 /* Pointer to descriptor with the last event. */
46 static struct pthread
*__nptl_last_event
;
48 /* Number of threads running. */
49 unsigned int __nptl_nthreads
= 1;
52 /* Code to allocate and deallocate a stack. */
53 #include "allocatestack.c"
55 /* Code to create the thread. */
56 #include "createthread.c"
59 /* Table of the key information. */
60 struct pthread_key_struct __pthread_keys
[PTHREAD_KEYS_MAX
]
61 __attribute__ ((nocommon
));
62 hidden_def (__pthread_keys
)
64 /* This is for libthread_db only. */
65 const int __pthread_pthread_sizeof_descr
= sizeof (struct pthread
);
69 __find_in_stack_list (pd
)
73 struct pthread
*result
= NULL
;
75 lll_lock (stack_cache_lock
);
77 list_for_each (entry
, &stack_used
)
81 curp
= list_entry (entry
, struct pthread
, list
);
90 list_for_each (entry
, &__stack_user
)
94 curp
= list_entry (entry
, struct pthread
, list
);
102 lll_unlock (stack_cache_lock
);
108 /* Deallocate POSIX thread-local-storage. */
111 deallocate_tsd (struct pthread
*pd
)
113 /* Maybe no data was ever allocated. This happens often so we have
115 if (pd
->specific_used
)
120 for (round
= 0, found_nonzero
= true;
121 found_nonzero
&& round
< PTHREAD_DESTRUCTOR_ITERATIONS
;
127 /* So far no new nonzero data entry. */
128 found_nonzero
= false;
130 for (cnt
= idx
= 0; cnt
< PTHREAD_KEY_1STLEVEL_SIZE
; ++cnt
)
131 if (pd
->specific
[cnt
] != NULL
)
135 for (inner
= 0; inner
< PTHREAD_KEY_2NDLEVEL_SIZE
;
138 void *data
= pd
->specific
[cnt
][inner
].data
;
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 && (pd
->specific
[cnt
][inner
].seq
148 == __pthread_keys
[idx
].seq
)
149 /* It is not necessary to register a destructor
151 && __pthread_keys
[idx
].destr
!= NULL
)
153 pd
->specific
[cnt
][inner
].data
= NULL
;
154 __pthread_keys
[idx
].destr (data
);
155 found_nonzero
= true;
161 /* The first block is allocated as part of the thread
163 free (pd
->specific
[cnt
]);
164 pd
->specific
[cnt
] = NULL
;
167 /* Clear the memory of the first block for reuse. */
168 memset (&pd
->specific_1stblock
, '\0',
169 sizeof (pd
->specific_1stblock
));
172 idx
+= PTHREAD_KEY_1STLEVEL_SIZE
;
175 pd
->specific_used
= false;
180 /* Deallocate a thread's stack after optionally making sure the thread
181 descriptor is still valid. */
184 __free_tcb (struct pthread
*pd
)
186 /* The thread is exiting now. */
187 if (__builtin_expect (atomic_bit_test_set (&pd
->cancelhandling
,
188 TERMINATED_BIT
) == 0, 1))
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. */
196 /* Run the destructor for the thread-local data. */
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
);
208 start_thread (void *arg
)
210 /* One more thread. */
211 atomic_increment (&__nptl_nthreads
);
213 struct pthread
*pd
= (struct pthread
*) arg
;
216 /* Remember the time when the thread was started. */
219 THREAD_SETMEM (pd
, cpuclock_offset
, now
);
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))
226 /* Run the code the user provided. */
227 THREAD_SETMEM (pd
, result
, pd
->start_routine (pd
->arg
));
230 /* Clean up any state libc stored in thread-local variables. */
231 __libc_thread_freeres ();
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. */
240 /* Report the death of the thread if this is wanted. */
241 if (__builtin_expect (pd
->report_events
, 0))
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
);
247 if ((mask
& (__nptl_threads_events
.event_bits
[idx
]
248 | pd
->eventbuf
.eventmask
.event_bits
[idx
])) != 0)
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
)
254 pd
->eventbuf
.eventnum
= TD_DEATH
;
255 pd
->eventbuf
.eventdata
= pd
;
258 pd
->nextevent
= __nptl_last_event
;
259 while (atomic_compare_and_exchange_acq (&__nptl_last_event
, pd
,
260 pd
->nextevent
) != 0);
263 /* Now call the function to signal the event. */
264 __nptl_death_event ();
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
);
273 /* If the thread is detached free the TCB. */
274 if (IS_DETACHED (pd
))
278 /* We cannot call '_exit' here. '_exit' will terminate the process.
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.
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);
293 /* Just list start_thread but we do some more things needed for a run
294 with a debugger attached. */
296 start_thread_debug (void *arg
)
298 struct pthread
*pd
= (struct pthread
*) arg
;
300 /* Get the lock the parent locked to force synchronization. */
302 /* And give it up right away. */
303 lll_unlock (pd
->lock
);
305 /* Now do the actual startup. */
306 return start_thread (arg
);
310 /* Default thread attributes for the case when the user does not
312 static const struct pthread_attr default_attr
=
314 /* Just some value > 0 which gets rounded to the nearest page size. */
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 *);
327 const struct pthread_attr
*iattr
;
331 iattr
= (struct pthread_attr
*) attr
;
333 /* Is this the best idea? On NUMA machines this could mean
334 accessing far-away memory. */
335 iattr
= &default_attr
;
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. */
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'. */
349 /* Reference to the TCB itself. */
352 /* Self-reference for TLS. */
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
;
362 /* Copy the thread attribute flags. */
363 pd
->flags
= iattr
->flags
;
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
368 pd
->joinid
= iattr
->flags
& ATTR_FLAG_DETACHSTATE
? pd
: NULL
;
370 /* The debug events are inherited from the parent. */
371 pd
->eventbuf
= THREAD_SELF
->eventbuf
;
374 /* Determine scheduling parameters for the thread.
375 XXX How to determine whether scheduling handling is needed? */
376 if (0 && attr
!= NULL
)
378 if (iattr
->flags
& ATTR_FLAG_NOTINHERITSCHED
)
380 /* Use the scheduling parameters the user provided. */
381 pd
->schedpolicy
= iattr
->schedpolicy
;
382 memcpy (&pd
->schedparam
, &iattr
->schedparam
,
383 sizeof (struct sched_param
));
387 /* Just store the scheduling attributes of the parent. */
388 pd
->schedpolicy
= __sched_getscheduler (0);
389 __sched_getparam (0, &pd
->schedparam
);
393 /* Pass the descriptor to the caller. */
394 *newthread
= (pthread_t
) pd
;
396 /* Start the thread. */
397 err
= create_thread (pd
, STACK_VARIABLES_ARGS
);
400 /* Something went wrong. Free the resources. */
401 __deallocate_stack (pd
);
407 versioned_symbol (libpthread
, __pthread_create_2_1
, pthread_create
, GLIBC_2_1
);
410 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
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 *);
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
;
425 struct pthread_attr
*iattr
= (struct pthread_attr
*) attr
;
426 size_t ps
= __getpagesize ();
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
;
433 /* Fill in default values for the fields not present in the old
435 new_attr
.guardsize
= ps
;
436 new_attr
.stackaddr
= NULL
;
437 new_attr
.stacksize
= 0;
439 /* We will pass this value on to the real implementation. */
440 attr
= (pthread_attr_t
*) &new_attr
;
443 return __pthread_create_2_1 (newthread
, attr
, start_routine
, arg
);
445 compat_symbol (libpthread
, __pthread_create_2_0
, pthread_create
,