aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/dirvec.c46
-rw-r--r--src/dirvec.h4
-rw-r--r--src/main.c3
3 files changed, 36 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;
}
diff --git a/src/dirvec.h b/src/dirvec.h
index fe983e258..dd49b9b57 100644
--- a/src/dirvec.h
+++ b/src/dirvec.h
@@ -8,6 +8,10 @@ struct dirvec {
size_t nr;
};
+void dirvec_init(void);
+
+void dirvec_deinit(void);
+
void dirvec_sort(struct dirvec *dv);
struct directory *dirvec_find(const struct dirvec *dv, const char *path);
diff --git a/src/main.c b/src/main.c
index e9f696bb0..1e7f0ac19 100644
--- a/src/main.c
+++ b/src/main.c
@@ -50,6 +50,7 @@
#include "zeroconf.h"
#include "main_notify.h"
#include "os_compat.h"
+#include "dirvec.h"
#ifdef ENABLE_ARCHIVE
#include "archive_list.h"
@@ -269,6 +270,7 @@ int main(int argc, char *argv[])
g_thread_init(NULL);
idle_init();
+ dirvec_init();
initConf();
parseOptions(argc, argv, &options);
@@ -383,6 +385,7 @@ int main(int argc, char *argv[])
music_pipe_free();
cleanUpPidFile();
finishConf();
+ dirvec_deinit();
idle_deinit();
close_log_files();