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 */
20 #include "internals.h"
23 #include <stackinfo.h>
27 int pthread_setcancelstate(int state
, int * oldstate
)
29 pthread_descr self
= thread_self();
30 if (state
< PTHREAD_CANCEL_ENABLE
|| state
> PTHREAD_CANCEL_DISABLE
)
32 if (oldstate
!= NULL
) *oldstate
= THREAD_GETMEM(self
, p_cancelstate
);
33 THREAD_SETMEM(self
, p_cancelstate
, state
);
34 if (THREAD_GETMEM(self
, p_canceled
) &&
35 THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
&&
36 THREAD_GETMEM(self
, p_canceltype
) == PTHREAD_CANCEL_ASYNCHRONOUS
)
37 __pthread_do_exit(PTHREAD_CANCELED
, CURRENT_STACK_FRAME
);
41 int pthread_setcanceltype(int type
, int * oldtype
)
43 pthread_descr self
= thread_self();
44 if (type
< PTHREAD_CANCEL_DEFERRED
|| type
> PTHREAD_CANCEL_ASYNCHRONOUS
)
46 if (oldtype
!= NULL
) *oldtype
= THREAD_GETMEM(self
, p_canceltype
);
47 THREAD_SETMEM(self
, p_canceltype
, type
);
48 if (THREAD_GETMEM(self
, p_canceled
) &&
49 THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
&&
50 THREAD_GETMEM(self
, p_canceltype
) == PTHREAD_CANCEL_ASYNCHRONOUS
)
51 __pthread_do_exit(PTHREAD_CANCELED
, CURRENT_STACK_FRAME
);
55 int pthread_cancel(pthread_t thread
)
57 pthread_handle handle
= thread_handle(thread
);
61 pthread_extricate_if
*pextricate
;
64 __pthread_lock(&handle
->h_lock
, NULL
);
65 if (invalid_handle(handle
, thread
)) {
66 __pthread_unlock(&handle
->h_lock
);
72 already_canceled
= th
->p_canceled
;
75 if (th
->p_cancelstate
== PTHREAD_CANCEL_DISABLE
|| already_canceled
) {
76 __pthread_unlock(&handle
->h_lock
);
80 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_do_exit(PTHREAD_CANCELED
, CURRENT_STACK_FRAME
);
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_do_exit(PTHREAD_CANCELED
, CURRENT_STACK_FRAME
);
164 void __pthread_perform_cleanup(char *currentframe
)
166 pthread_descr self
= thread_self();
167 struct _pthread_cleanup_buffer
* c
;
169 for (c
= THREAD_GETMEM(self
, p_cleanup
); c
!= NULL
; c
= c
->__prev
)
171 #if _STACK_GROWS_DOWN
172 if ((char *) c
<= currentframe
)
174 #elif _STACK_GROWS_UP
175 if ((char *) c
>= currentframe
)
178 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
180 c
->__routine(c
->__arg
);
183 /* And the TSD which needs special help. */
184 if (THREAD_GETMEM(self
, p_libc_specific
[_LIBC_TSD_KEY_RPC_VARS
]) != NULL
)
185 __rpc_thread_destroy ();
189 /* We need a hook to force the cancelation wrappers to be linked in when
190 static libpthread is used. */
191 extern const int __pthread_provide_wrappers
;
192 static const int * const __pthread_require_wrappers
=
193 &__pthread_provide_wrappers
;