Skip to content

Commit

Permalink
timers: add ability to set interval
Browse files Browse the repository at this point in the history
  • Loading branch information
michaldziuba03 committed Jul 27, 2024
1 parent 4346c5c commit f6ca964
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Simple async io library for C (it will be in the future, for now it's just HTTP

### TODO:

- [x] Timers using heap data structure (works like `setTimeout`).
- [x] Timers using heap data structure (works like `setTimeout`, `setInterval`).
- [x] Non-blocking IO for sockets using epoll.
- [x] Basic networking abstraction to handle TCP.
- [ ] HTTP module.
Expand Down
2 changes: 1 addition & 1 deletion src/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void lx_remove_event(lx_event_t *event, int fd) {

void lx_run(lx_io_t *ctx) {
struct epoll_event events[MAX_EVENTS];
int epoll_timeout = -1;
int epoll_timeout = lx_timers_run(ctx);

while(true) {
int events_count = epoll_wait(ctx->epoll_fd, events, MAX_EVENTS, epoll_timeout);
Expand Down
24 changes: 23 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,30 @@ void worker(int id, int port) {
lx_run(&ctx);
}

void timer_callback(lx_timer_t *timer) {
printf("Callback\n");
}

void timer_once(lx_timer_t *timer) {
printf("Should happen only once\n");
}

void timer_test() {
lx_io_t ctx = lx_init();
lx_timer_t timer;
lx_timer_init(&ctx, &timer);
lx_timer_start(&timer, timer_once, 5 * 1000);

lx_timer_t timer2;
lx_timer_init(&ctx, &timer2);
lx_timer_repeat(&timer2, timer_callback, 3 * 1000);

lx_run(&ctx);
}

int main() {
worker(1, 8000);
timer_test();
//worker(1, 8000);
return 0;
}
/*
Expand Down
9 changes: 9 additions & 0 deletions src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void lx_timer_init(lx_io_t *ctx, lx_timer_t *timer) {
timer->data = NULL;
timer->ontimeout = NULL;
timer->timeout = 0;
timer->interval = 0;
heap_init_node(&timer->hnode);
}

Expand All @@ -50,6 +51,11 @@ void lx_timer_start(lx_timer_t *timer, lx_timer_callback_t ontimeout, uint64_t t
heap_insert(&timer->ctx->timers, &timer->hnode);
}

void lx_timer_repeat(lx_timer_t *timer, lx_timer_callback_t ontimeout, uint64_t interval) {
timer->interval = interval;
lx_timer_start(timer, ontimeout, interval);
}

void lx_timer_stop(lx_timer_t *timer) {
if (timer->state != TIMER_ACTIVE)
return;
Expand All @@ -75,6 +81,9 @@ int lx_timers_run(lx_io_t *ctx) {

lx_timer_stop(min);
min->ontimeout(min);

if (min->interval != 0)
lx_timer_start(min, min->ontimeout, min->interval);
}

// return next timeout for event selector (like epoll):
Expand Down
2 changes: 2 additions & 0 deletions src/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef void (*lx_timer_callback_t)(struct lx_timer*);
typedef struct lx_timer {
lx_io_t *ctx;
uint64_t timeout;
uint64_t interval;
struct heap_node hnode;
void *data; // custom data for callback
lx_timer_callback_t ontimeout;
Expand All @@ -35,6 +36,7 @@ int timers_comparator(struct heap_node*, struct heap_node*);
void lx_timer_init(lx_io_t*, lx_timer_t*);
lx_timer_t *lx_timer_alloc(lx_io_t*);
void lx_timer_start(lx_timer_t*, lx_timer_callback_t, uint64_t);
void lx_timer_repeat(lx_timer_t*, lx_timer_callback_t, uint64_t);
void lx_timer_stop(lx_timer_t*);
void lx_timer_destroy(lx_timer_t*);
int lx_timers_run(lx_io_t*);

0 comments on commit f6ca964

Please sign in to comment.