update & main_notify: migrate from pthread to glib threads

This commit is contained in:
Thomas Jansen 2008-12-28 22:09:42 +01:00
parent 195cec505e
commit 248cd50a20
3 changed files with 30 additions and 24 deletions

View File

@ -30,9 +30,9 @@
static struct ioOps main_notify_IO;
static int main_pipe[2];
pthread_t main_task;
GThread *main_task;
static struct notify main_notify;
static pthread_mutex_t select_mutex = PTHREAD_MUTEX_INITIALIZER;
static GMutex *select_mutex = NULL;
static int ioops_fdset(fd_set * rfds,
G_GNUC_UNUSED fd_set * wfds,
@ -65,12 +65,14 @@ static int ioops_consume(int fd_count, fd_set * rfds,
void init_main_notify(void)
{
main_task = pthread_self();
g_assert(select_mutex == NULL);
select_mutex = g_mutex_new();
main_task = g_thread_self();
init_async_pipe(main_pipe);
main_notify_IO.fdset = ioops_fdset;
main_notify_IO.consume = ioops_consume;
registerIO(&main_notify_IO);
main_task = pthread_self();
main_task = g_thread_self();
notify_init(&main_notify);
}
@ -80,19 +82,22 @@ void deinit_main_notify(void)
deregisterIO(&main_notify_IO);
xclose(main_pipe[0]);
xclose(main_pipe[1]);
g_assert(select_mutex != NULL);
g_mutex_free(select_mutex);
select_mutex = NULL;
}
static int wakeup_via_pipe(void)
{
int ret = pthread_mutex_trylock(&select_mutex);
if (ret == EBUSY) {
gboolean ret = g_mutex_trylock(select_mutex);
if (ret == FALSE) {
ssize_t w = write(main_pipe[1], "", 1);
if (w < 0 && errno != EAGAIN && errno != EINTR)
FATAL("error writing to pipe: %s\n",
strerror(errno));
return 1;
} else {
pthread_mutex_unlock(&select_mutex);
g_mutex_unlock(select_mutex);
return 0;
}
}
@ -107,19 +112,19 @@ void wakeup_main_task(void)
void main_notify_lock(void)
{
assert(pthread_equal(main_task, pthread_self()));
pthread_mutex_lock(&select_mutex);
assert(main_task == g_thread_self());
g_mutex_lock(select_mutex);
}
void main_notify_unlock(void)
{
assert(pthread_equal(main_task, pthread_self()));
pthread_mutex_unlock(&select_mutex);
assert(main_task == g_thread_self());
g_mutex_unlock(select_mutex);
}
void wait_main_task(void)
{
assert(pthread_equal(main_task, pthread_self()));
assert(main_task == g_thread_self());
notify_wait(&main_notify);
}

View File

@ -21,9 +21,9 @@
#ifndef MPD_MAIN_NOTIFY_H
#define MPD_MAIN_NOTIFY_H
#include <pthread.h>
#include <glib.h>
extern pthread_t main_task;
extern GThread *main_task;
void init_main_notify(void);

View File

@ -47,7 +47,7 @@ static bool modified;
static char *update_paths[32];
static size_t update_paths_nr;
static pthread_t update_thr;
static GThread *update_thr;
static const unsigned update_task_id_max = 1 << 15;
@ -595,15 +595,14 @@ static void * update_task(void *_path)
static void spawn_update_task(char *path)
{
pthread_attr_t attr;
GError *e;
assert(pthread_equal(pthread_self(), main_task));
assert(g_thread_self() == main_task);
progress = UPDATE_PROGRESS_RUNNING;
modified = false;
pthread_attr_init(&attr);
if (pthread_create(&update_thr, &attr, update_task, path))
FATAL("Failed to spawn update task: %s\n", strerror(errno));
if (!(update_thr = g_thread_create(update_task, path, TRUE, &e)))
FATAL("Failed to spawn update task: %s\n", e->message);
if (++update_task_id > update_task_id_max)
update_task_id = 1;
DEBUG("spawned thread for update job id %i\n", update_task_id);
@ -612,7 +611,7 @@ static void spawn_update_task(char *path)
unsigned
directory_update_init(char *path)
{
assert(pthread_equal(pthread_self(), main_task));
assert(g_thread_self() == main_task);
if (progress != UPDATE_PROGRESS_IDLE) {
unsigned next_task_id;
@ -635,7 +634,7 @@ directory_update_init(char *path)
void reap_update_task(void)
{
assert(pthread_equal(pthread_self(), main_task));
assert(g_thread_self() == main_task);
if (progress == UPDATE_PROGRESS_IDLE)
return;
@ -652,8 +651,7 @@ void reap_update_task(void)
if (progress != UPDATE_PROGRESS_DONE)
return;
if (pthread_join(update_thr, NULL))
FATAL("error joining update thread: %s\n", strerror(errno));
g_thread_join(update_thr);
if (modified) {
playlistVersionChange();
@ -679,8 +677,11 @@ void update_global_init(void)
follow_outside_symlinks =
config_get_bool(CONF_FOLLOW_OUTSIDE_SYMLINKS,
DEFAULT_FOLLOW_OUTSIDE_SYMLINKS);
cond_init(&delete_cond);
}
void update_global_finish(void)
{
cond_destroy(&delete_cond);
}