#include "pls/internal/base/thread.h"

namespace pls {
namespace internal {
namespace base {

thread::thread() :
    pthread_thread_{},
    running_{false} {}

thread::~thread() {
  if (running_) {
    join();
  }
}

thread::thread(thread &&other) noexcept :
    pthread_thread_{other.pthread_thread_},
    running_{other.running_} {
  other.running_ = false;
}

thread &thread::operator=(thread &&other) noexcept {
  this->pthread_thread_ = other.pthread_thread_;
  this->running_ = other.running_;

  other.running_ = false;

  return *this;
}

#ifdef PLS_THREAD_SPECIFIC_PTHREAD
pthread_key_t this_thread::local_storage_key_ = false;
bool this_thread::local_storage_key_initialized_;
#endif
#ifdef PLS_THREAD_SPECIFIC_COMPILER
__thread void *this_thread::local_state_;
#endif

void thread::join() {
  pthread_join(pthread_thread_, nullptr);
  running_ = false;
}

}
}
}
