18 #define THREAD_START(fn) void* (fn)(void *arg)
22 #define THREAD_STOP(fn) void (fn)(void *arg)
26 #define THREAD_CLEAN(fn) void (fn)(void *arg)
30 #define THREAD_PING(fn) void (fn)(void *arg)
36 #define INSTALL_MUTEX_CLEANUP(mutex, logStream) \
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
45 #define UNINSTALL_MUTEX_CLEANUP(logStream) \
46 pthread_cleanup_pop(0);\
47 if(pthread_setcanceltype(oldtype, NULL) != 0) \
48 logStream.appendSysError(true, "pthread_setcanceltype");\
72 void start(
void* arg);
138 Thread(THREAD_START(*startFn), THREAD_CLEAN(*cleanFn),
139 THREAD_PING(*pingFn), std::string name);
145 Thread(THREAD_START(*startFn), THREAD_CLEAN(*cleanFn),
146 THREAD_PING(*pingFn), std::string name,
147 unsigned startOrder,
unsigned cancelOrder);
167 void raise(
int sigNo);
180 inline unsigned startOrder() {
184 inline unsigned cancelOrder() {
188 void waitUntilReady();
190 inline std::string name() {
196 unsigned startOrder_;
197 unsigned cancelOrder_;
214 pthread_mutex_t runningGuard_;
230 pthread_mutex_t guard;
258 void privateConstructor(THREAD_START(*startFn),
259 THREAD_CLEAN(*cleanFn),
260 THREAD_PING(*pingFn),
262 unsigned startOrder,
unsigned cancelOrder);
267 static THREAD_START(startThread);
272 static THREAD_CLEAN(cleanThread);
279 THREAD_START(*userStartFn_);
286 THREAD_CLEAN(*userCleanFn_);
292 THREAD_PING(*userPingFn_);
298 bool setRunStatePrivate(
bool state);
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.
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.
void broadcastDone()
Let other threads know we are done.