blob: fab62eb178a79f01090ee82c9f3662f38eeaf5ef [file] [log] [blame]
Brian Silverman798c7782013-03-28 16:48:02 -07001#include "aos/common/util/thread.h"
2
3#include <pthread.h>
4#include <assert.h>
5
6namespace aos {
7namespace util {
8
9Thread::Thread() : started_(false), joined_(false), should_terminate_(false) {}
10
11Thread::~Thread() {
12 if (started_ && !joined_) {
13 assert(false);
14 }
15}
16
17void Thread::Start() {
18 assert(!started_);
19 started_ = true;
20 assert(pthread_create(&thread_, NULL, &Thread::StaticRun, this) == 0);
21}
22
23void Thread::Join() {
24 assert(!joined_ && started_);
25 joined_ = true;
26 {
27 MutexLocker locker(&should_terminate_mutex_);
28 should_terminate_ = true;
29 }
30 assert(pthread_join(thread_, NULL) == 0);
31}
32
33void *Thread::StaticRun(void *self) {
34 static_cast<Thread *>(self)->Run();
35 return NULL;
36}
37
38} // namespace util
39} // namespace aos