diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2016-01-16 17:25:59 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2016-01-16 17:59:49 +0100 |
commit | 87bafa33dc6b1d58a67147a802bd498f9a2f80d6 (patch) | |
tree | e6c6e048d4f49e1c72bcb16ad7cec83a917850ae /plugins/tests/test-port-context.c | |
parent | 99ae6777893d0b149cbefac3f290c63c47e29f42 (diff) |
tests,port-context: properly setup per-thread main context
Instead of creating a new main context to be used in the thread, we were using
the global context. So, fix that, and create a totally new pair of main context
and main loop to be used within the thread.
Diffstat (limited to 'plugins/tests/test-port-context.c')
-rw-r--r-- | plugins/tests/test-port-context.c | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/plugins/tests/test-port-context.c b/plugins/tests/test-port-context.c index de8e6d3c..d3a35728 100644 --- a/plugins/tests/test-port-context.c +++ b/plugins/tests/test-port-context.c @@ -28,6 +28,7 @@ struct _TestPortContext { GCond ready_cond; GMutex ready_mutex; GMainLoop *loop; + GMainContext *context; GSocketService *socket_service; GList *clients; GHashTable *commands; @@ -235,7 +236,7 @@ client_new (TestPortContext *self, (GSourceFunc)connection_readable_cb, client, NULL); - g_source_attach (client->connection_readable_source, NULL); + g_source_attach (client->connection_readable_source, self->context); return client; } @@ -313,13 +314,23 @@ create_socket_service (TestPortContext *self) /*****************************************************************************/ +static gboolean +cancel_loop_cb (TestPortContext *self) +{ + g_main_loop_quit (self->loop); + return FALSE; +} + void test_port_context_stop (TestPortContext *self) { g_assert (self->thread != NULL); g_assert (self->loop != NULL); + g_assert (self->context != NULL); - g_main_loop_quit (self->loop); + /* Cancel main loop of the port context thread, by scheduling an idle task + * in the thread-owned main context */ + g_main_context_invoke (self->context, (GSourceFunc) cancel_loop_cb, self); g_thread_join (self->thread); self->thread = NULL; @@ -328,13 +339,23 @@ test_port_context_stop (TestPortContext *self) static gpointer port_context_thread_func (TestPortContext *self) { + g_assert (self->loop == NULL); + g_assert (self->context == NULL); + + /* Define main context and loop for the thread */ + self->context = g_main_context_new (); + self->loop = g_main_loop_new (self->context, FALSE); + g_main_context_push_thread_default (self->context); + + /* Once the thread default context is setup, launch service */ create_socket_service (self); - g_assert (self->loop == NULL); - self->loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE); g_main_loop_run (self->loop); + g_main_loop_unref (self->loop); self->loop = NULL; + g_main_context_unref (self->context); + self->context = NULL; return NULL; } |