9 Thread::Thread(int synchronous, int realtime, int autodelete)
11 this->synchronous = synchronous;
12 this->realtime = realtime;
13 this->autodelete = autodelete;
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());
34 thread->thread_running = 0;
36 if(thread->autodelete && !thread->synchronous) delete thread;
43 struct sched_param param;
45 pthread_attr_init(&attr);
48 if(!synchronous) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
50 if(realtime && getuid() == 0)
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, ¶m) < 0)
56 perror("PFCThread::start pthread_attr_setschedparam");
60 if(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED) < 0)
61 perror("Thread::start pthread_attr_setinheritsched");
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);
73 int Thread::end() // need to join after this if synchronous
81 if((int)tid > 0) pthread_cancel(tid);
82 if(!synchronous) tid = (pthread_t)-1;
86 int Thread::join() // join this thread
91 result = pthread_join(tid, 0);
96 // Don't execute anything after this.
97 if(autodelete && synchronous) delete this;
101 int Thread::enable_cancel()
103 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
107 int Thread::disable_cancel()
109 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
113 int Thread::exit_thread()
116 if(!synchronous) tid = (pthread_t)-1;
121 int Thread::suspend_thread()
123 if((int)tid > 0) pthread_kill(tid, SIGSTOP);
127 int Thread::continue_thread()
129 if((int)tid > 0) pthread_kill(tid, SIGCONT);
133 int Thread::running()
135 return thread_running;
138 int Thread::set_synchronous(int value)
140 this->synchronous = value;
144 int Thread::set_realtime(int value)
146 this->realtime = value;
150 int Thread::set_autodelete(int value)
152 this->autodelete = value;
156 int Thread::get_autodelete()
161 int Thread::get_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()
178 int Thread::get_tid()