| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- #include "hloop.h"
- #include "hevent.h"
- #include "iowatcher.h"
- #include "hdef.h"
- #include "hlog.h"
- #include "hmath.h"
- #define PAUSE_TIME 10 // ms
- #define MAX_BLOCK_TIME 1000 // ms
- #define IO_ARRAY_INIT_SIZE 64
- static void hio_init(hio_t* io);
- static void hio_deinit(hio_t* io);
- static void hio_reset(hio_t* io);
- static void hio_free(hio_t* io);
- static int timers_compare(const struct heap_node* lhs, const struct heap_node* rhs) {
- return TIMER_ENTRY(lhs)->next_timeout < TIMER_ENTRY(rhs)->next_timeout;
- }
- static int hloop_process_idles(hloop_t* loop) {
- int nidles = 0;
- struct list_node* node = loop->idles.next;
- hidle_t* idle = NULL;
- while (node != &loop->idles) {
- idle = IDLE_ENTRY(node);
- if (idle->destroy) goto destroy;
- if (!idle->active) goto next;
- if (idle->repeat == 0) {
- hidle_del(idle);
- //goto next;
- goto destroy;
- }
- if (idle->repeat != INFINITE) {
- --idle->repeat;
- }
- EVENT_PENDING(idle);
- ++nidles;
- next:
- node = node->next;
- continue;
- destroy:
- node = node->next;
- list_del(node->prev);
- SAFE_FREE(idle);
- }
- return nidles;
- }
- static int hloop_process_timers(hloop_t* loop) {
- int ntimers = 0;
- htimer_t* timer = NULL;
- uint64_t now_hrtime = hloop_now_hrtime(loop);
- while (loop->timers.root) {
- timer = TIMER_ENTRY(loop->timers.root);
- if (timer->destroy) goto destroy;
- if (timer->repeat == 0) {
- htimer_del(timer);
- goto destroy;
- }
- if (timer->next_timeout > now_hrtime) {
- break;
- }
- if (timer->repeat != INFINITE) {
- --timer->repeat;
- }
- heap_dequeue(&loop->timers);
- if (timer->event_type == HEVENT_TYPE_TIMEOUT) {
- timer->next_timeout += ((htimeout_t*)timer)->timeout*1000;
- }
- else if (timer->event_type == HEVENT_TYPE_PERIOD) {
- hperiod_t* period = (hperiod_t*)timer;
- timer->next_timeout = calc_next_timeout(period->minute, period->hour, period->day,
- period->week, period->month) * 1e6;
- }
- heap_insert(&loop->timers, &timer->node);
- EVENT_PENDING(timer);
- ++ntimers;
- continue;
- destroy:
- heap_dequeue(&loop->timers);
- SAFE_FREE(timer);
- }
- return ntimers;
- }
- static int hloop_process_ios(hloop_t* loop, int timeout) {
- int nevents = iowatcher_poll_events(loop, timeout);
- if (nevents < 0) {
- hloge("poll_events error=%d", -nevents);
- }
- return nevents < 0 ? 0 : nevents;
- }
- static int hloop_process_pendings(hloop_t* loop) {
- if (loop->npendings == 0) return 0;
- hevent_t* prev = NULL;
- hevent_t* next = NULL;
- int ncbs = 0;
- for (int i = HEVENT_PRIORITY_SIZE-1; i >= 0; --i) {
- next = loop->pendings[i];
- while (next) {
- if (next->active && next->pending && next->cb) {
- next->cb(next);
- ++ncbs;
- }
- prev = next;
- next = next->pending_next;
- prev->pending = 0;
- prev->pending_next = NULL;
- }
- loop->pendings[i] = NULL;
- }
- loop->npendings = 0;
- return ncbs;
- }
- static int hloop_process_events(hloop_t* loop) {
- // ios -> timers -> idles
- int nios, ntimers, nidles;
- nios = ntimers = nidles = 0;
- int32_t blocktime = MAX_BLOCK_TIME;
- hloop_update_time(loop);
- if (loop->timers.root) {
- uint64_t next_min_timeout = TIMER_ENTRY(loop->timers.root)->next_timeout;
- blocktime = next_min_timeout - hloop_now_hrtime(loop);
- if (blocktime <= 0) goto process_timers;
- blocktime /= 1000;
- ++blocktime;
- blocktime = MIN(blocktime, MAX_BLOCK_TIME);
- }
- if (loop->nios) {
- nios = hloop_process_ios(loop, blocktime);
- }
- else {
- msleep(blocktime);
- }
- hloop_update_time(loop);
- process_timers:
- if (loop->ntimers) {
- ntimers = hloop_process_timers(loop);
- }
- if (loop->npendings == 0) {
- if (loop->nidles) {
- nidles= hloop_process_idles(loop);
- }
- }
- //printd("blocktime=%d nios=%d ntimers=%d nidles=%d nactives=%d npendings=%d\n", blocktime, nios, ntimers, nidles, loop->nactives, loop->npendings);
- return hloop_process_pendings(loop);
- }
- int hloop_init(hloop_t* loop) {
- memset(loop, 0, sizeof(hloop_t));
- loop->status = HLOOP_STATUS_STOP;
- // idles
- list_init(&loop->idles);
- // timers
- heap_init(&loop->timers, timers_compare);
- // ios: init when hio_add
- //io_array_init(&loop->ios, IO_ARRAY_INIT_SIZE);
- // iowatcher: init when iowatcher_add_event
- //iowatcher_init(loop);
- // time
- time(&loop->start_time);
- loop->start_hrtime = loop->cur_hrtime = gethrtime();
- return 0;
- }
- void hloop_cleanup(hloop_t* loop) {
- // pendings
- printd("cleanup pendings...\n");
- for (int i = 0; i < HEVENT_PRIORITY_SIZE; ++i) {
- loop->pendings[i] = NULL;
- }
- // idles
- printd("cleanup idles...\n");
- struct list_node* node = loop->idles.next;
- hidle_t* idle;
- while (node != &loop->idles) {
- idle = IDLE_ENTRY(node);
- node = node->next;
- SAFE_FREE(idle);
- }
- list_init(&loop->idles);
- // timers
- printd("cleanup timers...\n");
- htimer_t* timer;
- while (loop->timers.root) {
- timer = TIMER_ENTRY(loop->timers.root);
- heap_dequeue(&loop->timers);
- SAFE_FREE(timer);
- }
- heap_init(&loop->timers, NULL);
- // ios
- printd("cleanup ios...\n");
- for (int i = 0; i < loop->ios.maxsize; ++i) {
- hio_t* io = loop->ios.ptr[i];
- if (io) {
- hio_free(io);
- }
- }
- io_array_cleanup(&loop->ios);
- // iowatcher
- iowatcher_cleanup(loop);
- }
- int hloop_run(hloop_t* loop) {
- loop->loop_cnt = 0;
- loop->status = HLOOP_STATUS_RUNNING;
- while (loop->status != HLOOP_STATUS_STOP) {
- if (loop->status == HLOOP_STATUS_PAUSE) {
- msleep(PAUSE_TIME);
- hloop_update_time(loop);
- continue;
- }
- ++loop->loop_cnt;
- if (loop->nactives == 0) break;
- hloop_process_events(loop);
- }
- loop->status = HLOOP_STATUS_STOP;
- loop->end_hrtime = gethrtime();
- hloop_cleanup(loop);
- return 0;
- }
- int hloop_stop(hloop_t* loop) {
- loop->status = HLOOP_STATUS_STOP;
- return 0;
- }
- int hloop_pause(hloop_t* loop) {
- if (loop->status == HLOOP_STATUS_RUNNING) {
- loop->status = HLOOP_STATUS_PAUSE;
- }
- return 0;
- }
- int hloop_resume(hloop_t* loop) {
- if (loop->status == HLOOP_STATUS_PAUSE) {
- loop->status = HLOOP_STATUS_RUNNING;
- }
- return 0;
- }
- hidle_t* hidle_add(hloop_t* loop, hidle_cb cb, uint32_t repeat) {
- hidle_t* idle;
- SAFE_ALLOC_SIZEOF(idle);
- idle->event_type = HEVENT_TYPE_IDLE;
- idle->priority = HEVENT_LOWEST_PRIORITY;
- idle->repeat = repeat;
- list_add(&idle->node, &loop->idles);
- EVENT_ADD(loop, idle, cb);
- loop->nidles++;
- return idle;
- }
- void hidle_del(hidle_t* idle) {
- if (idle->destroy) return;
- idle->loop->nidles--;
- EVENT_DEL(idle);
- }
- htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint64_t timeout, uint32_t repeat) {
- if (timeout == 0) return NULL;
- htimeout_t* timer;
- SAFE_ALLOC_SIZEOF(timer);
- timer->event_type = HEVENT_TYPE_TIMEOUT;
- timer->priority = HEVENT_HIGHEST_PRIORITY;
- timer->repeat = repeat;
- timer->timeout = timeout;
- hloop_update_time(loop);
- timer->next_timeout = hloop_now_hrtime(loop) + timeout*1000;
- heap_insert(&loop->timers, &timer->node);
- EVENT_ADD(loop, timer, cb);
- loop->ntimers++;
- return (htimer_t*)timer;
- }
- void htimer_reset(htimer_t* timer) {
- if (timer->event_type != HEVENT_TYPE_TIMEOUT || timer->pending) {
- return;
- }
- hloop_t* loop = timer->loop;
- htimeout_t* timeout = (htimeout_t*)timer;
- heap_remove(&loop->timers, &timer->node);
- timer->next_timeout = hloop_now_hrtime(loop) + timeout->timeout*1000;
- heap_insert(&loop->timers, &timer->node);
- }
- htimer_t* htimer_add_period(hloop_t* loop, htimer_cb cb,
- int8_t minute, int8_t hour, int8_t day,
- int8_t week, int8_t month, uint32_t repeat) {
- if (minute > 59 || hour > 23 || day > 31 || week > 6 || month > 12) {
- return NULL;
- }
- hperiod_t* timer;
- SAFE_ALLOC_SIZEOF(timer);
- timer->event_type = HEVENT_TYPE_PERIOD;
- timer->priority = HEVENT_HIGH_PRIORITY;
- timer->repeat = repeat;
- timer->minute = minute;
- timer->hour = hour;
- timer->day = day;
- timer->month = month;
- timer->week = week;
- timer->next_timeout = calc_next_timeout(minute, hour, day, week, month) * 1e6;
- heap_insert(&loop->timers, &timer->node);
- EVENT_ADD(loop, timer, cb);
- loop->ntimers++;
- return (htimer_t*)timer;
- }
- void htimer_del(htimer_t* timer) {
- if (timer->destroy) return;
- timer->loop->ntimers--;
- EVENT_DEL(timer);
- }
- void hio_init(hio_t* io) {
- memset(io, 0, sizeof(hio_t));
- io->event_type = HEVENT_TYPE_IO;
- io->event_index[0] = io->event_index[1] = -1;
- // write_queue init when hwrite try_write failed
- //write_queue_init(&io->write_queue, 4);;
- }
- void hio_reset(hio_t* io) {
- io->accept = io->connect = io->closed = 0;
- io->error = 0;
- io->events = io->revents = 0;
- io->read_cb = NULL;
- io->write_cb = NULL;
- io->close_cb = 0;
- io->accept_cb = 0;
- io->connect_cb = 0;
- io->event_index[0] = io->event_index[1] = -1;
- io->hovlp = NULL;
- }
- void hio_deinit(hio_t* io) {
- offset_buf_t* pbuf = NULL;
- while (!write_queue_empty(&io->write_queue)) {
- pbuf = write_queue_front(&io->write_queue);
- SAFE_FREE(pbuf->base);
- write_queue_pop_front(&io->write_queue);
- }
- write_queue_cleanup(&io->write_queue);
- }
- void hio_free(hio_t* io) {
- if (io == NULL) return;
- hio_deinit(io);
- SAFE_FREE(io->localaddr);
- SAFE_FREE(io->peeraddr);
- SAFE_FREE(io);
- }
- hio_t* hio_add(hloop_t* loop, hio_cb cb, int fd, int events) {
- printd("hio_add fd=%d events=%d\n", fd, events);
- if (loop->ios.maxsize == 0) {
- io_array_init(&loop->ios, IO_ARRAY_INIT_SIZE);
- }
- if (fd >= loop->ios.maxsize) {
- int newsize = ceil2e(fd);
- io_array_resize(&loop->ios, newsize > fd ? newsize : 2*fd);
- }
- hio_t* io = loop->ios.ptr[fd];
- if (io == NULL) {
- SAFE_ALLOC_SIZEOF(io);
- loop->ios.ptr[fd] = io;
- hio_init(io);
- }
- if (io->destroy) {
- io->destroy = 0;
- hio_reset(io);
- }
- if (!io->active) {
- EVENT_ADD(loop, io, cb);
- loop->nios++;
- }
- io->fd = fd;
- if (cb) {
- io->cb = (hevent_cb)cb;
- }
- iowatcher_add_event(loop, fd, events);
- io->events |= events;
- return io;
- }
- void hio_del(hio_t* io, int events) {
- printd("hio_del fd=%d io->events=%d events=%d\n", io->fd, io->events, events);
- if (io->destroy) return;
- iowatcher_del_event(io->loop, io->fd, events);
- io->events &= ~events;
- if (io->events == 0) {
- io->loop->nios--;
- EVENT_DEL(io);
- hio_deinit(io);
- }
- }
- #include "hsocket.h"
- hio_t* hlisten (hloop_t* loop, int port, haccept_cb accept_cb) {
- int listenfd = Listen(port);
- if (listenfd < 0) {
- return NULL;
- }
- hio_t* io = haccept(loop, listenfd, accept_cb);
- if (io == NULL) {
- closesocket(listenfd);
- }
- return io;
- }
|