Skip to content

Commit

Permalink
allocate GMutex and GCond statically, or embed in structures
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Matthew committed Jul 21, 2012
1 parent 6686ded commit 30fde08
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 335 deletions.
181 changes: 90 additions & 91 deletions backends/gstreamer/rb-player-gst-xfade.c

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions lib/rb-file-helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ typedef struct {
gpointer user_data;
GDestroyNotify data_destroy;

GMutex *results_lock;
GMutex results_lock;
guint results_idle_id;
GList *file_results;
GList *dir_results;
Expand Down Expand Up @@ -844,7 +844,7 @@ _recurse_async_idle_cb (RBUriHandleRecursivelyAsyncData *data)
{
GList *ul, *dl;

g_mutex_lock (data->results_lock);
g_mutex_lock (&data->results_lock);

for (ul = data->file_results, dl = data->dir_results;
ul != NULL;
Expand All @@ -862,7 +862,7 @@ _recurse_async_idle_cb (RBUriHandleRecursivelyAsyncData *data)
data->dir_results = NULL;

data->results_idle_id = 0;
g_mutex_unlock (data->results_lock);
g_mutex_unlock (&data->results_lock);
return FALSE;
}

Expand Down Expand Up @@ -895,23 +895,22 @@ _recurse_async_data_free (RBUriHandleRecursivelyAsyncData *data)
}

g_free (data->uri);
g_mutex_free (data->results_lock);
return FALSE;
}

/* runs in worker thread */
static gboolean
_recurse_async_cb (GFile *file, gboolean dir, RBUriHandleRecursivelyAsyncData *data)
{
g_mutex_lock (data->results_lock);
g_mutex_lock (&data->results_lock);

data->file_results = g_list_prepend (data->file_results, g_object_ref (file));
data->dir_results = g_list_prepend (data->dir_results, GINT_TO_POINTER (dir ? 1 : 0));
if (data->results_idle_id == 0) {
g_idle_add ((GSourceFunc)_recurse_async_idle_cb, data);
}

g_mutex_unlock (data->results_lock);
g_mutex_unlock (&data->results_lock);
return TRUE;
}

Expand Down Expand Up @@ -961,7 +960,7 @@ rb_uri_handle_recursively_async (const char *uri,
}
data->data_destroy = data_destroy;

data->results_lock = g_mutex_new ();
g_mutex_init (&data->results_lock);
data->func = func;
data->user_data = user_data;

Expand Down
33 changes: 15 additions & 18 deletions lib/rb-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#include "rb-util.h"
#include "rb-debug.h"

static GPrivate * private_is_primary_thread;
static GPrivate private_is_primary_thread;

/**
* rb_true_function: (skip):
Expand Down Expand Up @@ -139,9 +139,9 @@ rb_gvalue_compare (GValue *a, GValue *b)
retval = 1;
break;
case G_TYPE_CHAR:
if (g_value_get_char (a) < g_value_get_char (b))
if (g_value_get_schar (a) < g_value_get_schar (b))
retval = -1;
else if (g_value_get_char (a) == g_value_get_char (b))
else if (g_value_get_schar (a) == g_value_get_schar (b))
retval = 0;
else
retval = 1;
Expand Down Expand Up @@ -409,7 +409,7 @@ gboolean
rb_is_main_thread (void)
{
if (g_thread_supported()) {
return GPOINTER_TO_UINT(g_private_get (private_is_primary_thread)) == 1;
return GPOINTER_TO_UINT(g_private_get (&private_is_primary_thread)) == 1;
} else {
return TRUE;
}
Expand All @@ -423,19 +423,19 @@ purge_useless_threads (gpointer data)
}


static GStaticRecMutex rb_gdk_mutex;
static GRecMutex rb_gdk_mutex;
static gboolean mutex_recurses;

static void
_threads_enter (void)
{
g_static_rec_mutex_lock (&rb_gdk_mutex);
g_rec_mutex_lock (&rb_gdk_mutex);
}

static void
_threads_leave (void)
{
g_static_rec_mutex_unlock (&rb_gdk_mutex);
g_rec_mutex_unlock (&rb_gdk_mutex);
}


Expand All @@ -461,23 +461,20 @@ rb_assert_locked (GMutex *mutex)
void
rb_threads_init (void)
{
GMutex *m;
GMutex m;

private_is_primary_thread = g_private_new (NULL);
g_private_set (private_is_primary_thread, GUINT_TO_POINTER (1));
g_private_set (&private_is_primary_thread, GUINT_TO_POINTER (1));

g_static_rec_mutex_init (&rb_gdk_mutex);
g_rec_mutex_init (&rb_gdk_mutex);
gdk_threads_set_lock_functions (_threads_enter, _threads_leave);
gdk_threads_init ();

m = g_mutex_new ();

g_mutex_lock (m);
mutex_recurses = g_mutex_trylock (m);
g_mutex_init (&m);
g_mutex_lock (&m);
mutex_recurses = g_mutex_trylock (&m);
if (mutex_recurses)
g_mutex_unlock (m);
g_mutex_unlock (m);
g_mutex_free (m);
g_mutex_unlock (&m);
g_mutex_unlock (&m);

rb_debug ("GMutex %s recursive", mutex_recurses ? "is" : "isn't");

Expand Down
30 changes: 12 additions & 18 deletions plugins/mtpdevice/rb-mtp-gst-sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ struct _RBMTPSink
GstPad *ghostpad;

GError *upload_error;
GMutex *upload_mutex;
GCond *upload_cond;
GMutex upload_mutex;
GCond upload_cond;
gboolean got_folder;
gboolean upload_done;
};
Expand Down Expand Up @@ -124,9 +124,6 @@ rb_mtp_sink_init (RBMTPSink *sink, RBMTPSinkClass *klass)
{
GstPad *pad;

sink->upload_mutex = g_mutex_new ();
sink->upload_cond = g_cond_new ();

/* create actual sink */
sink->fdsink = gst_element_factory_make ("fdsink", NULL);
if (sink->fdsink == NULL) {
Expand Down Expand Up @@ -179,7 +176,7 @@ rb_mtp_sink_close_tempfile (RBMTPSink *sink)
static void
folder_callback (uint32_t folder_id, RBMTPSink *sink)
{
g_mutex_lock (sink->upload_mutex);
g_mutex_lock (&sink->upload_mutex);
if (folder_id == 0) {
rb_debug ("mtp folder create failed");
} else {
Expand All @@ -189,23 +186,23 @@ folder_callback (uint32_t folder_id, RBMTPSink *sink)

sink->got_folder = TRUE;

g_cond_signal (sink->upload_cond);
g_mutex_unlock (sink->upload_mutex);
g_cond_signal (&sink->upload_cond);
g_mutex_unlock (&sink->upload_mutex);
}

static void
upload_callback (LIBMTP_track_t *track, GError *error, RBMTPSink *sink)
{
rb_debug ("mtp upload callback for %s: item ID %d", track->filename, track->item_id);
g_mutex_lock (sink->upload_mutex);
g_mutex_lock (&sink->upload_mutex);

if (error != NULL) {
sink->upload_error = g_error_copy (error);
}
sink->upload_done = TRUE;

g_cond_signal (sink->upload_cond);
g_mutex_unlock (sink->upload_mutex);
g_cond_signal (&sink->upload_cond);
g_mutex_unlock (&sink->upload_mutex);
}

static void
Expand All @@ -231,7 +228,7 @@ rb_mtp_sink_handle_message (GstBin *bin, GstMessage *message)
/* we can just block waiting for mtp thread operations to finish here
* as we're on a streaming thread.
*/
g_mutex_lock (sink->upload_mutex);
g_mutex_lock (&sink->upload_mutex);

if (sink->folder_path != NULL) {
/* find or create the target folder.
Expand All @@ -245,7 +242,7 @@ rb_mtp_sink_handle_message (GstBin *bin, GstMessage *message)
g_object_ref (sink),
g_object_unref);
while (sink->got_folder == FALSE) {
g_cond_wait (sink->upload_cond, sink->upload_mutex);
g_cond_wait (&sink->upload_cond, &sink->upload_mutex);
}
}

Expand All @@ -259,9 +256,9 @@ rb_mtp_sink_handle_message (GstBin *bin, GstMessage *message)
g_object_unref);

while (sink->upload_done == FALSE) {
g_cond_wait (sink->upload_cond, sink->upload_mutex);
g_cond_wait (&sink->upload_cond, &sink->upload_mutex);
}
g_mutex_unlock (sink->upload_mutex);
g_mutex_unlock (&sink->upload_mutex);

/* post error message if the upload failed - this should get there before
* this EOS message does, so it should work OK.
Expand Down Expand Up @@ -406,9 +403,6 @@ rb_mtp_sink_finalize (GObject *object)
RBMTPSink *sink;
sink = RB_MTP_SINK (object);

g_mutex_free (sink->upload_mutex);
g_cond_free (sink->upload_cond);

if (sink->upload_error) {
g_error_free (sink->upload_error);
}
Expand Down
21 changes: 8 additions & 13 deletions plugins/mtpdevice/rb-mtp-gst-src.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ struct _RBMTPSrc
guint64 read_position;

GError *download_error;
GMutex *download_mutex;
GCond *download_cond;
GMutex download_mutex;
GCond download_cond;
gboolean download_done;
};

Expand Down Expand Up @@ -121,8 +121,6 @@ rb_mtp_src_base_init (gpointer g_class)
static void
rb_mtp_src_init (RBMTPSrc *src, RBMTPSrcClass *klass)
{
src->download_mutex = g_mutex_new ();
src->download_cond = g_cond_new ();
}

static gboolean
Expand Down Expand Up @@ -222,7 +220,7 @@ static void
download_cb (LIBMTP_track_t *track, const char *filename, GError *error, RBMTPSrc *src)
{
rb_debug ("mtp download callback for %s: %s", filename, error ? error->message : "OK");
g_mutex_lock (src->download_mutex);
g_mutex_lock (&src->download_mutex);

if (filename == NULL) {
src->download_error = g_error_copy (error);
Expand All @@ -231,8 +229,8 @@ download_cb (LIBMTP_track_t *track, const char *filename, GError *error, RBMTPSr
}
src->download_done = TRUE;

g_cond_signal (src->download_cond);
g_mutex_unlock (src->download_mutex);
g_cond_signal (&src->download_cond);
g_mutex_unlock (&src->download_mutex);
}

static gboolean
Expand All @@ -242,7 +240,7 @@ rb_mtp_src_start (GstBaseSrc *basesrc)

/* download the file, if we haven't already */
if (src->tempfile == NULL) {
g_mutex_lock (src->download_mutex);
g_mutex_lock (&src->download_mutex);
src->download_done = FALSE;
rb_mtp_thread_download_track (src->device_thread,
src->track_id,
Expand All @@ -252,9 +250,9 @@ rb_mtp_src_start (GstBaseSrc *basesrc)
g_object_unref);

while (src->download_done == FALSE) {
g_cond_wait (src->download_cond, src->download_mutex);
g_cond_wait (&src->download_cond, &src->download_mutex);
}
g_mutex_unlock (src->download_mutex);
g_mutex_unlock (&src->download_mutex);
rb_debug ("download finished");

if (src->download_error) {
Expand Down Expand Up @@ -371,9 +369,6 @@ rb_mtp_src_finalize (GObject *object)
RBMTPSrc *src;
src = RB_MTP_SRC (object);

g_mutex_free (src->download_mutex);
g_cond_free (src->download_cond);

if (src->download_error) {
g_error_free (src->download_error);
}
Expand Down
19 changes: 8 additions & 11 deletions rhythmdb/rb-refstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "rb-refstring.h"

GHashTable *rb_refstrings;
GMutex *rb_refstrings_mutex;
GMutex rb_refstrings_mutex;

struct RBRefString
{
Expand Down Expand Up @@ -63,8 +63,6 @@ rb_refstring_free (RBRefString *refstr)
void
rb_refstring_system_init ()
{
rb_refstrings_mutex = g_mutex_new ();

rb_refstrings = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, (GDestroyNotify) rb_refstring_free);
}
Expand All @@ -84,12 +82,12 @@ rb_refstring_new (const char *init)
{
RBRefString *ret;

g_mutex_lock (rb_refstrings_mutex);
g_mutex_lock (&rb_refstrings_mutex);
ret = g_hash_table_lookup (rb_refstrings, init);

if (ret) {
rb_refstring_ref (ret);
g_mutex_unlock (rb_refstrings_mutex);
g_mutex_unlock (&rb_refstrings_mutex);
return ret;
}

Expand All @@ -101,7 +99,7 @@ rb_refstring_new (const char *init)
ret->sortkey = NULL;

g_hash_table_insert (rb_refstrings, ret->value, ret);
g_mutex_unlock (rb_refstrings_mutex);
g_mutex_unlock (&rb_refstrings_mutex);
return ret;
}

Expand All @@ -120,13 +118,13 @@ rb_refstring_find (const char *init)
{
RBRefString *ret;

g_mutex_lock (rb_refstrings_mutex);
g_mutex_lock (&rb_refstrings_mutex);
ret = g_hash_table_lookup (rb_refstrings, init);

if (ret)
rb_refstring_ref (ret);

g_mutex_unlock (rb_refstrings_mutex);
g_mutex_unlock (&rb_refstrings_mutex);
return ret;
}

Expand All @@ -146,12 +144,12 @@ rb_refstring_unref (RBRefString *val)
g_return_if_fail (g_atomic_int_get (&val->refcount) > 0);

if (g_atomic_int_dec_and_test (&val->refcount)) {
g_mutex_lock (rb_refstrings_mutex);
g_mutex_lock (&rb_refstrings_mutex);
/* ensure it's still not referenced, as something may have called
* rb_refstring_new since we decremented the count */
if (g_atomic_int_get (&val->refcount) == 0)
g_hash_table_remove (rb_refstrings, val->value);
g_mutex_unlock (rb_refstrings_mutex);
g_mutex_unlock (&rb_refstrings_mutex);
}
}

Expand All @@ -165,7 +163,6 @@ void
rb_refstring_system_shutdown (void)
{
g_hash_table_destroy (rb_refstrings);
g_mutex_free (rb_refstrings_mutex);
}

/**
Expand Down
Loading

0 comments on commit 30fde08

Please sign in to comment.