lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Linuxthreads - a simple clone()-based implementation of Posix */ |
| 2 | /* threads for Linux. */ |
| 3 | /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */ |
| 4 | /* */ |
| 5 | /* This program is free software; you can redistribute it and/or */ |
| 6 | /* modify it under the terms of the GNU Library General Public License */ |
| 7 | /* as published by the Free Software Foundation; either version 2 */ |
| 8 | /* of the License, or (at your option) any later version. */ |
| 9 | /* */ |
| 10 | /* This program 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 */ |
| 13 | /* GNU Library General Public License for more details. */ |
| 14 | |
| 15 | /* The "thread manager" thread: manages creation and termination of threads */ |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <errno.h> |
| 19 | #include <sched.h> |
| 20 | #include <stddef.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | #include <sys/poll.h> /* for poll */ |
| 26 | #include <sys/mman.h> /* for mmap */ |
| 27 | #include <sys/param.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/wait.h> /* for waitpid macros */ |
| 30 | #include <locale.h> /* for __uselocale */ |
| 31 | #include <resolv.h> /* for __resp */ |
| 32 | |
| 33 | #include "pthread.h" |
| 34 | #include "internals.h" |
| 35 | #include "spinlock.h" |
| 36 | #include "restart.h" |
| 37 | #include "semaphore.h" |
| 38 | #include <not-cancel.h> |
| 39 | |
| 40 | /* For debugging purposes put the maximum number of threads in a variable. */ |
| 41 | const int __linuxthreads_pthread_threads_max = PTHREAD_THREADS_MAX; |
| 42 | |
| 43 | #ifndef THREAD_SELF |
| 44 | /* Indicate whether at least one thread has a user-defined stack (if 1), |
| 45 | or if all threads have stacks supplied by LinuxThreads (if 0). */ |
| 46 | int __pthread_nonstandard_stacks; |
| 47 | #endif |
| 48 | |
| 49 | /* Number of active entries in __pthread_handles (used by gdb) */ |
| 50 | __volatile__ int __pthread_handles_num = 2; |
| 51 | |
| 52 | /* Whether to use debugger additional actions for thread creation |
| 53 | (set to 1 by gdb) */ |
| 54 | __volatile__ int __pthread_threads_debug; |
| 55 | |
| 56 | /* Globally enabled events. */ |
| 57 | __volatile__ td_thr_events_t __pthread_threads_events; |
| 58 | |
| 59 | /* Pointer to thread descriptor with last event. */ |
| 60 | __volatile__ pthread_descr __pthread_last_event; |
| 61 | |
| 62 | static pthread_descr manager_thread; |
| 63 | |
| 64 | /* Mapping from stack segment to thread descriptor. */ |
| 65 | /* Stack segment numbers are also indices into the __pthread_handles array. */ |
| 66 | /* Stack segment number 0 is reserved for the initial thread. */ |
| 67 | |
| 68 | #if FLOATING_STACKS |
| 69 | # define thread_segment(seq) NULL |
| 70 | #else |
| 71 | static __inline__ pthread_descr thread_segment(int seg) |
| 72 | { |
| 73 | # ifdef _STACK_GROWS_UP |
| 74 | return (pthread_descr)(THREAD_STACK_START_ADDRESS + (seg - 1) * STACK_SIZE) |
| 75 | + 1; |
| 76 | # else |
| 77 | return (pthread_descr)(THREAD_STACK_START_ADDRESS - (seg - 1) * STACK_SIZE) |
| 78 | - 1; |
| 79 | # endif |
| 80 | } |
| 81 | #endif |
| 82 | |
| 83 | /* Flag set in signal handler to record child termination */ |
| 84 | |
| 85 | static __volatile__ int terminated_children; |
| 86 | |
| 87 | /* Flag set when the initial thread is blocked on pthread_exit waiting |
| 88 | for all other threads to terminate */ |
| 89 | |
| 90 | static int main_thread_exiting; |
| 91 | |
| 92 | /* Counter used to generate unique thread identifier. |
| 93 | Thread identifier is pthread_threads_counter + segment. */ |
| 94 | |
| 95 | static pthread_t pthread_threads_counter; |
| 96 | |
| 97 | /* Forward declarations */ |
| 98 | |
| 99 | static int pthread_handle_create(pthread_t *thread, const pthread_attr_t *attr, |
| 100 | void * (*start_routine)(void *), void *arg, |
| 101 | sigset_t *mask, int father_pid, |
| 102 | int report_events, |
| 103 | td_thr_events_t *event_maskp); |
| 104 | static void pthread_handle_free(pthread_t th_id); |
| 105 | static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode) |
| 106 | __attribute__ ((noreturn)); |
| 107 | static void pthread_reap_children(void); |
| 108 | static void pthread_kill_all_threads(int sig, int main_thread_also); |
| 109 | static void pthread_for_each_thread(void *arg, |
| 110 | void (*fn)(void *, pthread_descr)); |
| 111 | |
| 112 | /* The server thread managing requests for thread creation and termination */ |
| 113 | |
| 114 | int |
| 115 | __attribute__ ((noreturn)) |
| 116 | __pthread_manager(void *arg) |
| 117 | { |
| 118 | pthread_descr self = manager_thread = arg; |
| 119 | int reqfd = __pthread_manager_reader; |
| 120 | struct pollfd ufd; |
| 121 | sigset_t manager_mask; |
| 122 | int n; |
| 123 | struct pthread_request request; |
| 124 | |
| 125 | /* If we have special thread_self processing, initialize it. */ |
| 126 | #ifdef INIT_THREAD_SELF |
| 127 | INIT_THREAD_SELF(self, 1); |
| 128 | #endif |
| 129 | #if !(USE_TLS && HAVE___THREAD) |
| 130 | /* Set the error variable. */ |
| 131 | self->p_errnop = &self->p_errno; |
| 132 | self->p_h_errnop = &self->p_h_errno; |
| 133 | #endif |
| 134 | /* Block all signals except __pthread_sig_cancel and SIGTRAP */ |
| 135 | __sigfillset(&manager_mask); |
| 136 | sigdelset(&manager_mask, __pthread_sig_cancel); /* for thread termination */ |
| 137 | sigdelset(&manager_mask, SIGTRAP); /* for debugging purposes */ |
| 138 | if (__pthread_threads_debug && __pthread_sig_debug > 0) |
| 139 | sigdelset(&manager_mask, __pthread_sig_debug); |
| 140 | sigprocmask(SIG_SETMASK, &manager_mask, NULL); |
| 141 | /* Raise our priority to match that of main thread */ |
| 142 | __pthread_manager_adjust_prio(__pthread_main_thread->p_priority); |
| 143 | /* Synchronize debugging of the thread manager */ |
| 144 | n = TEMP_FAILURE_RETRY(read_not_cancel(reqfd, (char *)&request, |
| 145 | sizeof(request))); |
| 146 | ASSERT(n == sizeof(request) && request.req_kind == REQ_DEBUG); |
| 147 | ufd.fd = reqfd; |
| 148 | ufd.events = POLLIN; |
| 149 | /* Enter server loop */ |
| 150 | while(1) { |
| 151 | n = __poll(&ufd, 1, 2000); |
| 152 | |
| 153 | /* Check for termination of the main thread */ |
| 154 | if (getppid() == 1) { |
| 155 | pthread_kill_all_threads(SIGKILL, 0); |
| 156 | _exit(0); |
| 157 | } |
| 158 | /* Check for dead children */ |
| 159 | if (terminated_children) { |
| 160 | terminated_children = 0; |
| 161 | pthread_reap_children(); |
| 162 | } |
| 163 | /* Read and execute request */ |
| 164 | if (n == 1 && (ufd.revents & POLLIN)) { |
| 165 | n = TEMP_FAILURE_RETRY(read_not_cancel(reqfd, (char *)&request, |
| 166 | sizeof(request))); |
| 167 | #ifdef DEBUG |
| 168 | if (n < 0) { |
| 169 | char d[64]; |
| 170 | write(STDERR_FILENO, d, snprintf(d, sizeof(d), "*** read err %m\n")); |
| 171 | } else if (n != sizeof(request)) { |
| 172 | write(STDERR_FILENO, "*** short read in manager\n", 26); |
| 173 | } |
| 174 | #endif |
| 175 | |
| 176 | switch(request.req_kind) { |
| 177 | case REQ_CREATE: |
| 178 | request.req_thread->p_retcode = |
| 179 | pthread_handle_create((pthread_t *) &request.req_thread->p_retval, |
| 180 | request.req_args.create.attr, |
| 181 | request.req_args.create.fn, |
| 182 | request.req_args.create.arg, |
| 183 | &request.req_args.create.mask, |
| 184 | request.req_thread->p_pid, |
| 185 | request.req_thread->p_report_events, |
| 186 | &request.req_thread->p_eventbuf.eventmask); |
| 187 | restart(request.req_thread); |
| 188 | break; |
| 189 | case REQ_FREE: |
| 190 | pthread_handle_free(request.req_args.free.thread_id); |
| 191 | break; |
| 192 | case REQ_PROCESS_EXIT: |
| 193 | pthread_handle_exit(request.req_thread, |
| 194 | request.req_args.exit.code); |
| 195 | /* NOTREACHED */ |
| 196 | break; |
| 197 | case REQ_MAIN_THREAD_EXIT: |
| 198 | main_thread_exiting = 1; |
| 199 | /* Reap children in case all other threads died and the signal handler |
| 200 | went off before we set main_thread_exiting to 1, and therefore did |
| 201 | not do REQ_KICK. */ |
| 202 | pthread_reap_children(); |
| 203 | |
| 204 | if (__pthread_main_thread->p_nextlive == __pthread_main_thread) { |
| 205 | restart(__pthread_main_thread); |
| 206 | /* The main thread will now call exit() which will trigger an |
| 207 | __on_exit handler, which in turn will send REQ_PROCESS_EXIT |
| 208 | to the thread manager. In case you are wondering how the |
| 209 | manager terminates from its loop here. */ |
| 210 | } |
| 211 | break; |
| 212 | case REQ_POST: |
| 213 | sem_post(request.req_args.post); |
| 214 | break; |
| 215 | case REQ_DEBUG: |
| 216 | /* Make gdb aware of new thread and gdb will restart the |
| 217 | new thread when it is ready to handle the new thread. */ |
| 218 | if (__pthread_threads_debug && __pthread_sig_debug > 0) |
| 219 | raise(__pthread_sig_debug); |
| 220 | break; |
| 221 | case REQ_KICK: |
| 222 | /* This is just a prod to get the manager to reap some |
| 223 | threads right away, avoiding a potential delay at shutdown. */ |
| 224 | break; |
| 225 | case REQ_FOR_EACH_THREAD: |
| 226 | pthread_for_each_thread(request.req_args.for_each.arg, |
| 227 | request.req_args.for_each.fn); |
| 228 | restart(request.req_thread); |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | int __pthread_manager_event(void *arg) |
| 236 | { |
| 237 | pthread_descr self = arg; |
| 238 | /* If we have special thread_self processing, initialize it. */ |
| 239 | #ifdef INIT_THREAD_SELF |
| 240 | INIT_THREAD_SELF(self, 1); |
| 241 | #endif |
| 242 | |
| 243 | /* Get the lock the manager will free once all is correctly set up. */ |
| 244 | __pthread_lock (THREAD_GETMEM(self, p_lock), NULL); |
| 245 | /* Free it immediately. */ |
| 246 | __pthread_unlock (THREAD_GETMEM(self, p_lock)); |
| 247 | |
| 248 | return __pthread_manager(arg); |
| 249 | } |
| 250 | |
| 251 | /* Process creation */ |
| 252 | |
| 253 | static int |
| 254 | __attribute__ ((noreturn)) |
| 255 | pthread_start_thread(void *arg) |
| 256 | { |
| 257 | pthread_descr self = (pthread_descr) arg; |
| 258 | struct pthread_request request; |
| 259 | void * outcome; |
| 260 | #if HP_TIMING_AVAIL |
| 261 | hp_timing_t tmpclock; |
| 262 | #endif |
| 263 | /* Initialize special thread_self processing, if any. */ |
| 264 | #ifdef INIT_THREAD_SELF |
| 265 | INIT_THREAD_SELF(self, self->p_nr); |
| 266 | #endif |
| 267 | #if HP_TIMING_AVAIL |
| 268 | HP_TIMING_NOW (tmpclock); |
| 269 | THREAD_SETMEM (self, p_cpuclock_offset, tmpclock); |
| 270 | #endif |
| 271 | /* Make sure our pid field is initialized, just in case we get there |
| 272 | before our father has initialized it. */ |
| 273 | THREAD_SETMEM(self, p_pid, __getpid()); |
| 274 | /* Initial signal mask is that of the creating thread. (Otherwise, |
| 275 | we'd just inherit the mask of the thread manager.) */ |
| 276 | sigprocmask(SIG_SETMASK, &self->p_start_args.mask, NULL); |
| 277 | /* Set the scheduling policy and priority for the new thread, if needed */ |
| 278 | if (THREAD_GETMEM(self, p_start_args.schedpolicy) >= 0) |
| 279 | /* Explicit scheduling attributes were provided: apply them */ |
| 280 | __sched_setscheduler(THREAD_GETMEM(self, p_pid), |
| 281 | THREAD_GETMEM(self, p_start_args.schedpolicy), |
| 282 | &self->p_start_args.schedparam); |
| 283 | else if (manager_thread->p_priority > 0) |
| 284 | /* Default scheduling required, but thread manager runs in realtime |
| 285 | scheduling: switch new thread to SCHED_OTHER policy */ |
| 286 | { |
| 287 | struct sched_param default_params; |
| 288 | default_params.sched_priority = 0; |
| 289 | __sched_setscheduler(THREAD_GETMEM(self, p_pid), |
| 290 | SCHED_OTHER, &default_params); |
| 291 | } |
| 292 | #if !(USE_TLS && HAVE___THREAD) |
| 293 | /* Initialize thread-locale current locale to point to the global one. |
| 294 | With __thread support, the variable's initializer takes care of this. */ |
| 295 | __uselocale (LC_GLOBAL_LOCALE); |
| 296 | #elif defined __UCLIBC_HAS_RESOLVER_SUPPORT__ |
| 297 | /* Initialize __resp. */ |
| 298 | __resp = &self->p_res; |
| 299 | #endif |
| 300 | /* Make gdb aware of new thread */ |
| 301 | if (__pthread_threads_debug && __pthread_sig_debug > 0) { |
| 302 | request.req_thread = self; |
| 303 | request.req_kind = REQ_DEBUG; |
| 304 | TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request, |
| 305 | (char *) &request, sizeof(request))); |
| 306 | suspend(self); |
| 307 | } |
| 308 | /* Run the thread code */ |
| 309 | outcome = self->p_start_args.start_routine(THREAD_GETMEM(self, |
| 310 | p_start_args.arg)); |
| 311 | /* Exit with the given return value */ |
| 312 | __pthread_do_exit(outcome, CURRENT_STACK_FRAME); |
| 313 | } |
| 314 | |
| 315 | static int |
| 316 | __attribute__ ((noreturn)) |
| 317 | pthread_start_thread_event(void *arg) |
| 318 | { |
| 319 | pthread_descr self = (pthread_descr) arg; |
| 320 | |
| 321 | #ifdef INIT_THREAD_SELF |
| 322 | INIT_THREAD_SELF(self, self->p_nr); |
| 323 | #endif |
| 324 | /* Make sure our pid field is initialized, just in case we get there |
| 325 | before our father has initialized it. */ |
| 326 | THREAD_SETMEM(self, p_pid, __getpid()); |
| 327 | /* Get the lock the manager will free once all is correctly set up. */ |
| 328 | __pthread_lock (THREAD_GETMEM(self, p_lock), NULL); |
| 329 | /* Free it immediately. */ |
| 330 | __pthread_unlock (THREAD_GETMEM(self, p_lock)); |
| 331 | |
| 332 | /* Continue with the real function. */ |
| 333 | pthread_start_thread (arg); |
| 334 | } |
| 335 | |
| 336 | #if defined USE_TLS && !FLOATING_STACKS |
| 337 | # error "TLS can only work with floating stacks" |
| 338 | #endif |
| 339 | |
| 340 | static int pthread_allocate_stack(const pthread_attr_t *attr, |
| 341 | pthread_descr default_new_thread, |
| 342 | int pagesize, |
| 343 | char ** out_new_thread, |
| 344 | char ** out_new_thread_bottom, |
| 345 | char ** out_guardaddr, |
| 346 | size_t * out_guardsize, |
| 347 | size_t * out_stacksize) |
| 348 | { |
| 349 | pthread_descr new_thread; |
| 350 | char * new_thread_bottom; |
| 351 | char * guardaddr; |
| 352 | size_t stacksize, guardsize; |
| 353 | |
| 354 | #ifdef USE_TLS |
| 355 | /* TLS cannot work with fixed thread descriptor addresses. */ |
| 356 | assert (default_new_thread == NULL); |
| 357 | #endif |
| 358 | |
| 359 | if (attr != NULL && attr->__stackaddr_set) |
| 360 | { |
| 361 | #ifdef _STACK_GROWS_UP |
| 362 | /* The user provided a stack. */ |
| 363 | # ifdef USE_TLS |
| 364 | /* This value is not needed. */ |
| 365 | new_thread = (pthread_descr) attr->__stackaddr; |
| 366 | new_thread_bottom = (char *) new_thread; |
| 367 | # else |
| 368 | new_thread = (pthread_descr) attr->__stackaddr; |
| 369 | new_thread_bottom = (char *) (new_thread + 1); |
| 370 | # endif |
| 371 | guardaddr = attr->__stackaddr + attr->__stacksize; |
| 372 | guardsize = 0; |
| 373 | #else |
| 374 | /* The user provided a stack. For now we interpret the supplied |
| 375 | address as 1 + the highest addr. in the stack segment. If a |
| 376 | separate register stack is needed, we place it at the low end |
| 377 | of the segment, relying on the associated stacksize to |
| 378 | determine the low end of the segment. This differs from many |
| 379 | (but not all) other pthreads implementations. The intent is |
| 380 | that on machines with a single stack growing toward higher |
| 381 | addresses, stackaddr would be the lowest address in the stack |
| 382 | segment, so that it is consistently close to the initial sp |
| 383 | value. */ |
| 384 | # ifdef USE_TLS |
| 385 | new_thread = (pthread_descr) attr->__stackaddr; |
| 386 | # else |
| 387 | new_thread = |
| 388 | (pthread_descr) ((long)(attr->__stackaddr) & -sizeof(void *)) - 1; |
| 389 | # endif |
| 390 | new_thread_bottom = (char *) attr->__stackaddr - attr->__stacksize; |
| 391 | guardaddr = new_thread_bottom; |
| 392 | guardsize = 0; |
| 393 | #endif |
| 394 | #ifndef THREAD_SELF |
| 395 | __pthread_nonstandard_stacks = 1; |
| 396 | #endif |
| 397 | #ifndef USE_TLS |
| 398 | /* Clear the thread data structure. */ |
| 399 | memset (new_thread, '\0', sizeof (*new_thread)); |
| 400 | #endif |
| 401 | stacksize = attr->__stacksize; |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | #ifdef NEED_SEPARATE_REGISTER_STACK |
| 406 | const size_t granularity = 2 * pagesize; |
| 407 | /* Try to make stacksize/2 a multiple of pagesize */ |
| 408 | #else |
| 409 | const size_t granularity = pagesize; |
| 410 | #endif |
| 411 | void *map_addr; |
| 412 | |
| 413 | /* Allocate space for stack and thread descriptor at default address */ |
| 414 | #if FLOATING_STACKS |
| 415 | if (attr != NULL) |
| 416 | { |
| 417 | guardsize = page_roundup (attr->__guardsize, granularity); |
| 418 | stacksize = __pthread_max_stacksize - guardsize; |
| 419 | stacksize = MIN (stacksize, |
| 420 | page_roundup (attr->__stacksize, granularity)); |
| 421 | } |
| 422 | else |
| 423 | { |
| 424 | guardsize = granularity; |
| 425 | stacksize = __pthread_max_stacksize - guardsize; |
| 426 | } |
| 427 | |
| 428 | map_addr = mmap(NULL, stacksize + guardsize, |
| 429 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 430 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 431 | if (map_addr == MAP_FAILED) |
| 432 | /* No more memory available. */ |
| 433 | return -1; |
| 434 | |
| 435 | # ifdef NEED_SEPARATE_REGISTER_STACK |
| 436 | guardaddr = map_addr + stacksize / 2; |
| 437 | if (guardsize > 0) |
| 438 | mprotect (guardaddr, guardsize, PROT_NONE); |
| 439 | |
| 440 | new_thread_bottom = (char *) map_addr; |
| 441 | # ifdef USE_TLS |
| 442 | new_thread = ((pthread_descr) (new_thread_bottom + stacksize |
| 443 | + guardsize)); |
| 444 | # else |
| 445 | new_thread = ((pthread_descr) (new_thread_bottom + stacksize |
| 446 | + guardsize)) - 1; |
| 447 | # endif |
| 448 | # elif defined _STACK_GROWS_DOWN |
| 449 | guardaddr = map_addr; |
| 450 | if (guardsize > 0) |
| 451 | mprotect (guardaddr, guardsize, PROT_NONE); |
| 452 | |
| 453 | new_thread_bottom = (char *) map_addr + guardsize; |
| 454 | # ifdef USE_TLS |
| 455 | new_thread = ((pthread_descr) (new_thread_bottom + stacksize)); |
| 456 | # else |
| 457 | new_thread = ((pthread_descr) (new_thread_bottom + stacksize)) - 1; |
| 458 | # endif |
| 459 | # elif defined _STACK_GROWS_UP |
| 460 | guardaddr = map_addr + stacksize; |
| 461 | if (guardsize > 0) |
| 462 | mprotect (guardaddr, guardsize, PROT_NONE); |
| 463 | |
| 464 | new_thread = (pthread_descr) map_addr; |
| 465 | # ifdef USE_TLS |
| 466 | new_thread_bottom = (char *) new_thread; |
| 467 | # else |
| 468 | new_thread_bottom = (char *) (new_thread + 1); |
| 469 | # endif |
| 470 | # else |
| 471 | # error You must define a stack direction |
| 472 | # endif /* Stack direction */ |
| 473 | #else /* !FLOATING_STACKS */ |
| 474 | # if !defined NEED_SEPARATE_REGISTER_STACK && defined _STACK_GROWS_DOWN |
| 475 | void *res_addr; |
| 476 | # endif |
| 477 | |
| 478 | if (attr != NULL) |
| 479 | { |
| 480 | guardsize = page_roundup (attr->__guardsize, granularity); |
| 481 | stacksize = STACK_SIZE - guardsize; |
| 482 | stacksize = MIN (stacksize, |
| 483 | page_roundup (attr->__stacksize, granularity)); |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | guardsize = granularity; |
| 488 | stacksize = STACK_SIZE - granularity; |
| 489 | } |
| 490 | |
| 491 | # ifdef NEED_SEPARATE_REGISTER_STACK |
| 492 | new_thread = default_new_thread; |
| 493 | new_thread_bottom = (char *) (new_thread + 1) - stacksize - guardsize; |
| 494 | /* Includes guard area, unlike the normal case. Use the bottom |
| 495 | end of the segment as backing store for the register stack. |
| 496 | Needed on IA64. In this case, we also map the entire stack at |
| 497 | once. According to David Mosberger, that's cheaper. It also |
| 498 | avoids the risk of intermittent failures due to other mappings |
| 499 | in the same region. The cost is that we might be able to map |
| 500 | slightly fewer stacks. */ |
| 501 | |
| 502 | /* First the main stack: */ |
| 503 | map_addr = (caddr_t)((char *)(new_thread + 1) - stacksize / 2); |
| 504 | res_addr = mmap(map_addr, stacksize / 2, |
| 505 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 506 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 507 | if (res_addr != map_addr) |
| 508 | { |
| 509 | /* Bad luck, this segment is already mapped. */ |
| 510 | if (res_addr != MAP_FAILED) |
| 511 | munmap(res_addr, stacksize / 2); |
| 512 | return -1; |
| 513 | } |
| 514 | /* Then the register stack: */ |
| 515 | map_addr = (caddr_t)new_thread_bottom; |
| 516 | res_addr = mmap(map_addr, stacksize/2, |
| 517 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 518 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 519 | if (res_addr != map_addr) |
| 520 | { |
| 521 | if (res_addr != MAP_FAILED) |
| 522 | munmap(res_addr, stacksize / 2); |
| 523 | munmap((caddr_t)((char *)(new_thread + 1) - stacksize/2), |
| 524 | stacksize/2); |
| 525 | return -1; |
| 526 | } |
| 527 | |
| 528 | guardaddr = new_thread_bottom + stacksize/2; |
| 529 | /* We leave the guard area in the middle unmapped. */ |
| 530 | # else /* !NEED_SEPARATE_REGISTER_STACK */ |
| 531 | # ifdef _STACK_GROWS_DOWN |
| 532 | new_thread = default_new_thread; |
| 533 | new_thread_bottom = (char *) (new_thread + 1) - stacksize; |
| 534 | map_addr = new_thread_bottom - guardsize; |
| 535 | res_addr = mmap(map_addr, stacksize + guardsize, |
| 536 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 537 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 538 | if (res_addr != map_addr) |
| 539 | { |
| 540 | /* Bad luck, this segment is already mapped. */ |
| 541 | if (res_addr != MAP_FAILED) |
| 542 | munmap (res_addr, stacksize + guardsize); |
| 543 | return -1; |
| 544 | } |
| 545 | |
| 546 | /* We manage to get a stack. Protect the guard area pages if |
| 547 | necessary. */ |
| 548 | guardaddr = map_addr; |
| 549 | if (guardsize > 0) |
| 550 | mprotect (guardaddr, guardsize, PROT_NONE); |
| 551 | # else |
| 552 | /* The thread description goes at the bottom of this area, and |
| 553 | * the stack starts directly above it. |
| 554 | */ |
| 555 | new_thread = (pthread_descr)((unsigned long)default_new_thread &~ (STACK_SIZE - 1)); |
| 556 | map_addr = mmap(new_thread, stacksize + guardsize, |
| 557 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 558 | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 559 | if (map_addr == MAP_FAILED) |
| 560 | return -1; |
| 561 | |
| 562 | new_thread_bottom = map_addr + sizeof(*new_thread); |
| 563 | guardaddr = map_addr + stacksize; |
| 564 | if (guardsize > 0) |
| 565 | mprotect (guardaddr, guardsize, PROT_NONE); |
| 566 | |
| 567 | # endif /* stack direction */ |
| 568 | # endif /* !NEED_SEPARATE_REGISTER_STACK */ |
| 569 | #endif /* !FLOATING_STACKS */ |
| 570 | } |
| 571 | *out_new_thread = (char *) new_thread; |
| 572 | *out_new_thread_bottom = new_thread_bottom; |
| 573 | *out_guardaddr = guardaddr; |
| 574 | *out_guardsize = guardsize; |
| 575 | #ifdef NEED_SEPARATE_REGISTER_STACK |
| 576 | *out_stacksize = stacksize / 2; |
| 577 | #else |
| 578 | *out_stacksize = stacksize; |
| 579 | #endif |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | static int pthread_handle_create(pthread_t *thread, const pthread_attr_t *attr, |
| 584 | void * (*start_routine)(void *), void *arg, |
| 585 | sigset_t * mask, int father_pid, |
| 586 | int report_events, |
| 587 | td_thr_events_t *event_maskp) |
| 588 | { |
| 589 | size_t sseg; |
| 590 | int pid; |
| 591 | pthread_descr new_thread; |
| 592 | char *stack_addr; |
| 593 | char * new_thread_bottom; |
| 594 | pthread_t new_thread_id; |
| 595 | char *guardaddr = NULL; |
| 596 | size_t guardsize = 0, stksize = 0; |
| 597 | int pagesize = __getpagesize(); |
| 598 | int saved_errno = 0; |
| 599 | |
| 600 | #ifdef USE_TLS |
| 601 | new_thread = _dl_allocate_tls (NULL); |
| 602 | if (new_thread == NULL) |
| 603 | return EAGAIN; |
| 604 | # if defined(TLS_DTV_AT_TP) |
| 605 | /* pthread_descr is below TP. */ |
| 606 | new_thread = (pthread_descr) ((char *) new_thread - TLS_PRE_TCB_SIZE); |
| 607 | # endif |
| 608 | #else |
| 609 | /* Prevent warnings. */ |
| 610 | new_thread = NULL; |
| 611 | #endif |
| 612 | |
| 613 | /* First check whether we have to change the policy and if yes, whether |
| 614 | we can do this. Normally this should be done by examining the |
| 615 | return value of the __sched_setscheduler call in pthread_start_thread |
| 616 | but this is hard to implement. FIXME */ |
| 617 | if (attr != NULL && attr->__schedpolicy != SCHED_OTHER && geteuid () != 0) |
| 618 | return EPERM; |
| 619 | /* Find a free segment for the thread, and allocate a stack if needed */ |
| 620 | for (sseg = 2; ; sseg++) |
| 621 | { |
| 622 | if (sseg >= PTHREAD_THREADS_MAX) |
| 623 | { |
| 624 | #ifdef USE_TLS |
| 625 | # if defined(TLS_DTV_AT_TP) |
| 626 | new_thread = (pthread_descr) ((char *) new_thread + TLS_PRE_TCB_SIZE); |
| 627 | # endif |
| 628 | _dl_deallocate_tls (new_thread, true); |
| 629 | #endif |
| 630 | return EAGAIN; |
| 631 | } |
| 632 | if (__pthread_handles[sseg].h_descr != NULL) |
| 633 | continue; |
| 634 | if (pthread_allocate_stack(attr, thread_segment(sseg), |
| 635 | pagesize, &stack_addr, &new_thread_bottom, |
| 636 | &guardaddr, &guardsize, &stksize) == 0) |
| 637 | { |
| 638 | #ifdef USE_TLS |
| 639 | new_thread->p_stackaddr = stack_addr; |
| 640 | #else |
| 641 | new_thread = (pthread_descr) stack_addr; |
| 642 | #endif |
| 643 | break; |
| 644 | #ifndef __ARCH_USE_MMU__ |
| 645 | } else { |
| 646 | /* When there is MMU, mmap () is used to allocate the stack. If one |
| 647 | * segment is already mapped, we should continue to see if we can |
| 648 | * use the next one. However, when there is no MMU, malloc () is used. |
| 649 | * It's waste of CPU cycles to continue to try if it fails. */ |
| 650 | return EAGAIN; |
| 651 | #endif |
| 652 | } |
| 653 | } |
| 654 | __pthread_handles_num++; |
| 655 | /* Allocate new thread identifier */ |
| 656 | pthread_threads_counter += PTHREAD_THREADS_MAX; |
| 657 | new_thread_id = sseg + pthread_threads_counter; |
| 658 | /* Initialize the thread descriptor. Elements which have to be |
| 659 | initialized to zero already have this value. */ |
| 660 | #if !defined USE_TLS || !TLS_DTV_AT_TP |
| 661 | new_thread->p_header.data.tcb = new_thread; |
| 662 | new_thread->p_header.data.self = new_thread; |
| 663 | #endif |
| 664 | #if TLS_MULTIPLE_THREADS_IN_TCB || !defined USE_TLS || !TLS_DTV_AT_TP |
| 665 | new_thread->p_multiple_threads = 1; |
| 666 | #endif |
| 667 | new_thread->p_tid = new_thread_id; |
| 668 | new_thread->p_lock = &(__pthread_handles[sseg].h_lock); |
| 669 | new_thread->p_cancelstate = PTHREAD_CANCEL_ENABLE; |
| 670 | new_thread->p_canceltype = PTHREAD_CANCEL_DEFERRED; |
| 671 | #if !(USE_TLS && HAVE___THREAD) |
| 672 | new_thread->p_errnop = &new_thread->p_errno; |
| 673 | new_thread->p_h_errnop = &new_thread->p_h_errno; |
| 674 | new_thread->p_resp = &new_thread->p_res; |
| 675 | #endif |
| 676 | new_thread->p_guardaddr = guardaddr; |
| 677 | new_thread->p_guardsize = guardsize; |
| 678 | new_thread->p_nr = sseg; |
| 679 | new_thread->p_inheritsched = attr ? attr->__inheritsched : 0; |
| 680 | new_thread->p_alloca_cutoff = stksize / 4 > __MAX_ALLOCA_CUTOFF |
| 681 | ? __MAX_ALLOCA_CUTOFF : stksize / 4; |
| 682 | /* Initialize the thread handle */ |
| 683 | __pthread_init_lock(&__pthread_handles[sseg].h_lock); |
| 684 | __pthread_handles[sseg].h_descr = new_thread; |
| 685 | __pthread_handles[sseg].h_bottom = new_thread_bottom; |
| 686 | /* Determine scheduling parameters for the thread */ |
| 687 | new_thread->p_start_args.schedpolicy = -1; |
| 688 | if (attr != NULL) { |
| 689 | new_thread->p_detached = attr->__detachstate; |
| 690 | new_thread->p_userstack = attr->__stackaddr_set; |
| 691 | |
| 692 | switch(attr->__inheritsched) { |
| 693 | case PTHREAD_EXPLICIT_SCHED: |
| 694 | new_thread->p_start_args.schedpolicy = attr->__schedpolicy; |
| 695 | memcpy (&new_thread->p_start_args.schedparam, &attr->__schedparam, |
| 696 | sizeof (struct sched_param)); |
| 697 | break; |
| 698 | case PTHREAD_INHERIT_SCHED: |
| 699 | new_thread->p_start_args.schedpolicy = __sched_getscheduler(father_pid); |
| 700 | __sched_getparam(father_pid, &new_thread->p_start_args.schedparam); |
| 701 | break; |
| 702 | } |
| 703 | new_thread->p_priority = |
| 704 | new_thread->p_start_args.schedparam.sched_priority; |
| 705 | } |
| 706 | /* Finish setting up arguments to pthread_start_thread */ |
| 707 | new_thread->p_start_args.start_routine = start_routine; |
| 708 | new_thread->p_start_args.arg = arg; |
| 709 | new_thread->p_start_args.mask = *mask; |
| 710 | /* Make the new thread ID available already now. If any of the later |
| 711 | functions fail we return an error value and the caller must not use |
| 712 | the stored thread ID. */ |
| 713 | *thread = new_thread_id; |
| 714 | /* Raise priority of thread manager if needed */ |
| 715 | __pthread_manager_adjust_prio(new_thread->p_priority); |
| 716 | /* Do the cloning. We have to use two different functions depending |
| 717 | on whether we are debugging or not. */ |
| 718 | pid = 0; /* Note that the thread never can have PID zero. */ |
| 719 | if (report_events) |
| 720 | { |
| 721 | /* See whether the TD_CREATE event bit is set in any of the |
| 722 | masks. */ |
| 723 | int idx = __td_eventword (TD_CREATE); |
| 724 | uint32_t mask = __td_eventmask (TD_CREATE); |
| 725 | |
| 726 | if ((mask & (__pthread_threads_events.event_bits[idx] |
| 727 | | event_maskp->event_bits[idx])) != 0) |
| 728 | { |
| 729 | /* Lock the mutex the child will use now so that it will stop. */ |
| 730 | __pthread_lock(new_thread->p_lock, NULL); |
| 731 | |
| 732 | /* We have to report this event. */ |
| 733 | #ifdef NEED_SEPARATE_REGISTER_STACK |
| 734 | /* Perhaps this version should be used on all platforms. But |
| 735 | this requires that __clone2 be uniformly supported |
| 736 | everywhere. |
| 737 | |
| 738 | And there is some argument for changing the __clone2 |
| 739 | interface to pass sp and bsp instead, making it more IA64 |
| 740 | specific, but allowing stacks to grow outward from each |
| 741 | other, to get less paging and fewer mmaps. */ |
| 742 | pid = __clone2(pthread_start_thread_event, |
| 743 | (void **)new_thread_bottom, |
| 744 | (char *)stack_addr - new_thread_bottom, |
| 745 | CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_SYSVSEM | |
| 746 | __pthread_sig_cancel, new_thread); |
| 747 | #elif defined _STACK_GROWS_UP |
| 748 | pid = __clone(pthread_start_thread_event, (void *) new_thread_bottom, |
| 749 | CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_SYSVSEM | |
| 750 | __pthread_sig_cancel, new_thread); |
| 751 | #else |
| 752 | pid = __clone(pthread_start_thread_event, stack_addr, |
| 753 | CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_SYSVSEM | |
| 754 | __pthread_sig_cancel, new_thread); |
| 755 | #endif |
| 756 | saved_errno = errno; |
| 757 | if (pid != -1) |
| 758 | { |
| 759 | /* Now fill in the information about the new thread in |
| 760 | the newly created thread's data structure. We cannot let |
| 761 | the new thread do this since we don't know whether it was |
| 762 | already scheduled when we send the event. */ |
| 763 | new_thread->p_eventbuf.eventdata = new_thread; |
| 764 | new_thread->p_eventbuf.eventnum = TD_CREATE; |
| 765 | __pthread_last_event = new_thread; |
| 766 | |
| 767 | /* We have to set the PID here since the callback function |
| 768 | in the debug library will need it and we cannot guarantee |
| 769 | the child got scheduled before the debugger. */ |
| 770 | new_thread->p_pid = pid; |
| 771 | |
| 772 | /* Now call the function which signals the event. */ |
| 773 | __linuxthreads_create_event (); |
| 774 | |
| 775 | /* Now restart the thread. */ |
| 776 | __pthread_unlock(new_thread->p_lock); |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | if (pid == 0) |
| 781 | { |
| 782 | #ifdef NEED_SEPARATE_REGISTER_STACK |
| 783 | pid = __clone2(pthread_start_thread, |
| 784 | (void **)new_thread_bottom, |
| 785 | (char *)stack_addr - new_thread_bottom, |
| 786 | CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_SYSVSEM | |
| 787 | __pthread_sig_cancel, new_thread); |
| 788 | #elif defined _STACK_GROWS_UP |
| 789 | pid = __clone(pthread_start_thread, (void *) new_thread_bottom, |
| 790 | CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_SYSVSEM | |
| 791 | __pthread_sig_cancel, new_thread); |
| 792 | #else |
| 793 | pid = __clone(pthread_start_thread, stack_addr, |
| 794 | CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_SYSVSEM | |
| 795 | __pthread_sig_cancel, new_thread); |
| 796 | #endif /* !NEED_SEPARATE_REGISTER_STACK */ |
| 797 | saved_errno = errno; |
| 798 | } |
| 799 | /* Check if cloning succeeded */ |
| 800 | if (pid == -1) { |
| 801 | /* Free the stack if we allocated it */ |
| 802 | if (attr == NULL || !attr->__stackaddr_set) |
| 803 | { |
| 804 | #ifdef NEED_SEPARATE_REGISTER_STACK |
| 805 | size_t stacksize = ((char *)(new_thread->p_guardaddr) |
| 806 | - new_thread_bottom); |
| 807 | munmap((caddr_t)new_thread_bottom, |
| 808 | 2 * stacksize + new_thread->p_guardsize); |
| 809 | #elif defined _STACK_GROWS_UP |
| 810 | # ifdef USE_TLS |
| 811 | size_t stacksize = guardaddr - stack_addr; |
| 812 | munmap(stack_addr, stacksize + guardsize); |
| 813 | # else |
| 814 | size_t stacksize = guardaddr - (char *)new_thread; |
| 815 | munmap(new_thread, stacksize + guardsize); |
| 816 | # endif |
| 817 | #else |
| 818 | # ifdef USE_TLS |
| 819 | size_t stacksize = stack_addr - new_thread_bottom; |
| 820 | # else |
| 821 | size_t stacksize = (char *)(new_thread+1) - new_thread_bottom; |
| 822 | # endif |
| 823 | munmap(new_thread_bottom - guardsize, guardsize + stacksize); |
| 824 | #endif |
| 825 | } |
| 826 | #ifdef USE_TLS |
| 827 | # if defined(TLS_DTV_AT_TP) |
| 828 | new_thread = (pthread_descr) ((char *) new_thread + TLS_PRE_TCB_SIZE); |
| 829 | # endif |
| 830 | _dl_deallocate_tls (new_thread, true); |
| 831 | #endif |
| 832 | __pthread_handles[sseg].h_descr = NULL; |
| 833 | __pthread_handles[sseg].h_bottom = NULL; |
| 834 | __pthread_handles_num--; |
| 835 | return saved_errno; |
| 836 | } |
| 837 | /* Insert new thread in doubly linked list of active threads */ |
| 838 | new_thread->p_prevlive = __pthread_main_thread; |
| 839 | new_thread->p_nextlive = __pthread_main_thread->p_nextlive; |
| 840 | __pthread_main_thread->p_nextlive->p_prevlive = new_thread; |
| 841 | __pthread_main_thread->p_nextlive = new_thread; |
| 842 | /* Set pid field of the new thread, in case we get there before the |
| 843 | child starts. */ |
| 844 | new_thread->p_pid = pid; |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | |
| 849 | /* Try to free the resources of a thread when requested by pthread_join |
| 850 | or pthread_detach on a terminated thread. */ |
| 851 | |
| 852 | static void pthread_free(pthread_descr th) |
| 853 | { |
| 854 | pthread_handle handle; |
| 855 | pthread_readlock_info *iter, *next; |
| 856 | |
| 857 | ASSERT(th->p_exited); |
| 858 | /* Make the handle invalid */ |
| 859 | handle = thread_handle(th->p_tid); |
| 860 | __pthread_lock(&handle->h_lock, NULL); |
| 861 | handle->h_descr = NULL; |
| 862 | handle->h_bottom = (char *)(-1L); |
| 863 | __pthread_unlock(&handle->h_lock); |
| 864 | #ifdef FREE_THREAD |
| 865 | FREE_THREAD(th, th->p_nr); |
| 866 | #endif |
| 867 | /* One fewer threads in __pthread_handles */ |
| 868 | __pthread_handles_num--; |
| 869 | |
| 870 | /* Destroy read lock list, and list of free read lock structures. |
| 871 | If the former is not empty, it means the thread exited while |
| 872 | holding read locks! */ |
| 873 | |
| 874 | for (iter = th->p_readlock_list; iter != NULL; iter = next) |
| 875 | { |
| 876 | next = iter->pr_next; |
| 877 | free(iter); |
| 878 | } |
| 879 | |
| 880 | for (iter = th->p_readlock_free; iter != NULL; iter = next) |
| 881 | { |
| 882 | next = iter->pr_next; |
| 883 | free(iter); |
| 884 | } |
| 885 | |
| 886 | /* If initial thread, nothing to free */ |
| 887 | if (!th->p_userstack) |
| 888 | { |
| 889 | size_t guardsize = th->p_guardsize; |
| 890 | /* Free the stack and thread descriptor area */ |
| 891 | char *guardaddr = th->p_guardaddr; |
| 892 | #ifdef _STACK_GROWS_UP |
| 893 | # ifdef USE_TLS |
| 894 | size_t stacksize = guardaddr - th->p_stackaddr; |
| 895 | guardaddr = th->p_stackaddr; |
| 896 | # else |
| 897 | size_t stacksize = guardaddr - (char *)th; |
| 898 | guardaddr = (char *)th; |
| 899 | # endif |
| 900 | #else |
| 901 | /* Guardaddr is always set, even if guardsize is 0. This allows |
| 902 | us to compute everything else. */ |
| 903 | # ifdef USE_TLS |
| 904 | size_t stacksize = th->p_stackaddr - guardaddr - guardsize; |
| 905 | # else |
| 906 | size_t stacksize = (char *)(th+1) - guardaddr - guardsize; |
| 907 | # endif |
| 908 | # ifdef NEED_SEPARATE_REGISTER_STACK |
| 909 | /* Take account of the register stack, which is below guardaddr. */ |
| 910 | guardaddr -= stacksize; |
| 911 | stacksize *= 2; |
| 912 | # endif |
| 913 | #endif |
| 914 | /* Unmap the stack. */ |
| 915 | munmap(guardaddr, stacksize + guardsize); |
| 916 | |
| 917 | } |
| 918 | |
| 919 | #ifdef USE_TLS |
| 920 | # if defined(TLS_DTV_AT_TP) |
| 921 | th = (pthread_descr) ((char *) th + TLS_PRE_TCB_SIZE); |
| 922 | # endif |
| 923 | _dl_deallocate_tls (th, true); |
| 924 | #endif |
| 925 | } |
| 926 | |
| 927 | /* Handle threads that have exited */ |
| 928 | |
| 929 | static void pthread_exited(pid_t pid) |
| 930 | { |
| 931 | pthread_descr th; |
| 932 | int detached; |
| 933 | /* Find thread with that pid */ |
| 934 | for (th = __pthread_main_thread->p_nextlive; |
| 935 | th != __pthread_main_thread; |
| 936 | th = th->p_nextlive) { |
| 937 | if (th->p_pid == pid) { |
| 938 | /* Remove thread from list of active threads */ |
| 939 | th->p_nextlive->p_prevlive = th->p_prevlive; |
| 940 | th->p_prevlive->p_nextlive = th->p_nextlive; |
| 941 | /* Mark thread as exited, and if detached, free its resources */ |
| 942 | __pthread_lock(th->p_lock, NULL); |
| 943 | th->p_exited = 1; |
| 944 | /* If we have to signal this event do it now. */ |
| 945 | if (th->p_report_events) |
| 946 | { |
| 947 | /* See whether TD_REAP is in any of the mask. */ |
| 948 | int idx = __td_eventword (TD_REAP); |
| 949 | uint32_t mask = __td_eventmask (TD_REAP); |
| 950 | |
| 951 | if ((mask & (__pthread_threads_events.event_bits[idx] |
| 952 | | th->p_eventbuf.eventmask.event_bits[idx])) != 0) |
| 953 | { |
| 954 | /* Yep, we have to signal the reapage. */ |
| 955 | th->p_eventbuf.eventnum = TD_REAP; |
| 956 | th->p_eventbuf.eventdata = th; |
| 957 | __pthread_last_event = th; |
| 958 | |
| 959 | /* Now call the function to signal the event. */ |
| 960 | __linuxthreads_reap_event(); |
| 961 | } |
| 962 | } |
| 963 | detached = th->p_detached; |
| 964 | __pthread_unlock(th->p_lock); |
| 965 | if (detached) |
| 966 | pthread_free(th); |
| 967 | break; |
| 968 | } |
| 969 | } |
| 970 | /* If all threads have exited and the main thread is pending on a |
| 971 | pthread_exit, wake up the main thread and terminate ourselves. */ |
| 972 | if (main_thread_exiting && |
| 973 | __pthread_main_thread->p_nextlive == __pthread_main_thread) { |
| 974 | restart(__pthread_main_thread); |
| 975 | /* Same logic as REQ_MAIN_THREAD_EXIT. */ |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | static void pthread_reap_children(void) |
| 980 | { |
| 981 | pid_t pid; |
| 982 | int status; |
| 983 | |
| 984 | while ((pid = waitpid_not_cancel(-1, &status, WNOHANG | __WCLONE)) > 0) { |
| 985 | pthread_exited(pid); |
| 986 | if (WIFSIGNALED(status)) { |
| 987 | /* If a thread died due to a signal, send the same signal to |
| 988 | all other threads, including the main thread. */ |
| 989 | pthread_kill_all_threads(WTERMSIG(status), 1); |
| 990 | _exit(0); |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | /* Try to free the resources of a thread when requested by pthread_join |
| 996 | or pthread_detach on a terminated thread. */ |
| 997 | |
| 998 | static void pthread_handle_free(pthread_t th_id) |
| 999 | { |
| 1000 | pthread_handle handle = thread_handle(th_id); |
| 1001 | pthread_descr th; |
| 1002 | |
| 1003 | __pthread_lock(&handle->h_lock, NULL); |
| 1004 | if (nonexisting_handle(handle, th_id)) { |
| 1005 | /* pthread_reap_children has deallocated the thread already, |
| 1006 | nothing needs to be done */ |
| 1007 | __pthread_unlock(&handle->h_lock); |
| 1008 | return; |
| 1009 | } |
| 1010 | th = handle->h_descr; |
| 1011 | if (th->p_exited) { |
| 1012 | __pthread_unlock(&handle->h_lock); |
| 1013 | pthread_free(th); |
| 1014 | } else { |
| 1015 | /* The Unix process of the thread is still running. |
| 1016 | Mark the thread as detached so that the thread manager will |
| 1017 | deallocate its resources when the Unix process exits. */ |
| 1018 | th->p_detached = 1; |
| 1019 | __pthread_unlock(&handle->h_lock); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | /* Send a signal to all running threads */ |
| 1024 | |
| 1025 | static void pthread_kill_all_threads(int sig, int main_thread_also) |
| 1026 | { |
| 1027 | pthread_descr th; |
| 1028 | for (th = __pthread_main_thread->p_nextlive; |
| 1029 | th != __pthread_main_thread; |
| 1030 | th = th->p_nextlive) { |
| 1031 | kill(th->p_pid, sig); |
| 1032 | } |
| 1033 | if (main_thread_also) { |
| 1034 | kill(__pthread_main_thread->p_pid, sig); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | static void pthread_for_each_thread(void *arg, |
| 1039 | void (*fn)(void *, pthread_descr)) |
| 1040 | { |
| 1041 | pthread_descr th; |
| 1042 | |
| 1043 | for (th = __pthread_main_thread->p_nextlive; |
| 1044 | th != __pthread_main_thread; |
| 1045 | th = th->p_nextlive) { |
| 1046 | fn(arg, th); |
| 1047 | } |
| 1048 | |
| 1049 | fn(arg, __pthread_main_thread); |
| 1050 | } |
| 1051 | |
| 1052 | /* Process-wide exit() */ |
| 1053 | |
| 1054 | static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode) |
| 1055 | { |
| 1056 | pthread_descr th; |
| 1057 | __pthread_exit_requested = 1; |
| 1058 | __pthread_exit_code = exitcode; |
| 1059 | /* A forced asynchronous cancellation follows. Make sure we won't |
| 1060 | get stuck later in the main thread with a system lock being held |
| 1061 | by one of the cancelled threads. Ideally one would use the same |
| 1062 | code as in pthread_atfork(), but we can't distinguish system and |
| 1063 | user handlers there. */ |
| 1064 | __flockfilelist(); |
| 1065 | /* Send the CANCEL signal to all running threads, including the main |
| 1066 | thread, but excluding the thread from which the exit request originated |
| 1067 | (that thread must complete the exit, e.g. calling atexit functions |
| 1068 | and flushing stdio buffers). */ |
| 1069 | for (th = issuing_thread->p_nextlive; |
| 1070 | th != issuing_thread; |
| 1071 | th = th->p_nextlive) { |
| 1072 | kill(th->p_pid, __pthread_sig_cancel); |
| 1073 | } |
| 1074 | /* Now, wait for all these threads, so that they don't become zombies |
| 1075 | and their times are properly added to the thread manager's times. */ |
| 1076 | for (th = issuing_thread->p_nextlive; |
| 1077 | th != issuing_thread; |
| 1078 | th = th->p_nextlive) { |
| 1079 | waitpid(th->p_pid, NULL, __WCLONE); |
| 1080 | } |
| 1081 | __fresetlockfiles(); |
| 1082 | restart(issuing_thread); |
| 1083 | _exit(0); |
| 1084 | } |
| 1085 | |
| 1086 | /* Handler for __pthread_sig_cancel in thread manager thread */ |
| 1087 | |
| 1088 | void __pthread_manager_sighandler(int sig) |
| 1089 | { |
| 1090 | int kick_manager = terminated_children == 0 && main_thread_exiting; |
| 1091 | terminated_children = 1; |
| 1092 | |
| 1093 | /* If the main thread is terminating, kick the thread manager loop |
| 1094 | each time some threads terminate. This eliminates a two second |
| 1095 | shutdown delay caused by the thread manager sleeping in the |
| 1096 | call to __poll(). Instead, the thread manager is kicked into |
| 1097 | action, reaps the outstanding threads and resumes the main thread |
| 1098 | so that it can complete the shutdown. */ |
| 1099 | |
| 1100 | if (kick_manager) { |
| 1101 | struct pthread_request request; |
| 1102 | request.req_thread = 0; |
| 1103 | request.req_kind = REQ_KICK; |
| 1104 | TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request, |
| 1105 | (char *) &request, sizeof(request))); |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | /* Adjust priority of thread manager so that it always run at a priority |
| 1110 | higher than all threads */ |
| 1111 | |
| 1112 | void __pthread_manager_adjust_prio(int thread_prio) |
| 1113 | { |
| 1114 | struct sched_param param; |
| 1115 | |
| 1116 | if (thread_prio <= manager_thread->p_priority) return; |
| 1117 | param.sched_priority = |
| 1118 | thread_prio < __sched_get_priority_max(SCHED_FIFO) |
| 1119 | ? thread_prio + 1 : thread_prio; |
| 1120 | __sched_setscheduler(manager_thread->p_pid, SCHED_FIFO, ¶m); |
| 1121 | manager_thread->p_priority = thread_prio; |
| 1122 | } |