Files
rocm-systems/rocclr/runtime/thread/thread.cpp
T

166 خطوط
3.2 KiB
C++

2014-07-04 16:17:05 -04:00
//
// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved.
//
#include "thread/thread.hpp"
#include "thread/semaphore.hpp"
#include "thread/monitor.hpp"
#include "os/os.hpp"
#if defined(_WIN32) || defined(__CYGWIN__)
#include <windows.h>
#endif // _WIN32
2014-07-04 16:17:05 -04:00
namespace amd {
HostThread::HostThread() : Thread("HostThread", 0, false) {
setCurrent();
Os::currentStackInfo(&stackBase_, &stackSize_);
setState(RUNNABLE);
2014-07-04 16:17:05 -04:00
}
void Thread::create() {
created_ = new Semaphore();
lock_ = new Semaphore();
suspend_ = new Semaphore();
2014-07-04 16:17:05 -04:00
selfSuspendLock_ = new Monitor();
2014-07-04 16:17:05 -04:00
data_ = NULL;
handle_ = NULL;
setState(CREATED);
2014-07-04 16:17:05 -04:00
}
Thread::Thread(const std::string& name, size_t stackSize, bool spawn)
: handle_(NULL), name_(name), stackSize_(stackSize) {
create();
2014-07-04 16:17:05 -04:00
if (!spawn) return;
2014-07-04 16:17:05 -04:00
if ((handle_ = Os::createOsThread(this))) {
// Now we need to wait for Thread::main to report back.
while (state() != Thread::INITIALIZED) {
created_->wait();
2014-07-04 16:17:05 -04:00
}
}
2014-07-04 16:17:05 -04:00
}
Thread::~Thread() {
2014-07-04 16:17:05 -04:00
#if defined(_WIN32)
if (handle_ != NULL) {
::CloseHandle((HANDLE)handle_);
}
2014-07-04 16:17:05 -04:00
#endif
delete created_;
delete lock_;
delete suspend_;
2014-07-04 16:17:05 -04:00
delete selfSuspendLock_;
2014-07-04 16:17:05 -04:00
}
void* Thread::main() {
2014-07-04 16:17:05 -04:00
#ifdef DEBUG
Os::setCurrentThreadName(name().c_str());
#endif // DEBUG
Os::currentStackInfo(&stackBase_, &stackSize_);
setCurrent();
2014-07-04 16:17:05 -04:00
// Notify the parent thread that we are up and running.
{
ScopedLock sl(selfSuspendLock_);
setState(INITIALIZED);
created_->post();
selfSuspendLock_->wait();
}
2014-07-04 16:17:05 -04:00
if (state() == RUNNABLE) {
run(data_);
}
setState(FINISHED);
return NULL;
2014-07-04 16:17:05 -04:00
}
bool Thread::start(void* data) {
if (state() != INITIALIZED) {
return false;
}
2014-07-04 16:17:05 -04:00
data_ = data;
{
ScopedLock sl(selfSuspendLock_);
setState(RUNNABLE);
selfSuspendLock_->notify();
}
2014-07-04 16:17:05 -04:00
return true;
2014-07-04 16:17:05 -04:00
}
void Thread::resume() {
ScopedLock sl(selfSuspendLock_);
selfSuspendLock_->notify();
2014-07-04 16:17:05 -04:00
}
#if defined(__linux__)
namespace details {
__thread Thread* thread_ __attribute__((tls_model("initial-exec")));
} // namespace details
2014-07-04 16:17:05 -04:00
void Thread::registerStack(address base, address top) {
// Nothing to do.
2014-07-04 16:17:05 -04:00
}
void Thread::setCurrent() { details::thread_ = this; }
2014-07-04 16:17:05 -04:00
#elif defined(_WIN32)
namespace details {
#if defined(USE_DECLSPEC_THREAD)
__declspec(thread) Thread* thread_;
#else // !USE_DECLSPEC_THREAD
2014-07-04 16:17:05 -04:00
DWORD threadIndex_ = TlsAlloc();
#endif // !USE_DECLSPEC_THREAD
2014-07-04 16:17:05 -04:00
} // namespace details
2014-07-04 16:17:05 -04:00
void Thread::registerStack(address base, address top) {
// Nothing to do.
2014-07-04 16:17:05 -04:00
}
void Thread::setCurrent() {
2014-07-04 16:17:05 -04:00
#if defined(USE_DECLSPEC_THREAD)
details::thread_ = this;
#else // !USE_DECLSPEC_THREAD
TlsSetValue(details::threadIndex_, this);
#endif // !USE_DECLSPEC_THREAD
2014-07-04 16:17:05 -04:00
}
#endif
bool Thread::init() {
static bool initialized_ = false;
2014-07-04 16:17:05 -04:00
// We could use InitOnceExecuteOnce/pthread_once here:
if (initialized_) {
return true;
}
initialized_ = true;
2014-07-04 16:17:05 -04:00
// Register the main thread
return NULL != new HostThread();
2014-07-04 16:17:05 -04:00
}
void Thread::tearDown() {
2014-07-04 16:17:05 -04:00
#if defined(_WIN32) && !defined(USE_DECLSPEC_THREAD)
if (details::threadIndex_ != TLS_OUT_OF_INDEXES) {
TlsFree(threadIndex_);
}
#endif // _WIN32 && !USE_DECLSPEC_THREAD
2014-07-04 16:17:05 -04:00
}
} // namespace amd