Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / guicast / thread.C
blobc7b1404f52a94f94aaa018981eae5eeacb70ad31
1 #ifndef NO_GUICAST
2 #include "bcsignals.h"
3 #endif
4 #include <sys/wait.h>
5 #include <sched.h>
6 #include <signal.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include "thread.h"
12 Thread::Thread(int synchronous, int realtime, int autodelete)
14         this->synchronous = synchronous;
15         this->realtime = realtime;
16         this->autodelete = autodelete;
17         tid = (pthread_t)-1;
18         tid_valid = 0;
19         thread_running = 0;
20         cancel_enabled = 0;
23 Thread::~Thread()
27 void* Thread::entrypoint(void *parameters)
29         Thread *thread = (Thread*)parameters;
31 // allow thread to be cancelled at any point during a region where it is enabled.
32         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
33 // Disable cancellation by default.
34         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
35         thread->cancel_enabled = 0;
37 // Set realtime here seince it doesn't work in start
38         if(thread->realtime && getuid() == 0)
39         {
40                 struct sched_param param = 
41                 {
42                         sched_priority : 1
43                 };
44                 if(pthread_setschedparam(thread->tid, SCHED_RR, &param) < 0)
45                         perror("Thread::entrypoint pthread_attr_setschedpolicy");
46         }
48         thread->run();
50         thread->thread_running = 0;
52         if(thread->autodelete && !thread->synchronous) delete thread;
53         return NULL;
56 void Thread::start()
58         pthread_attr_t  attr;
59         struct sched_param param;
61         pthread_attr_init(&attr);
63         thread_running = 1;
65 // Inherit realtime from current thread the easy way.
66         if(!realtime) realtime = calculate_realtime();
69         if(!synchronous) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
71         if(realtime && getuid() == 0)
72         {
73                 if(pthread_attr_setschedpolicy(&attr, SCHED_RR) < 0)
74                         perror("Thread::start pthread_attr_setschedpolicy");
75                 param.sched_priority = 50;
76                 if(pthread_attr_setschedparam(&attr, &param) < 0)
77                         perror("Thread::start pthread_attr_setschedparam");
78         }
79         else
80         {
81                 if(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED) < 0)
82                         perror("Thread::start pthread_attr_setinheritsched");
83         }
85         pthread_create(&tid, &attr, Thread::entrypoint, this);
86         tid_valid = 1;
89 int Thread::end(pthread_t tid)           // need to join after this if synchronous
91         if(tid_valid)
92         {
93                 pthread_cancel(tid);
94         }
95         return 0;
98 int Thread::end()           // need to join after this if synchronous
100         cancel();
101         return 0;
104 int Thread::cancel()
106         if(tid_valid) pthread_cancel(tid);
107         if(!synchronous)
108         {
109                 tid = (pthread_t)-1;
110                 tid_valid = 0;
111         }
112         return 0;
115 int Thread::join()   // join this thread
117         int result = 0;
118         if(tid_valid)
119         {
120                 result = pthread_join(tid, 0);
121         }
123         tid = (pthread_t)-1;
124         tid_valid = 0;
126 // Don't execute anything after this.
127         if(autodelete && synchronous) delete this;
128         return 0;
131 int Thread::enable_cancel()
133         cancel_enabled = 1;
134         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
135         return 0;
138 int Thread::disable_cancel()
140         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
141         cancel_enabled = 0;
142         return 0;
145 int Thread::get_cancel_enabled()
147         return cancel_enabled;
150 int Thread::exit_thread()
152         pthread_exit(0);
153         if(!synchronous)
154         {
155                 tid = (pthread_t)-1;
156                 tid_valid = 0;
157         }
158         return 0;
162 int Thread::suspend_thread()
164         if(tid_valid) pthread_kill(tid, SIGSTOP);
165         return 0;
168 int Thread::continue_thread()
170         if(tid_valid) pthread_kill(tid, SIGCONT);
171         return 0;
174 int Thread::running()
176         return thread_running;
179 int Thread::set_synchronous(int value)
181         this->synchronous = value;
182         return 0;
185 int Thread::set_realtime(int value)
187         this->realtime = value;
188         return 0;
191 int Thread::set_autodelete(int value)
193         this->autodelete = value;
194         return 0;
197 int Thread::get_autodelete()
199         return autodelete;
202 int Thread::get_synchronous()
204         return synchronous;
207 int Thread::calculate_realtime()
209 //printf("Thread::calculate_realtime %d %d\n", getpid(), sched_getscheduler(0));
210         return (sched_getscheduler(0) == SCHED_RR ||
211                 sched_getscheduler(0) == SCHED_FIFO);
214 int Thread::get_realtime()
216         return realtime;
219 int Thread::get_tid()
221         return tid;