[K/N] Add utilities to check if thread is registered now

This commit is contained in:
Pavel Kunyavskiy
2021-06-09 13:28:51 +03:00
committed by Space
parent aff9d96e8a
commit 1db2903e4f
4 changed files with 9 additions and 2 deletions
@@ -192,13 +192,17 @@ struct DestructorRecord {
static void onThreadExitCallback(void* value) { static void onThreadExitCallback(void* value) {
DestructorRecord* record = reinterpret_cast<DestructorRecord*>(value); DestructorRecord* record = reinterpret_cast<DestructorRecord*>(value);
pthread_setspecific(terminationKey, nullptr);
while (record != nullptr) { while (record != nullptr) {
record->destructor(record->destructorParameter); record->destructor(record->destructorParameter);
auto next = record->next; auto next = record->next;
free(record); free(record);
record = next; record = next;
} }
pthread_setspecific(terminationKey, nullptr); }
bool isOnThreadExitNotSetOrAlreadyStarted() {
return terminationKey != 0 && pthread_getspecific(terminationKey) == nullptr;
} }
#if KONAN_LINUX #if KONAN_LINUX
@@ -41,6 +41,7 @@ RUNTIME_NORETURN void exit(int32_t status);
// Thread control. // Thread control.
void onThreadExit(void (*destructor)(void*), void* destructorParameter); void onThreadExit(void (*destructor)(void*), void* destructorParameter);
bool isOnThreadExitNotSetOrAlreadyStarted();
// String/byte operations. // String/byte operations.
// memcpy/memmove/memcmp are not here intentionally, as frequently implemented/optimized // memcpy/memmove/memcmp are not here intentionally, as frequently implemented/optimized
@@ -20,7 +20,7 @@ mm::ThreadRegistry::Node* mm::ThreadRegistry::RegisterCurrentThread() noexcept {
auto* threadDataNode = list_.Emplace(pthread_self()); auto* threadDataNode = list_.Emplace(pthread_self());
AssertThreadState(threadDataNode->Get(), ThreadState::kNative); AssertThreadState(threadDataNode->Get(), ThreadState::kNative);
Node*& currentDataNode = currentThreadDataNode_; Node*& currentDataNode = currentThreadDataNode_;
RuntimeAssert(currentDataNode == nullptr, "This thread already had some data assigned to it."); RuntimeAssert(!IsCurrentThreadRegistered(), "This thread already had some data assigned to it.");
currentDataNode = threadDataNode; currentDataNode = threadDataNode;
return threadDataNode; return threadDataNode;
} }
@@ -41,6 +41,8 @@ public:
ALWAYS_INLINE ThreadData* CurrentThreadData() const noexcept; ALWAYS_INLINE ThreadData* CurrentThreadData() const noexcept;
Node* CurrentThreadDataNode() const noexcept { return currentThreadDataNode_; } Node* CurrentThreadDataNode() const noexcept { return currentThreadDataNode_; }
bool IsCurrentThreadRegistered() const noexcept { return currentThreadDataNode_ != nullptr; }
class TestSupport { class TestSupport {
public: public:
static void ClearCurrentThreadData() { currentThreadDataNode_ = nullptr; } static void ClearCurrentThreadData() { currentThreadDataNode_ = nullptr; }