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