4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "bcsignals.h"
33 Thread::Thread(int synchronous, int realtime, int autodelete)
35 this->synchronous = synchronous != 0;
36 this->realtime = realtime != 0;
37 this->autodelete = autodelete != 0;
41 cancel_enabled = false;
48 void* Thread::entrypoint(void *parameters)
50 Thread *thread = (Thread*)parameters;
52 // allow thread to be cancelled at any point during a region where it is enabled.
53 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
54 // Disable cancellation by default.
55 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
56 thread->cancel_enabled = false;
58 // Set realtime here seince it doesn't work in start
59 if( thread->realtime && getuid() == 0 ) {
60 struct sched_param param = { sched_priority : 1 };
61 if(pthread_setschedparam(thread->tid, SCHED_RR, ¶m) < 0)
62 perror("Thread::entrypoint pthread_attr_setschedpolicy");
66 thread->finished = true;
67 if( !thread->synchronous ) {
68 if( thread->autodelete ) delete thread;
69 else if( !thread->cancelled ) TheList::dbg_del(thread->tid);
70 else thread->tid = ((pthread_t)-1);
78 struct sched_param param;
80 pthread_attr_init(&attr);
82 // previously run, and did not join, join to clean up zombie
83 if( synchronous && exists() )
90 // Inherit realtime from current thread the easy way.
92 realtime = calculate_realtime();
95 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
97 if( realtime && getuid() == 0 ) {
98 if(pthread_attr_setschedpolicy(&attr, SCHED_RR) < 0)
99 perror("Thread::start pthread_attr_setschedpolicy");
100 param.sched_priority = 50;
101 if(pthread_attr_setschedparam(&attr, ¶m) < 0)
102 perror("Thread::start pthread_attr_setschedparam");
105 if(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED) < 0)
106 perror("Thread::start pthread_attr_setinheritsched");
109 // autodelete may delete this immediately after create
110 int autodelete = this->autodelete;
112 pthread_create(&tid, &attr, Thread::entrypoint, this);
115 TheList::dbg_add(tid, owner, typeid(*this).name());
120 if( exists() && !cancelled ) {
121 LOCK_LOCKS("Thread::cancel");
124 if( !synchronous ) TheList::dbg_del(tid);
130 int Thread::join() // join this thread
132 if( !exists() ) return 0;
134 // NOTE: this fails if the thread is not synchronous or
135 // or another thread is already waiting to join.
136 int ret = pthread_join(tid, 0);
139 fprintf(stderr, "Thread %p: %s\n", (void*)tid, strerror(ret));
142 CLEAR_LOCKS_TID(tid);
143 TheList::dbg_del(tid);
144 tid = ((pthread_t)-1);
145 // Don't execute anything after this.
146 if( autodelete ) delete this;
150 while( running() && !cancelled ) {
151 int ret = pthread_kill(tid, 0);
155 tid = ((pthread_t)-1);
160 int Thread::enable_cancel()
162 if( !cancel_enabled ) {
163 cancel_enabled = true;
164 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
169 int Thread::disable_cancel()
171 if( cancel_enabled ) {
172 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
173 cancel_enabled = false;
178 int Thread::get_cancel_enabled()
180 return cancel_enabled;
183 void Thread::exit_thread()
190 int Thread::suspend_thread()
193 pthread_kill(tid, SIGSTOP);
197 int Thread::continue_thread()
200 pthread_kill(tid, SIGCONT);
204 int Thread::set_synchronous(int value)
206 this->synchronous = value != 0;
210 int Thread::set_realtime(int value)
212 this->realtime = value != 0;
216 int Thread::set_autodelete(int value)
218 this->autodelete = value != 0;
222 int Thread::get_autodelete()
224 return autodelete ? 1 : 0;
227 int Thread::get_synchronous()
229 return synchronous ? 1 : 0;
232 bool Thread::calculate_realtime()
234 //printf("Thread::calculate_realtime %d %d\n", getpid(), sched_getscheduler(0));
235 return (sched_getscheduler(0) == SCHED_RR ||
236 sched_getscheduler(0) == SCHED_FIFO);
239 int Thread::get_realtime()
241 return realtime ? 1 : 0;
244 unsigned long Thread::get_tid()