Ian Hodson | 2ee91b4 | 2012-05-14 12:29:36 +0100 | [diff] [blame] | 1 | // Copyright 2009 The RE2 Authors. All Rights Reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | #include <pthread.h> |
| 6 | |
| 7 | #include "util/util.h" |
| 8 | #include "util/thread.h" |
| 9 | |
| 10 | Thread::Thread() { |
| 11 | pid_ = 0; |
| 12 | running_ = 0; |
| 13 | joinable_ = 0; |
| 14 | } |
| 15 | |
| 16 | Thread::~Thread() { |
| 17 | } |
| 18 | |
| 19 | void *startThread(void *v) { |
| 20 | Thread* t = (Thread*)v; |
| 21 | t->Run(); |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | void Thread::Start() { |
| 26 | CHECK(!running_); |
| 27 | pthread_create(&pid_, 0, startThread, this); |
| 28 | running_ = true; |
| 29 | if (!joinable_) |
| 30 | pthread_detach(pid_); |
| 31 | } |
| 32 | |
| 33 | void Thread::Join() { |
| 34 | CHECK(running_); |
| 35 | CHECK(joinable_); |
| 36 | void *val; |
| 37 | pthread_join(pid_, &val); |
| 38 | running_ = 0; |
| 39 | } |
| 40 | |
| 41 | void Thread::SetJoinable(bool j) { |
| 42 | CHECK(!running_); |
| 43 | joinable_ = j; |
| 44 | } |