CommonLibs: Add thread cancellation capability

For clean shutdown in the transceiver we need to cancel and join
running threads for orderly unwinding. Thread cancellation points
already exist, so we just need to be able to call on the threads to
exit out when stopping or shutting down.

Don't error when joining a NULL thread, which would be the case if a
thread was stopped before ever being started to begin with.

Signed-off-by: Tom Tsou <tom@tsou.cc>
This commit is contained in:
Tom Tsou 2014-11-21 12:25:22 -08:00
parent 1ae25561fa
commit b999759175
1 changed files with 8 additions and 1 deletions

View File

@ -172,8 +172,15 @@ class Thread {
void start(void *(*task)(void*), void *arg);
/** Join a thread that will stop on its own. */
void join() { int s = pthread_join(mThread,NULL); assert(!s); mThread = 0; }
void join() {
if (mThread) {
int s = pthread_join(mThread, NULL);
assert(!s);
}
}
/** Send cancelation to thread */
void cancel() { pthread_cancel(mThread); }
};