hloop.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #include "hloop.h"
  2. #include "hevent.h"
  3. #include "iowatcher.h"
  4. #include "hdef.h"
  5. #include "hlog.h"
  6. #include "hmath.h"
  7. #define PAUSE_TIME 10 // ms
  8. #define MAX_BLOCK_TIME 1000 // ms
  9. #define IO_ARRAY_INIT_SIZE 64
  10. static void hio_init(hio_t* io);
  11. static void hio_deinit(hio_t* io);
  12. static void hio_reset(hio_t* io);
  13. static void hio_free(hio_t* io);
  14. static int timers_compare(const struct heap_node* lhs, const struct heap_node* rhs) {
  15. return TIMER_ENTRY(lhs)->next_timeout < TIMER_ENTRY(rhs)->next_timeout;
  16. }
  17. static int hloop_process_idles(hloop_t* loop) {
  18. int nidles = 0;
  19. struct list_node* node = loop->idles.next;
  20. hidle_t* idle = NULL;
  21. while (node != &loop->idles) {
  22. idle = IDLE_ENTRY(node);
  23. if (idle->destroy) goto destroy;
  24. if (!idle->active) goto next;
  25. if (idle->repeat == 0) {
  26. hidle_del(idle);
  27. //goto next;
  28. goto destroy;
  29. }
  30. if (idle->repeat != INFINITE) {
  31. --idle->repeat;
  32. }
  33. EVENT_PENDING(idle);
  34. ++nidles;
  35. next:
  36. node = node->next;
  37. continue;
  38. destroy:
  39. node = node->next;
  40. list_del(node->prev);
  41. SAFE_FREE(idle);
  42. }
  43. return nidles;
  44. }
  45. static int hloop_process_timers(hloop_t* loop) {
  46. int ntimers = 0;
  47. htimer_t* timer = NULL;
  48. uint64_t now_hrtime = hloop_now_hrtime(loop);
  49. while (loop->timers.root) {
  50. timer = TIMER_ENTRY(loop->timers.root);
  51. if (timer->destroy) goto destroy;
  52. if (timer->repeat == 0) {
  53. htimer_del(timer);
  54. goto destroy;
  55. }
  56. if (timer->next_timeout > now_hrtime) {
  57. break;
  58. }
  59. if (timer->repeat != INFINITE) {
  60. --timer->repeat;
  61. }
  62. heap_dequeue(&loop->timers);
  63. if (timer->event_type == HEVENT_TYPE_TIMEOUT) {
  64. timer->next_timeout += ((htimeout_t*)timer)->timeout*1000;
  65. }
  66. else if (timer->event_type == HEVENT_TYPE_PERIOD) {
  67. hperiod_t* period = (hperiod_t*)timer;
  68. timer->next_timeout = calc_next_timeout(period->minute, period->hour, period->day,
  69. period->week, period->month) * 1e6;
  70. }
  71. heap_insert(&loop->timers, &timer->node);
  72. EVENT_PENDING(timer);
  73. ++ntimers;
  74. continue;
  75. destroy:
  76. heap_dequeue(&loop->timers);
  77. SAFE_FREE(timer);
  78. }
  79. return ntimers;
  80. }
  81. static int hloop_process_ios(hloop_t* loop, int timeout) {
  82. int nevents = iowatcher_poll_events(loop, timeout);
  83. if (nevents < 0) {
  84. hloge("poll_events error=%d", -nevents);
  85. }
  86. return nevents < 0 ? 0 : nevents;
  87. }
  88. static int hloop_process_pendings(hloop_t* loop) {
  89. if (loop->npendings == 0) return 0;
  90. hevent_t* prev = NULL;
  91. hevent_t* next = NULL;
  92. int ncbs = 0;
  93. for (int i = HEVENT_PRIORITY_SIZE-1; i >= 0; --i) {
  94. next = loop->pendings[i];
  95. while (next) {
  96. if (next->active && next->pending && next->cb) {
  97. next->cb(next);
  98. ++ncbs;
  99. }
  100. prev = next;
  101. next = next->pending_next;
  102. prev->pending = 0;
  103. prev->pending_next = NULL;
  104. }
  105. loop->pendings[i] = NULL;
  106. }
  107. loop->npendings = 0;
  108. return ncbs;
  109. }
  110. static int hloop_process_events(hloop_t* loop) {
  111. // ios -> timers -> idles
  112. int nios, ntimers, nidles;
  113. nios = ntimers = nidles = 0;
  114. int32_t blocktime = MAX_BLOCK_TIME;
  115. hloop_update_time(loop);
  116. if (loop->timers.root) {
  117. uint64_t next_min_timeout = TIMER_ENTRY(loop->timers.root)->next_timeout;
  118. blocktime = next_min_timeout - hloop_now_hrtime(loop);
  119. if (blocktime <= 0) goto process_timers;
  120. blocktime /= 1000;
  121. ++blocktime;
  122. blocktime = MIN(blocktime, MAX_BLOCK_TIME);
  123. }
  124. if (loop->nios) {
  125. nios = hloop_process_ios(loop, blocktime);
  126. }
  127. else {
  128. msleep(blocktime);
  129. }
  130. hloop_update_time(loop);
  131. process_timers:
  132. if (loop->ntimers) {
  133. ntimers = hloop_process_timers(loop);
  134. }
  135. if (loop->npendings == 0) {
  136. if (loop->nidles) {
  137. nidles= hloop_process_idles(loop);
  138. }
  139. }
  140. //printd("blocktime=%d nios=%d ntimers=%d nidles=%d nactives=%d npendings=%d\n", blocktime, nios, ntimers, nidles, loop->nactives, loop->npendings);
  141. return hloop_process_pendings(loop);
  142. }
  143. int hloop_init(hloop_t* loop) {
  144. memset(loop, 0, sizeof(hloop_t));
  145. loop->status = HLOOP_STATUS_STOP;
  146. // idles
  147. list_init(&loop->idles);
  148. // timers
  149. heap_init(&loop->timers, timers_compare);
  150. // ios: init when hio_add
  151. //io_array_init(&loop->ios, IO_ARRAY_INIT_SIZE);
  152. // iowatcher: init when iowatcher_add_event
  153. //iowatcher_init(loop);
  154. // time
  155. time(&loop->start_time);
  156. loop->start_hrtime = loop->cur_hrtime = gethrtime();
  157. return 0;
  158. }
  159. void hloop_cleanup(hloop_t* loop) {
  160. // pendings
  161. printd("cleanup pendings...\n");
  162. for (int i = 0; i < HEVENT_PRIORITY_SIZE; ++i) {
  163. loop->pendings[i] = NULL;
  164. }
  165. // idles
  166. printd("cleanup idles...\n");
  167. struct list_node* node = loop->idles.next;
  168. hidle_t* idle;
  169. while (node != &loop->idles) {
  170. idle = IDLE_ENTRY(node);
  171. node = node->next;
  172. SAFE_FREE(idle);
  173. }
  174. list_init(&loop->idles);
  175. // timers
  176. printd("cleanup timers...\n");
  177. htimer_t* timer;
  178. while (loop->timers.root) {
  179. timer = TIMER_ENTRY(loop->timers.root);
  180. heap_dequeue(&loop->timers);
  181. SAFE_FREE(timer);
  182. }
  183. heap_init(&loop->timers, NULL);
  184. // ios
  185. printd("cleanup ios...\n");
  186. for (int i = 0; i < loop->ios.maxsize; ++i) {
  187. hio_t* io = loop->ios.ptr[i];
  188. if (io) {
  189. hio_free(io);
  190. }
  191. }
  192. io_array_cleanup(&loop->ios);
  193. // iowatcher
  194. iowatcher_cleanup(loop);
  195. }
  196. int hloop_run(hloop_t* loop) {
  197. loop->loop_cnt = 0;
  198. loop->status = HLOOP_STATUS_RUNNING;
  199. while (loop->status != HLOOP_STATUS_STOP) {
  200. if (loop->status == HLOOP_STATUS_PAUSE) {
  201. msleep(PAUSE_TIME);
  202. hloop_update_time(loop);
  203. continue;
  204. }
  205. ++loop->loop_cnt;
  206. if (loop->nactives == 0) break;
  207. hloop_process_events(loop);
  208. }
  209. loop->status = HLOOP_STATUS_STOP;
  210. loop->end_hrtime = gethrtime();
  211. hloop_cleanup(loop);
  212. return 0;
  213. }
  214. int hloop_stop(hloop_t* loop) {
  215. loop->status = HLOOP_STATUS_STOP;
  216. return 0;
  217. }
  218. int hloop_pause(hloop_t* loop) {
  219. if (loop->status == HLOOP_STATUS_RUNNING) {
  220. loop->status = HLOOP_STATUS_PAUSE;
  221. }
  222. return 0;
  223. }
  224. int hloop_resume(hloop_t* loop) {
  225. if (loop->status == HLOOP_STATUS_PAUSE) {
  226. loop->status = HLOOP_STATUS_RUNNING;
  227. }
  228. return 0;
  229. }
  230. hidle_t* hidle_add(hloop_t* loop, hidle_cb cb, uint32_t repeat) {
  231. hidle_t* idle;
  232. SAFE_ALLOC_SIZEOF(idle);
  233. idle->event_type = HEVENT_TYPE_IDLE;
  234. idle->priority = HEVENT_LOWEST_PRIORITY;
  235. idle->repeat = repeat;
  236. list_add(&idle->node, &loop->idles);
  237. EVENT_ADD(loop, idle, cb);
  238. loop->nidles++;
  239. return idle;
  240. }
  241. void hidle_del(hidle_t* idle) {
  242. if (idle->destroy) return;
  243. idle->loop->nidles--;
  244. EVENT_DEL(idle);
  245. }
  246. htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint64_t timeout, uint32_t repeat) {
  247. if (timeout == 0) return NULL;
  248. htimeout_t* timer;
  249. SAFE_ALLOC_SIZEOF(timer);
  250. timer->event_type = HEVENT_TYPE_TIMEOUT;
  251. timer->priority = HEVENT_HIGHEST_PRIORITY;
  252. timer->repeat = repeat;
  253. timer->timeout = timeout;
  254. hloop_update_time(loop);
  255. timer->next_timeout = hloop_now_hrtime(loop) + timeout*1000;
  256. heap_insert(&loop->timers, &timer->node);
  257. EVENT_ADD(loop, timer, cb);
  258. loop->ntimers++;
  259. return (htimer_t*)timer;
  260. }
  261. void htimer_reset(htimer_t* timer) {
  262. if (timer->event_type != HEVENT_TYPE_TIMEOUT || timer->pending) {
  263. return;
  264. }
  265. hloop_t* loop = timer->loop;
  266. htimeout_t* timeout = (htimeout_t*)timer;
  267. heap_remove(&loop->timers, &timer->node);
  268. timer->next_timeout = hloop_now_hrtime(loop) + timeout->timeout*1000;
  269. heap_insert(&loop->timers, &timer->node);
  270. }
  271. htimer_t* htimer_add_period(hloop_t* loop, htimer_cb cb,
  272. int8_t minute, int8_t hour, int8_t day,
  273. int8_t week, int8_t month, uint32_t repeat) {
  274. if (minute > 59 || hour > 23 || day > 31 || week > 6 || month > 12) {
  275. return NULL;
  276. }
  277. hperiod_t* timer;
  278. SAFE_ALLOC_SIZEOF(timer);
  279. timer->event_type = HEVENT_TYPE_PERIOD;
  280. timer->priority = HEVENT_HIGH_PRIORITY;
  281. timer->repeat = repeat;
  282. timer->minute = minute;
  283. timer->hour = hour;
  284. timer->day = day;
  285. timer->month = month;
  286. timer->week = week;
  287. timer->next_timeout = calc_next_timeout(minute, hour, day, week, month) * 1e6;
  288. heap_insert(&loop->timers, &timer->node);
  289. EVENT_ADD(loop, timer, cb);
  290. loop->ntimers++;
  291. return (htimer_t*)timer;
  292. }
  293. void htimer_del(htimer_t* timer) {
  294. if (timer->destroy) return;
  295. timer->loop->ntimers--;
  296. EVENT_DEL(timer);
  297. }
  298. void hio_init(hio_t* io) {
  299. memset(io, 0, sizeof(hio_t));
  300. io->event_type = HEVENT_TYPE_IO;
  301. io->event_index[0] = io->event_index[1] = -1;
  302. // write_queue init when hwrite try_write failed
  303. //write_queue_init(&io->write_queue, 4);;
  304. }
  305. void hio_reset(hio_t* io) {
  306. io->accept = io->connect = io->closed = 0;
  307. io->error = 0;
  308. io->events = io->revents = 0;
  309. io->read_cb = NULL;
  310. io->write_cb = NULL;
  311. io->close_cb = 0;
  312. io->accept_cb = 0;
  313. io->connect_cb = 0;
  314. io->event_index[0] = io->event_index[1] = -1;
  315. io->hovlp = NULL;
  316. }
  317. void hio_deinit(hio_t* io) {
  318. offset_buf_t* pbuf = NULL;
  319. while (!write_queue_empty(&io->write_queue)) {
  320. pbuf = write_queue_front(&io->write_queue);
  321. SAFE_FREE(pbuf->base);
  322. write_queue_pop_front(&io->write_queue);
  323. }
  324. write_queue_cleanup(&io->write_queue);
  325. }
  326. void hio_free(hio_t* io) {
  327. if (io == NULL) return;
  328. hio_deinit(io);
  329. SAFE_FREE(io->localaddr);
  330. SAFE_FREE(io->peeraddr);
  331. SAFE_FREE(io);
  332. }
  333. hio_t* hio_add(hloop_t* loop, hio_cb cb, int fd, int events) {
  334. printd("hio_add fd=%d events=%d\n", fd, events);
  335. if (loop->ios.maxsize == 0) {
  336. io_array_init(&loop->ios, IO_ARRAY_INIT_SIZE);
  337. }
  338. if (fd >= loop->ios.maxsize) {
  339. int newsize = ceil2e(fd);
  340. io_array_resize(&loop->ios, newsize > fd ? newsize : 2*fd);
  341. }
  342. hio_t* io = loop->ios.ptr[fd];
  343. if (io == NULL) {
  344. SAFE_ALLOC_SIZEOF(io);
  345. loop->ios.ptr[fd] = io;
  346. hio_init(io);
  347. }
  348. if (io->destroy) {
  349. io->destroy = 0;
  350. hio_reset(io);
  351. }
  352. if (!io->active) {
  353. EVENT_ADD(loop, io, cb);
  354. loop->nios++;
  355. }
  356. io->fd = fd;
  357. if (cb) {
  358. io->cb = (hevent_cb)cb;
  359. }
  360. iowatcher_add_event(loop, fd, events);
  361. io->events |= events;
  362. return io;
  363. }
  364. void hio_del(hio_t* io, int events) {
  365. printd("hio_del fd=%d io->events=%d events=%d\n", io->fd, io->events, events);
  366. if (io->destroy) return;
  367. iowatcher_del_event(io->loop, io->fd, events);
  368. io->events &= ~events;
  369. if (io->events == 0) {
  370. io->loop->nios--;
  371. EVENT_DEL(io);
  372. hio_deinit(io);
  373. }
  374. }
  375. #include "hsocket.h"
  376. hio_t* hlisten (hloop_t* loop, int port, haccept_cb accept_cb) {
  377. int listenfd = Listen(port);
  378. if (listenfd < 0) {
  379. return NULL;
  380. }
  381. hio_t* io = haccept(loop, listenfd, accept_cb);
  382. if (io == NULL) {
  383. closesocket(listenfd);
  384. }
  385. return io;
  386. }