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
23 #include "bcsignals.h"
33 // track unjoined threads at termination
35 #include "arraylist.h"
40 static Mutex the_lock;
42 MLocker() { the_lock.lock(); }
43 ~MLocker() { the_lock.unlock(); }
45 Mutex MLocker::the_lock;
49 pthread_t tid; const char *name;
50 the_dbg(pthread_t t, const char *nm) { tid = t; name = nm; }
55 static class the_list : public ArrayList<the_dbg*> {
57 static void dump_threads(FILE *fp);
65 static void dbg_add(pthread_t tid, const char *nm)
68 int i = thread_list.size();
69 while( --i >= 0 && thread_list[i]->tid != tid );
71 printf("dbg_add, dup %016lx %s %s\n",
72 (unsigned long)tid, nm, thread_list[i]->name);
75 thread_list.append(new the_dbg(tid, nm));
78 static void dbg_del(pthread_t tid)
81 int i = thread_list.size();
82 while( --i >= 0 && thread_list[i]->tid != tid );
84 printf("dbg_del, mis %016lx\n",(unsigned long)tid);
87 thread_list.remove_object_number(i);
90 static class the_chkr {
94 int i = thread_list.size();
96 printf("unjoined tids %d\n", i);
97 while( --i >= 0 ) printf(" %016lx %s\n",
98 (unsigned long)thread_list[i]->tid,
99 thread_list[i]->name);
103 #define dbg_add(t, nm) do {} while(0)
104 #define dbg_del(t) do {} while(0)
107 Thread::Thread(int synchronous, int realtime, int autodelete)
109 this->synchronous = synchronous != 0;
110 this->realtime = realtime != 0;
111 this->autodelete = autodelete != 0;
115 cancel_enabled = false;
122 void* Thread::entrypoint(void *parameters)
124 Thread *thread = (Thread*)parameters;
126 // allow thread to be cancelled at any point during a region where it is enabled.
127 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
128 // Disable cancellation by default.
129 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
130 thread->cancel_enabled = false;
132 // Set realtime here seince it doesn't work in start
133 if( thread->realtime && getuid() == 0 ) {
134 struct sched_param param = { sched_priority : 1 };
135 if(pthread_setschedparam(thread->tid, SCHED_RR, ¶m) < 0)
136 perror("Thread::entrypoint pthread_attr_setschedpolicy");
140 thread->finished = true;
141 if( !thread->synchronous ) {
142 if( !thread->cancelled ) dbg_del(thread->tid);
143 if( thread->autodelete ) delete thread;
144 else thread->tid = ((pthread_t)-1);
152 struct sched_param param;
154 pthread_attr_init(&attr);
156 // previously run, and did not join, join to clean up zombie
157 if( synchronous && exists() )
164 // Inherit realtime from current thread the easy way.
166 realtime = calculate_realtime();
169 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
171 if( realtime && getuid() == 0 ) {
172 if(pthread_attr_setschedpolicy(&attr, SCHED_RR) < 0)
173 perror("Thread::start pthread_attr_setschedpolicy");
174 param.sched_priority = 50;
175 if(pthread_attr_setschedparam(&attr, ¶m) < 0)
176 perror("Thread::start pthread_attr_setschedparam");
179 if(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED) < 0)
180 perror("Thread::start pthread_attr_setinheritsched");
183 pthread_create(&tid, &attr, Thread::entrypoint, this);
185 dbg_add(tid, typeid(*this).name());
190 if( exists() && !cancelled ) {
191 LOCK_LOCKS("Thread::cancel");
194 if( !synchronous ) dbg_del(tid);
200 int Thread::join() // join this thread
202 if( !exists() ) return 0;
204 // NOTE: this does not do anything if the thread is not synchronous
205 int ret = pthread_join(tid, 0);
206 if( ret ) strerror(ret);
207 CLEAR_LOCKS_TID(tid);
209 tid = ((pthread_t)-1);
210 // Don't execute anything after this.
211 if( autodelete ) delete this;
215 while( running() && !cancelled ) {
216 int ret = pthread_kill(tid, 0);
220 tid = ((pthread_t)-1);
225 int Thread::enable_cancel()
227 if( !cancel_enabled ) {
228 cancel_enabled = true;
229 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
234 int Thread::disable_cancel()
236 if( cancel_enabled ) {
237 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
238 cancel_enabled = false;
243 int Thread::get_cancel_enabled()
245 return cancel_enabled;
248 void Thread::exit_thread()
255 int Thread::suspend_thread()
258 pthread_kill(tid, SIGSTOP);
262 int Thread::continue_thread()
265 pthread_kill(tid, SIGCONT);
269 bool Thread::exists()
271 return tid != ((pthread_t)-1);
274 bool Thread::running()
276 return exists() & ~finished;
279 int Thread::set_synchronous(int value)
281 this->synchronous = value != 0;
285 int Thread::set_realtime(int value)
287 this->realtime = value != 0;
291 int Thread::set_autodelete(int value)
293 this->autodelete = value != 0;
297 int Thread::get_autodelete()
299 return autodelete ? 1 : 0;
302 int Thread::get_synchronous()
304 return synchronous ? 1 : 0;
307 bool Thread::calculate_realtime()
309 //printf("Thread::calculate_realtime %d %d\n", getpid(), sched_getscheduler(0));
310 return (sched_getscheduler(0) == SCHED_RR ||
311 sched_getscheduler(0) == SCHED_FIFO);
314 int Thread::get_realtime()
316 return realtime ? 1 : 0;
319 unsigned long Thread::get_tid()
324 void Thread::dump_threads(FILE *fp)
326 int i = thread_list.size();
328 fprintf(fp, "thread %016lx, %s\n",
329 (unsigned long)thread_list[i]->tid,
330 thread_list[i]->name);