no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / libbthread-master / just-pt.cc
1 /* Cancel a thread.
2    Copyright (C) 2002 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
9
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.
14
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.  */
19 #include <stdio.h>
20 #if defined(__TERMUX__)
21
22 #include <pthread.h>
23
24 #include <pt-internal.h>
25
26 #include <errno.h>
27
28 int
29 pthread_cancel (pthread_t t)
30 {
31   int err = 0;
32   struct pthread_internal_t *p = (struct pthread_internal_t*) t;
33         
34         pthread_init();
35         
36   pthread_mutex_lock (&p->cancel_lock);
37   if (p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_PENDING)
38     {
39       pthread_mutex_unlock (&p->cancel_lock);
40       return 0;
41     }
42     
43   p->attr.flags |= PTHREAD_ATTR_FLAG_CANCEL_PENDING;
44
45   if (!(p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_ENABLE))
46     {
47       pthread_mutex_unlock (&p->cancel_lock);
48       return 0;
49     }
50
51   if (p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_ASYNCRONOUS) {
52                 pthread_mutex_unlock (&p->cancel_lock);
53     err = __pthread_do_cancel (p);
54         } else {
55                 // DEFERRED CANCEL NOT IMPLEMENTED YET
56                 pthread_mutex_unlock (&p->cancel_lock);
57         }
58
59   return err;
60 }
61 /* Cancel a thread. */
62
63 #include <pthread.h>
64
65 #include <pt-internal.h>
66
67 static void
68 call_exit (void)
69 {
70   pthread_exit (0);
71 }
72
73 int
74 __pthread_do_cancel (struct pthread_internal_t *p)
75 {
76         
77         if(p == (struct pthread_internal_t *)pthread_self())
78     call_exit ();
79   else if(p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_HANDLER)
80     pthread_kill((pthread_t)p, SIGRTMIN);
81         else
82                 pthread_kill((pthread_t)p, SIGTERM);
83
84   return 0;
85 }
86 /* Init a thread. */
87
88 #include <pthread.h>
89 #include <bthread.h>
90 #include <pt-internal.h>
91 #include <signal.h>
92
93 void pthread_cancel_handler(int signum) {
94         pthread_exit(0);
95 }
96
97 void pthread_init(void) {
98         struct sigaction sa;
99         struct pthread_internal_t * p = (struct pthread_internal_t *)pthread_self();
100         
101         if(p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_HANDLER)
102                 return;
103         
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;
107         
108         sa.sa_handler = pthread_cancel_handler;
109         sigemptyset(&(sa.sa_mask));
110         sa.sa_flags = 0;
111         
112         sigaction(SIGRTMIN, &sa, NULL);
113 }
114 /* Set the cancel state for the calling thread.  */
115
116 #include <pthread.h>
117 #include <bthread.h>
118 #include <pt-internal.h>
119 #include <errno.h>
120
121 int
122 pthread_setcancelstate (int state, int *oldstate)
123 {
124   struct pthread_internal_t *p = (struct pthread_internal_t*)pthread_self();
125         int newflags;
126
127         pthread_init();
128         
129   switch (state)
130     {
131     default:
132       return EINVAL;
133     case PTHREAD_CANCEL_ENABLE:
134     case PTHREAD_CANCEL_DISABLE:
135       break;
136     }
137
138   pthread_mutex_lock (&p->cancel_lock);
139   if (oldstate)
140     *oldstate = p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_ENABLE;
141   
142         if(state == PTHREAD_ATTR_FLAG_CANCEL_ENABLE)
143                 p->attr.flags |= PTHREAD_ATTR_FLAG_CANCEL_ENABLE;
144         else
145                 p->attr.flags &= ~PTHREAD_ATTR_FLAG_CANCEL_ENABLE;
146         newflags=p->attr.flags;
147   pthread_mutex_unlock (&p->cancel_lock);
148
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);
151         
152   return 0;
153 }
154 /*   */
155
156 #include <pthread.h>
157 #include <bthread.h>
158 #include <pt-internal.h>
159 #include <errno.h>
160
161 int
162 pthread_setcanceltype (int type, int *oldtype)
163 {
164   struct pthread_internal_t *p = (struct pthread_internal_t*)pthread_self();
165         int newflags;
166         
167         pthread_init();
168         
169   switch (type)
170     {
171     default:
172       return EINVAL;
173     case PTHREAD_CANCEL_DEFERRED:
174     case PTHREAD_CANCEL_ASYNCHRONOUS:
175       break;
176     }
177
178   pthread_mutex_lock (&p->cancel_lock);
179   if (oldtype)
180     *oldtype = p->attr.flags & PTHREAD_ATTR_FLAG_CANCEL_ASYNCRONOUS;
181         
182         if(type == PTHREAD_CANCEL_ASYNCHRONOUS)
183                 p->attr.flags |= PTHREAD_ATTR_FLAG_CANCEL_ASYNCRONOUS;
184         else
185                 p->attr.flags &= ~PTHREAD_ATTR_FLAG_CANCEL_ASYNCRONOUS;
186         newflags=p->attr.flags;
187   pthread_mutex_unlock (&p->cancel_lock);
188
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);
191         
192   return 0;
193 }
194 /* Add an explicit cancelation point.  */
195
196 #include <pthread.h>
197 #include <bthread.h>
198 #include <pt-internal.h>
199
200 void
201 pthread_testcancel (void)
202 {
203   struct pthread_internal_t *p = (struct pthread_internal_t*)pthread_self();
204   int cancelled;
205         
206         pthread_init();
207
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);
211
212   if (cancelled)
213     pthread_exit (PTHREAD_CANCELED);
214                 
215 }
216 #endif