From 55ebaec066819d068bf13a42998ebc3f31377990 Mon Sep 17 00:00:00 2001 From: Kozlov Dmitry Date: Fri, 27 Aug 2010 17:15:02 +0400 Subject: rewriting triton library ... --- accel-pptpd/triton/md.c | 12 +-- accel-pptpd/triton/spinlock.h | 17 ++++ accel-pptpd/triton/triton.c | 183 +++++++++++++++++------------------------- accel-pptpd/triton/triton.h | 8 +- 4 files changed, 100 insertions(+), 120 deletions(-) create mode 100644 accel-pptpd/triton/spinlock.h (limited to 'accel-pptpd/triton') diff --git a/accel-pptpd/triton/md.c b/accel-pptpd/triton/md.c index 4570347f..05af432a 100644 --- a/accel-pptpd/triton/md.c +++ b/accel-pptpd/triton/md.c @@ -16,8 +16,6 @@ static void* md_thread(void *arg) int md_init() { - signal(SIGPIPE,SIG_IGN); - epoll_fd=epoll_create(0); if (epoll_fd<0) { @@ -55,7 +53,7 @@ void md_terminate() static void* md_thread(void *arg) { - int max_fd=0,t; + int max_fd=0,t,r; struct triton_md_handler_t *h; struct timeval tv1,tv2,twait0; struct list_head *p1,*p2; @@ -74,12 +72,14 @@ static void* md_thread(void *arg) for(i=0; ictx->lock); + spin_lock(&h->ctx->lock); h->trig_epoll_events=epoll_events[i].events; list_add_tail(&h->entry2,&h->ctx->pending_handlers); h->pending=1; - triton_queue_ctx(h->ctx); - pthread_mutex_unlock(&h->ctx->lock); + r=triton_queue_ctx(h->ctx); + spin_unlock(&h->ctx->lock); + if (r) + triton_thread_wakeup(ctx->thread); } } diff --git a/accel-pptpd/triton/spinlock.h b/accel-pptpd/triton/spinlock.h new file mode 100644 index 00000000..a75e29cb --- /dev/null +++ b/accel-pptpd/triton/spinlock.h @@ -0,0 +1,17 @@ +#ifndef __TRITON_SPINLOCK_H +#define __TRITON_SPINLOCK_H + +#ifdef USE_SPINLOCK +typedef spinlock_t unsigned char; +#define spin_lock(l) {while(__sync_lock_test_and_set(l,1);} +#define spin_unlock(l) __sync_lock_release(l) +#define SPINLOCK_INITIALIZER 0 +#else +typedef spinlock_t pthread_mutex_t; +#define spin_lock(l) pthread_mutex_lock(l) +#define spin_unlock(l) pthread_mutex_unlock(l) +#define SPINLOCK_INITIALIZER PTHREAD_MUTEX_INITIALIZER +#endif + +#endif + diff --git a/accel-pptpd/triton/triton.c b/accel-pptpd/triton/triton.c index 1fe0a4fd..25759b58 100644 --- a/accel-pptpd/triton/triton.c +++ b/accel-pptpd/triton/triton.c @@ -1,55 +1,44 @@ +#include + #include "triton_p.h" -int max_threads=128; -int thread_idletime=60; //seconds +int thread_count=64; -static pthread_mutex_t threads_lock=PTHREAD_MUTEX_INITIALIZER; +static spinlock_t threads_lock=SPINLOCK_INITIALIZER; static LIST_HEAD(threads); static int threads_count; -static pthread_mutex_t ctx_queue_lock=PTHREAD_MUTEX_INITIALIZER; +static spinlock_t ctx_queue_lock=SPINLOCK_INITIALIZER; static LIST_HEAD(ctx_queue); -static pthread_mutex_t ctx_list_lock=PTHREAD_MUTEX_INITIALIZER; +static spinlock_t ctx_list_lock=SPINLOCK_INITIALIZER; static LIST_HEAD(ctx_list); struct triton_ctx_t *default_ctx; void triton_thread_wakeup(struct triton_thread_t *thread) { - pthread_mutex_lock(&h->ctx->thread->lock); - pthread_cont_signal(&h->ctx->thread->cond); - pthread_mutex_unlock(&h->ctx->thread->lock); + pthread_kill(&thread->thread,SIGUSR1); } static void* triton_thread(struct triton_thread_t *thread) { struct triton_md_handler_t *h; struct triton_timer_t *t; - struct timespec abstime; + sigset_t set; + int sig; + + sigemptyset(&set); + sigaddset(&set,SIGUSR1); + sigaddset(&set,SIGQUIT); while(1) { - abstime.tv_time=time(NULL)+thread_idletime; - abstime.tv_nsec=0; - pthread_mutex_lock(&thread->lock); - if (pthread_cond_timedwait(&thread->cond,&thread->lock,&abstime) && !thread->ctx) - thread->destroing=1; - pthread_mutex_unlock(&thread->lock); + sigwait(&set,&sig); if (thread->terminate) return NULL; - if (thread->destroing) - { - pthread_mutex_lock(&threads_lock); - list_del(&thread->entry); - --threads_count; - pthread_mutex_unlock(&threads_lock); - free(thread); - return NULL; - } - cont: if (thread->ctx->close) { @@ -61,74 +50,55 @@ cont: while (1) { - pthread_mutex_lock(&thread->ctx->lock); - if (list_empty(&thread->ctx->pending_timers)) + spin_lock(&thread->ctx->lock); + if (!list_empty(&thread->ctx->pending_timers)) { - pthread_mutex_unlock(&thread->ctx->lock); - break; + t=list_entry(thread->ctx->pending_timers.next); + list_del(&t->entry2); + spin_unlock(&thread->ctx->lock); + if (t->expire(t)) + continue; } - t=list_entry(thread->ctx->pending_timers.next); - list_del(&t->entry2); - pthread_mutex_unlock(&thread->ctx->lock); - if (t->expire(t)) - continue; - } - - while (1) - { - pthread_mutex_lock(&thread->ctx->lock); - if (list_empty(&thread->ctx->pending_events)) + if (!list_empty(&thread->ctx->pending_events)) { - pthread_mutex_unlock(&thread->ctx->lock); - break; + h=list_entry(thread->ctx->pending_events.next); + list_del(&h->entry2); + h->pending=0; + spin_unlock(&thread->ctx->lock); + + if (h->trig_epoll_events&(EPOLLIN|EPOLLERR|EPOLLHUP)) + if (h->read) + if (h->read(h)) + continue; + if (h->trig_epoll_events&(EPOLLOUT|EPOLLERR|EPOLLHUP)) + if (h->write) + if (h->write(h)) + continue; + h->trig_epoll_events=0; + continue; } - - h=list_entry(thread->ctx->pending_events.next); - list_del(&h->entry2); - h->pending=0; - pthread_mutex_unlock(&thread->ctx->lock); - - if (h->trig_epoll_events&(EPOLLIN|EPOLLERR|EPOLLHUP)) - if (h->read) - if (h->read(h)) - continue; - if (h->trig_epoll_events&(EPOLLOUT|EPOLLERR|EPOLLHUP)) - if (h->write) - if (h->write(h)) - continue; - h->trig_epoll_events=0; - /*if (h->twait==0) - if (h->timeout) - if (h->timeout(h)) - continue; - if (h->twait>0) - triton_md_set_timeout(h,h->twait);*/ + thread->ctx->thread=NULL; + spin_unlock(&thread->ctx->lock); + thread->ctx=NULL; + break; } - pthread_mutex_lock(&thread->ctx->lock); - if (!list_empty(&thread->ctx->pending_events) || !list_empty(&thread->ctx->pending_timers)) - { - pthread_mutex_unlock(&thread->ctx->lock); - goto cont; - } - thread->ctx->thread=NULL; - thread->ctx=NULL; - pthread_mutex_unlock(&thread->ctx->lock); - - pthread_mutex_lock(&threads_lock); + spin_lock(&threads_lock); if (!list_empty(&ctx_queue)) { thread->ctx=list_entry(ctx_queue.next); - pthread_mutex_lock(&thread->ctx->lock); - list_del(&ctx->entry2); + list_del(&thread->ctx->entry2); + spin_unlock(&threads_lock); + spin_lock(&thread->ctx->lock); ctx->thread=thread; ctx->queue=0; - pthread_mutex_unlock(&thread->ctx->lock); - pthread_mutex_unlock(&threads_lock); + spin_unlock(&thread->ctx->lock); goto cont; + }else + { + list_add(&thread->entry,&threads); + spin_unlock(&threads_lock); } - list_add(&thread->entry,&threads); - pthread_mutex_unlock(&threads_lock); } } @@ -148,35 +118,22 @@ struct triton_thread_t *create_thread() void triton_queue_ctx(struct triton_ctx_t *ctx) { if (ctx->thread || ctx->queued) - return; + return 0; - pthread_mutex_lock(&threads_lock); + spin_lock(&threads_lock); if (list_empty(&threads)) { - if (threads_count>=max_threads) - { - list_add_tail(&ctx->entry2,&ctx_queue); - ctx->queued=1; - pthread_mutex_unlock(&threads_lock); - return; - } - ctx->thread=create_thread(); - }else - { - ctx->thread=list_entry(threads.next); - pthread_mutex_lock(&ctx->thread->lock); - if (ctx->thread->destroing) - { - pthread_mutex_unlock(&ctx->thread->lock); - ctx->thread=create_thread(); - }else - { - list_del(&ctx->thread->entry); - pthread_mutex_unlock(&ctx->thread->lock); - } + list_add_tail(&ctx->entry2,&ctx_queue); + spin_unlock(&threads_lock); + ctx->queued=1; + return 0; } - pthread_mutex_unlock(&threads_lock); - triton_thread_wakeup(ctx->thread); + + ctx->thread=list_entry(threads.next); + list_del(&ctx->thread->entry); + spin_unlock(&threads_lock); + + return 1; } void triton_register_ctx(struct triton_ctx_t *ctx) @@ -187,16 +144,16 @@ void triton_register_ctx(struct triton_ctx_t *ctx) INIT_LIST_HEAD(&ctx->pending_handlers); INIT_LIST_HEAD(&ctx->pending_timers); - pthread_mutex_lock(&ctx_list_lock); + spin_lock(&ctx_list_lock); list_add_tail(&ctx->entry,&ctx_list); - pthread_mutex_unlock(&ctx_list_lock); + spin_unlock(&ctx_list_lock); } void triton_unregister_ctx(struct triton_ctx_t *ctx) { - pthread_mutex_lock(&ctx_list_lock); + spin_lock(&ctx_list_lock); list_add_tail(&ctx->entry,&ctx_list); - pthread_mutex_unlock(&ctx_list_lock); + spin_unlock(&ctx_list_lock); } void triton_init() @@ -207,6 +164,14 @@ void triton_init() void triton_run() { + struct triton_thread_t *t; + int i; + + for(i=0;ientry,&threads); + } md_run(); timer_run(); } diff --git a/accel-pptpd/triton/triton.h b/accel-pptpd/triton/triton.h index 240f31f6..0e9b73bf 100644 --- a/accel-pptpd/triton/triton.h +++ b/accel-pptpd/triton/triton.h @@ -5,15 +5,13 @@ #include #include +#include + struct triton_thread_t { struct list_head entry; - pthread_mutex_t lock; - pthread_cond_t cond; pthread_t thread; int terminate:1; - int destroing:1; - struct timeval tv; struct triton_ctx_t *ctx; }; @@ -21,7 +19,7 @@ struct triton_ctx_t { struct list_head entry; struct list_head entry2; - pthread_mutex_t lock; + spinlock_t lock; struct list_head handlers; struct list_head timers; -- cgit v1.2.3