2 Copyright (C) 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #if defined(__TERMUX__)
24 #include <pt-internal.h>
29 pthread_cancel (pthread_t t)
32 struct pthread_internal_t *p = (struct pthread_internal_t*) t;
36 pthread_mutex_lock (&p->cancel_lock);
37 if (p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_PENDING)
39 pthread_mutex_unlock (&p->cancel_lock);
43 p->attr.flags |= PTHREAD_ATTR_FLAG_CANCEL_PENDING;
45 if (!(p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_ENABLE))
47 pthread_mutex_unlock (&p->cancel_lock);
51 if (p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_ASYNCRONOUS) {
52 pthread_mutex_unlock (&p->cancel_lock);
53 err = __pthread_do_cancel (p);
55 // DEFERRED CANCEL NOT IMPLEMENTED YET
56 pthread_mutex_unlock (&p->cancel_lock);
61 /* Cancel a thread. */
65 #include <pt-internal.h>
74 __pthread_do_cancel (struct pthread_internal_t *p)
77 if(p == (struct pthread_internal_t *)pthread_self())
79 else if(p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_HANDLER)
80 pthread_kill((pthread_t)p, SIGRTMIN);
82 pthread_kill((pthread_t)p, SIGTERM);
90 #include <pt-internal.h>
93 void pthread_cancel_handler(int signum) {
97 void pthread_init(void) {
99 struct pthread_internal_t * p = (struct pthread_internal_t *)pthread_self();
101 if(p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_HANDLER)
104 // set thread status as pthread_create should do.
105 // ASYNCROUNOUS is not set, see pthread_setcancelstate(3)
106 p->attr.flags |= PTHREAD_ATTR_FLAG_CANCEL_HANDLER|PTHREAD_ATTR_FLAG_CANCEL_ENABLE;
108 sa.sa_handler = pthread_cancel_handler;
109 sigemptyset(&(sa.sa_mask));
112 sigaction(SIGRTMIN, &sa, NULL);
114 /* Set the cancel state for the calling thread. */
118 #include <pt-internal.h>
122 pthread_setcancelstate (int state, int *oldstate)
124 struct pthread_internal_t *p = (struct pthread_internal_t*)pthread_self();
133 case PTHREAD_CANCEL_ENABLE:
134 case PTHREAD_CANCEL_DISABLE:
138 pthread_mutex_lock (&p->cancel_lock);
140 *oldstate = p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_ENABLE;
142 if(state == PTHREAD_ATTR_FLAG_CANCEL_ENABLE)
143 p->attr.flags |= PTHREAD_ATTR_FLAG_CANCEL_ENABLE;
145 p->attr.flags &= ~PTHREAD_ATTR_FLAG_CANCEL_ENABLE;
146 newflags=p->attr.flags;
147 pthread_mutex_unlock (&p->cancel_lock);
149 if((newflags & PTHREAD_ATTR_FLAG_CANCEL_PENDING) && (newflags & PTHREAD_ATTR_FLAG_CANCEL_ENABLE) && (newflags & PTHREAD_ATTR_FLAG_CANCEL_ASYNCRONOUS))
150 __pthread_do_cancel(p);
158 #include <pt-internal.h>
162 pthread_setcanceltype (int type, int *oldtype)
164 struct pthread_internal_t *p = (struct pthread_internal_t*)pthread_self();
173 case PTHREAD_CANCEL_DEFERRED:
174 case PTHREAD_CANCEL_ASYNCHRONOUS:
178 pthread_mutex_lock (&p->cancel_lock);
180 *oldtype = p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_ASYNCRONOUS;
182 if(type == PTHREAD_CANCEL_ASYNCHRONOUS)
183 p->attr.flags |= PTHREAD_ATTR_FLAG_CANCEL_ASYNCRONOUS;
185 p->attr.flags &= ~PTHREAD_ATTR_FLAG_CANCEL_ASYNCRONOUS;
186 newflags=p->attr.flags;
187 pthread_mutex_unlock (&p->cancel_lock);
189 if((newflags & PTHREAD_ATTR_FLAG_CANCEL_PENDING) && (newflags & PTHREAD_ATTR_FLAG_CANCEL_ENABLE) && (newflags & PTHREAD_ATTR_FLAG_CANCEL_ASYNCRONOUS))
190 __pthread_do_cancel(p);
194 /* Add an explicit cancelation point. */
198 #include <pt-internal.h>
201 pthread_testcancel (void)
203 struct pthread_internal_t *p = (struct pthread_internal_t*)pthread_self();
208 pthread_mutex_lock (&p->cancel_lock);
209 cancelled = (p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_ENABLE) && (p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_PENDING);
210 pthread_mutex_unlock (&p->cancel_lock);
213 pthread_exit (PTHREAD_CANCELED);