Try to get a bit better at detaching threads that have used JNI from the VM.

This commit is contained in:
Henrik Rydgård committed 2023-01-05 08:38:49 +01:00
1 parent a73ccd77e3
commit 916404f0e4
8 files changed
+71 -7

No files matched your search

+7 -4
View File
@@ -44,6 +44,7 @@ struct ThreadContext {
std::atomic<bool> cancelled;
std::atomic<Task *> private_single;
std::deque<Task *> private_queue;
char name[16];
};
ThreadManager::ThreadManager() : global_(new GlobalThreadContext()) {
@@ -124,14 +125,13 @@ bool ThreadManager::TeardownTask(Task *task, bool enqueue) {
}
static void WorkerThreadFunc(GlobalThreadContext *global, ThreadContext *thread) {
char threadName[16];
if (thread->type == TaskType::CPU_COMPUTE) {
snprintf(threadName, sizeof(threadName), "PoolWorker %d", thread->index);
snprintf(thread->name, sizeof(thread->name), "PoolWorker %d", thread->index);
} else {
_assert_(thread->type == TaskType::IO_BLOCKING);
snprintf(threadName, sizeof(threadName), "PoolWorkerIO %d", thread->index);
snprintf(thread->name, sizeof(thread->name), "PoolWorkerIO %d", thread->index);
}
SetCurrentThreadName(threadName);
SetCurrentThreadName(thread->name);
const bool isCompute = thread->type == TaskType::CPU_COMPUTE;
const auto global_queue_size = [isCompute, &global]() -> int {
@@ -185,6 +185,9 @@ static void WorkerThreadFunc(GlobalThreadContext *global, ThreadContext *thread)
thread->queue_size--;
}
}
// In case it got attached to JNI, detach it. Don't think this has any side effects if called redundantly.
DetachThreadFromJNI();
}
void ThreadManager::Init(int numRealCores, int numLogicalCoresPerCpu) {