Skip to content

Ensure dbus callbacks are called/run in the correct context #21

@EddyTheCo

Description

@EddyTheCo

The current implementation of the DBus class starts a thread(worker thread) that runs a Glib main loop.

glib_thread_ = std::thread([this]() {
loop_ = g_main_loop_new(nullptr, FALSE);
g_idle_add(&DBus::on_loop_started, this);
g_main_loop_run(loop_);
g_main_loop_unref(loop_);
loop_ = nullptr;
});

The purpose was to run all DBus asynchronous methods and callbacks in this thread.
The loop runs using the global-default instead of the thread-default context (more on this).

This causes the fact that if the consumer of this library creates a GMainLoop before the library loop is created on the worker thread, the callbacks are run on the thread that created the GMainLoop in the consumer.
The latter is not a problem, but it is not the purpose of the DBus class, and the worker thread is created for nothing.
It also changes where the callbacks are executed, depending on whether the consumer creates a GMainLoop or not.

Forcing to run callbacks on the worker thread should be investigated, maybe using https://docs.gtk.org/glib/method.MainContext.push_thread_default.html

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions