Skip to content

Commit

Permalink
feat timer: added expire functions
Browse files Browse the repository at this point in the history
Signed-off-by: John Sanpe <[email protected]>
  • Loading branch information
sanpeqf committed Jun 9, 2024
1 parent 6295009 commit e30a97b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/event/xdbd_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,5 +403,7 @@ void xdbd_process_events_and_timers(xdbd_t *xdbd) {

xdbd_event_process_posted(xdbd, &xdbd_posted_accept_events);

xdbd_expire_timers();

xdbd_event_process_posted(xdbd, &xdbd_posted_events);
}
25 changes: 24 additions & 1 deletion src/event/xdbd_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ xdbd_remove_timer(xdbd_timer_t *timer)
}

xdbd_msec_t
xdbd_first_timeout()
xdbd_first_timeout(void)
{
xdbd_timer_t *timer;
xdbd_msec_t timeout;
Expand All @@ -65,3 +65,26 @@ xdbd_first_timeout()

return (xdbd_msec_t) (timeout > 0 ? timeout : 0);
}

int
xdbd_expire_timers(void)
{
xdbd_timer_t *timer;
xdbd_msec_t timeout;
int retval;

for (;;) {
timeout = xdbd_first_timeout();
if (timeout)
break;

timer = xdbd_first_timer();
xdbd_remove_timer(timer);

retval = timer->func(timer, timer->pdata);
if (retval)
return retval;
}

return 0;
}
11 changes: 10 additions & 1 deletion src/event/xdbd_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
#include <xdbd.h>
#include <bfdev.h>

typedef int
(*xdbd_timer_func_t)(xdbd_timer_t *timer, void *pdata);

struct xdbd_timer_s {
bfdev_heap_node_t node;
xdbd_msec_t time;

xdbd_timer_func_t func;
void *pdata;
};

extern bfdev_heap_root_t
Expand All @@ -27,6 +33,9 @@ extern void
xdbd_remove_timer(xdbd_timer_t *timer);

extern xdbd_msec_t
xdbd_first_timeout();
xdbd_first_timeout(void);

extern int
xdbd_expire_timers(void);

#endif /* __XDBD_TIMER__H__ */

0 comments on commit e30a97b

Please sign in to comment.