initial commit
[goodguy/history.git] / cinelerra-5.0 / guicast / thread.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * 
20  */
21
22 #ifndef NO_GUICAST
23 #include "bcsignals.h"
24 #endif
25 #include <sys/wait.h>
26 #include <sched.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include "thread.h"
32
33 // track unjoined threads at termination
34 #ifndef NO_GUICAST
35 #include "arraylist.h"
36 #include "mutex.h"
37 #include <typeinfo>
38
39 class MLocker {
40         static Mutex the_lock;
41 public:
42         MLocker() { the_lock.lock(); }
43         ~MLocker() { the_lock.unlock(); }
44 };
45 Mutex MLocker::the_lock;
46
47 class the_dbg {
48 public:
49         pthread_t tid;  const char *name;
50         the_dbg(pthread_t t, const char *nm) { tid = t; name = nm; }
51         ~the_dbg() {}
52 };
53
54
55 static class the_list : public ArrayList<the_dbg*> {
56 public:
57         static void dump_threads(FILE *fp);
58          the_list() {}
59         ~the_list() {
60                 MLocker mlkr;
61                 remove_all_objects();
62         }
63 } thread_list;
64
65 static void dbg_add(pthread_t tid, const char *nm)
66 {
67         MLocker mlkr;
68         int i = thread_list.size();
69         while( --i >= 0 && thread_list[i]->tid != tid );
70         if( i >= 0 ) {
71                 printf("dbg_add, dup %016lx %s %s\n",
72                         (unsigned long)tid, nm, thread_list[i]->name);
73                 return;
74         }
75         thread_list.append(new the_dbg(tid, nm));
76 }
77
78 static void dbg_del(pthread_t tid)
79 {
80         MLocker mlkr;
81         int i = thread_list.size();
82         while( --i >= 0 && thread_list[i]->tid != tid );
83         if( i < 0 ) {
84                 printf("dbg_del, mis %016lx\n",(unsigned long)tid);
85                 return;
86         }
87         thread_list.remove_object_number(i);
88 }
89
90 static class the_chkr {
91 public:
92         the_chkr() {}
93         ~the_chkr() {
94                 int i = thread_list.size();
95                 if( !i ) return;
96                 printf("unjoined tids %d\n", i);
97                 while( --i >= 0 ) printf("  %016lx %s\n",
98                         (unsigned long)thread_list[i]->tid,
99                         thread_list[i]->name);
100         }
101 } the_chk;
102 #else
103 #define dbg_add(t, nm) do {} while(0)
104 #define dbg_del(t) do {} while(0)
105 #endif
106
107 Thread::Thread(int synchronous, int realtime, int autodelete)
108 {
109         this->synchronous = synchronous != 0;
110         this->realtime = realtime != 0;
111         this->autodelete = autodelete != 0;
112         tid = (pthread_t)-1;
113         finished = false;
114         cancelled = false;
115         cancel_enabled = false;
116 }
117
118 Thread::~Thread()
119 {
120 }
121
122 void* Thread::entrypoint(void *parameters)
123 {
124         Thread *thread = (Thread*)parameters;
125
126 // allow thread to be cancelled at any point during a region where it is enabled.
127         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
128 // Disable cancellation by default.
129         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
130         thread->cancel_enabled = false;
131
132 // Set realtime here seince it doesn't work in start
133         if( thread->realtime && getuid() == 0 ) {
134                 struct sched_param param = { sched_priority : 1 };
135                 if(pthread_setschedparam(thread->tid, SCHED_RR, &param) < 0)
136                         perror("Thread::entrypoint pthread_attr_setschedpolicy");
137         }
138
139         thread->run();
140         thread->finished = true;
141         if( !thread->synchronous ) {
142                 if( !thread->cancelled ) dbg_del(thread->tid);
143                 if( thread->autodelete ) delete thread;
144                 else thread->tid = ((pthread_t)-1);
145         }
146         return NULL;
147 }
148
149 void Thread::start()
150 {
151         pthread_attr_t  attr;
152         struct sched_param param;
153
154         pthread_attr_init(&attr);
155
156 // previously run, and did not join, join to clean up zombie
157         if( synchronous && exists() )
158                 join();
159
160         finished = false;
161         cancelled = false;
162         owner = get_self();
163
164 // Inherit realtime from current thread the easy way.
165         if( !realtime )
166                 realtime = calculate_realtime();
167
168         if( !synchronous )
169                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
170
171         if( realtime && getuid() == 0 ) {
172                 if(pthread_attr_setschedpolicy(&attr, SCHED_RR) < 0)
173                         perror("Thread::start pthread_attr_setschedpolicy");
174                 param.sched_priority = 50;
175                 if(pthread_attr_setschedparam(&attr, &param) < 0)
176                         perror("Thread::start pthread_attr_setschedparam");
177         }
178         else {
179                 if(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED) < 0)
180                         perror("Thread::start pthread_attr_setinheritsched");
181         }
182
183         pthread_create(&tid, &attr, Thread::entrypoint, this);
184
185         dbg_add(tid, typeid(*this).name());
186 }
187
188 int Thread::cancel()
189 {
190         if( exists() && !cancelled ) {
191                 LOCK_LOCKS("Thread::cancel");
192                 pthread_cancel(tid);
193                 cancelled = true;
194                 if( !synchronous ) dbg_del(tid);
195                 UNLOCK_LOCKS;
196         }
197         return 0;
198 }
199
200 int Thread::join()   // join this thread
201 {
202         if( !exists() ) return 0;
203         if( synchronous ) {
204 // NOTE: this does not do anything if the thread is not synchronous
205                 int ret = pthread_join(tid, 0);
206                 if( ret ) strerror(ret);
207                 CLEAR_LOCKS_TID(tid);
208                 dbg_del(tid);
209                 tid = ((pthread_t)-1);
210 // Don't execute anything after this.
211                 if( autodelete ) delete this;
212         }
213         else {
214 // kludge
215                 while( running() && !cancelled ) {
216                         int ret = pthread_kill(tid, 0);
217                         if( ret ) break;
218                         usleep(200000);
219                 }
220                 tid = ((pthread_t)-1);
221         }
222         return 0;
223 }
224
225 int Thread::enable_cancel()
226 {
227         if( !cancel_enabled ) {
228                 cancel_enabled = true;
229                 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
230         }
231         return 0;
232 }
233
234 int Thread::disable_cancel()
235 {
236         if( cancel_enabled ) {
237                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
238                 cancel_enabled = false;
239         }
240         return 0;
241 }
242
243 int Thread::get_cancel_enabled()
244 {
245         return cancel_enabled;
246 }
247
248 void Thread::exit_thread()
249 {
250         finished = true;
251         pthread_exit(0);
252 }
253
254
255 int Thread::suspend_thread()
256 {
257         if( exists() )
258                 pthread_kill(tid, SIGSTOP);
259         return 0;
260 }
261
262 int Thread::continue_thread()
263 {
264         if( exists() )
265                 pthread_kill(tid, SIGCONT);
266         return 0;
267 }
268
269 bool Thread::exists()
270 {
271         return tid != ((pthread_t)-1);
272 }
273
274 bool Thread::running()
275 {
276         return exists() & ~finished;
277 }
278
279 int Thread::set_synchronous(int value)
280 {
281         this->synchronous = value != 0;
282         return 0;
283 }
284
285 int Thread::set_realtime(int value)
286 {
287         this->realtime = value != 0;
288         return 0;
289 }
290
291 int Thread::set_autodelete(int value)
292 {
293         this->autodelete = value != 0;
294         return 0;
295 }
296
297 int Thread::get_autodelete()
298 {
299         return autodelete ? 1 : 0;
300 }
301
302 int Thread::get_synchronous()
303 {
304         return synchronous ? 1 : 0;
305 }
306
307 bool Thread::calculate_realtime()
308 {
309 //printf("Thread::calculate_realtime %d %d\n", getpid(), sched_getscheduler(0));
310         return (sched_getscheduler(0) == SCHED_RR ||
311                 sched_getscheduler(0) == SCHED_FIFO);
312 }
313
314 int Thread::get_realtime()
315 {
316         return realtime ? 1 : 0;
317 }
318
319 unsigned long Thread::get_tid()
320 {
321         return tid;
322 }
323
324 void Thread::dump_threads(FILE *fp)
325 {
326         int i = thread_list.size();
327         while( --i >= 0 ) {
328                 fprintf(fp, "thread %016lx, %s\n",
329                         (unsigned long)thread_list[i]->tid,
330                         thread_list[i]->name);
331         }
332 }
333