rewriting event timer module

This commit is contained in:
zhengshuxin 2021-02-09 15:07:10 +08:00
parent ccf561d7ed
commit b59c22ae1a
2 changed files with 8 additions and 6 deletions

View File

@ -380,8 +380,8 @@ static void event_loop(ACL_EVENT *eventp)
const char *myname = "event_loop"; const char *myname = "event_loop";
EVENT_SELECT *ev = (EVENT_SELECT *) eventp; EVENT_SELECT *ev = (EVENT_SELECT *) eventp;
ACL_SOCKET sockfd; ACL_SOCKET sockfd;
int nready, when, i; int nready, i;
acl_int64 delay; acl_int64 delay, when;
ACL_EVENT_FDTABLE *fdp; ACL_EVENT_FDTABLE *fdp;
struct timeval tv, *tvp; struct timeval tv, *tvp;
fd_set rmask; /* enabled read events */ fd_set rmask; /* enabled read events */
@ -402,11 +402,12 @@ static void event_loop(ACL_EVENT *eventp)
when = event_timer_when(eventp); when = event_timer_when(eventp);
if (when >= 0) { if (when >= 0) {
acl_int64 n = when - eventp->present; acl_int64 n = when - eventp->present;
if (n <= 0) if (n <= 0) {
delay = 0; delay = 0;
else if (n < delay) } else if (n < delay) {
delay = n; delay = n;
} }
}
/* 调用 event_prepare 检查有多少个描述字需要通过 select 进行检测 */ /* 调用 event_prepare 检查有多少个描述字需要通过 select 进行检测 */

View File

@ -72,8 +72,9 @@ static int avl_cmp_fn(const void *v1, const void *v2)
void event_timer_init(ACL_EVENT *eventp) void event_timer_init(ACL_EVENT *eventp)
{ {
eventp->timers2 = (EVENT_TIMERS*) acl_mymalloc(sizeof(EVENT_TIMERS)); eventp->timers2 = (EVENT_TIMERS*) acl_mymalloc(sizeof(EVENT_TIMERS));
eventp->timers2->table = acl_htable_create(1024, 0);
avl_create(&eventp->timers2->avl, avl_cmp_fn, sizeof(TIMER_INFO), avl_create(&eventp->timers2->avl, avl_cmp_fn, sizeof(TIMER_INFO),
offsetof(TIMER_INFO, node)); offsetof(TIMER_NODE, node));
} }
acl_int64 event_timer_when(ACL_EVENT *eventp) acl_int64 event_timer_when(ACL_EVENT *eventp)
@ -145,7 +146,7 @@ acl_int64 event_timer_request(ACL_EVENT *eventp, ACL_EVENT_NOTIFY_TIME callback,
if (info == NULL) { if (info == NULL) {
/* If not found, schedule a new timer request. */ /* If not found, schedule a new timer request. */
info = (TIMER_INFO *) acl_mymalloc(sizeof(TIMER_INFO)); info = (TIMER_INFO *) acl_mycalloc(1, sizeof(TIMER_INFO));
acl_assert(info); acl_assert(info);
info->delay = delay; info->delay = delay;
info->keep = keep; info->keep = keep;