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"
31 #if defined(__TERMUX__)
36 Thread::Thread(int synchronous, int realtime, int autodelete)
38 this->synchronous = synchronous != 0;
39 this->realtime = realtime != 0;
40 this->autodelete = autodelete != 0;
44 cancel_enabled = false;
51 void* Thread::entrypoint(void *parameters)
53 Thread *thread = (Thread*)parameters;
55 // allow thread to be cancelled at any point during a region where it is enabled.
56 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
57 // Disable cancellation by default.
58 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
59 thread->cancel_enabled = false;
61 // Set realtime here since it doesn't work in start
62 if( thread->realtime && getuid() == 0 ) {
63 struct sched_param param = { sched_priority : 1 };
64 if(pthread_setschedparam(thread->tid, SCHED_RR, ¶m) < 0)
65 perror("Thread::entrypoint pthread_attr_setschedpolicy");
69 thread->finished = true;
70 if( !thread->synchronous ) {
71 if( thread->autodelete ) delete thread;
72 else if( !thread->cancelled ) TheList::dbg_del(thread->tid);
73 else thread->tid = ((pthread_t)-1);
81 struct sched_param param;
83 pthread_attr_init(&attr);
85 // previously run, and did not join, join to clean up zombie
86 if( synchronous && exists() )
93 // Inherit realtime from current thread the easy way.
95 realtime = calculate_realtime();
98 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
100 if( realtime && getuid() == 0 ) {
101 if(pthread_attr_setschedpolicy(&attr, SCHED_RR) < 0)
102 perror("Thread::start pthread_attr_setschedpolicy");
103 param.sched_priority = 50;
104 if(pthread_attr_setschedparam(&attr, ¶m) < 0)
105 perror("Thread::start pthread_attr_setschedparam");
108 #if !defined(__TERMUX__)
109 if(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED) < 0)
110 perror("Thread::start pthread_attr_setinheritsched");
114 // autodelete may delete this immediately after create
115 int autodelete = this->autodelete;
117 pthread_create(&tid, &attr, Thread::entrypoint, this);
120 TheList::dbg_add(tid, owner, typeid(*this).name());
125 if( exists() && !cancelled ) {
126 LOCK_LOCKS("Thread::cancel");
129 if( !synchronous ) TheList::dbg_del(tid);
135 int Thread::join() // join this thread
137 if( !exists() ) return 0;
139 // NOTE: this fails if the thread is not synchronous or
140 // or another thread is already waiting to join.
141 int ret = pthread_join(tid, 0);
144 fprintf(stderr, "Thread %p: %s\n", (void*)tid, strerror(ret));
147 CLEAR_LOCKS_TID(tid);
148 TheList::dbg_del(tid);
149 tid = ((pthread_t)-1);
150 // Don't execute anything after this.
151 if( autodelete ) delete this;
155 while( running() && !cancelled ) {
156 int ret = pthread_kill(tid, 0);
160 tid = ((pthread_t)-1);
165 int Thread::enable_cancel()
167 if( !cancel_enabled ) {
168 cancel_enabled = true;
169 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
174 int Thread::disable_cancel()
176 if( cancel_enabled ) {
177 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
178 cancel_enabled = false;
183 int Thread::get_cancel_enabled()
185 return cancel_enabled;
188 void Thread::exit_thread()
195 int Thread::suspend_thread()
198 pthread_kill(tid, SIGSTOP);
202 int Thread::continue_thread()
205 pthread_kill(tid, SIGCONT);
209 int Thread::set_synchronous(int value)
211 this->synchronous = value != 0;
215 int Thread::set_realtime(int value)
217 this->realtime = value != 0;
221 int Thread::set_autodelete(int value)
223 this->autodelete = value != 0;
227 int Thread::get_autodelete()
229 return autodelete ? 1 : 0;
232 int Thread::get_synchronous()
234 return synchronous ? 1 : 0;
237 bool Thread::calculate_realtime()
239 //printf("Thread::calculate_realtime %d %d\n", getpid(), sched_getscheduler(0));
240 return (sched_getscheduler(0) == SCHED_RR ||
241 sched_getscheduler(0) == SCHED_FIFO);
244 int Thread::get_realtime()
246 return realtime ? 1 : 0;
249 unsigned long Thread::get_tid()
251 return (unsigned long)tid;