diff options
author | Dmitry Kozlov <xeb@mail.ru> | 2011-01-03 17:39:49 +0300 |
---|---|---|
committer | Dmitry Kozlov <xeb@mail.ru> | 2011-01-03 17:39:49 +0300 |
commit | 709dd1595b4877471cb18397be28352730120067 (patch) | |
tree | c677f845297bdbc5a289540f2b8874a8196532f8 /accel-pptpd/triton | |
parent | 04a9d5a8284a656ec64c4c82a2910c807e47e260 (diff) | |
download | accel-ppp-709dd1595b4877471cb18397be28352730120067.tar.gz accel-ppp-709dd1595b4877471cb18397be28352730120067.zip |
core: fix thread start/terminate synchronization
Diffstat (limited to 'accel-pptpd/triton')
-rw-r--r-- | accel-pptpd/triton/triton.c | 25 | ||||
-rw-r--r-- | accel-pptpd/triton/triton_p.h | 5 |
2 files changed, 18 insertions, 12 deletions
diff --git a/accel-pptpd/triton/triton.c b/accel-pptpd/triton/triton.c index 08d33a8..00dfcf6 100644 --- a/accel-pptpd/triton/triton.c +++ b/accel-pptpd/triton/triton.c @@ -85,6 +85,9 @@ static void* triton_thread(struct _triton_thread_t *thread) sigaddset(&set, SIGUSR1); sigaddset(&set, SIGQUIT); + pthread_mutex_lock(&thread->sleep_lock); + pthread_mutex_unlock(&thread->sleep_lock); + while (1) { spin_lock(&threads_lock); if (!list_empty(&ctx_queue) && !need_config_reload) { @@ -105,6 +108,7 @@ static void* triton_thread(struct _triton_thread_t *thread) spin_unlock(&threads_lock); pthread_detach(pthread_self()); log_debug2("thread: %p: exit\n", thread); + _free(thread); return NULL; } log_debug2("thread: %p: sleeping\n", thread); @@ -222,11 +226,14 @@ static void ctx_thread(struct _triton_context_t *ctx) struct _triton_thread_t *create_thread() { - struct _triton_thread_t *thread = malloc(sizeof(*thread)); + struct _triton_thread_t *thread = _malloc(sizeof(*thread)); if (!thread) return NULL; memset(thread, 0, sizeof(*thread)); + pthread_mutex_init(&thread->sleep_lock, NULL); + pthread_cond_init(&thread->sleep_cond, NULL); + pthread_mutex_lock(&thread->sleep_lock); if (pthread_create(&thread->thread, NULL, (void*(*)(void*))triton_thread, thread)) { triton_log_error("pthread_create: %s", strerror(errno)); return NULL; @@ -284,8 +291,6 @@ int __export triton_context_register(struct triton_context_t *ud, void *bf_arg) INIT_LIST_HEAD(&ctx->pending_handlers); INIT_LIST_HEAD(&ctx->pending_timers); INIT_LIST_HEAD(&ctx->pending_calls); - pthread_mutex_init(&ctx->sleep_lock, NULL); - pthread_cond_init(&ctx->sleep_cond, NULL); ud->tpd = ctx; @@ -366,7 +371,7 @@ void __export triton_context_schedule() log_debug2("ctx %p: enter schedule\n", ctx); __sync_add_and_fetch(&triton_stat.context_sleeping, 1); __sync_sub_and_fetch(&triton_stat.thread_active, 1); - pthread_mutex_lock(&ctx->sleep_lock); + pthread_mutex_lock(&ctx->thread->sleep_lock); while (1) { if (ctx->wakeup) { ctx->wakeup = 0; @@ -377,11 +382,12 @@ void __export triton_context_schedule() spin_lock(&threads_lock); list_add_tail(&t->entry, &threads); spin_unlock(&threads_lock); + pthread_mutex_unlock(&t->sleep_lock); } - pthread_cond_wait(&ctx->sleep_cond, &ctx->sleep_lock); + pthread_cond_wait(&ctx->thread->sleep_cond, &ctx->thread->sleep_lock); } } - pthread_mutex_unlock(&ctx->sleep_lock); + pthread_mutex_unlock(&ctx->thread->sleep_lock); __sync_sub_and_fetch(&triton_stat.context_sleeping, 1); __sync_add_and_fetch(&triton_stat.thread_active, 1); log_debug2("ctx %p: exit schedule\n", ctx); @@ -419,10 +425,10 @@ void __export triton_context_wakeup(struct triton_context_t *ud) return; } - pthread_mutex_lock(&ctx->sleep_lock); + pthread_mutex_lock(&ctx->thread->sleep_lock); ctx->wakeup = 1; - pthread_cond_signal(&ctx->sleep_cond); - pthread_mutex_unlock(&ctx->sleep_lock); + pthread_cond_signal(&ctx->thread->sleep_cond); + pthread_mutex_unlock(&ctx->thread->sleep_lock); } int __export triton_context_call(struct triton_context_t *ud, void (*func)(void *), void *arg) @@ -564,6 +570,7 @@ void __export triton_run() _exit(-1); list_add_tail(&t->entry, &threads); + pthread_mutex_unlock(&t->sleep_lock); } time(&triton_stat.start_time); diff --git a/accel-pptpd/triton/triton_p.h b/accel-pptpd/triton/triton_p.h index 03067a1..235eb70 100644 --- a/accel-pptpd/triton/triton_p.h +++ b/accel-pptpd/triton/triton_p.h @@ -16,6 +16,8 @@ struct _triton_thread_t pthread_t thread; int terminate; struct _triton_context_t *ctx; + pthread_mutex_t sleep_lock; + pthread_cond_t sleep_cond; }; struct _triton_context_t @@ -40,9 +42,6 @@ struct _triton_context_t int pending; int priority; - pthread_mutex_t sleep_lock; - pthread_cond_t sleep_cond; - struct triton_context_t *ud; void *bf_arg; }; |