Commit 248cd50a authored by Thomas Jansen's avatar Thomas Jansen

update & main_notify: migrate from pthread to glib threads

parent 195cec50
...@@ -30,9 +30,9 @@ ...@@ -30,9 +30,9 @@
static struct ioOps main_notify_IO; static struct ioOps main_notify_IO;
static int main_pipe[2]; static int main_pipe[2];
pthread_t main_task; GThread *main_task;
static struct notify main_notify; 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, static int ioops_fdset(fd_set * rfds,
G_GNUC_UNUSED fd_set * wfds, G_GNUC_UNUSED fd_set * wfds,
...@@ -65,12 +65,14 @@ static int ioops_consume(int fd_count, fd_set * rfds, ...@@ -65,12 +65,14 @@ static int ioops_consume(int fd_count, fd_set * rfds,
void init_main_notify(void) 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); init_async_pipe(main_pipe);
main_notify_IO.fdset = ioops_fdset; main_notify_IO.fdset = ioops_fdset;
main_notify_IO.consume = ioops_consume; main_notify_IO.consume = ioops_consume;
registerIO(&main_notify_IO); registerIO(&main_notify_IO);
main_task = pthread_self(); main_task = g_thread_self();
notify_init(&main_notify); notify_init(&main_notify);
} }
...@@ -80,19 +82,22 @@ void deinit_main_notify(void) ...@@ -80,19 +82,22 @@ void deinit_main_notify(void)
deregisterIO(&main_notify_IO); deregisterIO(&main_notify_IO);
xclose(main_pipe[0]); xclose(main_pipe[0]);
xclose(main_pipe[1]); xclose(main_pipe[1]);
g_assert(select_mutex != NULL);
g_mutex_free(select_mutex);
select_mutex = NULL;
} }
static int wakeup_via_pipe(void) static int wakeup_via_pipe(void)
{ {
int ret = pthread_mutex_trylock(&select_mutex); gboolean ret = g_mutex_trylock(select_mutex);
if (ret == EBUSY) { if (ret == FALSE) {
ssize_t w = write(main_pipe[1], "", 1); ssize_t w = write(main_pipe[1], "", 1);
if (w < 0 && errno != EAGAIN && errno != EINTR) if (w < 0 && errno != EAGAIN && errno != EINTR)
FATAL("error writing to pipe: %s\n", FATAL("error writing to pipe: %s\n",
strerror(errno)); strerror(errno));
return 1; return 1;
} else { } else {
pthread_mutex_unlock(&select_mutex); g_mutex_unlock(select_mutex);
return 0; return 0;
} }
} }
...@@ -107,19 +112,19 @@ void wakeup_main_task(void) ...@@ -107,19 +112,19 @@ void wakeup_main_task(void)
void main_notify_lock(void) void main_notify_lock(void)
{ {
assert(pthread_equal(main_task, pthread_self())); assert(main_task == g_thread_self());
pthread_mutex_lock(&select_mutex); g_mutex_lock(select_mutex);
} }
void main_notify_unlock(void) void main_notify_unlock(void)
{ {
assert(pthread_equal(main_task, pthread_self())); assert(main_task == g_thread_self());
pthread_mutex_unlock(&select_mutex); g_mutex_unlock(select_mutex);
} }
void wait_main_task(void) void wait_main_task(void)
{ {
assert(pthread_equal(main_task, pthread_self())); assert(main_task == g_thread_self());
notify_wait(&main_notify); notify_wait(&main_notify);
} }
......
...@@ -21,9 +21,9 @@ ...@@ -21,9 +21,9 @@
#ifndef MPD_MAIN_NOTIFY_H #ifndef MPD_MAIN_NOTIFY_H
#define 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); void init_main_notify(void);
......
...@@ -47,7 +47,7 @@ static bool modified; ...@@ -47,7 +47,7 @@ static bool modified;
static char *update_paths[32]; static char *update_paths[32];
static size_t update_paths_nr; static size_t update_paths_nr;
static pthread_t update_thr; static GThread *update_thr;
static const unsigned update_task_id_max = 1 << 15; static const unsigned update_task_id_max = 1 << 15;
...@@ -595,15 +595,14 @@ static void * update_task(void *_path) ...@@ -595,15 +595,14 @@ static void * update_task(void *_path)
static void spawn_update_task(char *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; progress = UPDATE_PROGRESS_RUNNING;
modified = false; modified = false;
pthread_attr_init(&attr); if (!(update_thr = g_thread_create(update_task, path, TRUE, &e)))
if (pthread_create(&update_thr, &attr, update_task, path)) FATAL("Failed to spawn update task: %s\n", e->message);
FATAL("Failed to spawn update task: %s\n", strerror(errno));
if (++update_task_id > update_task_id_max) if (++update_task_id > update_task_id_max)
update_task_id = 1; update_task_id = 1;
DEBUG("spawned thread for update job id %i\n", update_task_id); DEBUG("spawned thread for update job id %i\n", update_task_id);
...@@ -612,7 +611,7 @@ static void spawn_update_task(char *path) ...@@ -612,7 +611,7 @@ static void spawn_update_task(char *path)
unsigned unsigned
directory_update_init(char *path) directory_update_init(char *path)
{ {
assert(pthread_equal(pthread_self(), main_task)); assert(g_thread_self() == main_task);
if (progress != UPDATE_PROGRESS_IDLE) { if (progress != UPDATE_PROGRESS_IDLE) {
unsigned next_task_id; unsigned next_task_id;
...@@ -635,7 +634,7 @@ directory_update_init(char *path) ...@@ -635,7 +634,7 @@ directory_update_init(char *path)
void reap_update_task(void) void reap_update_task(void)
{ {
assert(pthread_equal(pthread_self(), main_task)); assert(g_thread_self() == main_task);
if (progress == UPDATE_PROGRESS_IDLE) if (progress == UPDATE_PROGRESS_IDLE)
return; return;
...@@ -652,8 +651,7 @@ void reap_update_task(void) ...@@ -652,8 +651,7 @@ void reap_update_task(void)
if (progress != UPDATE_PROGRESS_DONE) if (progress != UPDATE_PROGRESS_DONE)
return; return;
if (pthread_join(update_thr, NULL)) g_thread_join(update_thr);
FATAL("error joining update thread: %s\n", strerror(errno));
if (modified) { if (modified) {
playlistVersionChange(); playlistVersionChange();
...@@ -679,8 +677,11 @@ void update_global_init(void) ...@@ -679,8 +677,11 @@ void update_global_init(void)
follow_outside_symlinks = follow_outside_symlinks =
config_get_bool(CONF_FOLLOW_OUTSIDE_SYMLINKS, config_get_bool(CONF_FOLLOW_OUTSIDE_SYMLINKS,
DEFAULT_FOLLOW_OUTSIDE_SYMLINKS); DEFAULT_FOLLOW_OUTSIDE_SYMLINKS);
cond_init(&delete_cond);
} }
void update_global_finish(void) void update_global_finish(void)
{ {
cond_destroy(&delete_cond);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment