From a4625fbad9d1fa5b52f96c5686431726e1891a53 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Mon, 6 Aug 2012 14:20:46 +0000 Subject: [PATCH] enough with the glib threading nonsense, it's only causing problems. revert to good old pthread stuff --- daemon/aux.c | 30 +++++++++++++++++++--- daemon/aux.h | 67 +++++++++++++++---------------------------------- daemon/poller.c | 2 +- 3 files changed, 47 insertions(+), 52 deletions(-) diff --git a/daemon/aux.c b/daemon/aux.c index c3e57e5b6..552eacf7f 100644 --- a/daemon/aux.c +++ b/daemon/aux.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "aux.h" @@ -119,19 +120,24 @@ void g_queue_clear(GQueue *q) { void thread_join_me() { + pthread_t *me; + + me = g_slice_alloc(sizeof(*me)); + *me = pthread_self(); mutex_lock(&threads_to_join_lock); - threads_to_join = g_slist_prepend(threads_to_join, g_thread_self()); + threads_to_join = g_slist_prepend(threads_to_join, me); mutex_unlock(&threads_to_join_lock); } void threads_join_all() { - GThread *t; + pthread_t *t; mutex_lock(&threads_to_join_lock); while (threads_to_join) { t = threads_to_join->data; - g_thread_join(t); + pthread_join(*t, NULL); threads_to_join = g_slist_delete_link(threads_to_join, threads_to_join); + g_slice_free1(sizeof(*t), t); } mutex_unlock(&threads_to_join_lock); } @@ -145,6 +151,22 @@ static gpointer thread_detach_func(gpointer d) { return NULL; } +int thread_create(void *(*func)(void *), void *arg, int joinable, pthread_t *handle) { + pthread_attr_t att; + pthread_t thr; + int ret; + + if (pthread_attr_init(&att)) + abort(); + if (pthread_attr_setdetachstate(&att, joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED)) + abort(); + ret = pthread_create(&thr, &att, func, arg); + pthread_attr_destroy(&att); + if (!ret && handle) + *handle = thr; + return ret; +} + void thread_create_detach(void (*f)(void *), void *d) { struct detach_thread *dt; @@ -152,6 +174,6 @@ void thread_create_detach(void (*f)(void *), void *d) { dt->func = f; dt->data = d; - if (!g_thread_create(thread_detach_func, dt, TRUE, NULL)) + if (thread_create(thread_detach_func, dt, 1, NULL)) abort(); } diff --git a/daemon/aux.h b/daemon/aux.h index a0ab09c46..38d0c1b3d 100644 --- a/daemon/aux.h +++ b/daemon/aux.h @@ -12,6 +12,7 @@ #include #include #include +#include @@ -146,60 +147,32 @@ static inline int smart_pton(int af, char *src, void *dst) { -#if !GLIB_CHECK_VERSION(2,32,0) +typedef pthread_mutex_t mutex_t; +typedef pthread_rwlock_t rwlock_t; +typedef pthread_cond_t cond_t; -typedef GStaticMutex mutex_t; -typedef GStaticRWLock rwlock_t; -typedef GCond *cond_t; +#define mutex_init(m) pthread_mutex_init(m, NULL) +#define mutex_destroy(m) pthread_mutex_destroy(m) +#define mutex_lock(m) pthread_mutex_lock(m) +#define mutex_trylock(m) pthread_mutex_trylock(m) +#define mutex_unlock(m) pthread_mutex_unlock(m) +#define MUTEX_STATIC_INIT PTHREAD_MUTEX_INITIALIZER -#define mutex_init(m) g_static_mutex_init(m) -#define mutex_destroy(m) g_static_mutex_free(m) -#define mutex_lock(m) g_static_mutex_lock(m) -#define mutex_trylock(m) g_static_mutex_trylock(m) -#define mutex_unlock(m) g_static_mutex_unlock(m) -#define MUTEX_STATIC_INIT G_STATIC_MUTEX_INIT - -#define rwlock_init(l) g_static_rw_lock_init(l) -#define rwlock_lock_r(l) g_static_rw_lock_reader_lock(l) -#define rwlock_unlock_r(l) g_static_rw_lock_reader_unlock(l) -#define rwlock_lock_w(l) g_static_rw_lock_writer_lock(l) -#define rwlock_unlock_w(l) g_static_rw_lock_writer_unlock(l) - -#define cond_init(c) *(c) = g_cond_new() -#define cond_wait(c,m) g_cond_wait(*(c),m) -#define cond_signal(c) g_cond_signal(*(c)) -#define cond_broadcast(c) g_cond_broadcast(*(c)) - -#else - -typedef GMutex mutex_t; -typedef GRWLock rwlock_t; -typedef GCond cond_t; - -#define mutex_init(m) g_mutex_init(m) -#define mutex_destroy(m) g_mutex_clear(m) -#define mutex_lock(m) g_mutex_lock(m) -#define mutex_trylock(m) g_mutex_trylock(m) -#define mutex_unlock(m) g_mutex_unlock(m) -#define MUTEX_STATIC_INIT {0} - -#define rwlock_init(l) g_rw_lock_init(l) -#define rwlock_lock_r(l) g_rw_lock_reader_lock(l) -#define rwlock_unlock_r(l) g_rw_lock_reader_unlock(l) -#define rwlock_lock_w(l) g_rw_lock_writer_lock(l) -#define rwlock_unlock_w(l) g_rw_lock_writer_unlock(l) - -#define cond_init(c) g_cond_init(c) -#define cond_wait(c,m) g_cond_wait(c,m) -#define cond_signal(c) g_cond_signal(c) -#define cond_broadcast(c) g_cond_broadcast(c) - -#endif +#define rwlock_init(l) pthread_rwlock_init(l, NULL) +#define rwlock_lock_r(l) pthread_rwlock_rdlock(l) +#define rwlock_unlock_r(l) pthread_rwlock_unlock(l) +#define rwlock_lock_w(l) pthread_rwlock_wrlock(l) +#define rwlock_unlock_w(l) pthread_rwlock_unlock(l) +#define cond_init(c) pthread_cond_init(c, NULL) +#define cond_wait(c,m) pthread_cond_wait(c,m) +#define cond_signal(c) pthread_cond_signal(c) +#define cond_broadcast(c) pthread_cond_broadcast(c) void thread_join_me(); void threads_join_all(); +int thread_create(void *(*)(void *), void *, int, pthread_t *); void thread_create_detach(void (*)(void *), void *); diff --git a/daemon/poller.c b/daemon/poller.c index d0cca9472..20276ffdd 100644 --- a/daemon/poller.c +++ b/daemon/poller.c @@ -459,7 +459,7 @@ static int poller_timer_link(struct poller *p, GSList **lp, void (*f)(void *), s mutex_lock(&p->timers_add_del_lock); *lp = g_slist_prepend(*lp, i); - if (mutex_trylock(&p->timers_lock)) { + if (!mutex_trylock(&p->timers_lock)) { poller_timers_mod(p); mutex_unlock(&p->timers_lock); }