r288: This commit was manufactured by cvs2svn to create tag 'hv_1_2_0'.
[cinelerra_cv/mob.git] / hvirtual / guicast / thread.C
blobb78118d043ce9977f8d534f27fc9010cd7fd2bbf
1 #include "bcsignals.h"
2 #include <sys/wait.h>
3 #include <signal.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include "thread.h"
9 Thread::Thread(int synchronous, int realtime, int autodelete)
11         this->synchronous = synchronous;
12         this->realtime = realtime;
13         this->autodelete = autodelete;
14         tid = (pthread_t)-1;
15         thread_running = 0;
18 Thread::~Thread()
22 void* Thread::entrypoint(void *parameters)
24         Thread *thread = (Thread*)parameters;
26 // allow thread to be cancelled at any point during a region where it is enabled.
27         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
28 // Disable cancellation by default.
29         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
30 //printf("Thread::entrypoint 1 %d\n", getpid());        
32         thread->run();
34         thread->thread_running = 0;
36         if(thread->autodelete && !thread->synchronous) delete thread;
37         return NULL;
40 void Thread::start()
42         pthread_attr_t  attr;
43         struct sched_param param;
45         pthread_attr_init(&attr);
47         thread_running = 1;
48         if(!synchronous) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
50         if(realtime && getuid() == 0)
51         {
52                 if(pthread_attr_setschedpolicy(&attr, SCHED_FIFO) < 0)
53                         perror("PFCThread::start pthread_attr_setschedpolicy");
54                 param.sched_priority = 50;
55                 if(pthread_attr_setschedparam(&attr, &param) < 0)
56                         perror("PFCThread::start pthread_attr_setschedparam");
57         }
58         else
59         {
60                 if(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED) < 0)
61                         perror("Thread::start pthread_attr_setinheritsched");
62         }
64         pthread_create(&tid, &attr, Thread::entrypoint, this);
67 int Thread::end(pthread_t tid)           // need to join after this if synchronous
69         if((int)tid > 0) pthread_cancel(tid);
70         return 0;
73 int Thread::end()           // need to join after this if synchronous
75         cancel();
76         return 0;
79 int Thread::cancel()
81         if((int)tid > 0) pthread_cancel(tid);
82         if(!synchronous) tid = (pthread_t)-1;
83         return 0;
86 int Thread::join()   // join this thread
88         int result = 0;
89         if((int)tid > 0)
90         {
91                 result = pthread_join(tid, 0);
92         }
94         tid = (pthread_t)-1;
96 // Don't execute anything after this.
97         if(autodelete && synchronous) delete this;
98         return 0;
101 int Thread::enable_cancel()
103         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
104         return 0;
107 int Thread::disable_cancel()
109         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
110         return 0;
113 int Thread::exit_thread()
115         pthread_exit(0);
116         if(!synchronous) tid = (pthread_t)-1;
117         return 0;
121 int Thread::suspend_thread()
123         if((int)tid > 0) pthread_kill(tid, SIGSTOP);
124         return 0;
127 int Thread::continue_thread()
129         if((int)tid > 0) pthread_kill(tid, SIGCONT);
130         return 0;
133 int Thread::running()
135         return thread_running;
138 int Thread::set_synchronous(int value)
140         this->synchronous = value;
141         return 0;
144 int Thread::set_realtime(int value)
146         this->realtime = value;
147         return 0;
150 int Thread::set_autodelete(int value)
152         this->autodelete = value;
153         return 0;
156 int Thread::get_autodelete()
158         return autodelete;
161 int Thread::get_synchronous()
163         return synchronous;
166 int Thread::calculate_realtime()
168 //printf("Thread::calculate_realtime %d %d\n", getpid(), sched_getscheduler(0));
169         return (sched_getscheduler(0) == SCHED_RR ||
170                 sched_getscheduler(0) == SCHED_FIFO);
173 int Thread::get_realtime()
175         return realtime;
178 int Thread::get_tid()
180         return tid;