timerqueue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. #include "precompile.h"
  2. #include "timerqueue.h"
  3. #include "ooputil.h"
  4. #include "memutil.h"
  5. #include "list.h"
  6. #include "array.h"
  7. #include "heaputil.h"
  8. #include "gettimeofday.h"
  9. #include "hash.h"
  10. struct timer_queue_op
  11. {
  12. int (*cancel)(timer_queue_t *q, timer_entry *entry, int cancel);
  13. int (*schedule)(timer_queue_t *q, timer_entry *entry, unsigned int delay);
  14. int (*get_count)(timer_queue_t *q);
  15. int (*poll_one)(timer_queue_t *q, timer_entry **p_entry, int *next_delay);
  16. void (*destroy)(timer_queue_t *q);
  17. };
  18. struct timer_queue_s
  19. {
  20. struct timer_queue_op *op;
  21. };
  22. TOOLKIT_API void timer_queue_destroy(timer_queue_t *q)
  23. {
  24. q->op->destroy(q);
  25. }
  26. TOOLKIT_API int timer_queue_schedule(timer_queue_t *q, timer_entry *entry, unsigned int delay)
  27. {
  28. return q->op->schedule(q, entry, delay);
  29. }
  30. TOOLKIT_API int timer_queue_cancel(timer_queue_t *q, timer_entry *entry, int cancel)
  31. {
  32. return q->op->cancel(q, entry, cancel);
  33. }
  34. TOOLKIT_API int timer_queue_get_count(timer_queue_t *q)
  35. {
  36. return q->op->get_count(q);
  37. }
  38. TOOLKIT_API int timer_queue_poll(timer_queue_t *q, int *next_delay)
  39. {
  40. int cnt = 0;
  41. int t;
  42. int delay = 0;
  43. do {
  44. t = timer_queue_poll_one(q, NULL, &delay);
  45. cnt += t;
  46. } while (t && delay == 0);
  47. if (next_delay)
  48. *next_delay = delay;
  49. return cnt;
  50. }
  51. TOOLKIT_API int timer_queue_poll_one(timer_queue_t *q, timer_entry **p_entry, int *next_delay)
  52. {
  53. return q->op->poll_one(q, p_entry, next_delay);
  54. }
  55. //
  56. // sorted list
  57. //
  58. struct timer_sortedlist_ext
  59. {
  60. struct list_head entry;
  61. struct timeval timer_value;
  62. };
  63. struct timer_sortedlist_s
  64. {
  65. OOP_EXTENDS(timer_queue_s);
  66. struct list_head node_list;
  67. int count;
  68. };
  69. static int timer_sortedlist_cancel(timer_queue_t *q, timer_entry *entry, int cancel)
  70. {
  71. struct timer_sortedlist_s *sortedlist = OOP_DOWNCAST(q, timer_queue_s, timer_sortedlist_s);
  72. struct timer_sortedlist_ext *ext;
  73. list_for_each_entry(ext, &sortedlist->node_list, struct timer_sortedlist_ext, entry) {
  74. timer_entry *te = container_of(ext, timer_entry, _private[0]);
  75. if (te == entry) {
  76. sortedlist->count--;
  77. list_del(&ext->entry);
  78. if (cancel) {
  79. te->cb(q, te, -1);
  80. }
  81. return 0;
  82. }
  83. }
  84. return -1;
  85. }
  86. static int timer_sortedlist_schedule(timer_queue_t *q, timer_entry *entry, unsigned int delay)
  87. {
  88. struct timer_sortedlist_s *sortedlist = OOP_DOWNCAST(q, timer_queue_s, timer_sortedlist_s);
  89. struct timer_sortedlist_ext *ext = (struct timer_sortedlist_ext *)&entry->_private[0];
  90. struct list_head *ps, *pn;
  91. assert(entry && entry->cb);
  92. gettimeofday(&ext->timer_value, NULL);
  93. timeval_add_msec(&ext->timer_value, delay);
  94. for (ps = &sortedlist->node_list, pn = ps->next; pn != &sortedlist->node_list; ps = pn, pn = ps->next){
  95. struct timer_sortedlist_ext *t = slist_entry(pn, struct timer_sortedlist_ext, entry);
  96. if (timeval_cmp(&ext->timer_value, &t->timer_value) < 0)
  97. break;
  98. }
  99. __list_add(&ext->entry, ps, pn);
  100. sortedlist->count++;
  101. return 0;
  102. }
  103. static int timer_sortedlist_get_count(timer_queue_t *q)
  104. {
  105. struct timer_sortedlist_s *sortedlist = OOP_DOWNCAST(q, timer_queue_s, timer_sortedlist_s);
  106. return sortedlist->count;
  107. }
  108. static int timer_sortedlist_poll_one(timer_queue_t *q, timer_entry **p_entry, int *next_delay)
  109. {
  110. struct timer_sortedlist_s *sortedlist = OOP_DOWNCAST(q, timer_queue_s, timer_sortedlist_s);
  111. struct timer_sortedlist_ext *ext;
  112. struct timeval now;
  113. int cnt = 0;
  114. if (sortedlist->count) {
  115. ext = list_first_entry(&sortedlist->node_list, struct timer_sortedlist_ext, entry);
  116. gettimeofday(&now, NULL);
  117. if (timeval_cmp(&ext->timer_value, &now) <= 0) {
  118. timer_entry *entry = container_of(ext, timer_entry, _private[0]);
  119. list_del(&ext->entry);
  120. sortedlist->count--;
  121. entry->cb(q, entry, 0);
  122. if (p_entry)
  123. *p_entry = entry;
  124. cnt++;
  125. }
  126. }
  127. if (next_delay) {
  128. if (sortedlist->count) {
  129. ext = list_first_entry(&sortedlist->node_list, struct timer_sortedlist_ext, entry);
  130. *next_delay = (int)timeval_sub(&ext->timer_value, &now);
  131. if (*next_delay < 0)
  132. *next_delay = 0;
  133. } else {
  134. *next_delay = INT_MAX;
  135. }
  136. }
  137. return cnt;
  138. }
  139. static void timer_sortedlist_destroy(timer_queue_t *q)
  140. {
  141. struct timer_sortedlist_s *sortedlist = OOP_DOWNCAST(q, timer_queue_s, timer_sortedlist_s);
  142. assert(list_empty(&sortedlist->node_list));
  143. free(sortedlist);
  144. }
  145. static struct timer_queue_op _sortedlist_ops =
  146. {
  147. &timer_sortedlist_cancel,
  148. &timer_sortedlist_schedule,
  149. &timer_sortedlist_get_count,
  150. &timer_sortedlist_poll_one,
  151. &timer_sortedlist_destroy
  152. };
  153. TOOLKIT_API int timer_sortedlist_create(timer_queue_t **p_q)
  154. {
  155. struct timer_sortedlist_s *sortedlist;
  156. struct timer_queue_s *q;
  157. sortedlist = MALLOC_T(struct timer_sortedlist_s);
  158. if (!sortedlist)
  159. return -1;
  160. q = OOP_UPCAST(sortedlist, timer_queue_s, timer_sortedlist_s);
  161. INIT_LIST_HEAD(&sortedlist->node_list);
  162. sortedlist->count = 0;
  163. q->op = &_sortedlist_ops;
  164. *p_q = q;
  165. return 0;
  166. }
  167. //
  168. // heap
  169. //
  170. struct timer_heap_ext
  171. {
  172. int timer_id;
  173. struct timeval timer_value;
  174. };
  175. struct timer_heap_s
  176. {
  177. OOP_EXTENDS(timer_queue_s);
  178. array_header_t *arr_heap;
  179. };
  180. static int timer_entry_cmp(timer_entry *x, timer_entry *y)
  181. {
  182. struct timer_heap_ext *ex = (struct timer_heap_ext *)&x->_private[0];
  183. struct timer_heap_ext *ey = (struct timer_heap_ext *)&y->_private[0];
  184. return timeval_cmp(&ex->timer_value, &ey->timer_value);
  185. }
  186. static void timer_entry_swap(struct timer_heap_s *heap, int i, int j)
  187. {
  188. timer_entry *ti = ARRAY_IDX(heap->arr_heap, i, timer_entry*);
  189. struct timer_heap_ext *tix = (struct timer_heap_ext *)&ti->_private[0];
  190. timer_entry *tj = ARRAY_IDX(heap->arr_heap, j, timer_entry*);
  191. struct timer_heap_ext *tjx = (struct timer_heap_ext *)&tj->_private[0];
  192. ARRAY_XCHG(heap->arr_heap, i, j, timer_entry*);
  193. tix->timer_id = j;
  194. tjx->timer_id = i;
  195. }
  196. static void timer_heap_up(struct timer_heap_s *heap, int slot)
  197. {
  198. int i;
  199. i = slot;
  200. while (i) {
  201. int p = heap_parent(i);
  202. if (timer_entry_cmp(ARRAY_IDX(heap->arr_heap, i, timer_entry*), ARRAY_IDX(heap->arr_heap, p, timer_entry*)) < 0) {
  203. timer_entry_swap(heap, i, p);
  204. i = p;
  205. } else {
  206. break;
  207. }
  208. }
  209. }
  210. static void timer_heap_down(struct timer_heap_s *heap, int slot)
  211. {
  212. int i = slot;
  213. int n = heap->arr_heap->nelts;
  214. while (i < n) {
  215. int l = heap_left_child(i);
  216. int r = heap_right_child(i);
  217. if (r < n) {
  218. int min = (timer_entry_cmp(ARRAY_IDX(heap->arr_heap, l, timer_entry*), ARRAY_IDX(heap->arr_heap, r, timer_entry*)) < 0) ? l : r;
  219. if (timer_entry_cmp(ARRAY_IDX(heap->arr_heap, min, timer_entry*), ARRAY_IDX(heap->arr_heap, i, timer_entry*)) < 0) {
  220. timer_entry_swap(heap, i, min);
  221. i = min;
  222. } else {
  223. break;
  224. }
  225. } else if (l < n) { /* r is out of range */
  226. if (timer_entry_cmp(ARRAY_IDX(heap->arr_heap, l, timer_entry*), ARRAY_IDX(heap->arr_heap, i, timer_entry*)) < 0) {
  227. timer_entry_swap(heap, i, l);
  228. i = l;
  229. }
  230. break;
  231. } else {
  232. break;
  233. }
  234. }
  235. }
  236. static timer_entry *pop_entry(struct timer_heap_s *heap)
  237. {
  238. timer_entry *entry = ARRAY_IDX(heap->arr_heap, 0, timer_entry*);
  239. struct timer_heap_ext *ext = (struct timer_heap_ext *)&entry->_private[0];
  240. int last = --heap->arr_heap->nelts;
  241. if (last) {
  242. timer_entry_swap(heap, 0, last);
  243. ext->timer_id = -1;
  244. timer_heap_down(heap, 0);
  245. }
  246. return entry;
  247. }
  248. static int timer_heap_cancel(timer_queue_t *q, timer_entry *entry, int cancel)
  249. {
  250. struct timer_heap_s *heap = OOP_DOWNCAST(q, timer_queue_s, timer_heap_s);
  251. struct timer_heap_ext *ext = (struct timer_heap_ext *)&entry->_private[0];
  252. int err = -1;
  253. int slot;
  254. slot = ext->timer_id;
  255. if (slot < heap->arr_heap->nelts && slot >= 0) {
  256. timer_entry *te = ARRAY_IDX(heap->arr_heap, slot, timer_entry*);
  257. if (te == entry) {
  258. int last = --heap->arr_heap->nelts;
  259. if (slot != last) {
  260. timer_entry_swap(heap, slot, last);
  261. timer_heap_up(heap, slot);
  262. timer_heap_down(heap, slot);
  263. }
  264. ext->timer_id = -1;
  265. if (cancel)
  266. entry->cb(q, entry, -1);
  267. err = 0;
  268. }
  269. }
  270. return err;
  271. }
  272. static int timer_heap_schedule(timer_queue_t *q, timer_entry *entry, unsigned int delay)
  273. {
  274. struct timer_heap_s *heap = OOP_DOWNCAST(q, timer_queue_s, timer_heap_s);
  275. struct timer_heap_ext *ext = (struct timer_heap_ext *)&entry->_private[0];
  276. gettimeofday(&ext->timer_value, NULL);
  277. timeval_add_msec(&ext->timer_value, delay);
  278. ext->timer_id = heap->arr_heap->nelts;
  279. ARRAY_PUSH(heap->arr_heap, timer_entry*) = entry;
  280. timer_heap_up(heap, ext->timer_id);
  281. return 0;
  282. }
  283. static int timer_heap_get_count(timer_queue_t *q)
  284. {
  285. struct timer_heap_s *heap = OOP_DOWNCAST(q, timer_queue_s, timer_heap_s);
  286. return heap->arr_heap->nelts;
  287. }
  288. static int timer_heap_poll_one(timer_queue_t *q, timer_entry **p_entry, int *next_delay)
  289. {
  290. struct timer_heap_s *heap = OOP_DOWNCAST(q, timer_queue_s, timer_heap_s);
  291. struct timeval now;
  292. int cnt = 0;
  293. if (heap->arr_heap->nelts) {
  294. timer_entry *entry = ARRAY_IDX(heap->arr_heap, 0, timer_entry*);
  295. struct timer_heap_ext *ext = (struct timer_heap_ext *)&entry->_private[0];
  296. gettimeofday(&now, NULL);
  297. if (timeval_cmp(&ext->timer_value, &now) <= 0) {
  298. pop_entry(heap);
  299. entry->cb(q, entry, 0);
  300. if (p_entry)
  301. *p_entry = entry;
  302. cnt++;
  303. }
  304. }
  305. if (next_delay) {
  306. if (heap->arr_heap->nelts) {
  307. timer_entry *entry = ARRAY_IDX(heap->arr_heap, 0, timer_entry*);
  308. struct timer_heap_ext *ext = (struct timer_heap_ext *)&entry->_private[0];
  309. // 解决向前调整系统日期导致返回值超过0x7FFFFFFF返回负数,导致next_delay始终返回为0,引发CPU100%问题
  310. *next_delay = (int)timeval_sub(&ext->timer_value, &now);
  311. if (*next_delay < 0)
  312. {
  313. if (timeval_cmp(&ext->timer_value, &now) <= 0)
  314. *next_delay = 0;
  315. else
  316. *next_delay = INT_MAX;
  317. }
  318. } else {
  319. *next_delay = INT_MAX;
  320. }
  321. }
  322. return cnt;
  323. }
  324. static void timer_heap_destroy(timer_queue_t *q)
  325. {
  326. struct timer_heap_s *heap = OOP_DOWNCAST(q, timer_queue_s, timer_heap_s);
  327. // assert(heap->arr_heap->nelts == 0);
  328. array_free(heap->arr_heap);
  329. free(heap);
  330. }
  331. static struct timer_queue_op _heap_ops =
  332. {
  333. &timer_heap_cancel,
  334. &timer_heap_schedule,
  335. &timer_heap_get_count,
  336. &timer_heap_poll_one,
  337. &timer_heap_destroy
  338. };
  339. TOOLKIT_API int timer_heap_create(timer_queue_t **p_q)
  340. {
  341. struct timer_heap_s *heap;
  342. struct timer_queue_s *q;
  343. heap = MALLOC_T(struct timer_heap_s);
  344. if (!heap)
  345. return -1;
  346. q = OOP_UPCAST(heap, timer_queue_s, timer_heap_s);
  347. heap->arr_heap = array_make(0, sizeof(timer_entry*));
  348. if (!heap->arr_heap) {
  349. free(heap);
  350. return -1;
  351. }
  352. // 指定定时器实现回调
  353. q->op = &_heap_ops;
  354. *p_q = q;
  355. return 0;
  356. }
  357. //
  358. // wheel
  359. //
  360. struct timer_wheel_ext
  361. {
  362. struct hlist_node hentry;
  363. int expire_circle;
  364. int expire_slot;
  365. };
  366. struct timer_wheel_s
  367. {
  368. OOP_EXTENDS(timer_queue_s);
  369. struct hlist_head *buckets;
  370. int count;
  371. int num_wheel;
  372. int wheel_time;
  373. int cursor_circle;
  374. int cursor_slot;
  375. };
  376. static int timer_wheel_cancel(timer_queue_t *q, timer_entry *entry, int cancel)
  377. {
  378. struct timer_wheel_s *wheel = OOP_DOWNCAST(q, timer_queue_s, timer_wheel_s);
  379. struct timer_wheel_ext *ext = (struct timer_wheel_ext *)&entry->_private[0];
  380. struct timer_wheel_ext *tpos;
  381. struct hlist_node *pos;
  382. hlist_for_each_entry(tpos, pos, &wheel->buckets[ext->expire_slot], struct timer_wheel_ext, hentry) {
  383. if (ext == tpos) {
  384. hlist_del(pos);
  385. if (cancel)
  386. entry->cb(q, entry, -1);
  387. return 0;
  388. }
  389. }
  390. return -1;
  391. }
  392. static int timer_wheel_schedule(timer_queue_t *q, timer_entry *entry, unsigned int delay)
  393. {
  394. struct timer_wheel_s *wheel = OOP_DOWNCAST(q, timer_queue_s, timer_wheel_s);
  395. struct timer_wheel_ext *ext = (struct timer_wheel_ext *)&entry->_private[0];
  396. int slot = (delay + wheel->wheel_time -1) / wheel->wheel_time + wheel->cursor_slot;
  397. ext->expire_circle = slot / wheel->num_wheel;
  398. slot = slot % wheel->num_wheel;
  399. ext->expire_slot = slot;
  400. if (hlist_empty(&wheel->buckets[slot])) {
  401. hlist_add_head(&ext->hentry, &wheel->buckets[slot]);
  402. } else {
  403. struct timer_wheel_ext *tpos, *last;
  404. struct hlist_node *pos;
  405. hlist_for_each_entry(tpos, pos, &wheel->buckets[slot], struct timer_wheel_ext, hentry) {
  406. if (ext->expire_circle < tpos->expire_circle) {
  407. hlist_add_before(&ext->hentry, pos);
  408. return 0;
  409. }
  410. last = tpos;
  411. }
  412. hlist_add_after(&last->hentry, &ext->hentry);
  413. }
  414. return 0;
  415. }
  416. static int timer_wheel_get_count(timer_queue_t *q)
  417. {
  418. struct timer_wheel_s *wheel = OOP_DOWNCAST(q, timer_queue_s, timer_wheel_s);
  419. return wheel->count;
  420. }
  421. static int timer_wheel_poll_one(timer_queue_t *q, timer_entry **p_entry, int *next_delay)
  422. {
  423. struct timer_wheel_s *wheel = OOP_DOWNCAST(q, timer_queue_s, timer_wheel_s);
  424. int cnt = 0, t;
  425. do {
  426. struct timer_wheel_ext *tpos;
  427. struct hlist_node *pos, *n;
  428. t = 0;
  429. hlist_for_each_entry_safe(tpos, pos, n, &wheel->buckets[wheel->cursor_slot], struct timer_wheel_ext, hentry) {
  430. if (wheel->cursor_circle >= tpos->expire_circle) {
  431. timer_entry *entry = container_of((void*)tpos, timer_entry, _private[0]);
  432. hlist_del(pos);
  433. entry->cb(q, entry, 0);
  434. if (p_entry)
  435. *p_entry = entry;
  436. t++;
  437. } else {
  438. break;
  439. }
  440. }
  441. cnt += t;
  442. } while (t > 0);
  443. wheel->cursor_slot ++;
  444. if (wheel->cursor_slot == wheel->num_wheel) {
  445. wheel->cursor_circle ++;
  446. wheel->cursor_slot = 0;
  447. }
  448. if (next_delay) {
  449. *next_delay = wheel->wheel_time;
  450. }
  451. return cnt;
  452. }
  453. static void timer_wheel_destroy(timer_queue_t *q)
  454. {
  455. struct timer_wheel_s *wheel = OOP_DOWNCAST(q, timer_queue_s, timer_wheel_s);
  456. assert(wheel->count == 0);
  457. free(wheel->buckets);
  458. free(wheel);
  459. }
  460. static struct timer_queue_op _wheel_ops =
  461. {
  462. &timer_wheel_cancel,
  463. &timer_wheel_schedule,
  464. &timer_wheel_get_count,
  465. &timer_wheel_poll_one,
  466. &timer_wheel_destroy
  467. };
  468. TOOLKIT_API int timer_wheel_create(int num_wheel, int wheel_time, timer_queue_t **p_q)
  469. {
  470. struct timer_wheel_s *wheel;
  471. struct timer_queue_s *q;
  472. assert(num_wheel > 0);
  473. assert(wheel_time > 0);
  474. wheel = MALLOC_T(struct timer_wheel_s);
  475. if (!wheel) {
  476. return -1;
  477. }
  478. q = OOP_UPCAST(wheel, timer_queue_s, timer_wheel_s);
  479. wheel->num_wheel = num_wheel;
  480. wheel->wheel_time = wheel_time;
  481. wheel->count = 0;
  482. wheel->cursor_slot = 0;
  483. wheel->cursor_circle = 0;
  484. wheel->buckets = (struct hlist_head*)calloc(num_wheel, sizeof(struct hlist_head));
  485. if (!wheel->buckets) {
  486. free(wheel);
  487. return -1;
  488. } else {
  489. int i;
  490. for (i = 0; i < num_wheel; ++i) {
  491. INIT_HLIST_HEAD(&wheel->buckets[i]);
  492. }
  493. }
  494. q->op = &_wheel_ops;
  495. *p_q = q;
  496. return 0;
  497. }