Browse Source

detached threads considered harmful, so simulate them by keeping a threads-to-join list

git.mgm/mediaproxy-ng/2.1
Richard Fuchs 14 years ago
parent
commit
31a5ff9c29
3 changed files with 64 additions and 5 deletions
  1. +53
    -0
      daemon/aux.c
  2. +8
    -0
      daemon/aux.h
  3. +3
    -5
      daemon/main.c

+ 53
- 0
daemon/aux.c View File

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <glib.h> #include <glib.h>
#include <pcre.h> #include <pcre.h>
#include <stdlib.h>
#include "aux.h" #include "aux.h"
@ -15,6 +16,18 @@
struct detach_thread {
GThreadFunc func;
gpointer data;
};
mutex_t threads_to_join_lock = MUTEX_STATIC_INIT;
static GSList *threads_to_join;
GList *g_list_link(GList *list, GList *el) { GList *g_list_link(GList *list, GList *el) {
el->prev = NULL; el->prev = NULL;
el->next = list; el->next = list;
@ -102,3 +115,43 @@ void g_queue_clear(GQueue *q) {
} }
#endif #endif
void thread_join_me() {
mutex_lock(&threads_to_join_lock);
threads_to_join = g_slist_prepend(threads_to_join, g_thread_self());
mutex_unlock(&threads_to_join_lock);
}
void threads_join_all() {
GThread *t;
mutex_lock(&threads_to_join_lock);
while (threads_to_join) {
t = threads_to_join->data;
g_thread_join(t);
threads_to_join = g_slist_delete_link(threads_to_join, threads_to_join);
}
mutex_unlock(&threads_to_join_lock);
}
static gpointer thread_detach_func(gpointer d) {
struct detach_thread *dt = d;
dt->func(dt->data);
g_slice_free1(sizeof(*dt), dt);
thread_join_me();
return NULL;
}
void thread_create_detach(GThreadFunc f, gpointer d) {
struct detach_thread *dt;
dt = g_slice_alloc(sizeof(*dt));
dt->func = f;
dt->data = d;
if (!g_thread_create(thread_detach_func, dt, TRUE, NULL))
abort();
}

+ 8
- 0
daemon/aux.h View File

@ -156,6 +156,7 @@ typedef GCond *cond_t;
#define mutex_lock(m) g_static_mutex_lock(m) #define mutex_lock(m) g_static_mutex_lock(m)
#define mutex_trylock(m) g_static_mutex_trylock(m) #define mutex_trylock(m) g_static_mutex_trylock(m)
#define mutex_unlock(m) g_static_mutex_unlock(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_init(l) g_static_rw_lock_init(l)
#define rwlock_lock_r(l) g_static_rw_lock_reader_lock(l) #define rwlock_lock_r(l) g_static_rw_lock_reader_lock(l)
@ -178,6 +179,7 @@ typedef GCond cond_t;
#define mutex_lock(m) g_mutex_lock(m) #define mutex_lock(m) g_mutex_lock(m)
#define mutex_trylock(m) g_mutex_trylock(m) #define mutex_trylock(m) g_mutex_trylock(m)
#define mutex_unlock(m) g_mutex_unlock(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_init(l) g_rw_lock_init(l)
#define rwlock_lock_r(l) g_rw_lock_reader_lock(l) #define rwlock_lock_r(l) g_rw_lock_reader_lock(l)
@ -194,4 +196,10 @@ typedef GCond cond_t;
void thread_join_me();
void threads_join_all();
void thread_create_detach(GThreadFunc, gpointer);
#endif #endif

+ 3
- 5
daemon/main.c View File

@ -312,7 +312,6 @@ int main(int argc, char **argv) {
int ret; int ret;
void *dlh; void *dlh;
const char **strp; const char **strp;
GThread *signal_handler_thread;
options(&argc, &argv); options(&argc, &argv);
g_thread_init(NULL); g_thread_init(NULL);
@ -405,17 +404,16 @@ int main(int argc, char **argv) {
die("Refusing to continue without working Redis database\n"); die("Refusing to continue without working Redis database\n");
} }
signal_handler_thread = g_thread_create(sighandler, NULL, TRUE, NULL);
if (!signal_handler_thread)
die("Failed to create thread\n");
thread_create_detach(sighandler, NULL);
while (!global_shutdown) { while (!global_shutdown) {
ret = poller_poll(p, 100); ret = poller_poll(p, 100);
if (ret == -1) if (ret == -1)
break; break;
threads_join_all();
} }
g_thread_join(signal_handler_thread);
threads_join_all();
mylog(LOG_INFO, "Version %s shutting down", MEDIAPROXY_VERSION); mylog(LOG_INFO, "Version %s shutting down", MEDIAPROXY_VERSION);


Loading…
Cancel
Save