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 cancellation */ |
| 16 | |
| 17 | #define __FORCE_GLIBC |
| 18 | #include <features.h> |
| 19 | #include <errno.h> |
| 20 | #include "pthread.h" |
| 21 | #include "internals.h" |
| 22 | #include "spinlock.h" |
| 23 | #include "restart.h" |
| 24 | #ifdef __UCLIBC_HAS_RPC__ |
| 25 | #include <rpc/rpc.h> |
| 26 | extern void __rpc_thread_destroy(void); |
| 27 | #endif |
| 28 | #include <bits/stackinfo.h> |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | |
| 32 | #ifdef _STACK_GROWS_DOWN |
| 33 | # define FRAME_LEFT(frame, other) ((char *) frame >= (char *) other) |
| 34 | #elif defined _STACK_GROWS_UP |
| 35 | # define FRAME_LEFT(frame, other) ((char *) frame <= (char *) other) |
| 36 | #else |
| 37 | # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP" |
| 38 | #endif |
| 39 | |
| 40 | libpthread_hidden_proto(pthread_setcancelstate) |
| 41 | libpthread_hidden_proto(pthread_setcanceltype) |
| 42 | |
| 43 | int pthread_setcancelstate(int state, int * oldstate) |
| 44 | { |
| 45 | pthread_descr self = thread_self(); |
| 46 | if (state < PTHREAD_CANCEL_ENABLE || state > PTHREAD_CANCEL_DISABLE) |
| 47 | return EINVAL; |
| 48 | if (oldstate != NULL) *oldstate = THREAD_GETMEM(self, p_cancelstate); |
| 49 | THREAD_SETMEM(self, p_cancelstate, state); |
| 50 | if (THREAD_GETMEM(self, p_canceled) && |
| 51 | THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE && |
| 52 | THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS) |
| 53 | __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME); |
| 54 | return 0; |
| 55 | } |
| 56 | libpthread_hidden_def(pthread_setcancelstate) |
| 57 | |
| 58 | int pthread_setcanceltype(int type, int * oldtype) |
| 59 | { |
| 60 | pthread_descr self = thread_self(); |
| 61 | if (type < PTHREAD_CANCEL_DEFERRED || type > PTHREAD_CANCEL_ASYNCHRONOUS) |
| 62 | return EINVAL; |
| 63 | if (oldtype != NULL) *oldtype = THREAD_GETMEM(self, p_canceltype); |
| 64 | THREAD_SETMEM(self, p_canceltype, type); |
| 65 | if (THREAD_GETMEM(self, p_canceled) && |
| 66 | THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE && |
| 67 | THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS) |
| 68 | __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME); |
| 69 | return 0; |
| 70 | } |
| 71 | libpthread_hidden_def(pthread_setcanceltype) |
| 72 | |
| 73 | int pthread_cancel(pthread_t thread) |
| 74 | { |
| 75 | pthread_handle handle = thread_handle(thread); |
| 76 | int pid; |
| 77 | int dorestart = 0; |
| 78 | pthread_descr th; |
| 79 | pthread_extricate_if *pextricate; |
| 80 | int already_canceled; |
| 81 | |
| 82 | __pthread_lock(&handle->h_lock, NULL); |
| 83 | if (invalid_handle(handle, thread)) { |
| 84 | __pthread_unlock(&handle->h_lock); |
| 85 | return ESRCH; |
| 86 | } |
| 87 | |
| 88 | th = handle->h_descr; |
| 89 | |
| 90 | already_canceled = th->p_canceled; |
| 91 | th->p_canceled = 1; |
| 92 | |
| 93 | if (th->p_cancelstate == PTHREAD_CANCEL_DISABLE || already_canceled) { |
| 94 | __pthread_unlock(&handle->h_lock); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | pextricate = th->p_extricate; |
| 99 | pid = th->p_pid; |
| 100 | |
| 101 | /* If the thread has registered an extrication interface, then |
| 102 | invoke the interface. If it returns 1, then we succeeded in |
| 103 | dequeuing the thread from whatever waiting object it was enqueued |
| 104 | with. In that case, it is our responsibility to wake it up. |
| 105 | And also to set the p_woken_by_cancel flag so the woken thread |
| 106 | can tell that it was woken by cancellation. */ |
| 107 | |
| 108 | if (pextricate != NULL) { |
| 109 | dorestart = pextricate->pu_extricate_func(pextricate->pu_object, th); |
| 110 | th->p_woken_by_cancel = dorestart; |
| 111 | } |
| 112 | |
| 113 | __pthread_unlock(&handle->h_lock); |
| 114 | |
| 115 | /* If the thread has suspended or is about to, then we unblock it by |
| 116 | issuing a restart, instead of a cancel signal. Otherwise we send |
| 117 | the cancel signal to unblock the thread from a cancellation point, |
| 118 | or to initiate asynchronous cancellation. The restart is needed so |
| 119 | we have proper accounting of restarts; suspend decrements the thread's |
| 120 | resume count, and restart() increments it. This also means that suspend's |
| 121 | handling of the cancel signal is obsolete. */ |
| 122 | |
| 123 | if (dorestart) |
| 124 | restart(th); |
| 125 | else |
| 126 | kill(pid, __pthread_sig_cancel); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | void pthread_testcancel(void) |
| 132 | { |
| 133 | pthread_descr self = thread_self(); |
| 134 | if (THREAD_GETMEM(self, p_canceled) |
| 135 | && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) |
| 136 | __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME); |
| 137 | } |
| 138 | |
| 139 | void _pthread_cleanup_push(struct _pthread_cleanup_buffer * buffer, |
| 140 | void (*routine)(void *), void * arg) |
| 141 | { |
| 142 | pthread_descr self = thread_self(); |
| 143 | buffer->__routine = routine; |
| 144 | buffer->__arg = arg; |
| 145 | buffer->__prev = THREAD_GETMEM(self, p_cleanup); |
| 146 | if (buffer->__prev != NULL && FRAME_LEFT (buffer, buffer->__prev)) |
| 147 | buffer->__prev = NULL; |
| 148 | THREAD_SETMEM(self, p_cleanup, buffer); |
| 149 | } |
| 150 | |
| 151 | void _pthread_cleanup_pop(struct _pthread_cleanup_buffer * buffer, |
| 152 | int execute) |
| 153 | { |
| 154 | pthread_descr self = thread_self(); |
| 155 | if (execute) buffer->__routine(buffer->__arg); |
| 156 | THREAD_SETMEM(self, p_cleanup, buffer->__prev); |
| 157 | } |
| 158 | |
| 159 | void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer * buffer, |
| 160 | void (*routine)(void *), void * arg) |
| 161 | { |
| 162 | pthread_descr self = thread_self(); |
| 163 | buffer->__routine = routine; |
| 164 | buffer->__arg = arg; |
| 165 | buffer->__canceltype = THREAD_GETMEM(self, p_canceltype); |
| 166 | buffer->__prev = THREAD_GETMEM(self, p_cleanup); |
| 167 | if (buffer->__prev != NULL && FRAME_LEFT (buffer, buffer->__prev)) |
| 168 | buffer->__prev = NULL; |
| 169 | THREAD_SETMEM(self, p_canceltype, PTHREAD_CANCEL_DEFERRED); |
| 170 | THREAD_SETMEM(self, p_cleanup, buffer); |
| 171 | } |
| 172 | strong_alias(_pthread_cleanup_push_defer,__pthread_cleanup_push_defer) |
| 173 | |
| 174 | void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer * buffer, |
| 175 | int execute) |
| 176 | { |
| 177 | pthread_descr self = thread_self(); |
| 178 | if (execute) buffer->__routine(buffer->__arg); |
| 179 | THREAD_SETMEM(self, p_cleanup, buffer->__prev); |
| 180 | THREAD_SETMEM(self, p_canceltype, buffer->__canceltype); |
| 181 | if (THREAD_GETMEM(self, p_canceled) && |
| 182 | THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE && |
| 183 | THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS) |
| 184 | __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME); |
| 185 | } |
| 186 | strong_alias(_pthread_cleanup_pop_restore,__pthread_cleanup_pop_restore) |
| 187 | |
| 188 | |
| 189 | void __pthread_perform_cleanup(char *currentframe) |
| 190 | { |
| 191 | pthread_descr self = thread_self(); |
| 192 | struct _pthread_cleanup_buffer * c; |
| 193 | |
| 194 | for (c = THREAD_GETMEM(self, p_cleanup); c != NULL; c = c->__prev) |
| 195 | { |
| 196 | #ifdef _STACK_GROWS_DOWN |
| 197 | if ((char *) c <= currentframe) |
| 198 | break; |
| 199 | #elif defined _STACK_GROWS_UP |
| 200 | if ((char *) c >= currentframe) |
| 201 | break; |
| 202 | #else |
| 203 | # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP" |
| 204 | #endif |
| 205 | c->__routine(c->__arg); |
| 206 | } |
| 207 | |
| 208 | #ifdef __UCLIBC_HAS_RPC__ |
| 209 | /* And the TSD which needs special help. */ |
| 210 | if (THREAD_GETMEM(self, p_libc_specific[_LIBC_TSD_KEY_RPC_VARS]) != NULL) |
| 211 | __rpc_thread_destroy (); |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | #ifndef __PIC__ |
| 216 | /* We need a hook to force the cancellation wrappers to be linked in when |
| 217 | static libpthread is used. */ |
| 218 | extern const char __pthread_provide_wrappers; |
| 219 | static const char *const __pthread_require_wrappers = |
| 220 | &__pthread_provide_wrappers; |
| 221 | #endif |