Commit 6f3d70b5 authored by Max Kellermann's avatar Max Kellermann

DecoderControl, InputStream: use Mutex/Cond instead of GMutex/GCond

parent 257a0dee
...@@ -82,7 +82,7 @@ archive_file_scan_next(struct archive_file *file) ...@@ -82,7 +82,7 @@ archive_file_scan_next(struct archive_file *file)
struct input_stream * struct input_stream *
archive_file_open_stream(struct archive_file *file, const char *path, archive_file_open_stream(struct archive_file *file, const char *path,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r) GError **error_r)
{ {
assert(file != NULL); assert(file != NULL);
......
...@@ -20,7 +20,9 @@ ...@@ -20,7 +20,9 @@
#ifndef MPD_ARCHIVE_PLUGIN_HXX #ifndef MPD_ARCHIVE_PLUGIN_HXX
#define MPD_ARCHIVE_PLUGIN_HXX #define MPD_ARCHIVE_PLUGIN_HXX
#include <glib.h> #include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
#include "gerror.h"
struct input_stream; struct input_stream;
struct archive_file; struct archive_file;
...@@ -71,7 +73,7 @@ struct archive_plugin { ...@@ -71,7 +73,7 @@ struct archive_plugin {
*/ */
struct input_stream *(*open_stream)(struct archive_file *af, struct input_stream *(*open_stream)(struct archive_file *af,
const char *path, const char *path,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r); GError **error_r);
/** /**
...@@ -101,7 +103,7 @@ archive_file_scan_next(struct archive_file *file); ...@@ -101,7 +103,7 @@ archive_file_scan_next(struct archive_file *file);
struct input_stream * struct input_stream *
archive_file_open_stream(struct archive_file *file, const char *path, archive_file_open_stream(struct archive_file *file, const char *path,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r); GError **error_r);
#endif #endif
...@@ -67,7 +67,7 @@ decoder_initialized(struct decoder *decoder, ...@@ -67,7 +67,7 @@ decoder_initialized(struct decoder *decoder,
dc->Lock(); dc->Lock();
dc->state = DECODE_STATE_DECODE; dc->state = DECODE_STATE_DECODE;
g_cond_signal(dc->client_cond); dc->client_cond.signal();
dc->Unlock(); dc->Unlock();
g_debug("audio_format=%s, seekable=%s", g_debug("audio_format=%s, seekable=%s",
...@@ -192,7 +192,7 @@ decoder_command_finished(struct decoder *decoder) ...@@ -192,7 +192,7 @@ decoder_command_finished(struct decoder *decoder)
} }
dc->command = DECODE_COMMAND_NONE; dc->command = DECODE_COMMAND_NONE;
g_cond_signal(dc->client_cond); dc->client_cond.signal();
dc->Unlock(); dc->Unlock();
} }
...@@ -285,7 +285,7 @@ size_t decoder_read(struct decoder *decoder, ...@@ -285,7 +285,7 @@ size_t decoder_read(struct decoder *decoder,
if (input_stream_available(is)) if (input_stream_available(is))
break; break;
g_cond_wait(is->cond, is->mutex); is->cond->wait(*is->mutex);
} }
nbytes = input_stream_read(is, buffer, length, &error); nbytes = input_stream_read(is, buffer, length, &error);
...@@ -324,7 +324,7 @@ do_send_tag(struct decoder *decoder, const struct tag *tag) ...@@ -324,7 +324,7 @@ do_send_tag(struct decoder *decoder, const struct tag *tag)
/* there is a partial chunk - flush it, we want the /* there is a partial chunk - flush it, we want the
tag in a new chunk */ tag in a new chunk */
decoder_flush_chunk(decoder); decoder_flush_chunk(decoder);
g_cond_signal(decoder->dc->client_cond); decoder->dc->client_cond.signal();
} }
assert(decoder->chunk == NULL); assert(decoder->chunk == NULL);
...@@ -437,7 +437,7 @@ decoder_data(struct decoder *decoder, ...@@ -437,7 +437,7 @@ decoder_data(struct decoder *decoder,
if (dest == NULL) { if (dest == NULL) {
/* the chunk is full, flush it */ /* the chunk is full, flush it */
decoder_flush_chunk(decoder); decoder_flush_chunk(decoder);
g_cond_signal(dc->client_cond); dc->client_cond.signal();
continue; continue;
} }
...@@ -456,7 +456,7 @@ decoder_data(struct decoder *decoder, ...@@ -456,7 +456,7 @@ decoder_data(struct decoder *decoder,
if (full) { if (full) {
/* the chunk is full, flush it */ /* the chunk is full, flush it */
decoder_flush_chunk(decoder); decoder_flush_chunk(decoder);
g_cond_signal(dc->client_cond); dc->client_cond.signal();
} }
data = (const uint8_t *)data + nbytes; data = (const uint8_t *)data + nbytes;
...@@ -551,7 +551,7 @@ decoder_replay_gain(struct decoder *decoder, ...@@ -551,7 +551,7 @@ decoder_replay_gain(struct decoder *decoder,
replay gain values affect the following replay gain values affect the following
samples */ samples */
decoder_flush_chunk(decoder); decoder_flush_chunk(decoder);
g_cond_signal(decoder->dc->client_cond); decoder->dc->client_cond.signal();
} }
} else } else
decoder->replay_gain_serial = 0; decoder->replay_gain_serial = 0;
......
...@@ -29,8 +29,6 @@ ...@@ -29,8 +29,6 @@
decoder_control::decoder_control() decoder_control::decoder_control()
:thread(nullptr), :thread(nullptr),
mutex(g_mutex_new()), cond(g_cond_new()),
client_cond(g_cond_new()),
state(DECODE_STATE_STOP), state(DECODE_STATE_STOP),
command(DECODE_COMMAND_NONE), command(DECODE_COMMAND_NONE),
song(nullptr), song(nullptr),
...@@ -45,9 +43,6 @@ decoder_control::~decoder_control() ...@@ -45,9 +43,6 @@ decoder_control::~decoder_control()
if (song != NULL) if (song != NULL)
song_free(song); song_free(song);
g_cond_free(client_cond);
g_cond_free(cond);
g_mutex_free(mutex);
g_free(mixramp_start); g_free(mixramp_start);
g_free(mixramp_end); g_free(mixramp_end);
g_free(mixramp_prev_end); g_free(mixramp_prev_end);
...@@ -57,7 +52,7 @@ static void ...@@ -57,7 +52,7 @@ static void
dc_command_wait_locked(struct decoder_control *dc) dc_command_wait_locked(struct decoder_control *dc)
{ {
while (dc->command != DECODE_COMMAND_NONE) while (dc->command != DECODE_COMMAND_NONE)
g_cond_wait(dc->client_cond, dc->mutex); dc->WaitForDecoder();
} }
static void static void
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include "decoder_command.h" #include "decoder_command.h"
#include "audio_format.h" #include "audio_format.h"
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
#include <glib.h> #include <glib.h>
...@@ -49,20 +51,20 @@ struct decoder_control { ...@@ -49,20 +51,20 @@ struct decoder_control {
/** /**
* This lock protects #state and #command. * This lock protects #state and #command.
*/ */
GMutex *mutex; mutable Mutex mutex;
/** /**
* Trigger this object after you have modified #command. This * Trigger this object after you have modified #command. This
* is also used by the decoder thread to notify the caller * is also used by the decoder thread to notify the caller
* when it has finished a command. * when it has finished a command.
*/ */
GCond *cond; Cond cond;
/** /**
* The trigger of this object's client. It is signalled * The trigger of this object's client. It is signalled
* whenever an event occurs. * whenever an event occurs.
*/ */
GCond *client_cond; Cond client_cond;
enum decoder_state state; enum decoder_state state;
enum decoder_command command; enum decoder_command command;
...@@ -137,14 +139,14 @@ struct decoder_control { ...@@ -137,14 +139,14 @@ struct decoder_control {
* Locks the object. * Locks the object.
*/ */
void Lock() const { void Lock() const {
g_mutex_lock(mutex); mutex.lock();
} }
/** /**
* Unlocks the object. * Unlocks the object.
*/ */
void Unlock() const { void Unlock() const {
g_mutex_unlock(mutex); mutex.unlock();
} }
/** /**
...@@ -153,7 +155,7 @@ struct decoder_control { ...@@ -153,7 +155,7 @@ struct decoder_control {
* calling this function. * calling this function.
*/ */
void Signal() { void Signal() {
g_cond_signal(cond); cond.signal();
} }
/** /**
...@@ -162,7 +164,7 @@ struct decoder_control { ...@@ -162,7 +164,7 @@ struct decoder_control {
* prior to calling this function. * prior to calling this function.
*/ */
void Wait() { void Wait() {
g_cond_wait(cond, mutex); cond.wait(mutex);
} }
/** /**
...@@ -171,7 +173,7 @@ struct decoder_control { ...@@ -171,7 +173,7 @@ struct decoder_control {
* is only valid in the player thread. * is only valid in the player thread.
*/ */
void WaitForDecoder() { void WaitForDecoder() {
g_cond_wait(client_cond, mutex); client_cond.wait(mutex);
} }
bool IsIdle() const { bool IsIdle() const {
......
...@@ -57,7 +57,7 @@ need_chunks(struct decoder_control *dc, bool do_wait) ...@@ -57,7 +57,7 @@ need_chunks(struct decoder_control *dc, bool do_wait)
if (do_wait) { if (do_wait) {
dc->Wait(); dc->Wait();
g_cond_signal(dc->client_cond); dc->client_cond.signal();
return dc->command; return dc->command;
} }
......
...@@ -58,7 +58,7 @@ decoder_command_finished_locked(struct decoder_control *dc) ...@@ -58,7 +58,7 @@ decoder_command_finished_locked(struct decoder_control *dc)
dc->command = DECODE_COMMAND_NONE; dc->command = DECODE_COMMAND_NONE;
g_cond_signal(dc->client_cond); dc->client_cond.signal();
} }
/** /**
...@@ -418,7 +418,7 @@ decoder_run_song(struct decoder_control *dc, ...@@ -418,7 +418,7 @@ decoder_run_song(struct decoder_control *dc,
g_free(allocated); g_free(allocated);
} }
g_cond_signal(dc->client_cond); dc->client_cond.signal();
} }
static void static void
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
void void
input_stream_init(struct input_stream *is, const struct input_plugin *plugin, input_stream_init(struct input_stream *is, const struct input_plugin *plugin,
const char *uri, GMutex *mutex, GCond *cond) const char *uri, Mutex &mutex, Cond &cond)
{ {
assert(is != NULL); assert(is != NULL);
assert(plugin != NULL); assert(plugin != NULL);
...@@ -33,8 +33,8 @@ input_stream_init(struct input_stream *is, const struct input_plugin *plugin, ...@@ -33,8 +33,8 @@ input_stream_init(struct input_stream *is, const struct input_plugin *plugin,
is->plugin = plugin; is->plugin = plugin;
is->uri = g_strdup(uri); is->uri = g_strdup(uri);
is->mutex = mutex; is->mutex = &mutex;
is->cond = cond; is->cond = &cond;
is->ready = false; is->ready = false;
is->seekable = false; is->seekable = false;
is->size = -1; is->size = -1;
...@@ -56,18 +56,16 @@ void ...@@ -56,18 +56,16 @@ void
input_stream_signal_client(struct input_stream *is) input_stream_signal_client(struct input_stream *is)
{ {
if (is->cond != NULL) if (is->cond != NULL)
g_cond_broadcast(is->cond); is->cond->broadcast();
} }
void void
input_stream_set_ready(struct input_stream *is) input_stream_set_ready(struct input_stream *is)
{ {
g_mutex_lock(is->mutex); const ScopeLock protect(*is->mutex);
if (!is->ready) { if (!is->ready) {
is->ready = true; is->ready = true;
input_stream_signal_client(is); input_stream_signal_client(is);
} }
g_mutex_unlock(is->mutex);
} }
...@@ -21,15 +21,15 @@ ...@@ -21,15 +21,15 @@
#define MPD_INPUT_INTERNAL_HXX #define MPD_INPUT_INTERNAL_HXX
#include "check.h" #include "check.h"
#include "thread/Mutex.hxx"
#include <glib.h> #include "thread/Cond.hxx"
struct input_stream; struct input_stream;
struct input_plugin; struct input_plugin;
void void
input_stream_init(struct input_stream *is, const struct input_plugin *plugin, input_stream_init(struct input_stream *is, const struct input_plugin *plugin,
const char *uri, GMutex *mutex, GCond *cond); const char *uri, Mutex &mutex, Cond &cond);
void void
input_stream_deinit(struct input_stream *is); input_stream_deinit(struct input_stream *is);
......
...@@ -48,7 +48,7 @@ struct input_plugin { ...@@ -48,7 +48,7 @@ struct input_plugin {
void (*finish)(void); void (*finish)(void);
struct input_stream *(*open)(const char *uri, struct input_stream *(*open)(const char *uri,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r); GError **error_r);
void (*close)(struct input_stream *is); void (*close)(struct input_stream *is);
......
...@@ -38,12 +38,11 @@ input_quark(void) ...@@ -38,12 +38,11 @@ input_quark(void)
struct input_stream * struct input_stream *
input_stream_open(const char *url, input_stream_open(const char *url,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r) GError **error_r)
{ {
GError *error = NULL; GError *error = NULL;
assert(mutex != NULL);
assert(error_r == NULL || *error_r == NULL); assert(error_r == NULL || *error_r == NULL);
input_plugins_for_each_enabled(plugin) { input_plugins_for_each_enabled(plugin) {
...@@ -102,7 +101,7 @@ input_stream_wait_ready(struct input_stream *is) ...@@ -102,7 +101,7 @@ input_stream_wait_ready(struct input_stream *is)
if (is->ready) if (is->ready)
break; break;
g_cond_wait(is->cond, is->mutex); is->cond->wait(*is->mutex);
} }
} }
...@@ -113,9 +112,8 @@ input_stream_lock_wait_ready(struct input_stream *is) ...@@ -113,9 +112,8 @@ input_stream_lock_wait_ready(struct input_stream *is)
assert(is->mutex != NULL); assert(is->mutex != NULL);
assert(is->cond != NULL); assert(is->cond != NULL);
g_mutex_lock(is->mutex); const ScopeLock protect(*is->mutex);
input_stream_wait_ready(is); input_stream_wait_ready(is);
g_mutex_unlock(is->mutex);
} }
const char * const char *
...@@ -197,10 +195,8 @@ input_stream_lock_seek(struct input_stream *is, goffset offset, int whence, ...@@ -197,10 +195,8 @@ input_stream_lock_seek(struct input_stream *is, goffset offset, int whence,
/* no locking */ /* no locking */
return input_stream_seek(is, offset, whence, error_r); return input_stream_seek(is, offset, whence, error_r);
g_mutex_lock(is->mutex); const ScopeLock protect(*is->mutex);
bool success = input_stream_seek(is, offset, whence, error_r); return input_stream_seek(is, offset, whence, error_r);
g_mutex_unlock(is->mutex);
return success;
} }
struct tag * struct tag *
...@@ -227,10 +223,8 @@ input_stream_lock_tag(struct input_stream *is) ...@@ -227,10 +223,8 @@ input_stream_lock_tag(struct input_stream *is)
/* no locking */ /* no locking */
return input_stream_tag(is); return input_stream_tag(is);
g_mutex_lock(is->mutex); const ScopeLock protect(*is->mutex);
struct tag *tag = input_stream_tag(is); return input_stream_tag(is);
g_mutex_unlock(is->mutex);
return tag;
} }
bool bool
...@@ -265,10 +259,8 @@ input_stream_lock_read(struct input_stream *is, void *ptr, size_t size, ...@@ -265,10 +259,8 @@ input_stream_lock_read(struct input_stream *is, void *ptr, size_t size,
/* no locking */ /* no locking */
return input_stream_read(is, ptr, size, error_r); return input_stream_read(is, ptr, size, error_r);
g_mutex_lock(is->mutex); const ScopeLock protect(*is->mutex);
size_t nbytes = input_stream_read(is, ptr, size, error_r); return input_stream_read(is, ptr, size, error_r);
g_mutex_unlock(is->mutex);
return nbytes;
} }
void input_stream_close(struct input_stream *is) void input_stream_close(struct input_stream *is)
...@@ -291,9 +283,7 @@ input_stream_lock_eof(struct input_stream *is) ...@@ -291,9 +283,7 @@ input_stream_lock_eof(struct input_stream *is)
/* no locking */ /* no locking */
return input_stream_eof(is); return input_stream_eof(is);
g_mutex_lock(is->mutex); const ScopeLock protect(*is->mutex);
bool eof = input_stream_eof(is); return input_stream_eof(is);
g_mutex_unlock(is->mutex);
return eof;
} }
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include "input_stream.h" #include "input_stream.h"
#include "check.h" #include "check.h"
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
#include "gcc.h" #include "gcc.h"
#include <glib.h> #include <glib.h>
...@@ -46,7 +48,7 @@ struct input_stream { ...@@ -46,7 +48,7 @@ struct input_stream {
* This object is allocated by the client, and the client is * This object is allocated by the client, and the client is
* responsible for freeing it. * responsible for freeing it.
*/ */
GMutex *mutex; Mutex *mutex;
/** /**
* A cond that gets signalled when the state of this object * A cond that gets signalled when the state of this object
...@@ -56,7 +58,7 @@ struct input_stream { ...@@ -56,7 +58,7 @@ struct input_stream {
* This object is allocated by the client, and the client is * This object is allocated by the client, and the client is
* responsible for freeing it. * responsible for freeing it.
*/ */
GCond *cond; Cond *cond;
/** /**
* indicates whether the stream is ready for reading and * indicates whether the stream is ready for reading and
...@@ -89,14 +91,14 @@ gcc_nonnull(1) ...@@ -89,14 +91,14 @@ gcc_nonnull(1)
static inline void static inline void
input_stream_lock(struct input_stream *is) input_stream_lock(struct input_stream *is)
{ {
g_mutex_lock(is->mutex); is->mutex->lock();
} }
gcc_nonnull(1) gcc_nonnull(1)
static inline void static inline void
input_stream_unlock(struct input_stream *is) input_stream_unlock(struct input_stream *is)
{ {
g_mutex_unlock(is->mutex); is->mutex->unlock();
} }
#endif #endif
...@@ -30,7 +30,7 @@ extern "C" { ...@@ -30,7 +30,7 @@ extern "C" {
#include <assert.h> #include <assert.h>
static struct playlist_provider * static struct playlist_provider *
playlist_open_remote(const char *uri, GMutex *mutex, GCond *cond, playlist_open_remote(const char *uri, Mutex &mutex, Cond &cond,
struct input_stream **is_r) struct input_stream **is_r)
{ {
assert(uri_has_scheme(uri)); assert(uri_has_scheme(uri));
...@@ -65,7 +65,7 @@ playlist_open_remote(const char *uri, GMutex *mutex, GCond *cond, ...@@ -65,7 +65,7 @@ playlist_open_remote(const char *uri, GMutex *mutex, GCond *cond,
} }
struct playlist_provider * struct playlist_provider *
playlist_open_any(const char *uri, GMutex *mutex, GCond *cond, playlist_open_any(const char *uri, Mutex &mutex, Cond &cond,
struct input_stream **is_r) struct input_stream **is_r)
{ {
return uri_has_scheme(uri) return uri_has_scheme(uri)
......
...@@ -20,7 +20,8 @@ ...@@ -20,7 +20,8 @@
#ifndef MPD_PLAYLIST_ANY_HXX #ifndef MPD_PLAYLIST_ANY_HXX
#define MPD_PLAYLIST_ANY_HXX #define MPD_PLAYLIST_ANY_HXX
#include <glib.h> #include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
struct playlist_provider; struct playlist_provider;
struct input_stream; struct input_stream;
...@@ -35,7 +36,7 @@ struct input_stream; ...@@ -35,7 +36,7 @@ struct input_stream;
* freed * freed
*/ */
struct playlist_provider * struct playlist_provider *
playlist_open_any(const char *uri, GMutex *mutex, GCond *cond, playlist_open_any(const char *uri, Mutex &mutex, Cond &cond,
struct input_stream **is_r); struct input_stream **is_r);
#endif #endif
...@@ -31,7 +31,7 @@ extern "C" { ...@@ -31,7 +31,7 @@ extern "C" {
#include <assert.h> #include <assert.h>
static struct playlist_provider * static struct playlist_provider *
playlist_open_path(const char *path_fs, GMutex *mutex, GCond *cond, playlist_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
struct input_stream **is_r) struct input_stream **is_r)
{ {
struct playlist_provider *playlist; struct playlist_provider *playlist;
...@@ -49,7 +49,7 @@ playlist_open_path(const char *path_fs, GMutex *mutex, GCond *cond, ...@@ -49,7 +49,7 @@ playlist_open_path(const char *path_fs, GMutex *mutex, GCond *cond,
* Load a playlist from the configured playlist directory. * Load a playlist from the configured playlist directory.
*/ */
static struct playlist_provider * static struct playlist_provider *
playlist_open_in_playlist_dir(const char *uri, GMutex *mutex, GCond *cond, playlist_open_in_playlist_dir(const char *uri, Mutex &mutex, Cond &cond,
struct input_stream **is_r) struct input_stream **is_r)
{ {
char *path_fs; char *path_fs;
...@@ -73,7 +73,7 @@ playlist_open_in_playlist_dir(const char *uri, GMutex *mutex, GCond *cond, ...@@ -73,7 +73,7 @@ playlist_open_in_playlist_dir(const char *uri, GMutex *mutex, GCond *cond,
* Load a playlist from the configured music directory. * Load a playlist from the configured music directory.
*/ */
static struct playlist_provider * static struct playlist_provider *
playlist_open_in_music_dir(const char *uri, GMutex *mutex, GCond *cond, playlist_open_in_music_dir(const char *uri, Mutex &mutex, Cond &cond,
struct input_stream **is_r) struct input_stream **is_r)
{ {
assert(uri_safe_local(uri)); assert(uri_safe_local(uri));
...@@ -86,7 +86,7 @@ playlist_open_in_music_dir(const char *uri, GMutex *mutex, GCond *cond, ...@@ -86,7 +86,7 @@ playlist_open_in_music_dir(const char *uri, GMutex *mutex, GCond *cond,
} }
struct playlist_provider * struct playlist_provider *
playlist_mapper_open(const char *uri, GMutex *mutex, GCond *cond, playlist_mapper_open(const char *uri, Mutex &mutex, Cond &cond,
struct input_stream **is_r) struct input_stream **is_r)
{ {
struct playlist_provider *playlist; struct playlist_provider *playlist;
......
...@@ -20,7 +20,8 @@ ...@@ -20,7 +20,8 @@
#ifndef MPD_PLAYLIST_MAPPER_HXX #ifndef MPD_PLAYLIST_MAPPER_HXX
#define MPD_PLAYLIST_MAPPER_HXX #define MPD_PLAYLIST_MAPPER_HXX
#include <glib.h> #include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
struct input_stream; struct input_stream;
...@@ -33,7 +34,7 @@ struct input_stream; ...@@ -33,7 +34,7 @@ struct input_stream;
* freed * freed
*/ */
struct playlist_provider * struct playlist_provider *
playlist_mapper_open(const char *uri, GMutex *mutex, GCond *cond, playlist_mapper_open(const char *uri, Mutex &mutex, Cond &cond,
struct input_stream **is_r); struct input_stream **is_r);
#endif #endif
...@@ -20,7 +20,8 @@ ...@@ -20,7 +20,8 @@
#ifndef MPD_PLAYLIST_PLUGIN_HXX #ifndef MPD_PLAYLIST_PLUGIN_HXX
#define MPD_PLAYLIST_PLUGIN_HXX #define MPD_PLAYLIST_PLUGIN_HXX
#include <glib.h> #include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
#include <stddef.h> #include <stddef.h>
...@@ -66,7 +67,7 @@ struct playlist_plugin { ...@@ -66,7 +67,7 @@ struct playlist_plugin {
* either matched one of the schemes or one of the suffixes. * either matched one of the schemes or one of the suffixes.
*/ */
struct playlist_provider *(*open_uri)(const char *uri, struct playlist_provider *(*open_uri)(const char *uri,
GMutex *mutex, GCond *cond); Mutex &mutex, Cond &cond);
/** /**
* Opens the playlist in the specified input stream. It has * Opens the playlist in the specified input stream. It has
...@@ -113,7 +114,7 @@ playlist_plugin_finish(const struct playlist_plugin *plugin) ...@@ -113,7 +114,7 @@ playlist_plugin_finish(const struct playlist_plugin *plugin)
static inline struct playlist_provider * static inline struct playlist_provider *
playlist_plugin_open_uri(const struct playlist_plugin *plugin, const char *uri, playlist_plugin_open_uri(const struct playlist_plugin *plugin, const char *uri,
GMutex *mutex, GCond *cond) Mutex &mutex, Cond &cond)
{ {
return plugin->open_uri(uri, mutex, cond); return plugin->open_uri(uri, mutex, cond);
} }
......
...@@ -174,17 +174,14 @@ playlist_provider_print(Client *client, const char *uri, ...@@ -174,17 +174,14 @@ playlist_provider_print(Client *client, const char *uri,
bool bool
playlist_file_print(Client *client, const char *uri, bool detail) playlist_file_print(Client *client, const char *uri, bool detail)
{ {
GMutex *mutex = g_mutex_new(); Mutex mutex;
GCond *cond = g_cond_new(); Cond cond;
struct input_stream *is; struct input_stream *is;
struct playlist_provider *playlist = struct playlist_provider *playlist =
playlist_open_any(uri, mutex, cond, &is); playlist_open_any(uri, mutex, cond, &is);
if (playlist == NULL) { if (playlist == NULL)
g_cond_free(cond);
g_mutex_free(mutex);
return false; return false;
}
playlist_provider_print(client, uri, playlist, detail); playlist_provider_print(client, uri, playlist, detail);
playlist_plugin_close(playlist); playlist_plugin_close(playlist);
...@@ -192,8 +189,5 @@ playlist_file_print(Client *client, const char *uri, bool detail) ...@@ -192,8 +189,5 @@ playlist_file_print(Client *client, const char *uri, bool detail)
if (is != NULL) if (is != NULL)
input_stream_close(is); input_stream_close(is);
g_cond_free(cond);
g_mutex_free(mutex);
return true; return true;
} }
...@@ -71,17 +71,14 @@ playlist_open_into_queue(const char *uri, ...@@ -71,17 +71,14 @@ playlist_open_into_queue(const char *uri,
struct playlist *dest, struct player_control *pc, struct playlist *dest, struct player_control *pc,
bool secure) bool secure)
{ {
GMutex *mutex = g_mutex_new(); Mutex mutex;
GCond *cond = g_cond_new(); Cond cond;
struct input_stream *is; struct input_stream *is;
struct playlist_provider *playlist = struct playlist_provider *playlist =
playlist_open_any(uri, mutex, cond, &is); playlist_open_any(uri, mutex, cond, &is);
if (playlist == NULL) { if (playlist == NULL)
g_cond_free(cond);
g_mutex_free(mutex);
return PLAYLIST_RESULT_NO_SUCH_LIST; return PLAYLIST_RESULT_NO_SUCH_LIST;
}
enum playlist_result result = enum playlist_result result =
playlist_load_into_queue(uri, playlist, start_index, end_index, playlist_load_into_queue(uri, playlist, start_index, end_index,
...@@ -91,8 +88,5 @@ playlist_open_into_queue(const char *uri, ...@@ -91,8 +88,5 @@ playlist_open_into_queue(const char *uri,
if (is != NULL) if (is != NULL)
input_stream_close(is); input_stream_close(is);
g_cond_free(cond);
g_mutex_free(mutex);
return result; return result;
} }
...@@ -125,7 +125,7 @@ playlist_list_global_finish(void) ...@@ -125,7 +125,7 @@ playlist_list_global_finish(void)
} }
static struct playlist_provider * static struct playlist_provider *
playlist_list_open_uri_scheme(const char *uri, GMutex *mutex, GCond *cond, playlist_list_open_uri_scheme(const char *uri, Mutex &mutex, Cond &cond,
bool *tried) bool *tried)
{ {
char *scheme; char *scheme;
...@@ -159,7 +159,7 @@ playlist_list_open_uri_scheme(const char *uri, GMutex *mutex, GCond *cond, ...@@ -159,7 +159,7 @@ playlist_list_open_uri_scheme(const char *uri, GMutex *mutex, GCond *cond,
} }
static struct playlist_provider * static struct playlist_provider *
playlist_list_open_uri_suffix(const char *uri, GMutex *mutex, GCond *cond, playlist_list_open_uri_suffix(const char *uri, Mutex &mutex, Cond &cond,
const bool *tried) const bool *tried)
{ {
const char *suffix; const char *suffix;
...@@ -188,7 +188,7 @@ playlist_list_open_uri_suffix(const char *uri, GMutex *mutex, GCond *cond, ...@@ -188,7 +188,7 @@ playlist_list_open_uri_suffix(const char *uri, GMutex *mutex, GCond *cond,
} }
struct playlist_provider * struct playlist_provider *
playlist_list_open_uri(const char *uri, GMutex *mutex, GCond *cond) playlist_list_open_uri(const char *uri, Mutex &mutex, Cond &cond)
{ {
struct playlist_provider *playlist; struct playlist_provider *playlist;
/** this array tracks which plugins have already been tried by /** this array tracks which plugins have already been tried by
...@@ -317,7 +317,7 @@ playlist_suffix_supported(const char *suffix) ...@@ -317,7 +317,7 @@ playlist_suffix_supported(const char *suffix)
} }
struct playlist_provider * struct playlist_provider *
playlist_list_open_path(const char *path_fs, GMutex *mutex, GCond *cond, playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
struct input_stream **is_r) struct input_stream **is_r)
{ {
GError *error = NULL; GError *error = NULL;
......
...@@ -20,6 +20,9 @@ ...@@ -20,6 +20,9 @@
#ifndef MPD_PLAYLIST_REGISTRY_HXX #ifndef MPD_PLAYLIST_REGISTRY_HXX
#define MPD_PLAYLIST_REGISTRY_HXX #define MPD_PLAYLIST_REGISTRY_HXX
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
#include <glib.h> #include <glib.h>
#include <stdbool.h> #include <stdbool.h>
...@@ -51,7 +54,7 @@ playlist_list_global_finish(void); ...@@ -51,7 +54,7 @@ playlist_list_global_finish(void);
* Opens a playlist by its URI. * Opens a playlist by its URI.
*/ */
struct playlist_provider * struct playlist_provider *
playlist_list_open_uri(const char *uri, GMutex *mutex, GCond *cond); playlist_list_open_uri(const char *uri, Mutex &mutex, Cond &cond);
/** /**
* Opens a playlist from an input stream. * Opens a playlist from an input stream.
...@@ -79,7 +82,7 @@ playlist_suffix_supported(const char *suffix); ...@@ -79,7 +82,7 @@ playlist_suffix_supported(const char *suffix);
* @return a playlist, or NULL on error * @return a playlist, or NULL on error
*/ */
struct playlist_provider * struct playlist_provider *
playlist_list_open_path(const char *path_fs, GMutex *mutex, GCond *cond, playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
struct input_stream **is_r); struct input_stream **is_r);
#endif #endif
...@@ -117,13 +117,8 @@ song_file_update(struct song *song) ...@@ -117,13 +117,8 @@ song_file_update(struct song *song)
song->mtime = st.st_mtime; song->mtime = st.st_mtime;
GMutex *mutex = NULL; Mutex mutex;
GCond *cond; Cond cond;
#if !GCC_CHECK_VERSION(4, 2)
/* work around "may be used uninitialized in this function"
false positive */
cond = NULL;
#endif
do { do {
/* load file tag */ /* load file tag */
...@@ -140,8 +135,6 @@ song_file_update(struct song *song) ...@@ -140,8 +135,6 @@ song_file_update(struct song *song)
/* open the input_stream (if not already /* open the input_stream (if not already
open) */ open) */
if (is == NULL) { if (is == NULL) {
mutex = g_mutex_new();
cond = g_cond_new();
is = input_stream_open(path_fs.c_str(), is = input_stream_open(path_fs.c_str(),
mutex, cond, mutex, cond,
NULL); NULL);
...@@ -168,11 +161,6 @@ song_file_update(struct song *song) ...@@ -168,11 +161,6 @@ song_file_update(struct song *song)
if (is != NULL) if (is != NULL)
input_stream_close(is); input_stream_close(is);
if (mutex != NULL) {
g_cond_free(cond);
g_mutex_free(mutex);
}
if (song->tag != NULL && tag_is_empty(song->tag)) if (song->tag != NULL && tag_is_empty(song->tag))
tag_scan_fallback(path_fs.c_str(), &full_tag_handler, tag_scan_fallback(path_fs.c_str(), &full_tag_handler,
song->tag); song->tag);
......
...@@ -50,8 +50,8 @@ tag_file_scan(const char *path_fs, ...@@ -50,8 +50,8 @@ tag_file_scan(const char *path_fs,
return false; return false;
struct input_stream *is = NULL; struct input_stream *is = NULL;
GMutex *mutex = NULL; Mutex mutex;
GCond *cond = NULL; Cond cond;
do { do {
/* load file tag */ /* load file tag */
...@@ -63,12 +63,9 @@ tag_file_scan(const char *path_fs, ...@@ -63,12 +63,9 @@ tag_file_scan(const char *path_fs,
if (plugin->scan_stream != NULL) { if (plugin->scan_stream != NULL) {
/* open the input_stream (if not already /* open the input_stream (if not already
open) */ open) */
if (is == NULL) { if (is == nullptr)
mutex = g_mutex_new();
cond = g_cond_new();
is = input_stream_open(path_fs, mutex, cond, is = input_stream_open(path_fs, mutex, cond,
NULL); NULL);
}
/* now try the stream_tag() method */ /* now try the stream_tag() method */
if (is != NULL) { if (is != NULL) {
...@@ -84,11 +81,8 @@ tag_file_scan(const char *path_fs, ...@@ -84,11 +81,8 @@ tag_file_scan(const char *path_fs,
plugin = decoder_plugin_from_suffix(suffix, plugin); plugin = decoder_plugin_from_suffix(suffix, plugin);
} while (plugin != NULL); } while (plugin != NULL);
if (is != NULL) { if (is != NULL)
input_stream_close(is); input_stream_close(is);
g_cond_free(cond);
g_mutex_free(mutex);
}
return plugin != NULL; return plugin != NULL;
} }
...@@ -132,10 +132,9 @@ bz2_open(const char *pathname, GError **error_r) ...@@ -132,10 +132,9 @@ bz2_open(const char *pathname, GError **error_r)
int len; int len;
//open archive //open archive
static GStaticMutex mutex = G_STATIC_MUTEX_INIT; static Mutex mutex;
context->istream = input_stream_open(pathname, static Cond cond;
g_static_mutex_get_mutex(&mutex), context->istream = input_stream_open(pathname, mutex, cond,
NULL,
error_r); error_r);
if (context->istream == NULL) { if (context->istream == NULL) {
delete context; delete context;
...@@ -186,7 +185,7 @@ bz2_close(struct archive_file *file) ...@@ -186,7 +185,7 @@ bz2_close(struct archive_file *file)
static struct input_stream * static struct input_stream *
bz2_open_stream(struct archive_file *file, const char *path, bz2_open_stream(struct archive_file *file, const char *path,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r) GError **error_r)
{ {
struct bz2_archive_file *context = (struct bz2_archive_file *) file; struct bz2_archive_file *context = (struct bz2_archive_file *) file;
......
...@@ -176,7 +176,7 @@ struct iso9660_input_stream { ...@@ -176,7 +176,7 @@ struct iso9660_input_stream {
static struct input_stream * static struct input_stream *
iso9660_archive_open_stream(struct archive_file *file, const char *pathname, iso9660_archive_open_stream(struct archive_file *file, const char *pathname,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r) GError **error_r)
{ {
struct iso9660_archive_file *context = struct iso9660_archive_file *context =
......
...@@ -143,7 +143,7 @@ struct zzip_input_stream { ...@@ -143,7 +143,7 @@ struct zzip_input_stream {
static struct input_stream * static struct input_stream *
zzip_archive_open_stream(struct archive_file *file, zzip_archive_open_stream(struct archive_file *file,
const char *pathname, const char *pathname,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r) GError **error_r)
{ {
struct zzip_archive *context = (struct zzip_archive *) file; struct zzip_archive *context = (struct zzip_archive *) file;
......
...@@ -464,7 +464,7 @@ wavpack_input_init(struct wavpack_input *isp, struct decoder *decoder, ...@@ -464,7 +464,7 @@ wavpack_input_init(struct wavpack_input *isp, struct decoder *decoder,
static struct input_stream * static struct input_stream *
wavpack_open_wvc(struct decoder *decoder, const char *uri, wavpack_open_wvc(struct decoder *decoder, const char *uri,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
struct wavpack_input *wpi) struct wavpack_input *wpi)
{ {
struct input_stream *is_wvc; struct input_stream *is_wvc;
...@@ -517,7 +517,7 @@ wavpack_streamdecode(struct decoder * decoder, struct input_stream *is) ...@@ -517,7 +517,7 @@ wavpack_streamdecode(struct decoder * decoder, struct input_stream *is)
struct wavpack_input isp, isp_wvc; struct wavpack_input isp, isp_wvc;
bool can_seek = is->seekable; bool can_seek = is->seekable;
is_wvc = wavpack_open_wvc(decoder, is->uri, is->mutex, is->cond, is_wvc = wavpack_open_wvc(decoder, is->uri, *is->mutex, *is->cond,
&isp_wvc); &isp_wvc);
if (is_wvc != NULL) { if (is_wvc != NULL) {
open_flags |= OPEN_WVC; open_flags |= OPEN_WVC;
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
*/ */
static struct input_stream * static struct input_stream *
input_archive_open(const char *pathname, input_archive_open(const char *pathname,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r) GError **error_r)
{ {
const struct archive_plugin *arplug; const struct archive_plugin *arplug;
......
...@@ -54,7 +54,7 @@ struct CdioParanoiaInputStream { ...@@ -54,7 +54,7 @@ struct CdioParanoiaInputStream {
char buffer[CDIO_CD_FRAMESIZE_RAW]; char buffer[CDIO_CD_FRAMESIZE_RAW];
int buffer_lsn; int buffer_lsn;
CdioParanoiaInputStream(const char *uri, GMutex *mutex, GCond *cond, CdioParanoiaInputStream(const char *uri, Mutex &mutex, Cond &cond,
int _trackno) int _trackno)
:drv(nullptr), cdio(nullptr), para(nullptr), :drv(nullptr), cdio(nullptr), para(nullptr),
trackno(_trackno) trackno(_trackno)
...@@ -157,7 +157,7 @@ cdio_detect_device(void) ...@@ -157,7 +157,7 @@ cdio_detect_device(void)
static struct input_stream * static struct input_stream *
input_cdio_open(const char *uri, input_cdio_open(const char *uri,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r) GError **error_r)
{ {
struct cdio_uri parsed_uri; struct cdio_uri parsed_uri;
......
...@@ -165,7 +165,7 @@ struct input_curl { ...@@ -165,7 +165,7 @@ struct input_curl {
GError *postponed_error; GError *postponed_error;
input_curl(const char *url, GMutex *mutex, GCond *cond) input_curl(const char *url, Mutex &mutex, Cond &cond)
:range(nullptr), request_headers(nullptr), :range(nullptr), request_headers(nullptr),
paused(false), paused(false),
meta_name(nullptr), meta_name(nullptr),
...@@ -462,11 +462,12 @@ input_curl_abort_all_requests(GError *error) ...@@ -462,11 +462,12 @@ input_curl_abort_all_requests(GError *error)
input_curl_easy_free(c); input_curl_easy_free(c);
g_mutex_lock(c->base.mutex); const ScopeLock protect(*c->base.mutex);
c->postponed_error = g_error_copy(error); c->postponed_error = g_error_copy(error);
c->base.ready = true; c->base.ready = true;
g_cond_broadcast(c->base.cond);
g_mutex_unlock(c->base.mutex); c->base.cond->broadcast();
} }
g_error_free(error); g_error_free(error);
...@@ -486,7 +487,7 @@ input_curl_request_done(struct input_curl *c, CURLcode result, long status) ...@@ -486,7 +487,7 @@ input_curl_request_done(struct input_curl *c, CURLcode result, long status)
assert(c->easy == NULL); assert(c->easy == NULL);
assert(c->postponed_error == NULL); assert(c->postponed_error == NULL);
g_mutex_lock(c->base.mutex); const ScopeLock protect(*c->base.mutex);
if (result != CURLE_OK) { if (result != CURLE_OK) {
c->postponed_error = g_error_new(curl_quark(), result, c->postponed_error = g_error_new(curl_quark(), result,
...@@ -499,8 +500,8 @@ input_curl_request_done(struct input_curl *c, CURLcode result, long status) ...@@ -499,8 +500,8 @@ input_curl_request_done(struct input_curl *c, CURLcode result, long status)
} }
c->base.ready = true; c->base.ready = true;
g_cond_broadcast(c->base.cond);
g_mutex_unlock(c->base.mutex); c->base.cond->broadcast();
} }
static void static void
...@@ -736,7 +737,7 @@ static bool ...@@ -736,7 +737,7 @@ static bool
fill_buffer(struct input_curl *c, GError **error_r) fill_buffer(struct input_curl *c, GError **error_r)
{ {
while (c->easy != NULL && c->buffers.empty()) while (c->easy != NULL && c->buffers.empty())
g_cond_wait(c->base.cond, c->base.mutex); c->base.cond->wait(*c->base.mutex);
if (c->postponed_error != NULL) { if (c->postponed_error != NULL) {
g_propagate_error(error_r, c->postponed_error); g_propagate_error(error_r, c->postponed_error);
...@@ -856,9 +857,9 @@ input_curl_read(struct input_stream *is, void *ptr, size_t size, ...@@ -856,9 +857,9 @@ input_curl_read(struct input_stream *is, void *ptr, size_t size,
is->offset += (goffset)nbytes; is->offset += (goffset)nbytes;
if (c->paused && curl_total_buffer_size(c) < CURL_RESUME_AT) { if (c->paused && curl_total_buffer_size(c) < CURL_RESUME_AT) {
g_mutex_unlock(c->base.mutex); c->base.mutex->unlock();
io_thread_call(input_curl_resume, c); io_thread_call(input_curl_resume, c);
g_mutex_lock(c->base.mutex); c->base.mutex->lock();
} }
return nbytes; return nbytes;
...@@ -975,20 +976,17 @@ input_curl_writefunction(void *ptr, size_t size, size_t nmemb, void *stream) ...@@ -975,20 +976,17 @@ input_curl_writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
if (size == 0) if (size == 0)
return 0; return 0;
g_mutex_lock(c->base.mutex); const ScopeLock protect(*c->base.mutex);
if (curl_total_buffer_size(c) + size >= CURL_MAX_BUFFERED) { if (curl_total_buffer_size(c) + size >= CURL_MAX_BUFFERED) {
c->paused = true; c->paused = true;
g_mutex_unlock(c->base.mutex);
return CURL_WRITEFUNC_PAUSE; return CURL_WRITEFUNC_PAUSE;
} }
c->buffers.emplace_back(ptr, size); c->buffers.emplace_back(ptr, size);
c->base.ready = true; c->base.ready = true;
g_cond_broadcast(c->base.cond); c->base.cond->broadcast();
g_mutex_unlock(c->base.mutex);
return size; return size;
} }
...@@ -1112,7 +1110,7 @@ input_curl_seek(struct input_stream *is, goffset offset, int whence, ...@@ -1112,7 +1110,7 @@ input_curl_seek(struct input_stream *is, goffset offset, int whence,
/* close the old connection and open a new one */ /* close the old connection and open a new one */
g_mutex_unlock(c->base.mutex); c->base.mutex->unlock();
input_curl_easy_free_indirect(c); input_curl_easy_free_indirect(c);
c->buffers.clear(); c->buffers.clear();
...@@ -1141,10 +1139,10 @@ input_curl_seek(struct input_stream *is, goffset offset, int whence, ...@@ -1141,10 +1139,10 @@ input_curl_seek(struct input_stream *is, goffset offset, int whence,
if (!input_curl_easy_add_indirect(c, error_r)) if (!input_curl_easy_add_indirect(c, error_r))
return false; return false;
g_mutex_lock(c->base.mutex); c->base.mutex->lock();
while (!c->base.ready) while (!c->base.ready)
g_cond_wait(c->base.cond, c->base.mutex); c->base.cond->wait(*c->base.mutex);
if (c->postponed_error != NULL) { if (c->postponed_error != NULL) {
g_propagate_error(error_r, c->postponed_error); g_propagate_error(error_r, c->postponed_error);
...@@ -1156,12 +1154,9 @@ input_curl_seek(struct input_stream *is, goffset offset, int whence, ...@@ -1156,12 +1154,9 @@ input_curl_seek(struct input_stream *is, goffset offset, int whence,
} }
static struct input_stream * static struct input_stream *
input_curl_open(const char *url, GMutex *mutex, GCond *cond, input_curl_open(const char *url, Mutex &mutex, Cond &cond,
GError **error_r) GError **error_r)
{ {
assert(mutex != NULL);
assert(cond != NULL);
if (strncmp(url, "http://", 7) != 0) if (strncmp(url, "http://", 7) != 0)
return NULL; return NULL;
......
...@@ -102,7 +102,7 @@ static void callback(G_GNUC_UNUSED struct despotify_session* ds, ...@@ -102,7 +102,7 @@ static void callback(G_GNUC_UNUSED struct despotify_session* ds,
static struct input_stream * static struct input_stream *
input_despotify_open(const char *url, input_despotify_open(const char *url,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
G_GNUC_UNUSED GError **error_r) G_GNUC_UNUSED GError **error_r)
{ {
struct input_despotify *ctx; struct input_despotify *ctx;
......
...@@ -82,7 +82,7 @@ input_ffmpeg_init(G_GNUC_UNUSED const struct config_param *param, ...@@ -82,7 +82,7 @@ input_ffmpeg_init(G_GNUC_UNUSED const struct config_param *param,
static struct input_stream * static struct input_stream *
input_ffmpeg_open(const char *uri, input_ffmpeg_open(const char *uri,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r) GError **error_r)
{ {
struct input_ffmpeg *i; struct input_ffmpeg *i;
......
...@@ -41,7 +41,7 @@ struct FileInputStream { ...@@ -41,7 +41,7 @@ struct FileInputStream {
int fd; int fd;
FileInputStream(const char *path, int _fd, off_t size, FileInputStream(const char *path, int _fd, off_t size,
GMutex *mutex, GCond *cond) Mutex &mutex, Cond &cond)
:fd(_fd) { :fd(_fd) {
input_stream_init(&base, &input_plugin_file, path, input_stream_init(&base, &input_plugin_file, path,
mutex, cond); mutex, cond);
...@@ -59,7 +59,7 @@ struct FileInputStream { ...@@ -59,7 +59,7 @@ struct FileInputStream {
static struct input_stream * static struct input_stream *
input_file_open(const char *filename, input_file_open(const char *filename,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r) GError **error_r)
{ {
int fd, ret; int fd, ret;
......
...@@ -40,7 +40,7 @@ struct MmsInputStream { ...@@ -40,7 +40,7 @@ struct MmsInputStream {
bool eof; bool eof;
MmsInputStream(const char *uri, MmsInputStream(const char *uri,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
mmsx_t *_mms) mmsx_t *_mms)
:mms(_mms), eof(false) { :mms(_mms), eof(false) {
input_stream_init(&base, &input_plugin_mms, uri, mutex, cond); input_stream_init(&base, &input_plugin_mms, uri, mutex, cond);
...@@ -66,7 +66,7 @@ mms_quark(void) ...@@ -66,7 +66,7 @@ mms_quark(void)
static struct input_stream * static struct input_stream *
input_mms_open(const char *url, input_mms_open(const char *url,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r) GError **error_r)
{ {
if (!g_str_has_prefix(url, "mms://") && if (!g_str_has_prefix(url, "mms://") &&
......
...@@ -63,7 +63,7 @@ struct RewindInputStream { ...@@ -63,7 +63,7 @@ struct RewindInputStream {
RewindInputStream(input_stream *_input) RewindInputStream(input_stream *_input)
:input(_input), tail(0) { :input(_input), tail(0) {
input_stream_init(&base, &rewind_input_plugin, input->uri, input_stream_init(&base, &rewind_input_plugin, input->uri,
input->mutex, input->cond); *input->mutex, *input->cond);
} }
~RewindInputStream() { ~RewindInputStream() {
......
...@@ -165,7 +165,7 @@ input_soup_session_callback(G_GNUC_UNUSED SoupSession *session, ...@@ -165,7 +165,7 @@ input_soup_session_callback(G_GNUC_UNUSED SoupSession *session,
assert(msg == s->msg); assert(msg == s->msg);
assert(!s->completed); assert(!s->completed);
g_mutex_lock(s->base.mutex); const ScopeLock protect(*s->base.mutex);
if (!s->base.ready) if (!s->base.ready)
input_soup_copy_error(s, msg); input_soup_copy_error(s, msg);
...@@ -174,8 +174,7 @@ input_soup_session_callback(G_GNUC_UNUSED SoupSession *session, ...@@ -174,8 +174,7 @@ input_soup_session_callback(G_GNUC_UNUSED SoupSession *session,
s->alive = false; s->alive = false;
s->completed = true; s->completed = true;
g_cond_broadcast(s->base.cond); s->base.cond->broadcast();
g_mutex_unlock(s->base.mutex);
} }
static void static void
...@@ -183,10 +182,10 @@ input_soup_got_headers(SoupMessage *msg, gpointer user_data) ...@@ -183,10 +182,10 @@ input_soup_got_headers(SoupMessage *msg, gpointer user_data)
{ {
struct input_soup *s = (struct input_soup *)user_data; struct input_soup *s = (struct input_soup *)user_data;
g_mutex_lock(s->base.mutex); s->base.mutex->lock();
if (!input_soup_copy_error(s, msg)) { if (!input_soup_copy_error(s, msg)) {
g_mutex_unlock(s->base.mutex); s->base.mutex->unlock();
soup_session_cancel_message(soup_session, msg, soup_session_cancel_message(soup_session, msg,
SOUP_STATUS_CANCELLED); SOUP_STATUS_CANCELLED);
...@@ -194,8 +193,8 @@ input_soup_got_headers(SoupMessage *msg, gpointer user_data) ...@@ -194,8 +193,8 @@ input_soup_got_headers(SoupMessage *msg, gpointer user_data)
} }
s->base.ready = true; s->base.ready = true;
g_cond_broadcast(s->base.cond); s->base.cond->broadcast();
g_mutex_unlock(s->base.mutex); s->base.mutex->unlock();
soup_message_body_set_accumulate(msg->response_body, false); soup_message_body_set_accumulate(msg->response_body, false);
} }
...@@ -207,7 +206,7 @@ input_soup_got_chunk(SoupMessage *msg, SoupBuffer *chunk, gpointer user_data) ...@@ -207,7 +206,7 @@ input_soup_got_chunk(SoupMessage *msg, SoupBuffer *chunk, gpointer user_data)
assert(msg == s->msg); assert(msg == s->msg);
g_mutex_lock(s->base.mutex); const ScopeLock protect(*s->base.mutex);
g_queue_push_tail(s->buffers, soup_buffer_copy(chunk)); g_queue_push_tail(s->buffers, soup_buffer_copy(chunk));
s->total_buffered += chunk->length; s->total_buffered += chunk->length;
...@@ -217,8 +216,8 @@ input_soup_got_chunk(SoupMessage *msg, SoupBuffer *chunk, gpointer user_data) ...@@ -217,8 +216,8 @@ input_soup_got_chunk(SoupMessage *msg, SoupBuffer *chunk, gpointer user_data)
soup_session_pause_message(soup_session, msg); soup_session_pause_message(soup_session, msg);
} }
g_cond_broadcast(s->base.cond); s->base.cond->broadcast();
g_mutex_unlock(s->base.mutex); s->base.mutex->unlock();
} }
static void static void
...@@ -228,14 +227,14 @@ input_soup_got_body(G_GNUC_UNUSED SoupMessage *msg, gpointer user_data) ...@@ -228,14 +227,14 @@ input_soup_got_body(G_GNUC_UNUSED SoupMessage *msg, gpointer user_data)
assert(msg == s->msg); assert(msg == s->msg);
g_mutex_lock(s->base.mutex); const ScopeLock protect(*s->base.mutex);
s->base.ready = true; s->base.ready = true;
s->eof = true; s->eof = true;
s->alive = false; s->alive = false;
g_cond_broadcast(s->base.cond); s->base.cond->broadcast();
g_mutex_unlock(s->base.mutex); s->base.mutex->unlock();
} }
static bool static bool
...@@ -253,7 +252,7 @@ input_soup_wait_data(struct input_soup *s) ...@@ -253,7 +252,7 @@ input_soup_wait_data(struct input_soup *s)
assert(s->current_consumed == 0); assert(s->current_consumed == 0);
g_cond_wait(s->base.cond, s->base.mutex); s->base.cond->wait(*s->base.mutex);
} }
} }
...@@ -270,7 +269,7 @@ input_soup_queue(gpointer data) ...@@ -270,7 +269,7 @@ input_soup_queue(gpointer data)
static struct input_stream * static struct input_stream *
input_soup_open(const char *uri, input_soup_open(const char *uri,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
G_GNUC_UNUSED GError **error_r) G_GNUC_UNUSED GError **error_r)
{ {
if (strncmp(uri, "http://", 7) != 0) if (strncmp(uri, "http://", 7) != 0)
...@@ -338,22 +337,22 @@ input_soup_close(struct input_stream *is) ...@@ -338,22 +337,22 @@ input_soup_close(struct input_stream *is)
{ {
struct input_soup *s = (struct input_soup *)is; struct input_soup *s = (struct input_soup *)is;
g_mutex_lock(s->base.mutex); s->base.mutex->lock();
if (!s->completed) { if (!s->completed) {
/* the messages's session callback hasn't been invoked /* the messages's session callback hasn't been invoked
yet; cancel it and wait for completion */ yet; cancel it and wait for completion */
g_mutex_unlock(s->base.mutex); s->base.mutex->unlock();
io_thread_call(input_soup_cancel, s); io_thread_call(input_soup_cancel, s);
g_mutex_lock(s->base.mutex); s->base.mutex->lock();
while (!s->completed) while (!s->completed)
g_cond_wait(s->base.cond, s->base.mutex); s->base.cond->wait(*s->base.mutex);
} }
g_mutex_unlock(s->base.mutex); s->base.mutex->unlock();
SoupBuffer *buffer; SoupBuffer *buffer;
while ((buffer = (SoupBuffer *)g_queue_pop_head(s->buffers)) != NULL) while ((buffer = (SoupBuffer *)g_queue_pop_head(s->buffers)) != NULL)
......
...@@ -33,7 +33,9 @@ struct input_stream; ...@@ -33,7 +33,9 @@ struct input_stream;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
/** /**
* Opens a new input stream. You may not access it until the "ready" * Opens a new input stream. You may not access it until the "ready"
...@@ -46,13 +48,15 @@ extern "C" { ...@@ -46,13 +48,15 @@ extern "C" {
* notifications * notifications
* @return an #input_stream object on success, NULL on error * @return an #input_stream object on success, NULL on error
*/ */
gcc_nonnull(1, 2) gcc_nonnull(1)
G_GNUC_MALLOC G_GNUC_MALLOC
struct input_stream * struct input_stream *
input_stream_open(const char *uri, input_stream_open(const char *uri,
GMutex *mutex, GCond *cond, Mutex &mutex, Cond &cond,
GError **error_r); GError **error_r);
#endif
/** /**
* Close the input stream and free resources. * Close the input stream and free resources.
* *
......
...@@ -113,8 +113,8 @@ despotify_playlist_finish(void) ...@@ -113,8 +113,8 @@ despotify_playlist_finish(void)
static struct playlist_provider * static struct playlist_provider *
despotify_playlist_open_uri(const char *url, G_GNUC_UNUSED GMutex *mutex, despotify_playlist_open_uri(const char *url,
G_GNUC_UNUSED GCond *cond) gcc_unused Mutex &mutex, gcc_unused Cond &cond)
{ {
struct despotify_playlist *ctx; struct despotify_playlist *ctx;
struct despotify_session *session; struct despotify_session *session;
......
...@@ -85,8 +85,8 @@ static const struct tag_handler embcue_tag_handler = { ...@@ -85,8 +85,8 @@ static const struct tag_handler embcue_tag_handler = {
static struct playlist_provider * static struct playlist_provider *
embcue_playlist_open_uri(const char *uri, embcue_playlist_open_uri(const char *uri,
G_GNUC_UNUSED GMutex *mutex, gcc_unused Mutex &mutex,
G_GNUC_UNUSED GCond *cond) gcc_unused Cond &cond)
{ {
if (!g_path_is_absolute(uri)) if (!g_path_is_absolute(uri))
/* only local files supported */ /* only local files supported */
......
...@@ -79,7 +79,7 @@ lastfm_finish(void) ...@@ -79,7 +79,7 @@ lastfm_finish(void)
* @return data fetched, or NULL on error. Must be freed with g_free. * @return data fetched, or NULL on error. Must be freed with g_free.
*/ */
static char * static char *
lastfm_get(const char *url, GMutex *mutex, GCond *cond) lastfm_get(const char *url, Mutex &mutex, Cond &cond)
{ {
struct input_stream *input_stream; struct input_stream *input_stream;
GError *error = NULL; GError *error = NULL;
...@@ -96,7 +96,7 @@ lastfm_get(const char *url, GMutex *mutex, GCond *cond) ...@@ -96,7 +96,7 @@ lastfm_get(const char *url, GMutex *mutex, GCond *cond)
return NULL; return NULL;
} }
g_mutex_lock(mutex); mutex.lock();
input_stream_wait_ready(input_stream); input_stream_wait_ready(input_stream);
...@@ -113,7 +113,7 @@ lastfm_get(const char *url, GMutex *mutex, GCond *cond) ...@@ -113,7 +113,7 @@ lastfm_get(const char *url, GMutex *mutex, GCond *cond)
break; break;
/* I/O error */ /* I/O error */
g_mutex_unlock(mutex); mutex.unlock();
input_stream_close(input_stream); input_stream_close(input_stream);
return NULL; return NULL;
} }
...@@ -121,7 +121,7 @@ lastfm_get(const char *url, GMutex *mutex, GCond *cond) ...@@ -121,7 +121,7 @@ lastfm_get(const char *url, GMutex *mutex, GCond *cond)
length += nbytes; length += nbytes;
} while (length < sizeof(buffer)); } while (length < sizeof(buffer));
g_mutex_unlock(mutex); mutex.unlock();
input_stream_close(input_stream); input_stream_close(input_stream);
return g_strndup(buffer, length); return g_strndup(buffer, length);
...@@ -154,7 +154,7 @@ lastfm_find(const char *response, const char *name) ...@@ -154,7 +154,7 @@ lastfm_find(const char *response, const char *name)
} }
static struct playlist_provider * static struct playlist_provider *
lastfm_open_uri(const char *uri, GMutex *mutex, GCond *cond) lastfm_open_uri(const char *uri, Mutex &mutex, Cond &cond)
{ {
struct lastfm_playlist *playlist; struct lastfm_playlist *playlist;
GError *error = NULL; GError *error = NULL;
...@@ -235,7 +235,7 @@ lastfm_open_uri(const char *uri, GMutex *mutex, GCond *cond) ...@@ -235,7 +235,7 @@ lastfm_open_uri(const char *uri, GMutex *mutex, GCond *cond)
return NULL; return NULL;
} }
g_mutex_lock(mutex); mutex.lock();
input_stream_wait_ready(playlist->is); input_stream_wait_ready(playlist->is);
...@@ -243,7 +243,7 @@ lastfm_open_uri(const char *uri, GMutex *mutex, GCond *cond) ...@@ -243,7 +243,7 @@ lastfm_open_uri(const char *uri, GMutex *mutex, GCond *cond)
:-( */ :-( */
input_stream_override_mime_type(playlist->is, "application/xspf+xml"); input_stream_override_mime_type(playlist->is, "application/xspf+xml");
g_mutex_unlock(mutex); mutex.unlock();
/* parse the XSPF playlist */ /* parse the XSPF playlist */
......
...@@ -247,7 +247,8 @@ static yajl_callbacks parse_callbacks = { ...@@ -247,7 +247,8 @@ static yajl_callbacks parse_callbacks = {
* @return -1 on error, 0 on success. * @return -1 on error, 0 on success.
*/ */
static int static int
soundcloud_parse_json(const char *url, yajl_handle hand, GMutex* mutex, GCond* cond) soundcloud_parse_json(const char *url, yajl_handle hand,
Mutex &mutex, Cond &cond)
{ {
struct input_stream *input_stream; struct input_stream *input_stream;
GError *error = NULL; GError *error = NULL;
...@@ -264,7 +265,7 @@ soundcloud_parse_json(const char *url, yajl_handle hand, GMutex* mutex, GCond* c ...@@ -264,7 +265,7 @@ soundcloud_parse_json(const char *url, yajl_handle hand, GMutex* mutex, GCond* c
return -1; return -1;
} }
g_mutex_lock(mutex); mutex.lock();
input_stream_wait_ready(input_stream); input_stream_wait_ready(input_stream);
yajl_status stat; yajl_status stat;
...@@ -280,7 +281,7 @@ soundcloud_parse_json(const char *url, yajl_handle hand, GMutex* mutex, GCond* c ...@@ -280,7 +281,7 @@ soundcloud_parse_json(const char *url, yajl_handle hand, GMutex* mutex, GCond* c
if (input_stream_eof(input_stream)) { if (input_stream_eof(input_stream)) {
done = true; done = true;
} else { } else {
g_mutex_unlock(mutex); mutex.unlock();
input_stream_close(input_stream); input_stream_close(input_stream);
return -1; return -1;
} }
...@@ -308,7 +309,7 @@ soundcloud_parse_json(const char *url, yajl_handle hand, GMutex* mutex, GCond* c ...@@ -308,7 +309,7 @@ soundcloud_parse_json(const char *url, yajl_handle hand, GMutex* mutex, GCond* c
} }
} }
g_mutex_unlock(mutex); mutex.unlock();
input_stream_close(input_stream); input_stream_close(input_stream);
return 0; return 0;
...@@ -323,7 +324,7 @@ soundcloud_parse_json(const char *url, yajl_handle hand, GMutex* mutex, GCond* c ...@@ -323,7 +324,7 @@ soundcloud_parse_json(const char *url, yajl_handle hand, GMutex* mutex, GCond* c
*/ */
static struct playlist_provider * static struct playlist_provider *
soundcloud_open_uri(const char *uri, GMutex *mutex, GCond *cond) soundcloud_open_uri(const char *uri, Mutex &mutex, Cond &cond)
{ {
struct soundcloud_playlist *playlist = NULL; struct soundcloud_playlist *playlist = NULL;
......
...@@ -186,8 +186,8 @@ int main(int argc, char **argv) ...@@ -186,8 +186,8 @@ int main(int argc, char **argv)
/* open the playlist */ /* open the playlist */
GMutex *mutex = g_mutex_new(); Mutex mutex;
GCond *cond = g_cond_new(); Cond cond;
playlist = playlist_list_open_uri(uri, mutex, cond); playlist = playlist_list_open_uri(uri, mutex, cond);
if (playlist == NULL) { if (playlist == NULL) {
...@@ -243,9 +243,6 @@ int main(int argc, char **argv) ...@@ -243,9 +243,6 @@ int main(int argc, char **argv)
if (is != NULL) if (is != NULL)
input_stream_close(is); input_stream_close(is);
g_cond_free(cond);
g_mutex_free(mutex);
decoder_plugin_deinit_all(); decoder_plugin_deinit_all();
playlist_list_global_finish(); playlist_list_global_finish();
input_stream_global_finish(); input_stream_global_finish();
......
...@@ -135,8 +135,8 @@ int main(int argc, char **argv) ...@@ -135,8 +135,8 @@ int main(int argc, char **argv)
/* open the stream and dump it */ /* open the stream and dump it */
GMutex *mutex = g_mutex_new(); Mutex mutex;
GCond *cond = g_cond_new(); Cond cond;
is = input_stream_open(argv[1], mutex, cond, &error); is = input_stream_open(argv[1], mutex, cond, &error);
if (is != NULL) { if (is != NULL) {
...@@ -151,9 +151,6 @@ int main(int argc, char **argv) ...@@ -151,9 +151,6 @@ int main(int argc, char **argv)
ret = 2; ret = 2;
} }
g_cond_free(cond);
g_mutex_free(mutex);
/* deinitialize everything */ /* deinitialize everything */
input_stream_global_finish(); input_stream_global_finish();
......
...@@ -186,8 +186,8 @@ int main(int argc, char **argv) ...@@ -186,8 +186,8 @@ int main(int argc, char **argv)
bool success = decoder_plugin_scan_file(plugin, path, bool success = decoder_plugin_scan_file(plugin, path,
&print_handler, NULL); &print_handler, NULL);
if (!success && plugin->scan_stream != NULL) { if (!success && plugin->scan_stream != NULL) {
GMutex *mutex = g_mutex_new(); Mutex mutex;
GCond *cond = g_cond_new(); Cond cond;
struct input_stream *is = struct input_stream *is =
input_stream_open(path, mutex, cond, &error); input_stream_open(path, mutex, cond, &error);
...@@ -199,15 +199,15 @@ int main(int argc, char **argv) ...@@ -199,15 +199,15 @@ int main(int argc, char **argv)
return 1; return 1;
} }
g_mutex_lock(mutex); mutex.lock();
while (!is->ready) { while (!is->ready) {
g_cond_wait(cond, mutex); cond.wait(mutex);
input_stream_update(is); input_stream_update(is);
} }
if (!input_stream_check(is, &error)) { if (!input_stream_check(is, &error)) {
g_mutex_unlock(mutex); mutex.unlock();
g_printerr("Failed to read %s: %s\n", g_printerr("Failed to read %s: %s\n",
path, error->message); path, error->message);
...@@ -216,14 +216,11 @@ int main(int argc, char **argv) ...@@ -216,14 +216,11 @@ int main(int argc, char **argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
g_mutex_unlock(mutex); mutex.unlock();
success = decoder_plugin_scan_stream(plugin, is, success = decoder_plugin_scan_stream(plugin, is,
&print_handler, NULL); &print_handler, NULL);
input_stream_close(is); input_stream_close(is);
g_cond_free(cond);
g_mutex_free(mutex);
} }
decoder_plugin_deinit_all(); decoder_plugin_deinit_all();
......
...@@ -188,8 +188,8 @@ int main(int argc, char **argv) ...@@ -188,8 +188,8 @@ int main(int argc, char **argv)
decoder_plugin_file_decode(decoder.plugin, &decoder, decoder_plugin_file_decode(decoder.plugin, &decoder,
decoder.uri); decoder.uri);
} else if (decoder.plugin->stream_decode != NULL) { } else if (decoder.plugin->stream_decode != NULL) {
GMutex *mutex = g_mutex_new(); Mutex mutex;
GCond *cond = g_cond_new(); Cond cond;
struct input_stream *is = struct input_stream *is =
input_stream_open(decoder.uri, mutex, cond, &error); input_stream_open(decoder.uri, mutex, cond, &error);
...@@ -206,9 +206,6 @@ int main(int argc, char **argv) ...@@ -206,9 +206,6 @@ int main(int argc, char **argv)
decoder_plugin_stream_decode(decoder.plugin, &decoder, is); decoder_plugin_stream_decode(decoder.plugin, &decoder, is);
input_stream_close(is); input_stream_close(is);
g_cond_free(cond);
g_mutex_free(mutex);
} else { } else {
g_printerr("Decoder plugin is not usable\n"); g_printerr("Decoder plugin is not usable\n");
return 1; return 1;
......
...@@ -149,8 +149,8 @@ int main(int argc, char **argv) ...@@ -149,8 +149,8 @@ int main(int argc, char **argv)
/* open the stream and dump it */ /* open the stream and dump it */
GMutex *mutex = g_mutex_new(); Mutex mutex;
GCond *cond = g_cond_new(); Cond cond;
is = input_stream_open(argv[1], mutex, cond, &error); is = input_stream_open(argv[1], mutex, cond, &error);
if (is != NULL) { if (is != NULL) {
...@@ -165,9 +165,6 @@ int main(int argc, char **argv) ...@@ -165,9 +165,6 @@ int main(int argc, char **argv)
ret = 2; ret = 2;
} }
g_cond_free(cond);
g_mutex_free(mutex);
/* deinitialize everything */ /* deinitialize everything */
input_stream_global_finish(); input_stream_global_finish();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment