timer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. * Permission is hereby granted, free of charge, to any person obtaining a copy
  3. * of this software and associated documentation files (the "Software"), to
  4. * deal in the Software without restriction, including without limitation the
  5. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  6. * sell copies of the Software, and to permit persons to whom the Software is
  7. * furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  18. * IN THE SOFTWARE.
  19. */
  20. #include "uv.h"
  21. #include "uv-common.h"
  22. #include "heap-inl.h"
  23. #include <assert.h>
  24. #include <limits.h>
  25. static struct heap *timer_heap(const uv_loop_t* loop) {
  26. #ifdef _WIN32
  27. return (struct heap*) loop->timer_heap;
  28. #else
  29. return (struct heap*) &loop->timer_heap;
  30. #endif
  31. }
  32. static int timer_less_than(const struct heap_node* ha,
  33. const struct heap_node* hb) {
  34. const uv_timer_t* a;
  35. const uv_timer_t* b;
  36. a = container_of(ha, uv_timer_t, heap_node);
  37. b = container_of(hb, uv_timer_t, heap_node);
  38. if (a->timeout < b->timeout)
  39. return 1;
  40. if (b->timeout < a->timeout)
  41. return 0;
  42. /* Compare start_id when both have the same timeout. start_id is
  43. * allocated with loop->timer_counter in uv_timer_start().
  44. */
  45. return a->start_id < b->start_id;
  46. }
  47. int uv_timer_init(uv_loop_t* loop, uv_timer_t* handle) {
  48. uv__handle_init(loop, (uv_handle_t*)handle, UV_TIMER);
  49. handle->timer_cb = NULL;
  50. handle->repeat = 0;
  51. return 0;
  52. }
  53. int uv_timer_start(uv_timer_t* handle,
  54. uv_timer_cb cb,
  55. uint64_t timeout,
  56. uint64_t repeat) {
  57. uint64_t clamped_timeout;
  58. if (uv__is_closing(handle) || cb == NULL)
  59. return UV_EINVAL;
  60. if (uv__is_active(handle))
  61. uv_timer_stop(handle);
  62. clamped_timeout = handle->loop->time + timeout;
  63. if (clamped_timeout < timeout)
  64. clamped_timeout = (uint64_t) -1;
  65. handle->timer_cb = cb;
  66. handle->timeout = clamped_timeout;
  67. handle->repeat = repeat;
  68. /* start_id is the second index to be compared in timer_less_than() */
  69. handle->start_id = handle->loop->timer_counter++;
  70. heap_insert(timer_heap(handle->loop),
  71. (struct heap_node*) &handle->heap_node,
  72. timer_less_than);
  73. uv__handle_start(handle);
  74. return 0;
  75. }
  76. int uv_timer_stop(uv_timer_t* handle) {
  77. if (!uv__is_active(handle))
  78. return 0;
  79. heap_remove(timer_heap(handle->loop),
  80. (struct heap_node*) &handle->heap_node,
  81. timer_less_than);
  82. uv__handle_stop(handle);
  83. return 0;
  84. }
  85. int uv_timer_again(uv_timer_t* handle) {
  86. if (handle->timer_cb == NULL)
  87. return UV_EINVAL;
  88. if (handle->repeat) {
  89. uv_timer_stop(handle);
  90. uv_timer_start(handle, handle->timer_cb, handle->repeat, handle->repeat);
  91. }
  92. return 0;
  93. }
  94. void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat) {
  95. handle->repeat = repeat;
  96. }
  97. uint64_t uv_timer_get_repeat(const uv_timer_t* handle) {
  98. return handle->repeat;
  99. }
  100. int uv__next_timeout(const uv_loop_t* loop) {
  101. const struct heap_node* heap_node;
  102. const uv_timer_t* handle;
  103. uint64_t diff;
  104. heap_node = heap_min(timer_heap(loop));
  105. if (heap_node == NULL)
  106. return -1; /* block indefinitely */
  107. handle = container_of(heap_node, uv_timer_t, heap_node);
  108. if (handle->timeout <= loop->time)
  109. return 0;
  110. diff = handle->timeout - loop->time;
  111. if (diff > INT_MAX)
  112. diff = INT_MAX;
  113. return (int) diff;
  114. }
  115. void uv__run_timers(uv_loop_t* loop) {
  116. struct heap_node* heap_node;
  117. uv_timer_t* handle;
  118. for (;;) {
  119. heap_node = heap_min(timer_heap(loop));
  120. if (heap_node == NULL)
  121. break;
  122. handle = container_of(heap_node, uv_timer_t, heap_node);
  123. if (handle->timeout > loop->time)
  124. break;
  125. uv_timer_stop(handle);
  126. uv_timer_again(handle);
  127. handle->timer_cb(handle);
  128. }
  129. }
  130. void uv__timer_close(uv_timer_t* handle) {
  131. uv_timer_stop(handle);
  132. }