Ver código fonte

Merge pull request #52 from leamus/libhv-leamus

fix next_timeout overflow
ithewei 4 anos atrás
pai
commit
7b0c16046d
3 arquivos alterados com 6 adições e 6 exclusões
  1. 1 1
      event/hevent.h
  2. 4 4
      event/hloop.c
  3. 1 1
      event/hloop.h

+ 1 - 1
event/hevent.h

@@ -71,7 +71,7 @@ struct htimer_s {
 
 struct htimeout_s {
     HTIMER_FIELDS
-    uint32_t    timeout;                \
+    uint64_t    timeout;                \
 };
 
 struct hperiod_s {

+ 4 - 4
event/hloop.c

@@ -71,12 +71,12 @@ static int hloop_process_timers(hloop_t* loop) {
             heap_dequeue(&loop->timers);
             if (timer->event_type == HEVENT_TYPE_TIMEOUT) {
                 while (timer->next_timeout <= now_hrtime) {
-                    timer->next_timeout += ((htimeout_t*)timer)->timeout * 1000;
+                    timer->next_timeout += (uint64_t)((htimeout_t*)timer)->timeout * 1000;
                 }
             }
             else if (timer->event_type == HEVENT_TYPE_PERIOD) {
                 hperiod_t* period = (hperiod_t*)timer;
-                timer->next_timeout = cron_next_timeout(period->minute, period->hour, period->day,
+                timer->next_timeout = (uint64_t)cron_next_timeout(period->minute, period->hour, period->day,
                         period->week, period->month) * 1000000;
             }
             heap_insert(&loop->timers, &timer->node);
@@ -499,7 +499,7 @@ void hidle_del(hidle_t* idle) {
     EVENT_DEL(idle);
 }
 
-htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint32_t timeout, uint32_t repeat) {
+htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint64_t timeout, uint32_t repeat) {
     if (timeout == 0)   return NULL;
     htimeout_t* timer;
     HV_ALLOC_SIZEOF(timer);
@@ -550,7 +550,7 @@ htimer_t* htimer_add_period(hloop_t* loop, htimer_cb cb,
     timer->day    = day;
     timer->month  = month;
     timer->week   = week;
-    timer->next_timeout = cron_next_timeout(minute, hour, day, week, month) * 1000000;
+    timer->next_timeout = (uint64_t)cron_next_timeout(minute, hour, day, week, month) * 1000000;
     heap_insert(&loop->timers, &timer->node);
     EVENT_ADD(loop, timer, cb);
     loop->ntimers++;

+ 1 - 1
event/hloop.h

@@ -155,7 +155,7 @@ HV_EXPORT void     hidle_del(hidle_t* idle);
 
 // timer
 // @param timeout: unit(ms)
-HV_EXPORT htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint32_t timeout, uint32_t repeat DEFAULT(INFINITE));
+HV_EXPORT htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint64_t timeout, uint32_t repeat DEFAULT(INFINITE));
 /*
  * minute   hour    day     week    month       cb
  * 0~59     0~23    1~31    0~6     1~12