1 /* Copyright (C) 2002 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 <shlib-compat.h>
31 /* Local function to start thread and handle cleanup. */
32 static int start_thread (void *arg
);
33 /* Similar version used when debugging. */
34 static int start_thread_debug (void *arg
);
37 /* Nozero if debugging mode is enabled. */
40 /* Globally enabled events. */
41 static td_thr_events_t __nptl_threads_events
;
43 /* Pointer to descriptor with the last event. */
44 static struct pthread
*__nptl_last_event
;
47 /* Code to allocate and deallocate a stack. */
48 #define DEFINE_DEALLOC
49 #include "allocatestack.c"
51 /* Code to create the thread. */
52 #include "createthread.c"
55 /* Table of the key information. */
56 struct pthread_key_struct __pthread_keys
[PTHREAD_KEYS_MAX
]
57 __attribute__ ((section (".bss")));
58 hidden_def (__pthread_keys
)
60 /* This is for libthread_db only. */
61 const int __pthread_pthread_sizeof_descr
= sizeof (struct pthread
);
64 __find_in_stack_list (pd
)
68 struct pthread
*result
= NULL
;
70 lll_lock (stack_cache_lock
);
72 list_for_each (entry
, &stack_used
)
76 curp
= list_entry (entry
, struct pthread
, header
.data
.list
);
85 list_for_each (entry
, &__stack_user
)
89 curp
= list_entry (entry
, struct pthread
, header
.data
.list
);
97 lll_unlock (stack_cache_lock
);
103 /* Deallocate POSIX thread-local-storage. */
105 deallocate_tsd (struct pthread
*pd
)
107 /* Maybe no data was ever allocated. This happens often so we have
109 if (pd
->specific_used
)
114 for (round
= 0, found_nonzero
= true;
115 found_nonzero
&& round
< PTHREAD_DESTRUCTOR_ITERATIONS
;
121 for (cnt
= idx
= 0; cnt
< PTHREAD_KEY_1STLEVEL_SIZE
; ++cnt
)
122 if (pd
->specific
[cnt
] != NULL
)
126 for (inner
= 0; inner
< PTHREAD_KEY_2NDLEVEL_SIZE
;
129 void *data
= pd
->specific
[cnt
][inner
].data
;
132 /* Make sure the data corresponds to a valid
133 key. This test fails if the key was
134 deallocated and also if it was
135 re-allocated. It is the user's
136 responsibility to free the memory in this
138 && (pd
->specific
[cnt
][inner
].seq
139 == __pthread_keys
[idx
].seq
)
140 /* It is not necessary to register a destructor
142 && __pthread_keys
[idx
].destr
!= NULL
)
144 pd
->specific
[cnt
][inner
].data
= NULL
;
145 __pthread_keys
[idx
].destr (data
);
146 found_nonzero
= true;
152 /* The first block is allocated as part of the thread
154 free (pd
->specific
[cnt
]);
155 pd
->specific
[cnt
] = NULL
;
158 /* Clear the memory of the first block for reuse. */
159 memset (pd
->specific
[0], '\0',
160 sizeof (struct pthread_key_data
));
163 idx
+= PTHREAD_KEY_1STLEVEL_SIZE
;
166 pd
->specific_used
= false;
171 /* Deallocate a thread's stack after optionally making sure the thread
172 descriptor is still valid. */
174 __free_tcb (struct pthread
*pd
)
176 /* The thread is exiting now. */
177 if (atomic_bit_test_set (&pd
->cancelhandling
, TERMINATED_BIT
) == 0)
179 /* Remove the descriptor from the list. */
180 if (DEBUGGING_P
&& __find_in_stack_list (pd
) == NULL
)
181 /* Something is really wrong. The descriptor for a still
182 running thread is gone. */
185 /* Run the destructor for the thread-local data. */
188 /* Queue the stack memory block for reuse and exit the process. The
189 kernel will signal via writing to the address returned by
190 QUEUE-STACK when the stack is available. */
191 __deallocate_stack (pd
);
197 start_thread (void *arg
)
199 struct pthread
*pd
= (struct pthread
*) arg
;
202 /* Remember the time when the thread was started. */
205 THREAD_SETMEM (pd
, cpuclock_offset
, now
);
208 /* This is where the try/finally block should be created. For
209 compilers without that support we do use setjmp. */
210 if (setjmp (pd
->cancelbuf
) == 0)
212 /* Run the code the user provided. */
213 pd
->result
= pd
->start_routine (pd
->arg
);
217 /* Report the death of the thread if this is wanted. */
218 if (__builtin_expect (pd
->report_events
, 0))
220 /* See whether TD_DEATH is in any of the mask. */
221 const int idx
= __td_eventword (TD_DEATH
);
222 const uint32_t mask
= __td_eventmask (TD_DEATH
);
224 if ((mask
& (__nptl_threads_events
.event_bits
[idx
]
225 | pd
->eventbuf
.eventmask
.event_bits
[idx
])) != 0)
227 /* Yep, we have to signal the death. Add the descriptor to
228 the list but only if it is not already on it. */
229 if (pd
->nextevent
== NULL
)
231 pd
->eventbuf
.eventnum
= TD_DEATH
;
232 pd
->eventbuf
.eventdata
= pd
;
235 pd
->nextevent
= __nptl_last_event
;
236 while (atomic_compare_and_exchange_acq (&__nptl_last_event
, pd
,
237 pd
->nextevent
) != 0);
240 /* Now call the function to signal the event. */
241 __nptl_death_event ();
246 /* The thread is exiting now. */
247 atomic_bit_set (&pd
->cancelhandling
, EXITING_BIT
);
249 /* If the thread is detached free the TCB. */
250 if (IS_DETACHED (pd
))
254 /* We cannot call '_exit' here. '_exit' will terminate the process.
256 The 'exit' implementation in the kernel will signal when the
257 process is really dead since 'clone' got passed the CLONE_CLEARTID
258 flag. The 'tid' field in the TCB will be set to zero.
260 The exit code is zero since in case all threads exit by calling
261 'pthread_exit' the exit status must be 0 (zero). */
262 __exit_thread_inline (0);
269 /* Just list start_thread but we do some more things needed for a run
270 with a debugger attached. */
272 start_thread_debug (void *arg
)
274 struct pthread
*pd
= (struct pthread
*) arg
;
276 /* Get the lock the parent locked to force synchronization. */
278 /* And give it up right away. */
279 lll_unlock (pd
->lock
);
281 /* Now do the actual startup. */
282 return start_thread (arg
);
286 /* Default thread attributes for the case when the user does not
288 static const struct pthread_attr default_attr
=
290 /* Just some value > 0 which gets rounded to the nearest page size. */
296 __pthread_create_2_1 (newthread
, attr
, start_routine
, arg
)
297 pthread_t
*newthread
;
298 const pthread_attr_t
*attr
;
299 void *(*start_routine
) (void *);
303 const struct pthread_attr
*iattr
;
307 iattr
= (struct pthread_attr
*) attr
;
309 /* Is this the best idea? On NUMA machines this could mean
310 accessing far-away memory. */
311 iattr
= &default_attr
;
313 err
= ALLOCATE_STACK (iattr
, &pd
);
315 /* Something went wrong. Maybe a parameter of the attributes is
316 invalid or we could not allocate memory. */
320 /* Initialize the TCB. All initializations with zero should be
321 performed in 'get_cached_stack'. This way we avoid doing this if
322 the stack freshly allocated with 'mmap'. */
324 /* Reference to the TCB itself. */
325 pd
->header
.data
.self
= pd
;
328 /* Self-reference. */
329 pd
->header
.data
.tcb
= pd
;
332 /* Store the address of the start routine and the parameter. Since
333 we do not start the function directly the stillborn thread will
334 get the information from its thread descriptor. */
335 pd
->start_routine
= start_routine
;
338 /* Copy the thread attribute flags. */
339 pd
->flags
= iattr
->flags
;
341 /* Initialize the field for the ID of the thread which is waiting
342 for us. This is a self-reference in case the thread is created
344 pd
->joinid
= iattr
->flags
& ATTR_FLAG_DETACHSTATE
? pd
: NULL
;
346 /* The debug events are inherited from the parent. */
347 pd
->eventbuf
= THREAD_SELF
->eventbuf
;
350 /* Determine scheduling parameters for the thread.
351 XXX How to determine whether scheduling handling is needed? */
352 if (0 && attr
!= NULL
)
354 if (iattr
->flags
& ATTR_FLAG_NOTINHERITSCHED
)
356 /* Use the scheduling parameters the user provided. */
357 pd
->schedpolicy
= iattr
->schedpolicy
;
358 memcpy (&pd
->schedparam
, &iattr
->schedparam
,
359 sizeof (struct sched_param
));
363 /* Just store the scheduling attributes of the parent. */
364 pd
->schedpolicy
= __sched_getscheduler (0);
365 __sched_getparam (0, &pd
->schedparam
);
369 /* Pass the descriptor to the caller. */
370 *newthread
= (pthread_t
) pd
;
372 /* Start the thread. */
373 err
= create_thread (pd
, STACK_VARIABLES_ARGS
);
376 /* Something went wrong. Free the resources. */
377 __deallocate_stack (pd
);
383 versioned_symbol (libpthread
, __pthread_create_2_1
, pthread_create
, GLIBC_2_1
);
386 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
388 __pthread_create_2_0 (newthread
, attr
, start_routine
, arg
)
389 pthread_t
*newthread
;
390 const pthread_attr_t
*attr
;
391 void *(*start_routine
) (void *);
394 /* The ATTR attribute is not really of type `pthread_attr_t *'. It has
395 the old size and access to the new members might crash the program.
396 We convert the struct now. */
397 struct pthread_attr new_attr
;
401 struct pthread_attr
*iattr
= (struct pthread_attr
*) attr
;
402 size_t ps
= __getpagesize ();
404 /* Copy values from the user-provided attributes. */
405 new_attr
.schedparam
= iattr
->schedparam
;
406 new_attr
.schedpolicy
= iattr
->schedpolicy
;
407 new_attr
.flags
= iattr
->flags
;
409 /* Fill in default values for the fields not present in the old
411 new_attr
.guardsize
= ps
;
412 new_attr
.stackaddr
= NULL
;
413 new_attr
.stacksize
= 0;
415 /* We will pass this value on to the real implementation. */
416 attr
= (pthread_attr_t
*) &new_attr
;
419 return __pthread_create_2_1 (newthread
, attr
, start_routine
, arg
);
421 compat_symbol (libpthread
, __pthread_create_2_0
, pthread_create
,