CSL  6.0
ThreadUtilities.cpp
Go to the documentation of this file.
1 //
2 // ThreadUtilities.cpp -- cross-platform Thread Utilities
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 //
5 
6 #include "ThreadUtilities.h"
7 #include "CGestalt.h"
8 
9 using namespace csl;
10 
11 #ifdef USE_JTHREADS
12 
13 // Thread is now concrete
14 
16  return new CThread();
17 }
18 
19 int CThread::createThread(VoidFcnPtr * func, void * args) {
20  (*func)(args); // call my function
21  return 0;
22 }
23 
24 //int CThread::createRealtimeThread(VOIDFCNPTR * func, void * args) {
25 // mFunc = * func;
26 // mArgs = args;
27 // run();
28 // return 0;
29 //}
30 
31 void CThread::run() {
32  logMsg("Start thread");
33  (*mFunc)(mArgs); // call my function
34 }
35 
36 #else // PTHREADS
37 
38 // This CThread uses pthreads
39 
40 CThread::CThread() : mThread(NULL) { }
41 
43  pthread_cancel(mThread);
44 }
45 
47  return new ThreadPthread();
48 }
49 
50 void CThread::stopThread (int timeOutMilliseconds) {
51  pthread_cancel(mThread);
52 }
53 
54 Synch *Synch::MakeSynch() { return new SynchPthread(); }
55 
56 // ~~~~~ SyncPthread Implementation ~~~~~
57 
58 // Constructor
59 
61  pthread_mutex_init(& mMutex, NULL);
62  pthread_cond_init(& mCond, NULL);
63 }
64 
66  pthread_mutex_destroy(&mMutex);
67  pthread_cond_destroy(&mCond);
68 }
69 
70 // The following functions are just wrappers around the pthreads respective commands.
71 
72 int SynchPthread::lock() { return pthread_mutex_lock(&mMutex); }
73 
74 int SynchPthread::unlock() { return pthread_mutex_unlock(&mMutex); }
75 
76 int SynchPthread::condWait() { return pthread_cond_wait(&mCond, &mMutex); }
77 
78 int SynchPthread::condSignal() { return pthread_cond_signal(&mCond); }
79 
80 // ~~~~~ ThreadPthread Implementation ~~~~~
81 
82 // Constructor and destructor for windows and other systems.
83 
84 #ifdef CSL_WINDOWS
85 
86 ThreadPthread::ThreadPthread() : CThread () /*, mThread(NULL)*/ {
87  pthread_attr_init(&mAttributes);
88 }
89 
91  pthread_attr_destroy(&mAttributes);
92 // if (mThread)
93 // pthread_cancel(mThread);
94 }
95 
96 #else // UNIX
97 
99  pthread_attr_init(&mAttributes);
100 }
101 
103  pthread_attr_destroy(&mAttributes);
104  if (mThread)
105  pthread_cancel(mThread);
106 }
107 
108 #endif
109 
110 int ThreadPthread::createThread(VoidFcnPtr * func, void * args) {
111  pthread_attr_setdetachstate(&mAttributes, PTHREAD_CREATE_DETACHED);
112  pthread_attr_setscope(&mAttributes, PTHREAD_SCOPE_SYSTEM);
113  return pthread_create(&mThread, &mAttributes, func, args);
114 }
115 
116 #ifdef CSL_MACOSX_OLD_WAY
117 
118 // Most of the code in these two functions is taken from Apple PlayAudioFile example
119 
120 #include <mach/mach.h> // used for setting policy of thread
121 
122 unsigned GetThreadBasePriority (pthread_t inThread) {
123  thread_basic_info_data_t threadInfo;
124  policy_info_data_t thePolicyInfo;
125  unsigned int count;
126 
127  // get basic info
128  count = THREAD_BASIC_INFO_COUNT;
129  thread_info (pthread_mach_thread_np (inThread), THREAD_BASIC_INFO, (integer_t*)&threadInfo, &count);
130  switch (threadInfo.policy) {
131  case POLICY_TIMESHARE:
132  count = POLICY_TIMESHARE_INFO_COUNT;
133  thread_info(pthread_mach_thread_np (inThread), THREAD_SCHED_TIMESHARE_INFO, (integer_t*)&(thePolicyInfo.ts), &count);
134  return thePolicyInfo.ts.base_priority;
135  break;
136  case POLICY_FIFO:
137  count = POLICY_FIFO_INFO_COUNT;
138  thread_info(pthread_mach_thread_np (inThread), THREAD_SCHED_FIFO_INFO, (integer_t*)&(thePolicyInfo.fifo), &count);
139  if (thePolicyInfo.fifo.depressed) {
140  return thePolicyInfo.fifo.depress_priority;
141  } else {
142  return thePolicyInfo.fifo.base_priority;
143  }
144  break;
145  case POLICY_RR:
146  count = POLICY_RR_INFO_COUNT;
147  thread_info(pthread_mach_thread_np (inThread), THREAD_SCHED_RR_INFO, (integer_t*)&(thePolicyInfo.rr), &count);
148  if (thePolicyInfo.rr.depressed)
149  return thePolicyInfo.rr.depress_priority;
150  else
151  return thePolicyInfo.rr.base_priority;
152  break;
153  }
154  return 0;
155 }
156 
157 int ThreadPthread::createRealtimeThread(VoidFcnPtr * func, void* args) {
158  // first create a thread
159  int value = createThread(func, args);
160  int result;
161  int threadPriority = 62; // the number in Apple's example code
162 
163  // bump up the priority and make it fixed priority
164  // code taken from Apple PlayAudioFile example
165  thread_extended_policy_data_t theFixedPolicy;
166  thread_precedence_policy_data_t thePrecedencePolicy;
167  int relativePriority;
168  // make thread fixed
169  theFixedPolicy.timeshare = false; // set to true for a non-fixed thread
170  result = thread_policy_set (pthread_mach_thread_np(mThread), THREAD_EXTENDED_POLICY, (thread_policy_t)&theFixedPolicy, THREAD_EXTENDED_POLICY_COUNT);
171  // set priority
172  // precedency policy's "importance" value is relative to spawning thread's priority
173  relativePriority = threadPriority - GetThreadBasePriority (pthread_self());
174  thePrecedencePolicy.importance = relativePriority;
175  result = thread_policy_set (pthread_mach_thread_np(mThread), THREAD_PRECEDENCE_POLICY, (thread_policy_t)&thePrecedencePolicy, THREAD_PRECEDENCE_POLICY_COUNT);
176  return value;
177 }
178 
179 #else
180 
181 //int ThreadPthread::createRealtimeThread(VOIDFCNPTR * func, void* args) {
182 // return createThread(func, args);
183 //}
184 
185 #endif
186 
187 #endif
188 
void logMsg(const char *format,...)
These are the public logging messages.
Definition: CGestalt.cpp:292
The PThreads CSL Thread class.
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
pthread_attr_t mAttributes
PThread version of Sync.
static Synch * MakeSynch()
Factory method Utilities.
Sync is a cross-thread synchronization object.
pthread_mutex_t mMutex
pthread_t mThread
void stopThread(int timeOutMilliseconds)
void * VoidFcnPtr(void *arg)
the generic void fcn pointer
Definition: CSL_Types.h:221
virtual ~CThread()
PThread version of Thread.
virtual int createThread(VoidFcnPtr *func, void *args)=0
static CThread * MakeThread()
factory method
int createThread(VoidFcnPtr *func, void *args)
pthread_cond_t mCond