aboutsummaryrefslogtreecommitdiffstats
path: root/src/dirvec.c
diff options
context:
space:
mode:
authorThomas Jansen <mithi@mithi.net>2008-12-28 21:02:30 +0100
committerThomas Jansen <mithi@mithi.net>2008-12-28 21:02:30 +0100
commit4ed895c79920fdc7acea2353723cdd1ff76a5d29 (patch)
tree2de0e08825cfc6c0a94ab71d4aca083073d87791 /src/dirvec.c
parent8332a70406140cbc7594ca9d54cb9a600b470bb4 (diff)
downloadmpd-4ed895c79920fdc7acea2353723cdd1ff76a5d29.tar.gz
mpd-4ed895c79920fdc7acea2353723cdd1ff76a5d29.tar.xz
mpd-4ed895c79920fdc7acea2353723cdd1ff76a5d29.zip
dirvec: migrate from pthread to glib threads
Diffstat (limited to 'src/dirvec.c')
-rw-r--r--src/dirvec.c46
1 files changed, 29 insertions, 17 deletions
diff --git a/src/dirvec.c b/src/dirvec.c
index b24512a1c..cf65fe659 100644
--- a/src/dirvec.c
+++ b/src/dirvec.c
@@ -5,9 +5,8 @@
#include <string.h>
#include <glib.h>
-#include <pthread.h>
-static pthread_mutex_t nr_lock = PTHREAD_MUTEX_INITIALIZER;
+static GMutex *nr_lock = NULL;
static size_t dv_size(const struct dirvec *dv)
{
@@ -22,11 +21,24 @@ static int dirvec_cmp(const void *d1, const void *d2)
return strcmp(a->path, b->path);
}
+void dirvec_init(void)
+{
+ g_assert(nr_lock == NULL);
+ nr_lock = g_mutex_new();
+}
+
+void dirvec_deinit(void)
+{
+ g_assert(nr_lock != NULL);
+ g_mutex_free(nr_lock);
+ nr_lock = NULL;
+}
+
void dirvec_sort(struct dirvec *dv)
{
- pthread_mutex_lock(&nr_lock);
+ g_mutex_lock(nr_lock);
qsort(dv->base, dv->nr, sizeof(struct directory *), dirvec_cmp);
- pthread_mutex_unlock(&nr_lock);
+ g_mutex_unlock(nr_lock);
}
struct directory *dirvec_find(const struct dirvec *dv, const char *path)
@@ -37,13 +49,13 @@ struct directory *dirvec_find(const struct dirvec *dv, const char *path)
base = g_path_get_basename(path);
- pthread_mutex_lock(&nr_lock);
+ g_mutex_lock(nr_lock);
for (i = dv->nr; --i >= 0; )
if (!strcmp(directory_get_name(dv->base[i]), base)) {
ret = dv->base[i];
break;
}
- pthread_mutex_unlock(&nr_lock);
+ g_mutex_unlock(nr_lock);
g_free(base);
return ret;
@@ -53,13 +65,13 @@ int dirvec_delete(struct dirvec *dv, struct directory *del)
{
size_t i;
- pthread_mutex_lock(&nr_lock);
+ g_mutex_lock(nr_lock);
for (i = 0; i < dv->nr; ++i) {
if (dv->base[i] != del)
continue;
/* we _don't_ call directory_free() here */
if (!--dv->nr) {
- pthread_mutex_unlock(&nr_lock);
+ g_mutex_unlock(nr_lock);
free(dv->base);
dv->base = NULL;
return i;
@@ -70,25 +82,25 @@ int dirvec_delete(struct dirvec *dv, struct directory *del)
}
break;
}
- pthread_mutex_unlock(&nr_lock);
+ g_mutex_unlock(nr_lock);
return i;
}
void dirvec_add(struct dirvec *dv, struct directory *add)
{
- pthread_mutex_lock(&nr_lock);
+ g_mutex_lock(nr_lock);
++dv->nr;
dv->base = xrealloc(dv->base, dv_size(dv));
dv->base[dv->nr - 1] = add;
- pthread_mutex_unlock(&nr_lock);
+ g_mutex_unlock(nr_lock);
}
void dirvec_destroy(struct dirvec *dv)
{
- pthread_mutex_lock(&nr_lock);
+ g_mutex_lock(nr_lock);
dv->nr = 0;
- pthread_mutex_unlock(&nr_lock);
+ g_mutex_unlock(nr_lock);
if (dv->base) {
free(dv->base);
dv->base = NULL;
@@ -101,20 +113,20 @@ int dirvec_for_each(const struct dirvec *dv,
size_t i;
size_t prev_nr;
- pthread_mutex_lock(&nr_lock);
+ g_mutex_lock(nr_lock);
for (i = 0; i < dv->nr; ) {
struct directory *dir = dv->base[i];
assert(dir);
prev_nr = dv->nr;
- pthread_mutex_unlock(&nr_lock);
+ g_mutex_unlock(nr_lock);
if (fn(dir, arg) < 0)
return -1;
- pthread_mutex_lock(&nr_lock); /* dv->nr may change in fn() */
+ g_mutex_lock(nr_lock); /* dv->nr may change in fn() */
if (prev_nr == dv->nr)
++i;
}
- pthread_mutex_unlock(&nr_lock);
+ g_mutex_unlock(nr_lock);
return 0;
}