void check_time_out(sche_t s,uint32_t now) { coro_t co; for( ; ;) { struct heapele *ele = minheap_min(s->_minheap); if(!ele) break; co = (coro_t)((int8_t*)ele - sizeof(co->next)); if(co->timeout < now) break; minheap_popmin(s->_minheap); if(co->status != CORO_ACTIVE) { co->status = CORO_ACTIVE; LINK_LIST_PUSH_BACK(s->active_list_1,co); }else if(co->status == CORO_DIE) { coro_destroy(&co); if(--s->coro_size == 0) s->stop = 1; //printf("a coro destroy\n"); } } s->next_check_timeout = now + 200;//check every 200 ms }
//更新定时器 void update_timer(struct timer *t,uint64_t now) { struct timer_item *item; while(NULL != (item = (struct timer_item*)minheap_min(t->_minheap))){ if(NULL == item->_ud){ minheap_popmin(t->_minheap); free(item); }else if(now >= item->_timeout){ minheap_popmin(t->_minheap); if(item->_callback(t,item,item->_ud)) free(item); }else break; } }
static bool el_poll_get_timeout(eventloop_t *el, struct timeval *tvp) { long now_sec, now_ms; if (!minheap_len(el->timers)) return false; ev_timer_t *t = (ev_timer_t *) minheap_min(el->timers); rr_dt_now(&now_sec, &now_ms); tvp->tv_sec = t->sec - now_sec; if (t->ms < now_ms) { tvp->tv_usec = ((t->ms+1000) - now_ms)*1000; tvp->tv_sec--; } else { tvp->tv_usec = (t->ms - now_ms)*1000; } if (tvp->tv_sec < 0) tvp->tv_sec = 0; if (tvp->tv_usec < 0) tvp->tv_usec = 0; return true; }
int el_timer_process(eventloop_t *el) { int processed = 0, ret; uint32_t len = minheap_len(el->timers); while (len) { ev_timer_t *t = (ev_timer_t *) minheap_min(el->timers); if (!rr_dt_is_past(t->sec, t->ms)) break; t = (ev_timer_t *) minheap_pop(el->timers); long long millisecond = t->timer_cb(el, t->ud); /* if the timer is still active, push it back to heap */ if (millisecond > 0) { rr_dt_expire_at(millisecond, &t->sec, &t->ms); ret = minheap_push(el->timers, t); assert(!ret); } processed++; len--; } return processed; }