CARMA C++
Thread.h
Go to the documentation of this file.
1 #ifndef thread_h
2 #define thread_h
3 
11 #include <iostream>
12 #include <pthread.h>
13 #include <string>
14 #include <unistd.h>
15 
16 // A thread start-up function
17 
18 #define THREAD_START(fn) void* (fn)(void *arg)
19 
20 // A thread shutdown function
21 
22 #define THREAD_STOP(fn) void (fn)(void *arg)
23 
24 // A thread cleanup function
25 
26 #define THREAD_CLEAN(fn) void (fn)(void *arg)
27 
28 // A method by which we can ping this thread
29 
30 #define THREAD_PING(fn) void (fn)(void *arg)
31 
32 // A pair of macros which we will use to safely lock/unlock a mutex.
33 // Also includes a fix to work-around the bug under kernel 2.4.20 that
34 // CORBA calls scramble the cancellation type.
35 
36 #define INSTALL_MUTEX_CLEANUP(mutex, logStream) \
37 {\
38  int oldtype;\
39  if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype) != 0) \
40  logStream.appendSysError(true, "pthread_setcanceltype");\
41  pthread_cleanup_push(&Thread::unlockMutex, (void *) (&mutex));\
42  if(oldtype != PTHREAD_CANCEL_DEFERRED && oldtype != PTHREAD_CANCEL_ASYNCHRONOUS) ;\
43  oldtype = PTHREAD_CANCEL_ASYNCHRONOUS
44 
45 #define UNINSTALL_MUTEX_CLEANUP(logStream) \
46  pthread_cleanup_pop(0);\
47  if(pthread_setcanceltype(oldtype, NULL) != 0) \
48  logStream.appendSysError(true, "pthread_setcanceltype");\
49 }
50 
51 namespace sza {
52  namespace util {
53 
57  class Thread {
58 
59  public:
60 
72  void start(void* arg);
73 
79  void cancel();
80 
86  void ping(void* arg);
87 
93  void broadcastReady();
94 
100  void broadcastDone();
101 
108  void setRunState(bool state);
109 
114  bool matchName(std::string compname);
115 
119  std::string strName();
120 
138  Thread(THREAD_START(*startFn), THREAD_CLEAN(*cleanFn),
139  THREAD_PING(*pingFn), std::string name);
140 
145  Thread(THREAD_START(*startFn), THREAD_CLEAN(*cleanFn),
146  THREAD_PING(*pingFn), std::string name,
147  unsigned startOrder, unsigned cancelOrder);
148 
152  ~Thread();
153 
157  bool isRunning();
158 
162  bool isPingable();
163 
167  void raise(int sigNo);
168 
173  static THREAD_CLEAN(unlockMutex);
174 
178  bool wasError_;
179 
180  inline unsigned startOrder() {
181  return startOrder_;
182  }
183 
184  inline unsigned cancelOrder() {
185  return cancelOrder_;
186  }
187 
188  void waitUntilReady();
189 
190  inline std::string name() {
191  return name_;
192  };
193 
194  private:
195 
196  unsigned startOrder_;
197  unsigned cancelOrder_;
198 
203  void* startupArg_;
204 
209  bool running_;
210 
214  pthread_mutex_t runningGuard_;
215 
219  pthread_t id_;
220 
225  struct CondVar {
226 
230  pthread_mutex_t guard;
231 
235  pthread_cond_t cond;
236  };
237 
242  CondVar ready_;
243 
248  CondVar done_;
249 
253  std::string name_;
254 
258  void privateConstructor(THREAD_START(*startFn),
259  THREAD_CLEAN(*cleanFn),
260  THREAD_PING(*pingFn),
261  std::string name,
262  unsigned startOrder, unsigned cancelOrder);
263 
267  static THREAD_START(startThread);
268 
272  static THREAD_CLEAN(cleanThread);
273 
279  THREAD_START(*userStartFn_);
280 
286  THREAD_CLEAN(*userCleanFn_);
287 
292  THREAD_PING(*userPingFn_);
293 
298  bool setRunStatePrivate(bool state);
299 
300  }; // End class Thread
301 
302  }; // End namespace util
303 }; // End namespace sza
304 
305 #endif
306 
307 
308 
309 
310 
311 
312 
313 
314 
void start(void *arg)
Function by which a caller can start up this thread.
void ping(void *arg)
Function by which the control thread can ping this thread.
void broadcastReady()
Let other threads know we are ready.
static void() unlockMutex(void *arg)
A wrapper around pthread_mutex_unlock() suitable for passing to pthread_cleanup_push() ...
std::string strName()
Print the name of this thread.
Thread(void *(*startFn)(void *arg), void(*cleanFn)(void *arg), void(*pingFn)(void *arg), std::string name)
Constructor method.
void setRunState(bool state)
Function by which we can safely alter the running state of this thread.
void cancel()
Calls pthread_cancel() for this thread.
~Thread()
Destructor method.
bool wasError_
A value to be returned if an error occurs on startup.
Definition: Thread.h:178
bool matchName(std::string compname)
Return true if the passed argument matches the name of this thread.
bool isPingable()
Return true if a ping function has been installed.
bool isRunning()
Return true once this thread is running.
Define a class to encapsulate thread handling.
Definition: Thread.h:57
void broadcastDone()
Let other threads know we are done.