2021-07-02 14:41:38 -07:00
|
|
|
/* Copyright (c) 2008 - 2021 Advanced Micro Devices, Inc.
|
2020-02-04 09:26:14 -08:00
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE. */
|
2014-07-04 16:17:05 -04:00
|
|
|
|
|
|
|
|
#include "thread/thread.hpp"
|
|
|
|
|
#include "thread/monitor.hpp"
|
|
|
|
|
#include "os/os.hpp"
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
2017-04-13 13:56:38 -04:00
|
|
|
#include <windows.h>
|
|
|
|
|
#endif // _WIN32
|
2014-07-04 16:17:05 -04:00
|
|
|
|
|
|
|
|
namespace amd {
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
void Thread::create() {
|
|
|
|
|
selfSuspendLock_ = new Monitor();
|
|
|
|
|
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)
|
2017-04-13 13:56:38 -04:00
|
|
|
: handle_(NULL), name_(name), stackSize_(stackSize) {
|
|
|
|
|
create();
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
if (!spawn) return;
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
if ((handle_ = Os::createOsThread(this))) {
|
2025-04-29 09:55:24 -04:00
|
|
|
// Now we need to wait for Thread::main() to report back.
|
|
|
|
|
ScopedLock sl(selfSuspendLock_);
|
|
|
|
|
// Wait for main() to update state as INITIALIZED
|
2017-04-13 13:56:38 -04:00
|
|
|
while (state() != Thread::INITIALIZED) {
|
2025-04-29 09:55:24 -04:00
|
|
|
selfSuspendLock_->wait();
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
2017-04-13 13:56:38 -04:00
|
|
|
}
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
Thread::~Thread() {
|
2014-07-04 16:17:05 -04:00
|
|
|
#if defined(_WIN32)
|
2017-04-13 13:56:38 -04:00
|
|
|
if (handle_ != NULL) {
|
|
|
|
|
::CloseHandle((HANDLE)handle_);
|
|
|
|
|
}
|
2014-07-04 16:17:05 -04:00
|
|
|
#endif
|
2017-04-13 13:56:38 -04:00
|
|
|
delete selfSuspendLock_;
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
void* Thread::main() {
|
2014-07-04 16:17:05 -04:00
|
|
|
#ifdef DEBUG
|
2017-04-13 13:56:38 -04:00
|
|
|
Os::setCurrentThreadName(name().c_str());
|
|
|
|
|
#endif // DEBUG
|
|
|
|
|
Os::currentStackInfo(&stackBase_, &stackSize_);
|
|
|
|
|
setCurrent();
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
// Notify the parent thread that we are up and running.
|
|
|
|
|
{
|
|
|
|
|
ScopedLock sl(selfSuspendLock_);
|
2025-04-29 09:55:24 -04:00
|
|
|
// Notify parent thread that the state is update as INITIALIZED
|
2017-04-13 13:56:38 -04:00
|
|
|
setState(INITIALIZED);
|
2025-04-29 09:55:24 -04:00
|
|
|
selfSuspendLock_->notify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now we need to wait for Thread::start() to report back.
|
|
|
|
|
{
|
|
|
|
|
ScopedLock sl(selfSuspendLock_);
|
|
|
|
|
// wait for start() to update state as RUNNABLE
|
|
|
|
|
while (state() != Thread::RUNNABLE) {
|
|
|
|
|
selfSuspendLock_->wait();
|
|
|
|
|
}
|
2017-04-13 13:56:38 -04:00
|
|
|
}
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2025-04-29 09:55:24 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
if (state() == RUNNABLE) {
|
|
|
|
|
run(data_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setState(FINISHED);
|
|
|
|
|
return NULL;
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
bool Thread::start(void* data) {
|
|
|
|
|
if (state() != INITIALIZED) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
data_ = data;
|
2025-04-29 09:55:24 -04:00
|
|
|
// Notify the thread that the parent thread are up and running.
|
2017-04-13 13:56:38 -04:00
|
|
|
{
|
|
|
|
|
ScopedLock sl(selfSuspendLock_);
|
2025-04-29 09:55:24 -04:00
|
|
|
// Notify main() that the state is updated as RUNNABLE
|
2017-04-13 13:56:38 -04:00
|
|
|
setState(RUNNABLE);
|
|
|
|
|
selfSuspendLock_->notify();
|
|
|
|
|
}
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
return true;
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-13 13:56:38 -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")));
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
} // namespace details
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
void Thread::registerStack(address base, address top) {
|
|
|
|
|
// Nothing to do.
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-13 13:56:38 -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_;
|
2017-04-13 13:56:38 -04:00
|
|
|
#else // !USE_DECLSPEC_THREAD
|
2014-07-04 16:17:05 -04:00
|
|
|
DWORD threadIndex_ = TlsAlloc();
|
2017-04-13 13:56:38 -04:00
|
|
|
#endif // !USE_DECLSPEC_THREAD
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
} // namespace details
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
void Thread::registerStack(address base, address top) {
|
|
|
|
|
// Nothing to do.
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
void Thread::setCurrent() {
|
2014-07-04 16:17:05 -04:00
|
|
|
#if defined(USE_DECLSPEC_THREAD)
|
2017-04-13 13:56:38 -04:00
|
|
|
details::thread_ = this;
|
|
|
|
|
#else // !USE_DECLSPEC_THREAD
|
|
|
|
|
TlsSetValue(details::threadIndex_, this);
|
|
|
|
|
#endif // !USE_DECLSPEC_THREAD
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
bool Thread::init() {
|
|
|
|
|
static bool initialized_ = false;
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
// We could use InitOnceExecuteOnce/pthread_once here:
|
|
|
|
|
if (initialized_) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
initialized_ = true;
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2025-04-29 09:55:24 -04:00
|
|
|
return true;
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
void Thread::tearDown() {
|
2014-07-04 16:17:05 -04:00
|
|
|
#if defined(_WIN32) && !defined(USE_DECLSPEC_THREAD)
|
2017-04-13 13:56:38 -04:00
|
|
|
if (details::threadIndex_ != TLS_OUT_OF_INDEXES) {
|
|
|
|
|
TlsFree(threadIndex_);
|
|
|
|
|
}
|
|
|
|
|
#endif // _WIN32 && !USE_DECLSPEC_THREAD
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
} // namespace amd
|