044062a046e44e6bf1b48da19b4e913e55a8d5c3
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / architecture / osclib / faust / src / threads / pthreads_impl.cpp
1 /*
2
3 Copyright (C) 2011 Grame
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
20 research@grame.fr
21
22 */
23
24 #ifdef WIN32
25
26 #else
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32
33 #include "TThreads.h"
34
35
36 //_____________________________________________________________________
37 static void * baseThreadProc (void * ptr)
38 {
39 TThreads* thread = (TThreads*)ptr;
40 thread->running (true);
41 thread->run();
42 thread->running (false);
43 return 0;
44 }
45
46 //_____________________________________________________________________
47 TThreads::TThreads () : fRunning(false), fThread(0) {}
48
49 //_____________________________________________________________________
50 int TThreads::SetPriority (int priority)
51 {
52 if (fThread) {
53 struct sched_param param;
54
55 param.sched_priority = priority;
56 if (!pthread_setschedparam (fThread, SCHED_OTHER, &param))
57 return true;
58 }
59 return false;
60 }
61
62 //_____________________________________________________________________
63 bool TThreads::start (int priority)
64 {
65 int ret = pthread_create(&fThread, NULL, baseThreadProc, this);
66 if (!ret) {
67 SetPriority (priority);
68 return true;
69 }
70 return false;
71 }
72
73 //_____________________________________________________________________
74 void TThreads::quit ()
75 {
76 if (fThread) {
77 void *threadRet;
78 pthread_cancel (fThread);
79 pthread_join (fThread, &threadRet);
80 fThread = 0;
81 }
82 }
83 #endif