JackPosixThread.cpp

00001 /*
00002 Copyright (C) 2001 Paul Davis 
00003 Copyright (C) 2004-2006 Grame
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 */
00020 
00021 #include "JackPosixThread.h"
00022 #include "JackError.h"
00023 #include <string.h> // for memset
00024 
00025 namespace Jack
00026 {
00027 
00028 void* JackPosixThread::ThreadHandler(void* arg)
00029 {
00030     JackPosixThread* obj = (JackPosixThread*)arg;
00031     JackRunnableInterface* runnable = obj->fRunnable;
00032     int err;
00033 
00034     if ((err = pthread_setcanceltype(obj->fCancellation, NULL)) != 0) {
00035         jack_error("pthread_setcanceltype err = %s", strerror(err));
00036     }
00037 
00038     // Call Init method
00039     if (!runnable->Init()) {
00040         jack_error("Thread init fails: thread quits");
00041         return 0;
00042     }
00043 
00044     JackLog("ThreadHandler: start\n");
00045 
00046         // If Init succeed, start the thread loop
00047         bool res = true;
00048     while (obj->fRunning && res) {
00049         res = runnable->Execute();
00050         //pthread_testcancel();
00051     }
00052 
00053     JackLog("ThreadHandler: exit\n");
00054     return 0;
00055 }
00056 
00057 int JackPosixThread::Start()
00058 {
00059         fRunning = true;
00060         
00061         // Check if the thread was correctly started
00062         if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) { 
00063                 fRunning = false;
00064                 return -1;
00065         } else {
00066                 return 0;
00067         }
00068 }
00069 
00070 int JackPosixThread::StartImp(pthread_t* thread, int priority, int realtime, void*(*start_routine)(void*), void* arg)
00071 {
00072     int res;
00073  
00074     if (realtime) {
00075 
00076         JackLog("Create RT thread\n");
00077 
00078         /* Get the client thread to run as an RT-FIFO
00079            scheduled thread of appropriate priority.
00080         */
00081         pthread_attr_t attributes;
00082         struct sched_param rt_param;
00083         pthread_attr_init(&attributes);
00084                 
00085                 if ((res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED))) {
00086             jack_error("Cannot request explicit scheduling for RT thread  %d %s", res, strerror(errno));
00087             return -1;
00088         }
00089                 
00090         if ((res = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_JOINABLE))) {
00091             jack_error("Cannot request joinable thread creation for RT thread  %d %s", res, strerror(errno));
00092             return -1;
00093         }
00094                 
00095         if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
00096             jack_error("Cannot set scheduling scope for RT thread %d %s", res, strerror(errno));
00097             return -1;
00098         }
00099 
00100         //if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_FIFO))) {
00101 
00102         if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_RR))) {
00103             jack_error("Cannot set FIFO scheduling class for RT thread  %d %s", res, strerror(errno));
00104             return -1;
00105         }
00106 
00107         if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
00108             jack_error("Cannot set scheduling scope for RT thread %d %s", res, strerror(errno));
00109             return -1;
00110         }
00111 
00112         memset(&rt_param, 0, sizeof(rt_param));
00113         rt_param.sched_priority = priority;
00114 
00115         if ((res = pthread_attr_setschedparam(&attributes, &rt_param))) {
00116             jack_error("Cannot set scheduling priority for RT thread %d %s", res, strerror(errno));
00117             return -1;
00118         }
00119                 
00120            if ((res = pthread_attr_setstacksize(&attributes, THREAD_STACK))) {
00121                         jack_error("setting thread stack size%d %s", res, strerror(errno));
00122                         return -1;
00123         }
00124 
00125         if ((res = pthread_create(thread, &attributes, start_routine, arg))) {
00126             jack_error("Cannot set create thread %d %s", res, strerror(errno));
00127             return -1;
00128         }
00129 
00130         return 0;
00131     } else {
00132         JackLog("Create non RT thread\n");
00133 
00134         if ((res = pthread_create(thread, 0, start_routine, arg))) {
00135             jack_error("Cannot set create thread %d %s", res, strerror(errno));
00136             return -1;
00137         }
00138         return 0;
00139     }
00140 }
00141 
00142 int JackPosixThread::StartSync()
00143 {
00144     jack_error("Not implemented yet");
00145     return -1;
00146 }
00147 
00148 int JackPosixThread::Kill()
00149 {
00150     if (fThread) { // If thread has been started
00151         JackLog("JackPosixThread::Kill\n");
00152         void* status;
00153         pthread_cancel(fThread);
00154         pthread_join(fThread, &status);
00155                 fRunning = false; 
00156                 fThread = (pthread_t)NULL;
00157         return 0;
00158     } else {
00159         return -1;
00160     }
00161 }
00162 
00163 int JackPosixThread::Stop()
00164 {
00165     if (fThread) { // If thread has been started
00166         JackLog("JackPosixThread::Stop\n");
00167         void* status;
00168         fRunning = false; // Request for the thread to stop
00169         pthread_join(fThread, &status);
00170                 fThread = (pthread_t)NULL;
00171         return 0;
00172     } else {
00173         return -1;
00174     }
00175 }
00176 
00177 int JackPosixThread::AcquireRealTime()
00178 {
00179         return (fThread) ? AcquireRealTimeImp(fThread, fPriority) : -1;
00180 }
00181 
00182 int JackPosixThread::AcquireRealTime(int priority)
00183 {
00184         fPriority = priority;
00185     return AcquireRealTime();
00186 }
00187 
00188 int JackPosixThread::AcquireRealTimeImp(pthread_t thread, int priority)
00189 {
00190    struct sched_param rtparam;
00191     int res;
00192     memset(&rtparam, 0, sizeof(rtparam));
00193     rtparam.sched_priority = priority;
00194 
00195     //if ((res = pthread_setschedparam(fThread, SCHED_FIFO, &rtparam)) != 0) {
00196 
00197     if ((res = pthread_setschedparam(thread, SCHED_RR, &rtparam)) != 0) {
00198         jack_error("Cannot use real-time scheduling (FIFO/%d) "
00199                    "(%d: %s)", rtparam.sched_priority, res,
00200                    strerror(res));
00201         return -1;
00202     }
00203     return 0;
00204 }
00205 
00206 int JackPosixThread::DropRealTime()
00207 {
00208     return (fThread) ? DropRealTimeImp(fThread) : -1;
00209 }
00210 
00211 int JackPosixThread::DropRealTimeImp(pthread_t thread)
00212 {
00213         struct sched_param rtparam;
00214     int res;
00215     memset(&rtparam, 0, sizeof(rtparam));
00216     rtparam.sched_priority = 0;
00217 
00218     if ((res = pthread_setschedparam(thread, SCHED_OTHER, &rtparam)) != 0) {
00219         jack_error("Cannot switch to normal scheduling priority(%s)\n", strerror(errno));
00220         return -1;
00221     }
00222     return 0;
00223 }
00224 
00225 pthread_t JackPosixThread::GetThreadID()
00226 {
00227     return fThread;
00228 }
00229 
00230 } // end of namespace
00231 

Generated on Mon Oct 15 17:11:14 2007 for Jackdmp by  doxygen 1.5.1