r801: Check for Freetype 2.
[cinelerra_cv/ct.git] / guicast / thread.C
blob6ae35a3f1b2c998469f75142ad052717f2801593
1 #include "bcsignals.h"
2 #include <sys/wait.h>
3 #include <sched.h>
4 #include <signal.h>
5 #include <stdio.h>
6 #include <unistd.h>
7 #include "thread.h"
10 Thread::Thread(int synchronous, int realtime, int autodelete)
12         this->synchronous = synchronous;
13         this->realtime = realtime;
14         this->autodelete = autodelete;
15         tid = (pthread_t)-1;
16         tid_valid = 0;
17         thread_running = 0;
18         cancel_enabled = 0;
21 Thread::~Thread()
25 void* Thread::entrypoint(void *parameters)
27         Thread *thread = (Thread*)parameters;
29 // allow thread to be cancelled at any point during a region where it is enabled.
30         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
31 // Disable cancellation by default.
32         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
33         thread->cancel_enabled = 0;
35 // Set realtime here seince it doesn't work in start
36         if(thread->realtime && getuid() == 0)
37         {
38                 struct sched_param param = 
39                 {
40                         sched_priority : 1
41                 };
42                 if(pthread_setschedparam(thread->tid, SCHED_RR, &param) < 0)
43                         perror("Thread::entrypoint pthread_attr_setschedpolicy");
44         }
46         thread->run();
48         thread->thread_running = 0;
50         if(thread->autodelete && !thread->synchronous) delete thread;
51         return NULL;
54 void Thread::start()
56         pthread_attr_t  attr;
57         struct sched_param param;
59         pthread_attr_init(&attr);
61         thread_running = 1;
63 // Inherit realtime from current thread the easy way.
64         if(!realtime) realtime = calculate_realtime();
67         if(!synchronous) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
69         if(realtime && getuid() == 0)
70         {
71                 if(pthread_attr_setschedpolicy(&attr, SCHED_RR) < 0)
72                         perror("Thread::start pthread_attr_setschedpolicy");
73                 param.sched_priority = 50;
74                 if(pthread_attr_setschedparam(&attr, &param) < 0)
75                         perror("Thread::start pthread_attr_setschedparam");
76         }
77         else
78         {
79                 if(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED) < 0)
80                         perror("Thread::start pthread_attr_setinheritsched");
81         }
83         pthread_create(&tid, &attr, Thread::entrypoint, this);
84         tid_valid = 1;
87 int Thread::end(pthread_t tid)           // need to join after this if synchronous
89         if(tid_valid)
90         {
91                 pthread_cancel(tid);
92         }
93         return 0;
96 int Thread::end()           // need to join after this if synchronous
98         cancel();
99         return 0;
102 int Thread::cancel()
104         if(tid_valid) pthread_cancel(tid);
105         if(!synchronous)
106         {
107                 tid = (pthread_t)-1;
108                 tid_valid = 0;
109         }
110         return 0;
113 int Thread::join()   // join this thread
115         int result = 0;
116         if(tid_valid)
117         {
118                 result = pthread_join(tid, 0);
119         }
121         tid = (pthread_t)-1;
122         tid_valid = 0;
124 // Don't execute anything after this.
125         if(autodelete && synchronous) delete this;
126         return 0;
129 int Thread::enable_cancel()
131         cancel_enabled = 1;
132         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
133         return 0;
136 int Thread::disable_cancel()
138         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
139         cancel_enabled = 0;
140         return 0;
143 int Thread::get_cancel_enabled()
145         return cancel_enabled;
148 int Thread::exit_thread()
150         pthread_exit(0);
151         if(!synchronous)
152         {
153                 tid = (pthread_t)-1;
154                 tid_valid = 0;
155         }
156         return 0;
160 int Thread::suspend_thread()
162         if(tid_valid) pthread_kill(tid, SIGSTOP);
163         return 0;
166 int Thread::continue_thread()
168         if(tid_valid) pthread_kill(tid, SIGCONT);
169         return 0;
172 int Thread::running()
174         return thread_running;
177 int Thread::set_synchronous(int value)
179         this->synchronous = value;
180         return 0;
183 int Thread::set_realtime(int value)
185         this->realtime = value;
186         return 0;
189 int Thread::set_autodelete(int value)
191         this->autodelete = value;
192         return 0;
195 int Thread::get_autodelete()
197         return autodelete;
200 int Thread::get_synchronous()
202         return synchronous;
205 int Thread::calculate_realtime()
207 //printf("Thread::calculate_realtime %d %d\n", getpid(), sched_getscheduler(0));
208         return (sched_getscheduler(0) == SCHED_RR ||
209                 sched_getscheduler(0) == SCHED_FIFO);
212 int Thread::get_realtime()
214         return realtime;
217 int Thread::get_tid()
219         return tid;