diff options
Diffstat (limited to 'src/dirvec.c')
-rw-r--r-- | src/dirvec.c | 46 |
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; } |