root/trunk/libffado/src/libutil/PosixThread.cpp

Revision 445, 5.7 kB (checked in by pieterpalmers, 17 years ago)

* name change from FreeBoB to FFADO
* replaced tabs by 4 spaces
* got rid of end-of-line spaces
* made all license and copyrights conform

library becomes LGPL, apps become GPL
explicitly state LGPL v2.1 and GPL v2 (don't like v3 draft)

copyrights are 2005-2007 Daniel & Pieter
except for the MotU stuff (C) Jonathan, Pieter

Line 
1 /*
2 Modifications for FFADO by Pieter Palmers
3
4 Copied from the jackd/jackdmp sources
5 function names changed in order to avoid naming problems when using this in
6 a jackd backend.
7
8 Copyright (C) 2001 Paul Davis
9 Copyright (C) 2004-2006 Grame
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 */
26
27 #include "PosixThread.h"
28 #include <string.h> // for memset
29 #include <errno.h>
30 #include <assert.h>
31
32 namespace Util
33 {
34
35 IMPL_DEBUG_MODULE( Thread, Thread, DEBUG_LEVEL_VERBOSE );
36
37 void* PosixThread::ThreadHandler(void* arg)
38 {
39     PosixThread* obj = (PosixThread*)arg;
40     RunnableInterface* runnable = obj->fRunnable;
41     int err;
42
43     if ((err = pthread_setcanceltype(obj->fCancellation, NULL)) != 0) {
44         debugError("pthread_setcanceltype err = %s", strerror(err));
45     }
46
47     // Call Init method
48     if (!runnable->Init()) {
49         debugError("Thread init fails: thread quits");
50         return 0;
51     }
52
53     debugOutput( DEBUG_LEVEL_VERBOSE, "ThreadHandler: start\n");
54
55     // If Init succeed start the thread loop
56     bool res = true;
57     while (obj->fRunning && res) {
58         res = runnable->Execute();
59         //pthread_testcancel();
60     }
61
62     debugOutput( DEBUG_LEVEL_VERBOSE, "ThreadHandler: exit\n");
63     return 0;
64 }
65
66 int PosixThread::Start()
67 {
68     int res;
69     fRunning = true;
70
71     if (fRealTime) {
72
73         debugOutput( DEBUG_LEVEL_VERBOSE, "Create RT thread with priority %d\n", fPriority);
74
75         /* Get the client thread to run as an RT-FIFO
76            scheduled thread of appropriate priority.
77         */
78         pthread_attr_t attributes;
79         struct sched_param rt_param;
80         pthread_attr_init(&attributes);
81
82         if ((res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED))) {
83             debugError("Cannot request explicit scheduling for RT thread  %d %s", res, strerror(errno));
84             return -1;
85         }
86         if ((res = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_JOINABLE))) {
87             debugError("Cannot request joinable thread creation for RT thread  %d %s", res, strerror(errno));
88             return -1;
89         }
90         if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
91             debugError("Cannot set scheduling scope for RT thread %d %s", res, strerror(errno));
92             return -1;
93         }
94
95         if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_FIFO))) {
96
97         //if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_RR))) {
98             debugError("Cannot set FIFO scheduling class for RT thread  %d %s", res, strerror(errno));
99             return -1;
100         }
101
102         memset(&rt_param, 0, sizeof(rt_param));
103         rt_param.sched_priority = fPriority;
104
105         if ((res = pthread_attr_setschedparam(&attributes, &rt_param))) {
106             debugError("Cannot set scheduling priority for RT thread %d %s", res, strerror(errno));
107             return -1;
108         }
109
110         if ((res = pthread_create(&fThread, &attributes, ThreadHandler, this))) {
111             debugError("Cannot set create thread %d %s", res, strerror(errno));
112             return -1;
113         }
114
115         return 0;
116     } else {
117         debugOutput( DEBUG_LEVEL_VERBOSE, "Create non RT thread\n");
118
119         if ((res = pthread_create(&fThread, 0, ThreadHandler, this))) {
120             debugError("Cannot set create thread %d %s", res, strerror(errno));
121             return -1;
122         }
123
124         return 0;
125     }
126 }
127
128 int PosixThread::Kill()
129 {
130     if (fThread) { // If thread has been started
131         debugOutput( DEBUG_LEVEL_VERBOSE, "PosixThread::Kill\n");
132         void* status;
133         pthread_cancel(fThread);
134         pthread_join(fThread, &status);
135         return 0;
136     } else {
137         return -1;
138     }
139 }
140
141 int PosixThread::Stop()
142 {
143     if (fThread) { // If thread has been started
144         debugOutput( DEBUG_LEVEL_VERBOSE, "PosixThread::Stop\n");
145         void* status;
146         fRunning = false; // Request for the thread to stop
147         pthread_join(fThread, &status);
148         return 0;
149     } else {
150         return -1;
151     }
152 }
153
154 int PosixThread::AcquireRealTime()
155 {
156     struct sched_param rtparam;
157     int res;
158
159     if (!fThread)
160         return -1;
161
162     memset(&rtparam, 0, sizeof(rtparam));
163     rtparam.sched_priority = fPriority;
164
165     //if ((res = pthread_setschedparam(fThread, SCHED_FIFO, &rtparam)) != 0) {
166
167     if ((res = pthread_setschedparam(fThread, SCHED_FIFO, &rtparam)) != 0) {
168         debugError("Cannot use real-time scheduling (FIFO/%d) "
169                    "(%d: %s)", rtparam.sched_priority, res,
170                    strerror(res));
171         return -1;
172     }
173     return 0;
174 }
175
176 int PosixThread::AcquireRealTime(int priority)
177 {
178     fPriority = priority;
179     return AcquireRealTime();
180 }
181
182 int PosixThread::DropRealTime()
183 {
184     struct sched_param rtparam;
185     int res;
186
187     if (!fThread)
188         return -1;
189
190     memset(&rtparam, 0, sizeof(rtparam));
191     rtparam.sched_priority = 0;
192
193     if ((res = pthread_setschedparam(fThread, SCHED_OTHER, &rtparam)) != 0) {
194         debugError("Cannot switch to normal scheduling priority(%s)\n", strerror(errno));
195         return -1;
196     }
197     return 0;
198 }
199
200 pthread_t PosixThread::GetThreadID()
201 {
202     return fThread;
203 }
204
205 } // end of namespace
206
Note: See TracBrowser for help on using the browser.