diff options
author | Kozlov Dmitry <dima@server> | 2010-08-31 18:21:14 +0400 |
---|---|---|
committer | Kozlov Dmitry <dima@server> | 2010-08-31 18:21:14 +0400 |
commit | 5bac5a2edb7bc7639c853fd0f7109dcddb7c4cee (patch) | |
tree | 14d1d6178e14e096dd7341eb33c3e73b02f0fd0c /accel-pptpd/triton | |
parent | 55ebaec066819d068bf13a42998ebc3f31377990 (diff) | |
download | accel-ppp-5bac5a2edb7bc7639c853fd0f7109dcddb7c4cee.tar.gz accel-ppp-5bac5a2edb7bc7639c853fd0f7109dcddb7c4cee.zip |
rewriting triton library...
Diffstat (limited to 'accel-pptpd/triton')
-rw-r--r-- | accel-pptpd/triton/md.c | 20 | ||||
-rw-r--r-- | accel-pptpd/triton/timer.c | 158 | ||||
-rw-r--r-- | accel-pptpd/triton/triton.c | 19 |
3 files changed, 125 insertions, 72 deletions
diff --git a/accel-pptpd/triton/md.c b/accel-pptpd/triton/md.c index 05af432..3080c5a 100644 --- a/accel-pptpd/triton/md.c +++ b/accel-pptpd/triton/md.c @@ -6,7 +6,7 @@ #include "triton_p.h" -int max_events=128; +extern int max_events; static int epoll_fd; static struct epoll_event *epoll_events; @@ -16,29 +16,20 @@ static void* md_thread(void *arg) int md_init() { - epoll_fd=epoll_create(0); + epoll_fd=epoll_create(1); if (epoll_fd<0) { perror("epoll_create"); return -1; } - epoll_events=malloc(MAX_EVENTS * sizeof(struct epoll_event)); + epoll_events=malloc(max_events * sizeof(struct epoll_event)); if (!epoll_events) { fprintf(stderr,"cann't allocate memory\n"); return -1; } - default_ctx=malloc(sizeof(*default_ctx)); - if (!default_ctx) - { - fprintf(stderr,"cann't allocate memory\n"); - return -1; - } - - triton_register_ctx(default_ctx); - return 0; } void md_run() @@ -53,11 +44,8 @@ void md_terminate() static void* md_thread(void *arg) { - int max_fd=0,t,r; + int i,n,r; struct triton_md_handler_t *h; - struct timeval tv1,tv2,twait0; - struct list_head *p1,*p2; - int timeout,i,n; n=epoll_wait(epoll_fd,epoll_events,MAX_EVENTS,-1); if (n<0) diff --git a/accel-pptpd/triton/timer.c b/accel-pptpd/triton/timer.c index 662ee7a..57270f6 100644 --- a/accel-pptpd/triton/timer.c +++ b/accel-pptpd/triton/timer.c @@ -1,23 +1,39 @@ #include <stdio.h> #include <stdlib.h> #include <sys/time.h> +#include <sys/epoll.h> +#include <sys/timerfd.h> #include <string.h> #include "triton_p.h" static pthread_thread_t timer_thr; +static void *timer_thread(void *arg); -static pthread_mutex_t timers_lock=PTHREAD_MUTEX_INITIALIZER; +static spinlock_t timers_lock=SPINLOCK_INITIALIZER; static LIST_HEAD(timers); -static timespec expire_ts; -static pthread_cond_t cond=PTHREAD_COND_INITIALIZER; +extern int max_events; +static epoll_fd; +static struct epoll_event *epoll_events; static void tv_add(struct timeval *tv,int msec); - void timer_init(void) { + epoll_fd=epoll_create(1); + if (epoll_fd<0) + { + perror("epoll_create"); + return -1; + } + + epoll_events=malloc(max_events * sizeof(struct epoll_event)); + if (!epoll_events) + { + fprintf(stderr,"cann't allocate memory\n"); + return -1; + } } void timer_run(void) @@ -33,66 +49,102 @@ void timer_terminate(void) void *timer_thread(void *arg) { + int i,n,r; struct triton_timer_t *t; - struct timeval tv; - - pthread_mutex_lock(&timers_lock); - while(1) + + n=epoll_wait(epoll_fd,epoll_events,MAX_EVENTS,-1); + if (n<0) { - if (expire_ts.tv_sec) - pthread_cond_timedwait(&cond,&timers_lock,&expire_ts); - else - pthread_cond_wait(&cond,&timers_lock); - - gettimeofday(&tv,NULL); - while(1) - { - if (list_empty(&timers)) - { - expire_ts.tv_sec=0; - break; - } - t=list_entry(timers.next,typeof(*t),entry); - if (t->expire_tv.tv_sec>tv.tv_sec || (t->expire_tv.tv_sec==tv.tv_sec && t->expire_tv.tv_usec>=tv.tv_usec)) - { - expire_ts.tv_sec=t->expire_tv.tv_sec; - expire_ts.tv_nsec=t->expire_tv.tv_usec*1000; - break; - } - list_del(&t->entry3); - pthread_mutex_lock(&t->ctx->lock); - t->pending=1; - list_add_tail(&t->entry2,&t->ctx->pending_timers); - triton_queue_ctx(&t->ctx); - pthread_mutex_unlock(&t->ctx->lock); - } + if (errno!=EINTR) + perror("epoll_wait"); + continue; + } + if (n==0) + return; + + for(i=0; i<n; i++) + { + t=(struct triton_md_handler_t*)epoll_events[i].data.ptr; + spin_lock(&t->ctx->lock); + list_add_tail(&t->entry2,&t->ctx->pending_timers); + t->pending=1; + r=triton_queue_ctx(t->ctx); + spin_unlock(&t->ctx->lock); + if (r) + triton_thread_wakeup(ctx->thread); } } -void triton_timer_add(struct triton_timer_t *t) +int triton_timer_add(struct triton_timer_t *t) { - struct triton_timer_t *t1; - pthread_mutex_lock(&timers_lock); - list_for_each_entry(t1,&timers,entry3) + t->epoll_event.data.ptr=t; + t->epoll_event.events=EPOLLIN|EPOLLET; + if (!t->ctx) + t->ctx=default_ctx; + t->fd=timerfd_create(CLOCK_MONOTONIC,0); + if (t->fd<0) + { + fprintf(stderr,"timer: timerfd_create failed: %s\n",strerror(errno)); + return -1; + } + + if (triton_timer_mod(t)) { - if (t->expire_tv.tv_sec<t1.expire_tv.tv_sec || (t->expire_tv.tv_sec==t1->expire_tv.tv_sec && t->expire_tv.tv_usec<t1->expire_tv.tv_usec)) - break; + close(t->fd); + return -1; } - list_add_tail(&t->entry3,&t1->entry3); - pthread_mutex_unlock(&timers_lock); + + spin_lock(&t->ctx->lock); + list_add_tail(&t->entry,&t->ctx->timers); + spin_unlock(&t->ctx->lock); + + if (epoll_ctl(epoll_fd,EPOLL_CTL_ADD,t->fd,&t->epoll_event)) + { + fprintf(stderr,"timer: epoll_ctl failed: %s\n",strerror(errno)); + spin_lock(&t->ctx->lock); + list_del(&t->entry); + spin_unlock(&t->ctx->lock); + close(t->fd); + return -1; + } + + return 0; +} +int triton_timer_mod(struct triton_timer_t *t) +{ + int flags; + + struct itimerspec ts= + { + .it_value.tv_sec=t->expire_tv.tv_sec, + .it_value.tv_nsec=t->expire_tv.tv_usec*1000, + .it_interval.tv_sec=t->period/1000, + .it_interval.tv_nsec=t->period%1000*1000, + }; + + if (t->expire_tv.tv_sec==0 && t->expire_tv.tv_usec==0) + { + ts.it_value=ts.interval; + flags=0; + }else + flags=TFD_TIMER_ABSTIME; + + if (timerfd_settime(t->fd,flags,&ts,NULL)) + { + fprintf(stderr,"timer: timerfd_settime failed: %s\n",strerror(errno)); + return -1; + } + + return 0; } void triton_timer_del(struct triton_timer_t *t) { - pthread_mutex_lock(&timers_lock); - pthread_mutex_lock(&t->ctx->lock); + epoll_ctl(epoll_fd,EPOLL_CTL_DEL,t->fd,&t->epoll_event); + close(t->fd); + spin_lock(&t->ctx->lock); + list_del(&t->entry); if (t->pending) list_del(&t->entry2); - else - { - list_del(&t->entry3); - if (t->expire_tv.tv_sec<expire_ts.tv_sec || (t->expire_tv.tv_sec==expire_ts.tv_sec && t->expire_tv.tv_usec<expire_ts.tv_nsec/1000)) - pthread_cond_signal(&cond); - } - pthread_mutex_unlock(&t->ctx->lock); - pthread_mutex_unlock(&timers_lock); + spin_unlock(&t->ctx->lock); } + diff --git a/accel-pptpd/triton/triton.c b/accel-pptpd/triton/triton.c index 25759b5..23a69c6 100644 --- a/accel-pptpd/triton/triton.c +++ b/accel-pptpd/triton/triton.c @@ -45,6 +45,9 @@ cont: 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; } @@ -156,10 +159,20 @@ void triton_unregister_ctx(struct triton_ctx_t *ctx) spin_unlock(&ctx_list_lock); } -void triton_init() +int triton_init() { - md_init(); - timer_init(); + default_ctx=malloc(sizeof(*default_ctx)); + if (!default_ctx) + { + fprintf(stderr,"cann't allocate memory\n"); + return -1; + } + triton_register_ctx(default_ctx); + + if (md_init()) + return -1; + if (timer_init()) + return -1; } void triton_run() |