diff options
Diffstat (limited to '')
-rw-r--r-- | src/IOThread.cxx (renamed from src/io_thread.c) | 104 |
1 files changed, 34 insertions, 70 deletions
diff --git a/src/io_thread.c b/src/IOThread.cxx index 7c080adcb..192d4cc49 100644 --- a/src/io_thread.c +++ b/src/IOThread.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project + * Copyright (C) 2003-2013 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -17,16 +17,19 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "io_thread.h" +#include "config.h" +#include "IOThread.hxx" +#include "thread/Mutex.hxx" +#include "thread/Cond.hxx" +#include "event/Loop.hxx" #include <assert.h> static struct { - GMutex *mutex; - GCond *cond; + Mutex mutex; + Cond cond; - GMainContext *context; - GMainLoop *loop; + EventLoop *loop; GThread *thread; } io; @@ -34,10 +37,9 @@ void io_thread_run(void) { assert(io_thread_inside()); - assert(io.context != NULL); assert(io.loop != NULL); - g_main_loop_run(io.loop); + io.loop->Run(); } static gpointer @@ -45,8 +47,8 @@ io_thread_func(G_GNUC_UNUSED gpointer arg) { /* lock+unlock to synchronize with io_thread_start(), to be sure that io.thread is set */ - g_mutex_lock(io.mutex); - g_mutex_unlock(io.mutex); + io.mutex.lock(); + io.mutex.unlock(); io_thread_run(); return NULL; @@ -55,26 +57,21 @@ io_thread_func(G_GNUC_UNUSED gpointer arg) void io_thread_init(void) { - assert(io.context == NULL); assert(io.loop == NULL); assert(io.thread == NULL); - io.mutex = g_mutex_new(); - io.cond = g_cond_new(); - io.context = g_main_context_new(); - io.loop = g_main_loop_new(io.context, false); + io.loop = new EventLoop(); } bool io_thread_start(GError **error_r) { - assert(io.context != NULL); assert(io.loop != NULL); assert(io.thread == NULL); - g_mutex_lock(io.mutex); + io.mutex.lock(); io.thread = g_thread_create(io_thread_func, NULL, true, error_r); - g_mutex_unlock(io.mutex); + io.mutex.unlock(); if (io.thread == NULL) return false; @@ -86,7 +83,7 @@ io_thread_quit(void) { assert(io.loop != NULL); - g_main_loop_quit(io.loop); + io.loop->Break(); } void @@ -98,20 +95,15 @@ io_thread_deinit(void) g_thread_join(io.thread); } - if (io.loop != NULL) - g_main_loop_unref(io.loop); - - if (io.context != NULL) - g_main_context_unref(io.context); - - g_cond_free(io.cond); - g_mutex_free(io.mutex); + delete io.loop; } -GMainContext * -io_thread_context(void) +EventLoop & +io_thread_get() { - return io.context; + assert(io.loop != nullptr); + + return *io.loop; } bool @@ -120,35 +112,6 @@ io_thread_inside(void) return io.thread != NULL && g_thread_self() == io.thread; } -guint -io_thread_idle_add(GSourceFunc function, gpointer data) -{ - GSource *source = g_idle_source_new(); - g_source_set_callback(source, function, data, NULL); - guint id = g_source_attach(source, io.context); - g_source_unref(source); - return id; -} - -GSource * -io_thread_timeout_add(guint interval_ms, GSourceFunc function, gpointer data) -{ - GSource *source = g_timeout_source_new(interval_ms); - g_source_set_callback(source, function, data, NULL); - g_source_attach(source, io.context); - return source; -} - -GSource * -io_thread_timeout_add_seconds(guint interval, - GSourceFunc function, gpointer data) -{ - GSource *source = g_timeout_source_new_seconds(interval); - g_source_set_callback(source, function, data, NULL); - g_source_attach(source, io.context); - return source; -} - struct call_data { GThreadFunc function; gpointer data; @@ -159,15 +122,15 @@ struct call_data { static gboolean io_thread_call_func(gpointer _data) { - struct call_data *data = _data; + struct call_data *data = (struct call_data *)_data; gpointer result = data->function(data->data); - g_mutex_lock(io.mutex); + io.mutex.lock(); data->done = true; data->result = result; - g_cond_broadcast(io.cond); - g_mutex_unlock(io.mutex); + io.cond.broadcast(); + io.mutex.unlock(); return false; } @@ -183,17 +146,18 @@ io_thread_call(GThreadFunc function, gpointer _data) return function(_data); struct call_data data = { - .function = function, - .data = _data, - .done = false, + function, + _data, + false, + nullptr, }; - io_thread_idle_add(io_thread_call_func, &data); + io.loop->AddIdle(io_thread_call_func, &data); - g_mutex_lock(io.mutex); + io.mutex.lock(); while (!data.done) - g_cond_wait(io.cond, io.mutex); - g_mutex_unlock(io.mutex); + io.cond.wait(io.mutex); + io.mutex.unlock(); return data.result; } |