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
10Thread::Thread() {
11  pid_ = 0;
12  running_ = 0;
13  joinable_ = 0;
14}
15
16Thread::~Thread() {
17}
18
19void *startThread(void *v) {
20  Thread* t = (Thread*)v;
21  t->Run();
22  return 0;
23}
24
25void Thread::Start() {
26  CHECK(!running_);
27  pthread_create(&pid_, 0, startThread, this);
28  running_ = true;
29  if (!joinable_)
30    pthread_detach(pid_);
31}
32
33void Thread::Join() {
34  CHECK(running_);
35  CHECK(joinable_);
36  void *val;
37  pthread_join(pid_, &val);
38  running_ = 0;
39}
40
41void Thread::SetJoinable(bool j) {
42  CHECK(!running_);
43  joinable_ = j;
44}
45