dirvec: migrate from pthread to glib threads

This commit is contained in:
Thomas Jansen 2008-12-28 21:02:30 +01:00
parent 8332a70406
commit 4ed895c799
3 changed files with 36 additions and 17 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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();