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