00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "JackPosixThread.h"
00022 #include "JackError.h"
00023 #include "JackTime.h"
00024 #include "JackGlobals.h"
00025 #include <string.h>
00026 #include <unistd.h>
00027
00028 #define JACK_SCHED_POLICY SCHED_RR
00029
00030
00031 namespace Jack
00032 {
00033
00034 void* JackPosixThread::ThreadHandler(void* arg)
00035 {
00036 JackPosixThread* obj = (JackPosixThread*)arg;
00037 JackRunnableInterface* runnable = obj->fRunnable;
00038 int err;
00039
00040 if ((err = pthread_setcanceltype(obj->fCancellation, NULL)) != 0) {
00041 jack_error("pthread_setcanceltype err = %s", strerror(err));
00042 }
00043
00044
00045 jack_log("ThreadHandler: start");
00046 obj->fStatus = kIniting;
00047
00048
00049 if (!runnable->Init()) {
00050 jack_error("Thread init fails: thread quits");
00051 return 0;
00052 }
00053
00054 obj->fStatus = kRunning;
00055
00056
00057 bool res = true;
00058 while (obj->fStatus == kRunning && res) {
00059 res = runnable->Execute();
00060 }
00061
00062 jack_log("ThreadHandler: exit");
00063 pthread_exit(0);
00064 return 0;
00065 }
00066
00067 int JackPosixThread::Start()
00068 {
00069 fStatus = kStarting;
00070
00071
00072 if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
00073 fStatus = kIdle;
00074 return -1;
00075 } else {
00076 return 0;
00077 }
00078 }
00079
00080 int JackPosixThread::StartSync()
00081 {
00082 fStatus = kStarting;
00083
00084 if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
00085 fStatus = kIdle;
00086 return -1;
00087 } else {
00088 int count = 0;
00089 while (fStatus == kStarting && ++count < 1000) {
00090 JackSleep(1000);
00091 }
00092 return (count == 1000) ? -1 : 0;
00093 }
00094 }
00095
00096 int JackPosixThread::StartImp(pthread_t* thread, int priority, int realtime, void*(*start_routine)(void*), void* arg)
00097 {
00098 pthread_attr_t attributes;
00099 struct sched_param rt_param;
00100 pthread_attr_init(&attributes);
00101 int res;
00102
00103 if ((res = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_JOINABLE))) {
00104 jack_error("Cannot request joinable thread creation for RT thread res = %d err = %s", res, strerror(errno));
00105 return -1;
00106 }
00107
00108 if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
00109 jack_error("Cannot set scheduling scope for RT thread res = %d err = %s", res, strerror(errno));
00110 return -1;
00111 }
00112
00113 if (realtime) {
00114
00115 jack_log("Create RT thread");
00116
00117 if ((res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED))) {
00118 jack_error("Cannot request explicit scheduling for RT thread res = %d err = %s", res, strerror(errno));
00119 return -1;
00120 }
00121
00122 if ((res = pthread_attr_setschedpolicy(&attributes, JACK_SCHED_POLICY))) {
00123 jack_error("Cannot set RR scheduling class for RT thread res = %d err = %s", res, strerror(errno));
00124 return -1;
00125 }
00126 } else {
00127 jack_log("Create non RT thread");
00128 }
00129
00130 memset(&rt_param, 0, sizeof(rt_param));
00131 rt_param.sched_priority = priority;
00132
00133 if ((res = pthread_attr_setschedparam(&attributes, &rt_param))) {
00134 jack_error("Cannot set scheduling priority for RT thread res = %d err = %s", res, strerror(errno));
00135 return -1;
00136 }
00137
00138 if ((res = pthread_attr_setstacksize(&attributes, THREAD_STACK))) {
00139 jack_error("Cannot set thread stack size res = %d err = %s", res, strerror(errno));
00140 return -1;
00141 }
00142
00143 if ((res = JackGlobals::fJackThreadCreator(thread, &attributes, start_routine, arg))) {
00144 jack_error("Cannot create thread res = %d err = %s", res, strerror(errno));
00145 return -1;
00146 }
00147
00148 pthread_attr_destroy(&attributes);
00149 return 0;
00150 }
00151
00152 int JackPosixThread::Kill()
00153 {
00154 if (fThread != (pthread_t)NULL) {
00155 jack_log("JackPosixThread::Kill");
00156 void* status;
00157 pthread_cancel(fThread);
00158 pthread_join(fThread, &status);
00159 fStatus = kIdle;
00160 fThread = (pthread_t)NULL;
00161 return 0;
00162 } else {
00163 return -1;
00164 }
00165 }
00166
00167 int JackPosixThread::Stop()
00168 {
00169 if (fThread != (pthread_t)NULL) {
00170 jack_log("JackPosixThread::Stop");
00171 void* status;
00172 fStatus = kIdle;
00173 pthread_join(fThread, &status);
00174 fThread = (pthread_t)NULL;
00175 return 0;
00176 } else {
00177 return -1;
00178 }
00179 }
00180
00181 int JackPosixThread::KillImp(pthread_t thread)
00182 {
00183 if (thread != (pthread_t)NULL) {
00184 jack_log("JackPosixThread::Kill");
00185 void* status;
00186 pthread_cancel(thread);
00187 pthread_join(thread, &status);
00188 return 0;
00189 } else {
00190 return -1;
00191 }
00192 }
00193
00194 int JackPosixThread::StopImp(pthread_t thread)
00195 {
00196 if (thread != (pthread_t)NULL) {
00197 jack_log("JackPosixThread::Stop");
00198 void* status;
00199 pthread_join(thread, &status);
00200 return 0;
00201 } else {
00202 return -1;
00203 }
00204 }
00205
00206 int JackPosixThread::AcquireRealTime()
00207 {
00208 return (fThread != (pthread_t)NULL) ? AcquireRealTimeImp(fThread, fPriority) : -1;
00209 }
00210
00211 int JackPosixThread::AcquireRealTime(int priority)
00212 {
00213 fPriority = priority;
00214 return AcquireRealTime();
00215 }
00216
00217 int JackPosixThread::AcquireRealTimeImp(pthread_t thread, int priority)
00218 {
00219 struct sched_param rtparam;
00220 int res;
00221 memset(&rtparam, 0, sizeof(rtparam));
00222 rtparam.sched_priority = priority;
00223
00224 if ((res = pthread_setschedparam(thread, JACK_SCHED_POLICY, &rtparam)) != 0) {
00225 jack_error("Cannot use real-time scheduling (RR/%d)"
00226 "(%d: %s)", rtparam.sched_priority, res,
00227 strerror(res));
00228 return -1;
00229 }
00230 return 0;
00231 }
00232
00233 int JackPosixThread::DropRealTime()
00234 {
00235 return (fThread != (pthread_t)NULL) ? DropRealTimeImp(fThread) : -1;
00236 }
00237
00238 int JackPosixThread::DropRealTimeImp(pthread_t thread)
00239 {
00240 struct sched_param rtparam;
00241 int res;
00242 memset(&rtparam, 0, sizeof(rtparam));
00243 rtparam.sched_priority = 0;
00244
00245 if ((res = pthread_setschedparam(thread, SCHED_OTHER, &rtparam)) != 0) {
00246 jack_error("Cannot switch to normal scheduling priority(%s)", strerror(errno));
00247 return -1;
00248 }
00249 return 0;
00250 }
00251
00252 pthread_t JackPosixThread::GetThreadID()
00253 {
00254 return fThread;
00255 }
00256
00257 void JackPosixThread::Terminate()
00258 {
00259 jack_log("JackPosixThread::Terminate");
00260 pthread_exit(0);
00261 }
00262
00263 SERVER_EXPORT void ThreadExit()
00264 {
00265 jack_log("ThreadExit");
00266 pthread_exit(0);
00267 }
00268
00269 }
00270
00271 bool jack_get_thread_realtime_priority_range(int * min_ptr, int * max_ptr)
00272 {
00273 #if defined(_POSIX_PRIORITY_SCHEDULING) && !defined(__APPLE__)
00274 int min, max;
00275
00276 min = sched_get_priority_min(JACK_SCHED_POLICY);
00277 if (min == -1)
00278 {
00279 jack_error("sched_get_priority_min() failed.");
00280 return false;
00281 }
00282
00283 max = sched_get_priority_max(JACK_SCHED_POLICY);
00284 if (max == -1)
00285 {
00286 jack_error("sched_get_priority_max() failed.");
00287 return false;
00288 }
00289
00290 *min_ptr = min;
00291 *max_ptr = max;
00292
00293 return true;
00294 #else
00295 return false;
00296 #endif
00297 }
00298
00299 bool jack_tls_allocate_key(jack_tls_key *key_ptr)
00300 {
00301 int ret;
00302
00303 ret = pthread_key_create(key_ptr, NULL);
00304 if (ret != 0)
00305 {
00306 jack_error("pthread_key_create() failed with error %d errno %s", ret, strerror(errno));
00307 return false;
00308 }
00309
00310 return true;
00311 }
00312
00313 bool jack_tls_free_key(jack_tls_key key)
00314 {
00315 int ret;
00316
00317 ret = pthread_key_delete(key);
00318 if (ret != 0)
00319 {
00320 jack_error("pthread_key_delete() failed with error %d errno %s", ret, strerror(errno));
00321 return false;
00322 }
00323
00324 return true;
00325 }
00326
00327 bool jack_tls_set(jack_tls_key key, void *data_ptr)
00328 {
00329 int ret;
00330
00331 ret = pthread_setspecific(key, (const void *)data_ptr);
00332 if (ret != 0)
00333 {
00334 jack_error("pthread_setspecific() failed with error %d errno %s", ret, strerror(errno));
00335 return false;
00336 }
00337
00338 return true;
00339 }
00340
00341 void *jack_tls_get(jack_tls_key key)
00342 {
00343 return pthread_getspecific(key);
00344 }