io_thread: convert to C++

This commit is contained in:
Max Kellermann
2013-01-10 10:33:20 +01:00
parent 33ba288622
commit e0a97a030f
12 changed files with 40 additions and 44 deletions

View File

@@ -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,13 +17,15 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "io_thread.h"
#include "IOThread.hxx"
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
#include <assert.h>
static struct {
GMutex *mutex;
GCond *cond;
Mutex mutex;
Cond cond;
GMainContext *context;
GMainLoop *loop;
@@ -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;
@@ -59,8 +61,6 @@ io_thread_init(void)
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);
}
@@ -72,9 +72,9 @@ io_thread_start(GError **error_r)
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;
@@ -103,9 +103,6 @@ io_thread_deinit(void)
if (io.context != NULL)
g_main_context_unref(io.context);
g_cond_free(io.cond);
g_mutex_free(io.mutex);
}
GMainContext *
@@ -159,15 +156,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 +180,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);
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;
}

View File

@@ -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,11 +17,10 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_IO_THREAD_H
#define MPD_IO_THREAD_H
#ifndef MPD_IO_THREAD_HXX
#define MPD_IO_THREAD_HXX
#include <glib.h>
#include <stdbool.h>
void
io_thread_init(void);

View File

@@ -45,10 +45,10 @@
#include "Log.hxx"
#include "GlobalEvents.hxx"
#include "InputInit.hxx"
#include "IOThread.hxx"
extern "C" {
#include "daemon.h"
#include "io_thread.h"
#include "path.h"
#include "stats.h"
#include "audio_config.h"

View File

@@ -26,9 +26,9 @@
extern "C" {
#include "input_internal.h"
#include "icy_metadata.h"
#include "io_thread.h"
}
#include "IOThread.hxx"
#include "glib_compat.h"
#include <assert.h>

View File

@@ -23,9 +23,9 @@
extern "C" {
#include "input_internal.h"
#include "io_thread.h"
}
#include "IOThread.hxx"
#include "conf.h"
extern "C" {