kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 0c4dbad2fb18b8cbcba5e7eea18f0181759afe37
parent e2ce032b02deb54fda3bc51a8c11b944f16e4194
Author: Joris Vink <joris@coders.se>
Date:   Fri, 15 May 2015 19:11:10 +0200

Kore timer improvements.

Introduces kore_timer_remove() and updates kore_timer_add()
to return the newly added timer as a struct kore_timer.

Also allow arguments to be passed to timers.

Diffstat:
includes/kore.h | 15+++++++++++++--
src/timer.c | 34++++++++++++++++++----------------
2 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/includes/kore.h b/includes/kore.h @@ -347,6 +347,16 @@ struct kore_wscbs { void (*disconnect)(struct connection *); }; +struct kore_timer { + u_int64_t nextrun; + u_int64_t interval; + int flags; + void *arg; + void (*cb)(void *, u_int64_t, u_int64_t); + + TAILQ_ENTRY(kore_timer) list; +}; + extern pid_t kore_pid; extern int foreground; extern int kore_debug; @@ -418,8 +428,9 @@ struct kore_auth *kore_auth_lookup(const char *); void kore_timer_init(void); u_int64_t kore_timer_run(u_int64_t); -void kore_timer_add(void (*cb)(u_int64_t, u_int64_t), - u_int64_t, int); +void kore_timer_remove(struct kore_timer *); +struct kore_timer *kore_timer_add(void (*cb)(void *, u_int64_t, + u_int64_t), u_int64_t, void *, int); int kore_tls_sni_cb(SSL *, int *, void *); int kore_server_bind(const char *, const char *); diff --git a/src/timer.c b/src/timer.c @@ -19,15 +19,7 @@ #include "kore.h" -struct timer { - u_int64_t nextrun; - u_int64_t interval; - int flags; - void (*cb)(u_int64_t, u_int64_t); - TAILQ_ENTRY(timer) list; -}; - -TAILQ_HEAD(timerlist, timer) kore_timers; +TAILQ_HEAD(timerlist, kore_timer) kore_timers; void kore_timer_init(void) @@ -35,14 +27,16 @@ kore_timer_init(void) TAILQ_INIT(&kore_timers); } -void -kore_timer_add(void (*cb)(u_int64_t, u_int64_t), u_int64_t interval, int flags) +struct kore_timer * +kore_timer_add(void (*cb)(void *, u_int64_t, u_int64_t), u_int64_t interval, + void *arg, int flags) { - struct timer *timer, *t; + struct kore_timer *timer, *t; timer = kore_malloc(sizeof(*timer)); timer->cb = cb; + timer->arg = arg; timer->flags = flags; timer->interval = interval; timer->nextrun = kore_time_ms() + timer->interval; @@ -50,18 +44,26 @@ kore_timer_add(void (*cb)(u_int64_t, u_int64_t), u_int64_t interval, int flags) TAILQ_FOREACH(t, &kore_timers, list) { if (t->nextrun > timer->nextrun) { TAILQ_INSERT_BEFORE(t, timer, list); - return; + return (timer); } } TAILQ_INSERT_TAIL(&kore_timers, timer, list); + return (timer); +} + +void +kore_timer_remove(struct kore_timer *timer) +{ + TAILQ_REMOVE(&kore_timers, timer, list); + kore_mem_free(timer); } u_int64_t kore_timer_run(u_int64_t now) { - struct timer *timer, *t; - u_int64_t next_timer, delta; + struct kore_timer *timer, *t; + u_int64_t next_timer, delta; next_timer = 100; @@ -73,7 +75,7 @@ kore_timer_run(u_int64_t now) TAILQ_REMOVE(&kore_timers, timer, list); delta = now - timer->nextrun; - timer->cb(now, delta); + timer->cb(timer->arg, now, delta); if (timer->flags & KORE_TIMER_ONESHOT) { kore_mem_free(timer);