Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for running dedicated built-in mi.Threads #138

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/core/python/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ MI_PY_EXPORT(Thread) {
.def_method(Thread, start)
.def_method(Thread, is_running)
.def_method(Thread, detach)
.def_method(Thread, join)
.def_static_method(Thread, sleep)
.def_static_method(Thread, wait_for_tasks);
.def_method(Thread, join, py::call_guard<py::gil_scoped_release>())
.def_static_method(Thread, sleep, py::call_guard<py::gil_scoped_release>())
.def_static_method(Thread, wait_for_tasks, py::call_guard<py::gil_scoped_release>());

py::class_<ThreadEnvironment>(m, "ThreadEnvironment", D(ThreadEnvironment))
.def(py::init<>());
Expand Down
42 changes: 31 additions & 11 deletions src/core/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,20 @@ std::atomic<uint32_t> WorkerThread::m_counter{0};
struct ThreadNotifier {
ThreadNotifier() {
// Do not register the main thread
if (m_counter > 0)
Thread::register_external_thread("wrk");
if (m_counter > 0 && !self)
registered_external = Thread::register_external_thread("wrk");
else
registered_external = false;
m_counter++;
}
~ThreadNotifier() {
if (self)
if (registered_external)
Thread::unregister_external_thread();
m_counter--;
}
void ensure_initialized() {}
static std::atomic<uint32_t> m_counter;
bool registered_external;
};

std::atomic<uint32_t> ThreadNotifier::m_counter{0};
Expand All @@ -115,7 +118,7 @@ struct Thread::ThreadPrivate {
bool external_thread = false;
bool critical = false;
int core_affinity = -1;
Thread::EPriority priority;
Thread::EPriority priority = Thread::ENormalPriority;
ref<Logger> logger;
ref<Thread> parent;
ref<FileResolver> fresolver;
Expand Down Expand Up @@ -392,14 +395,27 @@ void Thread::start() {
void Thread::dispatch() {
d->native_handle = d->thread.native_handle();

uint32_t id = thread_ctr++;
#if defined(__linux__) || defined(__APPLE__)
pthread_setspecific(this_thread_id, reinterpret_cast<void *>(id));
#elif defined(_WIN32)
this_thread_id = id;
#endif

struct WorkerSelf {
ref<Thread> self_ = self;
~WorkerSelf() {
// Avoid replaced Thread identity crashing with warning on destruction
if (self_)
self_->d->running = false;
}
} worker_identity;
self = this;
if (worker_identity.self_) {
worker_identity.self_->dec_ref();
notifier.registered_external = false;
}
else {
Comment on lines +410 to +411
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
else {
} else {

uint32_t id = thread_ctr++;
#if defined(__linux__) || defined(__APPLE__)
pthread_setspecific(this_thread_id, reinterpret_cast<void *>(id));
#elif defined(_WIN32)
this_thread_id = id;
#endif
}

if (d->priority != ENormalPriority)
set_priority(d->priority);
Expand Down Expand Up @@ -487,13 +503,17 @@ bool Thread::register_external_thread(const std::string &prefix) {
set_thread_name_(thread_name.c_str());
#endif

self->inc_ref();
return true;
}

bool Thread::unregister_external_thread() {
if (!self || !self->d->external_thread)
return false;
self->d->running = false;
Thread* worker = self;
self = nullptr;
worker->dec_ref();
return true;
}

Expand Down