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