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 | /* Thread termination and joining */ |
| 16 | |
| 17 | #include <features.h> |
| 18 | #include <errno.h> |
| 19 | #include <sched.h> |
| 20 | #include <unistd.h> |
| 21 | #include <stdlib.h> |
| 22 | #include "pthread.h" |
| 23 | #include "internals.h" |
| 24 | #include "spinlock.h" |
| 25 | #include "restart.h" |
| 26 | #include "debug.h" /* PDEBUG, added by StS */ |
| 27 | |
| 28 | libpthread_hidden_proto(pthread_exit) |
| 29 | void pthread_exit(void * retval) |
| 30 | { |
| 31 | __pthread_do_exit (retval, CURRENT_STACK_FRAME); |
| 32 | } |
| 33 | libpthread_hidden_def (pthread_exit) |
| 34 | |
| 35 | void __pthread_do_exit(void *retval, char *currentframe) |
| 36 | { |
| 37 | pthread_descr self = thread_self(); |
| 38 | pthread_descr joining; |
| 39 | struct pthread_request request; |
| 40 | PDEBUG("self=%p, pid=%d\n", self, self->p_pid); |
| 41 | |
| 42 | /* obey POSIX behavior and prevent cancellation functions from |
| 43 | * being called more than once. |
| 44 | * http://sourceware.org/ml/libc-ports/2006-10/msg00043.html |
| 45 | */ |
| 46 | THREAD_SETMEM(self, p_cancelstate, PTHREAD_CANCEL_DISABLE); |
| 47 | THREAD_SETMEM(self, p_canceltype, PTHREAD_CANCEL_DEFERRED); |
| 48 | |
| 49 | /* Call cleanup functions and destroy the thread-specific data */ |
| 50 | __pthread_perform_cleanup(currentframe); |
| 51 | __pthread_destroy_specifics(); |
| 52 | /* Store return value */ |
| 53 | __pthread_lock(THREAD_GETMEM(self, p_lock), self); |
| 54 | THREAD_SETMEM(self, p_retval, retval); |
| 55 | /* See whether we have to signal the death. */ |
| 56 | if (THREAD_GETMEM(self, p_report_events)) |
| 57 | { |
| 58 | /* See whether TD_DEATH is in any of the mask. */ |
| 59 | int idx = __td_eventword (TD_DEATH); |
| 60 | uint32_t mask = __td_eventmask (TD_DEATH); |
| 61 | |
| 62 | if ((mask & (__pthread_threads_events.event_bits[idx] |
| 63 | | THREAD_GETMEM_NC(self, |
| 64 | p_eventbuf.eventmask).event_bits[idx])) |
| 65 | != 0) |
| 66 | { |
| 67 | /* Yep, we have to signal the death. */ |
| 68 | THREAD_SETMEM(self, p_eventbuf.eventnum, TD_DEATH); |
| 69 | THREAD_SETMEM(self, p_eventbuf.eventdata, self); |
| 70 | __pthread_last_event = self; |
| 71 | |
| 72 | /* Now call the function to signal the event. */ |
| 73 | __linuxthreads_death_event(); |
| 74 | } |
| 75 | } |
| 76 | /* Say that we've terminated */ |
| 77 | THREAD_SETMEM(self, p_terminated, 1); |
| 78 | /* See if someone is joining on us */ |
| 79 | joining = THREAD_GETMEM(self, p_joining); |
| 80 | PDEBUG("joining = %p, pid=%d\n", joining, joining ? joining->p_pid : 0); |
| 81 | __pthread_unlock(THREAD_GETMEM(self, p_lock)); |
| 82 | /* Restart joining thread if any */ |
| 83 | if (joining != NULL) restart(joining); |
| 84 | /* If this is the initial thread, block until all threads have terminated. |
| 85 | If another thread calls exit, we'll be terminated from our signal |
| 86 | handler. */ |
| 87 | if (self == __pthread_main_thread && __pthread_manager_request >= 0) { |
| 88 | request.req_thread = self; |
| 89 | request.req_kind = REQ_MAIN_THREAD_EXIT; |
| 90 | TEMP_FAILURE_RETRY(write(__pthread_manager_request, |
| 91 | (char *)&request, sizeof(request))); |
| 92 | suspend(self); |
| 93 | /* Main thread flushes stdio streams and runs atexit functions. |
| 94 | * It also calls a handler within LinuxThreads which sends a process exit |
| 95 | * request to the thread manager. */ |
| 96 | exit(0); |
| 97 | } |
| 98 | /* Exit the process (but don't flush stdio streams, and don't run |
| 99 | atexit functions). */ |
| 100 | _exit(0); |
| 101 | } |
| 102 | |
| 103 | /* Function called by pthread_cancel to remove the thread from |
| 104 | waiting on a condition variable queue. */ |
| 105 | |
| 106 | static int join_extricate_func(void *obj, pthread_descr th attribute_unused) |
| 107 | { |
| 108 | volatile pthread_descr self = thread_self(); |
| 109 | pthread_handle handle = obj; |
| 110 | pthread_descr jo; |
| 111 | int did_remove = 0; |
| 112 | |
| 113 | __pthread_lock(&handle->h_lock, self); |
| 114 | jo = handle->h_descr; |
| 115 | did_remove = jo->p_joining != NULL; |
| 116 | jo->p_joining = NULL; |
| 117 | __pthread_unlock(&handle->h_lock); |
| 118 | |
| 119 | return did_remove; |
| 120 | } |
| 121 | |
| 122 | int pthread_join(pthread_t thread_id, void ** thread_return) |
| 123 | { |
| 124 | volatile pthread_descr self = thread_self(); |
| 125 | struct pthread_request request; |
| 126 | pthread_handle handle = thread_handle(thread_id); |
| 127 | pthread_descr th; |
| 128 | pthread_extricate_if extr; |
| 129 | int already_canceled = 0; |
| 130 | PDEBUG("\n"); |
| 131 | |
| 132 | /* Set up extrication interface */ |
| 133 | extr.pu_object = handle; |
| 134 | extr.pu_extricate_func = join_extricate_func; |
| 135 | |
| 136 | __pthread_lock(&handle->h_lock, self); |
| 137 | if (invalid_handle(handle, thread_id)) { |
| 138 | __pthread_unlock(&handle->h_lock); |
| 139 | return ESRCH; |
| 140 | } |
| 141 | th = handle->h_descr; |
| 142 | if (th == self) { |
| 143 | __pthread_unlock(&handle->h_lock); |
| 144 | return EDEADLK; |
| 145 | } |
| 146 | /* If detached or already joined, error */ |
| 147 | if (th->p_detached || th->p_joining != NULL) { |
| 148 | __pthread_unlock(&handle->h_lock); |
| 149 | return EINVAL; |
| 150 | } |
| 151 | /* If not terminated yet, suspend ourselves. */ |
| 152 | if (! th->p_terminated) { |
| 153 | /* Register extrication interface */ |
| 154 | __pthread_set_own_extricate_if(self, &extr); |
| 155 | if (!(THREAD_GETMEM(self, p_canceled) |
| 156 | && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)) |
| 157 | th->p_joining = self; |
| 158 | else |
| 159 | already_canceled = 1; |
| 160 | __pthread_unlock(&handle->h_lock); |
| 161 | |
| 162 | if (already_canceled) { |
| 163 | __pthread_set_own_extricate_if(self, 0); |
| 164 | __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME); |
| 165 | } |
| 166 | |
| 167 | PDEBUG("before suspend\n"); |
| 168 | suspend(self); |
| 169 | PDEBUG("after suspend\n"); |
| 170 | /* Deregister extrication interface */ |
| 171 | __pthread_set_own_extricate_if(self, 0); |
| 172 | |
| 173 | /* This is a cancellation point */ |
| 174 | if (THREAD_GETMEM(self, p_woken_by_cancel) |
| 175 | && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) { |
| 176 | THREAD_SETMEM(self, p_woken_by_cancel, 0); |
| 177 | __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME); |
| 178 | } |
| 179 | __pthread_lock(&handle->h_lock, self); |
| 180 | } |
| 181 | /* Get return value */ |
| 182 | if (thread_return != NULL) *thread_return = th->p_retval; |
| 183 | __pthread_unlock(&handle->h_lock); |
| 184 | /* Send notification to thread manager */ |
| 185 | if (__pthread_manager_request >= 0) { |
| 186 | request.req_thread = self; |
| 187 | request.req_kind = REQ_FREE; |
| 188 | request.req_args.free.thread_id = thread_id; |
| 189 | TEMP_FAILURE_RETRY(write(__pthread_manager_request, |
| 190 | (char *) &request, sizeof(request))); |
| 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | int pthread_detach(pthread_t thread_id) |
| 196 | { |
| 197 | int terminated; |
| 198 | struct pthread_request request; |
| 199 | pthread_handle handle = thread_handle(thread_id); |
| 200 | pthread_descr th; |
| 201 | |
| 202 | __pthread_lock(&handle->h_lock, NULL); |
| 203 | if (invalid_handle(handle, thread_id)) { |
| 204 | __pthread_unlock(&handle->h_lock); |
| 205 | return ESRCH; |
| 206 | } |
| 207 | th = handle->h_descr; |
| 208 | /* If already detached, error */ |
| 209 | if (th->p_detached) { |
| 210 | __pthread_unlock(&handle->h_lock); |
| 211 | return EINVAL; |
| 212 | } |
| 213 | /* If already joining, don't do anything. */ |
| 214 | if (th->p_joining != NULL) { |
| 215 | __pthread_unlock(&handle->h_lock); |
| 216 | return 0; |
| 217 | } |
| 218 | /* Mark as detached */ |
| 219 | th->p_detached = 1; |
| 220 | terminated = th->p_terminated; |
| 221 | __pthread_unlock(&handle->h_lock); |
| 222 | /* If already terminated, notify thread manager to reclaim resources */ |
| 223 | if (terminated && __pthread_manager_request >= 0) { |
| 224 | request.req_thread = thread_self(); |
| 225 | request.req_kind = REQ_FREE; |
| 226 | request.req_args.free.thread_id = thread_id; |
| 227 | TEMP_FAILURE_RETRY(write(__pthread_manager_request, |
| 228 | (char *) &request, sizeof(request))); |
| 229 | } |
| 230 | return 0; |
| 231 | } |