timerqueue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. #include "dbgutil.h"
  11. struct timer_queue_op
  12. {
  13. int (*cancel)(timer_queue_t *q, timer_entry *entry, int cancel);
  14. int (*schedule)(timer_queue_t *q, timer_entry *entry, unsigned int delay);
  15. int (*get_count)(timer_queue_t *q);
  16. int (*poll_one)(timer_queue_t *q, timer_entry **p_entry, int *next_delay);
  17. void (*destroy)(timer_queue_t *q);
  18. };
  19. struct timer_queue_s
  20. {
  21. struct timer_queue_op *op;
  22. };
  23. TOOLKIT_API void timer_queue_destroy(timer_queue_t *q)
  24. {
  25. q->op->destroy(q);
  26. }
  27. TOOLKIT_API int timer_queue_schedule(timer_queue_t *q, timer_entry *entry, unsigned int delay)
  28. {
  29. return q->op->schedule(q, entry, delay);
  30. }
  31. TOOLKIT_API int timer_queue_cancel(timer_queue_t *q, timer_entry *entry, int cancel)
  32. {
  33. return q->op->cancel(q, entry, cancel);
  34. }
  35. TOOLKIT_API int timer_queue_get_count(timer_queue_t *q)
  36. {
  37. return q->op->get_count(q);
  38. }
  39. TOOLKIT_API int timer_queue_poll(timer_queue_t *q, int *next_delay)
  40. {
  41. int cnt = 0;
  42. int t;
  43. int delay = 0;
  44. do {
  45. t = timer_queue_poll_one(q, NULL, &delay);
  46. cnt += t;
  47. } while (t && delay == 0);
  48. if (next_delay)
  49. *next_delay = delay;
  50. return cnt;
  51. }
  52. TOOLKIT_API int timer_queue_poll_one(timer_queue_t *q, timer_entry **p_entry, int *next_delay)
  53. {
  54. return q->op->poll_one(q, p_entry, next_delay);
  55. }
  56. //
  57. // sorted list
  58. //
  59. struct timer_sortedlist_ext
  60. {
  61. struct list_head entry;
  62. struct timeval timer_value;
  63. };
  64. struct timer_sortedlist_s
  65. {
  66. OOP_EXTENDS(timer_queue_s);
  67. struct list_head node_list;
  68. int count;
  69. };
  70. static int timer_sortedlist_cancel(timer_queue_t *q, timer_entry *entry, int cancel)
  71. {
  72. struct timer_sortedlist_s *sortedlist = OOP_DOWNCAST(q, timer_queue_s, timer_sortedlist_s);
  73. struct timer_sortedlist_ext *ext;
  74. list_for_each_entry(ext, &sortedlist->node_list, struct timer_sortedlist_ext, entry) {
  75. timer_entry *te = container_of(ext, timer_entry, _private[0]);
  76. if (te == entry) {
  77. sortedlist->count--;
  78. list_del(&ext->entry);
  79. if (cancel) {
  80. te->cb(q, te, -1);
  81. }
  82. return 0;
  83. }
  84. }
  85. return -1;
  86. }
  87. static int timer_sortedlist_schedule(timer_queue_t *q, timer_entry *entry, unsigned int delay)
  88. {
  89. struct timer_sortedlist_s *sortedlist = OOP_DOWNCAST(q, timer_queue_s, timer_sortedlist_s);
  90. struct timer_sortedlist_ext *ext = (struct timer_sortedlist_ext *)&entry->_private[0];
  91. struct list_head *ps, *pn;
  92. TOOLKIT_ASSERT(entry && entry->cb);
  93. gettimeofday(&ext->timer_value, NULL);
  94. timeval_add_msec(&ext->timer_value, delay);
  95. for (ps = &sortedlist->node_list, pn = ps->next; pn != &sortedlist->node_list; ps = pn, pn = ps->next){
  96. struct timer_sortedlist_ext *t = slist_entry(pn, struct timer_sortedlist_ext, entry);
  97. if (timeval_cmp(&ext->timer_value, &t->timer_value) < 0)
  98. break;
  99. }
  100. __list_add(&ext->entry, ps, pn);
  101. sortedlist->count++;
  102. return 0;
  103. }
  104. static int timer_sortedlist_get_count(timer_queue_t *q)
  105. {
  106. struct timer_sortedlist_s *sortedlist = OOP_DOWNCAST(q, timer_queue_s, timer_sortedlist_s);
  107. return sortedlist->count;
  108. }
  109. static int timer_sortedlist_poll_one(timer_queue_t *q, timer_entry **p_entry, int *next_delay)
  110. {
  111. struct timer_sortedlist_s *sortedlist = OOP_DOWNCAST(q, timer_queue_s, timer_sortedlist_s);
  112. struct timer_sortedlist_ext *ext;
  113. struct timeval now;
  114. int cnt = 0;
  115. if (sortedlist->count) {
  116. ext = list_first_entry(&sortedlist->node_list, struct timer_sortedlist_ext, entry);
  117. gettimeofday(&now, NULL);
  118. if (timeval_cmp(&ext->timer_value, &now) <= 0) {
  119. timer_entry *entry = container_of(ext, timer_entry, _private[0]);
  120. list_del(&ext->entry);
  121. sortedlist->count--;
  122. entry->cb(q, entry, 0);
  123. if (p_entry)
  124. *p_entry = entry;
  125. cnt++;
  126. }
  127. }
  128. if (next_delay) {
  129. if (sortedlist->count) {
  130. ext = list_first_entry(&sortedlist->node_list, struct timer_sortedlist_ext, entry);
  131. *next_delay = (int)timeval_sub(&ext->timer_value, &now);
  132. if (*next_delay < 0)
  133. *next_delay = 0;
  134. } else {
  135. *next_delay = INT_MAX;
  136. }
  137. }
  138. return cnt;
  139. }
  140. static void timer_sortedlist_destroy(timer_queue_t *q)
  141. {
  142. struct timer_sortedlist_s *sortedlist = OOP_DOWNCAST(q, timer_queue_s, timer_sortedlist_s);
  143. TOOLKIT_ASSERT(list_empty(&sortedlist->node_list));
  144. free(sortedlist);
  145. }
  146. static struct timer_queue_op _sortedlist_ops =
  147. {
  148. &timer_sortedlist_cancel,
  149. &timer_sortedlist_schedule,
  150. &timer_sortedlist_get_count,
  151. &timer_sortedlist_poll_one,
  152. &timer_sortedlist_destroy
  153. };
  154. TOOLKIT_API int timer_sortedlist_create(timer_queue_t **p_q)
  155. {
  156. struct timer_sortedlist_s *sortedlist;
  157. struct timer_queue_s *q;
  158. sortedlist = MALLOC_T(struct timer_sortedlist_s);
  159. if (!sortedlist)
  160. return -1;
  161. q = OOP_UPCAST(sortedlist, timer_queue_s, timer_sortedlist_s);
  162. INIT_LIST_HEAD(&sortedlist->node_list);
  163. sortedlist->count = 0;
  164. q->op = &_sortedlist_ops;
  165. *p_q = q;
  166. return 0;
  167. }
  168. //
  169. // heap
  170. //
  171. struct timer_heap_ext
  172. {
  173. int timer_id;
  174. struct timeval timer_value;
  175. };
  176. struct timer_heap_s
  177. {
  178. OOP_EXTENDS(timer_queue_s);
  179. array_header_t *arr_heap;
  180. };
  181. static int timer_entry_cmp(timer_entry *x, timer_entry *y)
  182. {
  183. struct timer_heap_ext *ex = (struct timer_heap_ext *)&x->_private[0];
  184. struct timer_heap_ext *ey = (struct timer_heap_ext *)&y->_private[0];
  185. return timeval_cmp(&ex->timer_value, &ey->timer_value);
  186. }
  187. static void timer_entry_swap(struct timer_heap_s *heap, int i, int j)
  188. {
  189. timer_entry *ti = ARRAY_IDX(heap->arr_heap, i, timer_entry*);
  190. struct timer_heap_ext *tix = (struct timer_heap_ext *)&ti->_private[0];
  191. timer_entry *tj = ARRAY_IDX(heap->arr_heap, j, timer_entry*);
  192. struct timer_heap_ext *tjx = (struct timer_heap_ext *)&tj->_private[0];
  193. ARRAY_XCHG(heap->arr_heap, i, j, timer_entry*);
  194. tix->timer_id = j;
  195. tjx->timer_id = i;
  196. }
  197. static void timer_heap_up(struct timer_heap_s *heap, int slot)
  198. {
  199. int i;
  200. i = slot;
  201. while (i) {
  202. int p = heap_parent(i);
  203. if (timer_entry_cmp(ARRAY_IDX(heap->arr_heap, i, timer_entry*), ARRAY_IDX(heap->arr_heap, p, timer_entry*)) < 0) {
  204. timer_entry_swap(heap, i, p);
  205. i = p;
  206. } else {
  207. break;
  208. }
  209. }
  210. }
  211. static void timer_heap_down(struct timer_heap_s *heap, int slot)
  212. {
  213. int i = slot;
  214. int n = heap->arr_heap->nelts;
  215. while (i < n) {
  216. int l = heap_left_child(i);
  217. int r = heap_right_child(i);
  218. if (r < n) {
  219. int min = (timer_entry_cmp(ARRAY_IDX(heap->arr_heap, l, timer_entry*), ARRAY_IDX(heap->arr_heap, r, timer_entry*)) < 0) ? l : r;
  220. if (timer_entry_cmp(ARRAY_IDX(heap->arr_heap, min, timer_entry*), ARRAY_IDX(heap->arr_heap, i, timer_entry*)) < 0) {
  221. timer_entry_swap(heap, i, min);
  222. i = min;
  223. } else {
  224. break;
  225. }
  226. } else if (l < n) { /* r is out of range */
  227. if (timer_entry_cmp(ARRAY_IDX(heap->arr_heap, l, timer_entry*), ARRAY_IDX(heap->arr_heap, i, timer_entry*)) < 0) {
  228. timer_entry_swap(heap, i, l);
  229. i = l;
  230. }
  231. break;
  232. } else {
  233. break;
  234. }
  235. }
  236. }
  237. static timer_entry *pop_entry(struct timer_heap_s *heap)
  238. {
  239. timer_entry *entry = ARRAY_IDX(heap->arr_heap, 0, timer_entry*);
  240. struct timer_heap_ext *ext = (struct timer_heap_ext *)&entry->_private[0];
  241. int last = --heap->arr_heap->nelts;
  242. if (last) {
  243. timer_entry_swap(heap, 0, last);
  244. ext->timer_id = -1;
  245. timer_heap_down(heap, 0);
  246. }
  247. return entry;
  248. }
  249. static int timer_heap_cancel(timer_queue_t *q, timer_entry *entry, int cancel)
  250. {
  251. struct timer_heap_s *heap = OOP_DOWNCAST(q, timer_queue_s, timer_heap_s);
  252. struct timer_heap_ext *ext = (struct timer_heap_ext *)&entry->_private[0];
  253. int err = -1;
  254. int slot;
  255. slot = ext->timer_id;
  256. if (slot < heap->arr_heap->nelts && slot >= 0) {
  257. timer_entry *te = ARRAY_IDX(heap->arr_heap, slot, timer_entry*);
  258. if (te == entry) {
  259. int last = --heap->arr_heap->nelts;
  260. if (slot != last) {
  261. timer_entry_swap(heap, slot, last);
  262. timer_heap_up(heap, slot);
  263. timer_heap_down(heap, slot);
  264. }
  265. ext->timer_id = -1;
  266. if (cancel)
  267. entry->cb(q, entry, -1);
  268. err = 0;
  269. }
  270. }
  271. return err;
  272. }
  273. static int timer_heap_schedule(timer_queue_t *q, timer_entry *entry, unsigned int delay)
  274. {
  275. struct timer_heap_s *heap = OOP_DOWNCAST(q, timer_queue_s, timer_heap_s);
  276. struct timer_heap_ext *ext = (struct timer_heap_ext *)&entry->_private[0];
  277. gettimeofday(&ext->timer_value, NULL);
  278. timeval_add_msec(&ext->timer_value, delay);
  279. ext->timer_id = heap->arr_heap->nelts;
  280. ARRAY_PUSH(heap->arr_heap, timer_entry*) = entry;
  281. timer_heap_up(heap, ext->timer_id);
  282. return 0;
  283. }
  284. static int timer_heap_get_count(timer_queue_t *q)
  285. {
  286. struct timer_heap_s *heap = OOP_DOWNCAST(q, timer_queue_s, timer_heap_s);
  287. return heap->arr_heap->nelts;
  288. }
  289. static int timer_heap_poll_one(timer_queue_t *q, timer_entry **p_entry, int *next_delay)
  290. {
  291. struct timer_heap_s *heap = OOP_DOWNCAST(q, timer_queue_s, timer_heap_s);
  292. struct timeval now;
  293. int cnt = 0;
  294. if (heap->arr_heap->nelts) {
  295. timer_entry *entry = ARRAY_IDX(heap->arr_heap, 0, timer_entry*);
  296. struct timer_heap_ext *ext = (struct timer_heap_ext *)&entry->_private[0];
  297. gettimeofday(&now, NULL);
  298. if (timeval_cmp(&ext->timer_value, &now) <= 0) {
  299. pop_entry(heap);
  300. entry->cb(q, entry, 0);
  301. if (p_entry)
  302. *p_entry = entry;
  303. cnt++;
  304. }
  305. }
  306. if (next_delay) {
  307. if (heap->arr_heap->nelts) {
  308. timer_entry *entry = ARRAY_IDX(heap->arr_heap, 0, timer_entry*);
  309. struct timer_heap_ext *ext = (struct timer_heap_ext *)&entry->_private[0];
  310. // 解决向前调整系统日期导致返回值超过0x7FFFFFFF返回负数,导致next_delay始终返回为0,引发CPU100%问题
  311. *next_delay = (int)timeval_sub(&ext->timer_value, &now);
  312. if (*next_delay < 0)
  313. {
  314. if (timeval_cmp(&ext->timer_value, &now) <= 0)
  315. *next_delay = 0;
  316. else
  317. *next_delay = INT_MAX;
  318. }
  319. } else {
  320. *next_delay = INT_MAX;
  321. }
  322. }
  323. return cnt;
  324. }
  325. static void timer_heap_destroy(timer_queue_t *q)
  326. {
  327. struct timer_heap_s *heap = OOP_DOWNCAST(q, timer_queue_s, timer_heap_s);
  328. // TOOLKIT_ASSERT(heap->arr_heap->nelts == 0);
  329. array_free(heap->arr_heap);
  330. free(heap);
  331. }
  332. static struct timer_queue_op _heap_ops =
  333. {
  334. &timer_heap_cancel,
  335. &timer_heap_schedule,
  336. &timer_heap_get_count,
  337. &timer_heap_poll_one,
  338. &timer_heap_destroy
  339. };
  340. TOOLKIT_API int timer_heap_create(timer_queue_t **p_q)
  341. {
  342. struct timer_heap_s *heap;
  343. struct timer_queue_s *q;
  344. heap = MALLOC_T(struct timer_heap_s);
  345. if (!heap)
  346. return -1;
  347. q = OOP_UPCAST(heap, timer_queue_s, timer_heap_s);
  348. heap->arr_heap = array_make(0, sizeof(timer_entry*));
  349. if (!heap->arr_heap) {
  350. free(heap);
  351. return -1;
  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. TOOLKIT_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. TOOLKIT_ASSERT(num_wheel > 0);
  473. TOOLKIT_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. }