1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
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. */
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. */
15 /* Thread cancellation */
21 #include "internals.h"
24 #ifdef __UCLIBC_HAS_RPC__
26 extern void __rpc_thread_destroy(void);
30 int pthread_setcancelstate(int state, int * oldstate)
32 pthread_descr self = thread_self();
33 if (state < PTHREAD_CANCEL_ENABLE || state > PTHREAD_CANCEL_DISABLE)
35 if (oldstate != NULL) *oldstate = THREAD_GETMEM(self, p_cancelstate);
36 THREAD_SETMEM(self, p_cancelstate, state);
37 if (THREAD_GETMEM(self, p_canceled) &&
38 THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE &&
39 THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
40 pthread_exit(PTHREAD_CANCELED);
44 int pthread_setcanceltype(int type, int * oldtype)
46 pthread_descr self = thread_self();
47 if (type < PTHREAD_CANCEL_DEFERRED || type > PTHREAD_CANCEL_ASYNCHRONOUS)
49 if (oldtype != NULL) *oldtype = THREAD_GETMEM(self, p_canceltype);
50 THREAD_SETMEM(self, p_canceltype, type);
51 if (THREAD_GETMEM(self, p_canceled) &&
52 THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE &&
53 THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
54 pthread_exit(PTHREAD_CANCELED);
58 int pthread_cancel(pthread_t thread)
60 pthread_handle handle = thread_handle(thread);
64 pthread_extricate_if *pextricate;
66 __pthread_lock(&handle->h_lock, NULL);
67 if (invalid_handle(handle, thread)) {
68 __pthread_unlock(&handle->h_lock);
75 __pthread_unlock(&handle->h_lock);
79 pextricate = th->p_extricate;
83 /* If the thread has registered an extrication interface, then
84 invoke the interface. If it returns 1, then we succeeded in
85 dequeuing the thread from whatever waiting object it was enqueued
86 with. In that case, it is our responsibility to wake it up.
87 And also to set the p_woken_by_cancel flag so the woken thread
88 can tell that it was woken by cancellation. */
90 if (pextricate != NULL) {
91 dorestart = pextricate->pu_extricate_func(pextricate->pu_object, th);
92 th->p_woken_by_cancel = dorestart;
95 __pthread_unlock(&handle->h_lock);
97 /* If the thread has suspended or is about to, then we unblock it by
98 issuing a restart, instead of a cancel signal. Otherwise we send
99 the cancel signal to unblock the thread from a cancellation point,
100 or to initiate asynchronous cancellation. The restart is needed so
101 we have proper accounting of restarts; suspend decrements the thread's
102 resume count, and restart() increments it. This also means that suspend's
103 handling of the cancel signal is obsolete. */
108 kill(pid, __pthread_sig_cancel);
113 void pthread_testcancel(void)
115 pthread_descr self = thread_self();
116 if (THREAD_GETMEM(self, p_canceled)
117 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)
118 pthread_exit(PTHREAD_CANCELED);
121 void _pthread_cleanup_push(struct _pthread_cleanup_buffer * buffer,
122 void (*routine)(void *), void * arg)
124 pthread_descr self = thread_self();
125 buffer->__routine = routine;
127 buffer->__prev = THREAD_GETMEM(self, p_cleanup);
128 THREAD_SETMEM(self, p_cleanup, buffer);
131 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer * buffer,
134 pthread_descr self = thread_self();
135 if (execute) buffer->__routine(buffer->__arg);
136 THREAD_SETMEM(self, p_cleanup, buffer->__prev);
139 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer * buffer,
140 void (*routine)(void *), void * arg)
142 pthread_descr self = thread_self();
143 buffer->__routine = routine;
145 buffer->__canceltype = THREAD_GETMEM(self, p_canceltype);
146 buffer->__prev = THREAD_GETMEM(self, p_cleanup);
147 THREAD_SETMEM(self, p_canceltype, PTHREAD_CANCEL_DEFERRED);
148 THREAD_SETMEM(self, p_cleanup, buffer);
151 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer * buffer,
154 pthread_descr self = thread_self();
155 if (execute) buffer->__routine(buffer->__arg);
156 THREAD_SETMEM(self, p_cleanup, buffer->__prev);
157 THREAD_SETMEM(self, p_canceltype, buffer->__canceltype);
158 if (THREAD_GETMEM(self, p_canceled) &&
159 THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE &&
160 THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
161 pthread_exit(PTHREAD_CANCELED);
164 void __pthread_perform_cleanup(void)
166 pthread_descr self = thread_self();
167 struct _pthread_cleanup_buffer * c;
168 for (c = THREAD_GETMEM(self, p_cleanup); c != NULL; c = c->__prev)
169 c->__routine(c->__arg);
171 #ifdef __UCLIBC_HAS_RPC__
172 /* And the TSD which needs special help. */
173 if (THREAD_GETMEM(self, p_libc_specific[_LIBC_TSD_KEY_RPC_VARS]) != NULL)
174 __rpc_thread_destroy ();
179 /* We need a hook to force the cancelation wrappers to be linked in when
180 static libpthread is used. */
181 extern const int __pthread_provide_wrappers;
182 static const int * const __pthread_require_wrappers =
183 &__pthread_provide_wrappers;