diff options
author | Kozlov Dmitry <dima@server> | 2010-09-01 19:29:43 +0400 |
---|---|---|
committer | Kozlov Dmitry <dima@server> | 2010-09-01 19:29:43 +0400 |
commit | 2b63c6e6e368d6ea39584a70ddb81a88e0924c47 (patch) | |
tree | 790cc5d46e691b4b920170b7dfde40d3c6ec4be6 /accel-pptpd/triton/triton.c | |
parent | 5bac5a2edb7bc7639c853fd0f7109dcddb7c4cee (diff) | |
download | accel-ppp-2b63c6e6e368d6ea39584a70ddb81a88e0924c47.tar.gz accel-ppp-2b63c6e6e368d6ea39584a70ddb81a88e0924c47.zip |
rewriting triton library ...
Diffstat (limited to 'accel-pptpd/triton/triton.c')
-rw-r--r-- | accel-pptpd/triton/triton.c | 104 |
1 files changed, 63 insertions, 41 deletions
diff --git a/accel-pptpd/triton/triton.c b/accel-pptpd/triton/triton.c index 23a69c60..e4fa117c 100644 --- a/accel-pptpd/triton/triton.c +++ b/accel-pptpd/triton/triton.c @@ -1,4 +1,8 @@ #include <signal.h> +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> #include "triton_p.h" @@ -6,19 +10,19 @@ int thread_count=64; static spinlock_t threads_lock=SPINLOCK_INITIALIZER; static LIST_HEAD(threads); -static int threads_count; +static LIST_HEAD(sleep_threads); -static spinlock_t ctx_queue_lock=SPINLOCK_INITIALIZER; static LIST_HEAD(ctx_queue); static spinlock_t ctx_list_lock=SPINLOCK_INITIALIZER; static LIST_HEAD(ctx_list); struct triton_ctx_t *default_ctx; +static int terminate; void triton_thread_wakeup(struct triton_thread_t *thread) { - pthread_kill(&thread->thread,SIGUSR1); + pthread_kill(thread->thread,SIGUSR1); } static void* triton_thread(struct triton_thread_t *thread) @@ -36,19 +40,11 @@ static void* triton_thread(struct triton_thread_t *thread) { sigwait(&set,&sig); - if (thread->terminate) - return NULL; - cont: - if (thread->ctx->close) + if (thread->ctx->need_close) { - list_for_each_entry(h,&thread->ctx->handlers,entry) - if (h->close) - h->close(h); - list_for_each_entry(t,&thread->ctx->timers,entry) - if (t->close) - t->close(t); - thread->ctx->close=0; + thread->ctx->close(thread->ctx); + thread->ctx->need_close=0; } while (1) @@ -56,15 +52,15 @@ cont: spin_lock(&thread->ctx->lock); if (!list_empty(&thread->ctx->pending_timers)) { - t=list_entry(thread->ctx->pending_timers.next); + t=list_entry(thread->ctx->pending_timers.next,typeof(*t),entry2); list_del(&t->entry2); spin_unlock(&thread->ctx->lock); if (t->expire(t)) continue; } - if (!list_empty(&thread->ctx->pending_events)) + if (!list_empty(&thread->ctx->pending_handlers)) { - h=list_entry(thread->ctx->pending_events.next); + h=list_entry(thread->ctx->pending_handlers.next,typeof(*h),entry2); list_del(&h->entry2); h->pending=0; spin_unlock(&thread->ctx->lock); @@ -73,7 +69,7 @@ cont: if (h->read) if (h->read(h)) continue; - if (h->trig_epoll_events&(EPOLLOUT|EPOLLERR|EPOLLHUP)) + if (h->trig_epoll_events&(EPOLLOUT)) if (h->write) if (h->write(h)) continue; @@ -82,6 +78,8 @@ cont: } thread->ctx->thread=NULL; spin_unlock(&thread->ctx->lock); + if (thread->ctx->need_free) + thread->ctx->free(thread->ctx); thread->ctx=NULL; break; } @@ -89,18 +87,21 @@ cont: spin_lock(&threads_lock); if (!list_empty(&ctx_queue)) { - thread->ctx=list_entry(ctx_queue.next); + thread->ctx=list_entry(ctx_queue.next,typeof(*thread->ctx),entry2); list_del(&thread->ctx->entry2); spin_unlock(&threads_lock); spin_lock(&thread->ctx->lock); - ctx->thread=thread; - ctx->queue=0; + thread->ctx->thread=thread; + thread->ctx->queued=0; spin_unlock(&thread->ctx->lock); goto cont; }else { - list_add(&thread->entry,&threads); + if (!terminate) + list_add(&thread->entry2,&sleep_threads); spin_unlock(&threads_lock); + if (terminate) + return NULL; } } } @@ -110,21 +111,18 @@ struct triton_thread_t *create_thread() struct triton_thread_t *thread=malloc(sizeof(*thread)); memset(thread,0,sizeof(*thread)); - pthread_mutex_init(&thread->lock); - pthread_cond_init(&thread->cond); - pthread_create(&thread->thread,NULL,md_thread,thread); - ++threads_count; + pthread_create(&thread->thread,NULL,(void*(*)(void*))triton_thread,thread); return thread; } -void triton_queue_ctx(struct triton_ctx_t *ctx) +int triton_queue_ctx(struct triton_ctx_t *ctx) { if (ctx->thread || ctx->queued) return 0; spin_lock(&threads_lock); - if (list_empty(&threads)) + if (list_empty(&sleep_threads)) { list_add_tail(&ctx->entry2,&ctx_queue); spin_unlock(&threads_lock); @@ -132,8 +130,8 @@ void triton_queue_ctx(struct triton_ctx_t *ctx) return 0; } - ctx->thread=list_entry(threads.next); - list_del(&ctx->thread->entry); + ctx->thread=list_entry(sleep_threads.next,typeof(*ctx->thread),entry2); + list_del(&ctx->thread->entry2); spin_unlock(&threads_lock); return 1; @@ -141,7 +139,7 @@ void triton_queue_ctx(struct triton_ctx_t *ctx) void triton_register_ctx(struct triton_ctx_t *ctx) { - pthread_mutex_init(&ctx->lock); + spinlock_init(&ctx->lock); INIT_LIST_HEAD(&ctx->handlers); INIT_LIST_HEAD(&ctx->timers); INIT_LIST_HEAD(&ctx->pending_handlers); @@ -154,12 +152,13 @@ void triton_register_ctx(struct triton_ctx_t *ctx) void triton_unregister_ctx(struct triton_ctx_t *ctx) { + ctx->need_free=1; spin_lock(&ctx_list_lock); - list_add_tail(&ctx->entry,&ctx_list); + list_del(&ctx->entry); spin_unlock(&ctx_list_lock); } -int triton_init() +int triton_init(const char *conf_file) { default_ctx=malloc(sizeof(*default_ctx)); if (!default_ctx) @@ -169,10 +168,19 @@ int triton_init() } triton_register_ctx(default_ctx); + if (conf_load(conf_file)) + return -1; + + if (log_init()) + return -1; + if (md_init()) return -1; + if (timer_init()) return -1; + + return 0; } void triton_run() @@ -180,11 +188,13 @@ void triton_run() struct triton_thread_t *t; int i; - for(i=0;i<max_threads;i++) + for(i=0;i<thread_count;i++) { t=create_thread(); list_add_tail(&t->entry,&threads); + list_add_tail(&t->entry2,&sleep_threads); } + md_run(); timer_run(); } @@ -192,17 +202,29 @@ void triton_run() void triton_terminate() { struct triton_ctx_t *ctx; - pthread_mutex_lock(&ctx_list_lock); + struct triton_thread_t *t; + + md_terminate(); + timer_terminate(); + + spin_lock(&ctx_list_lock); list_for_each_entry(ctx,&ctx_list,entry) { - pthread_mutex_lock(&ctx->lock); - ctx->close=1; + spin_lock(&ctx->lock); + ctx->need_close=1; triton_queue_ctx(ctx); - pthread_mutex_unlock(&ctx->lock); + spin_unlock(&ctx->lock); } - pthread_mutex_unlock(&ctx_list_lock); + spin_unlock(&ctx_list_lock); - timer_terminate(); - md_terminate(); + spin_lock(&threads_lock); + terminate=1; + spin_unlock(&threads_lock); + + list_for_each_entry(t,&threads,entry) + triton_thread_wakeup(t); + + list_for_each_entry(t,&threads,entry) + pthread_join(t->thread,NULL); } |