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