additional Andrew provided Termux mods +
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / thread.h
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 THREAD_H
23 #define THREAD_H
24
25 #include <stdio.h>
26 #include <pthread.h>
27 #include <unistd.h>
28
29 #if !defined(__TERMUX__)
30
31 #ifndef NO_TID
32 #include <sys/syscall.h>
33
34 // glibc >= 2.30 provides gettid() in unistd
35 #if !defined(__GLIBC_PREREQ) || !__GLIBC_PREREQ(2, 30)
36 static inline int gettid() { return syscall(SYS_gettid, 0, 0, 0); }
37 #endif
38 #else
39 static inline long gettid() { return (long)pthread_self(); }
40 #endif
41
42 #endif
43
44 // The thread does not autodelete by default.
45 // If autodelete is 1 the thread autodeletes.
46 // If it's synchronous the deletion occurs in join().
47 // If it's asynchronous the deletion occurs in entrypoint.
48
49
50 class Thread
51 {
52         static void* entrypoint(void *parameters);
53
54 protected:
55         virtual void run() = 0;
56 public:
57         Thread(int synchronous = 0, int realtime = 0, int autodelete = 0);
58         virtual ~Thread();
59         void start();
60         int cancel();            // end this thread
61         int join();              // join this thread
62         int suspend_thread();    // suspend this thread
63         int continue_thread();   // continue this thread
64         void exit_thread();      // exit this thread
65         int enable_cancel();
66         int disable_cancel();
67         int get_cancel_enabled();
68         bool exists() { return tid != ((pthread_t)-1); }
69         bool running() { return exists() && !finished; }
70
71         int get_synchronous();
72         int set_synchronous(int value);
73         int get_realtime();
74         int set_realtime(int value = 1);
75         int get_autodelete();
76         int set_autodelete(int value);
77 // Return realtime variable
78 // Return 1 if querying the kernel returned a realtime policy
79         static bool calculate_realtime();
80         unsigned long get_tid();
81         static pthread_t get_self() { return pthread_self(); }
82         static void yield() { sched_yield(); }
83         static void dump_threads(FILE *fp=stdout);
84
85 private:
86         bool synchronous;        // force join() to end
87         bool realtime;           // schedule realtime
88         bool autodelete;         // autodelete when run() finishes
89         bool finished;
90         bool cancel_enabled, cancelled;
91         pthread_t owner, tid;
92 };
93
94 #endif