io_thread: move global variables into a struct

This commit is contained in:
Max Kellermann 2011-08-25 18:27:10 +02:00
parent 89355edb8a
commit 0f1e4f0326

View File

@ -21,40 +21,42 @@
#include <assert.h> #include <assert.h>
static GMainContext *io_context; static struct {
static GMainLoop *io_loop; GMainContext *context;
static GThread *io_thread; GMainLoop *loop;
GThread *thread;
} io;
static gpointer static gpointer
io_thread_func(G_GNUC_UNUSED gpointer arg) io_thread_func(G_GNUC_UNUSED gpointer arg)
{ {
assert(io_context != NULL); assert(io.context != NULL);
assert(io_loop != NULL); assert(io.loop != NULL);
g_main_loop_run(io_loop); g_main_loop_run(io.loop);
return NULL; return NULL;
} }
void void
io_thread_init(void) io_thread_init(void)
{ {
assert(io_context == NULL); assert(io.context == NULL);
assert(io_loop == NULL); assert(io.loop == NULL);
assert(io_thread == NULL); assert(io.thread == NULL);
io_context = g_main_context_new(); io.context = g_main_context_new();
io_loop = g_main_loop_new(io_context, false); io.loop = g_main_loop_new(io.context, false);
} }
bool bool
io_thread_start(GError **error_r) io_thread_start(GError **error_r)
{ {
assert(io_context != NULL); assert(io.context != NULL);
assert(io_loop != NULL); assert(io.loop != NULL);
assert(io_thread == NULL); assert(io.thread == NULL);
io_thread = g_thread_create(io_thread_func, NULL, true, error_r); io.thread = g_thread_create(io_thread_func, NULL, true, error_r);
if (io_thread == NULL) if (io.thread == NULL)
return false; return false;
return true; return true;
@ -63,23 +65,23 @@ io_thread_start(GError **error_r)
void void
io_thread_deinit(void) io_thread_deinit(void)
{ {
if (io_thread != NULL) { if (io.thread != NULL) {
assert(io_loop != NULL); assert(io.loop != NULL);
g_main_loop_quit(io_loop); g_main_loop_quit(io.loop);
g_thread_join(io_thread); g_thread_join(io.thread);
} }
if (io_loop != NULL) if (io.loop != NULL)
g_main_loop_unref(io_loop); g_main_loop_unref(io.loop);
if (io_context != NULL) if (io.context != NULL)
g_main_context_unref(io_context); g_main_context_unref(io.context);
} }
GMainContext * GMainContext *
io_thread_context(void) io_thread_context(void)
{ {
return io_context; return io.context;
} }