EventLoop: add methodd IsInside()
Track which thread runs the EventLoop and provide a check whether we're currently inside.
This commit is contained in:
parent
c043b337b1
commit
9ab0a1f5f1
@ -23,7 +23,12 @@
|
|||||||
void
|
void
|
||||||
EventLoop::Run()
|
EventLoop::Run()
|
||||||
{
|
{
|
||||||
|
assert(thread == nullptr);
|
||||||
|
thread = g_thread_self();
|
||||||
|
|
||||||
g_main_loop_run(loop);
|
g_main_loop_run(loop);
|
||||||
|
|
||||||
|
assert(thread == g_thread_self());
|
||||||
}
|
}
|
||||||
|
|
||||||
guint
|
guint
|
||||||
|
@ -25,25 +25,44 @@
|
|||||||
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
class EventLoop {
|
class EventLoop {
|
||||||
GMainContext *context;
|
GMainContext *context;
|
||||||
GMainLoop *loop;
|
GMainLoop *loop;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A reference to the thread that is currently inside Run().
|
||||||
|
*/
|
||||||
|
GThread *thread;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EventLoop()
|
EventLoop()
|
||||||
:context(g_main_context_new()),
|
:context(g_main_context_new()),
|
||||||
loop(g_main_loop_new(context, false)) {}
|
loop(g_main_loop_new(context, false)),
|
||||||
|
thread(nullptr) {}
|
||||||
|
|
||||||
struct Default {};
|
struct Default {};
|
||||||
EventLoop(gcc_unused Default _dummy)
|
EventLoop(gcc_unused Default _dummy)
|
||||||
:context(g_main_context_ref(g_main_context_default())),
|
:context(g_main_context_ref(g_main_context_default())),
|
||||||
loop(g_main_loop_new(context, false)) {}
|
loop(g_main_loop_new(context, false)),
|
||||||
|
thread(nullptr) {}
|
||||||
|
|
||||||
~EventLoop() {
|
~EventLoop() {
|
||||||
g_main_loop_unref(loop);
|
g_main_loop_unref(loop);
|
||||||
g_main_context_unref(context);
|
g_main_context_unref(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Are we currently running inside this EventLoop's thread?
|
||||||
|
*/
|
||||||
|
gcc_pure
|
||||||
|
bool IsInside() const {
|
||||||
|
assert(thread != nullptr);
|
||||||
|
|
||||||
|
return g_thread_self() == thread;
|
||||||
|
}
|
||||||
|
|
||||||
GMainContext *GetContext() {
|
GMainContext *GetContext() {
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user