diff options
author | Kozlov Dmitry <dima@server> | 2010-09-03 14:00:45 +0400 |
---|---|---|
committer | Kozlov Dmitry <dima@server> | 2010-09-03 14:00:45 +0400 |
commit | 935c25b34dba5d22372de3f792dd806db6d729a8 (patch) | |
tree | 2d4916ec75b5750f8ee0b8f06dbb1fc55e9f6add /accel-pptpd/triton/timer.c | |
parent | b43d224c8a306ff54bbb913c5aab891f82541f6e (diff) | |
download | accel-ppp-xebd-935c25b34dba5d22372de3f792dd806db6d729a8.tar.gz accel-ppp-xebd-935c25b34dba5d22372de3f792dd806db6d729a8.zip |
rewrited triton library
Diffstat (limited to 'accel-pptpd/triton/timer.c')
-rw-r--r-- | accel-pptpd/triton/timer.c | 60 |
1 files changed, 44 insertions, 16 deletions
diff --git a/accel-pptpd/triton/timer.c b/accel-pptpd/triton/timer.c index 5f7de93..f34c19d 100644 --- a/accel-pptpd/triton/timer.c +++ b/accel-pptpd/triton/timer.c @@ -16,6 +16,8 @@ static struct epoll_event *epoll_events; static pthread_t timer_thr; static void *timer_thread(void *arg); +static mempool_t *timer_pool; + int timer_init(void) { epoll_fd = epoll_create(1); @@ -30,6 +32,8 @@ int timer_init(void) return -1; } + timer_pool = mempool_create(sizeof(struct _triton_timer_t)); + return 0; } @@ -50,7 +54,7 @@ void timer_terminate(void) void *timer_thread(void *arg) { int i,n,r; - struct triton_timer_t *t; + struct _triton_timer_t *t; while(1) { n = epoll_wait(epoll_fd, epoll_events, max_events, -1); @@ -62,11 +66,17 @@ void *timer_thread(void *arg) } for(i = 0; i < n; i++) { - t = (struct triton_timer_t *)epoll_events[i].data.ptr; + t = (struct _triton_timer_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); + if (t->ud) { + if (!t->pending) { + list_add_tail(&t->entry2, &t->ctx->pending_timers); + t->pending = 1; + r = triton_queue_ctx(t->ctx); + } else + r = 0; + } else + r = 0; spin_unlock(&t->ctx->lock); if (r) triton_thread_wakeup(t->ctx->thread); @@ -76,20 +86,30 @@ void *timer_thread(void *arg) return NULL; } -int __export triton_timer_add(struct triton_timer_t *t, int abs_time) +int __export triton_timer_add(struct triton_ctx_t *ctx, struct triton_timer_t *ud, int abs_time) { + struct _triton_timer_t *t = mempool_alloc(timer_pool); + + memset(t, 0, sizeof(*t)); + t->ud = ud; t->epoll_event.data.ptr = t; t->epoll_event.events = EPOLLIN | EPOLLET; - if (!t->ctx) - t->ctx = default_ctx; + if (ctx) + t->ctx = (struct _triton_ctx_t *)ctx->tpd; + else + t->ctx = (struct _triton_ctx_t *)default_ctx->tpd; t->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); if (t->fd < 0) { triton_log_error("timer:timerfd_create: %s" ,strerror(errno)); + mempool_free(t); return -1; } - if (triton_timer_mod(t, abs_time)) { + ud->tpd = t; + + if (triton_timer_mod(ud, abs_time)) { close(t->fd); + mempool_free(t); return -1; } @@ -100,24 +120,28 @@ int __export triton_timer_add(struct triton_timer_t *t, int abs_time) if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, t->fd, &t->epoll_event)) { triton_log_error("timer:epoll_ctl: %s", strerror(errno)); spin_lock(&t->ctx->lock); + t->ud = NULL; list_del(&t->entry); spin_unlock(&t->ctx->lock); close(t->fd); + mempool_free(t); + ud->tpd = NULL; return -1; } return 0; } -int __export triton_timer_mod(struct triton_timer_t *t,int abs_time) +int __export triton_timer_mod(struct triton_timer_t *ud,int abs_time) { + struct _triton_timer_t *t = (struct _triton_timer_t *)ud->tpd; 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, + .it_value.tv_sec = ud->expire_tv.tv_sec, + .it_value.tv_nsec = ud->expire_tv.tv_usec * 1000, + .it_interval.tv_sec = ud->period / 1000, + .it_interval.tv_nsec = (ud->period % 1000) * 1000, }; - if (t->expire_tv.tv_sec == 0 && t->expire_tv.tv_usec == 0) + if (ud->expire_tv.tv_sec == 0 && ud->expire_tv.tv_usec == 0) ts.it_value = ts.it_interval; if (timerfd_settime(t->fd, abs_time ? TFD_TIMER_ABSTIME : 0, &ts, NULL)) { @@ -127,14 +151,18 @@ int __export triton_timer_mod(struct triton_timer_t *t,int abs_time) return 0; } -void __export triton_timer_del(struct triton_timer_t *t) +void __export triton_timer_del(struct triton_timer_t *ud) { + struct _triton_timer_t *t = (struct _triton_timer_t *)ud->tpd; 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); + t->ud = NULL; spin_unlock(&t->ctx->lock); + sched_yield(); + mempool_free(t); } |