timer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. if (a->start_id < b->start_id)
  46. return 1;
  47. if (b->start_id < a->start_id)
  48. return 0;
  49. return 0;
  50. }
  51. int uv_timer_init(uv_loop_t* loop, uv_timer_t* handle) {
  52. uv__handle_init(loop, (uv_handle_t*)handle, UV_TIMER);
  53. handle->timer_cb = NULL;
  54. handle->repeat = 0;
  55. return 0;
  56. }
  57. int uv_timer_start(uv_timer_t* handle,
  58. uv_timer_cb cb,
  59. uint64_t timeout,
  60. uint64_t repeat) {
  61. uint64_t clamped_timeout;
  62. if (uv__is_closing(handle) || cb == NULL)
  63. return UV_EINVAL;
  64. if (uv__is_active(handle))
  65. uv_timer_stop(handle);
  66. clamped_timeout = handle->loop->time + timeout;
  67. if (clamped_timeout < timeout)
  68. clamped_timeout = (uint64_t) -1;
  69. handle->timer_cb = cb;
  70. handle->timeout = clamped_timeout;
  71. handle->repeat = repeat;
  72. /* start_id is the second index to be compared in timer_less_than() */
  73. handle->start_id = handle->loop->timer_counter++;
  74. heap_insert(timer_heap(handle->loop),
  75. (struct heap_node*) &handle->heap_node,
  76. timer_less_than);
  77. uv__handle_start(handle);
  78. return 0;
  79. }
  80. int uv_timer_stop(uv_timer_t* handle) {
  81. if (!uv__is_active(handle))
  82. return 0;
  83. heap_remove(timer_heap(handle->loop),
  84. (struct heap_node*) &handle->heap_node,
  85. timer_less_than);
  86. uv__handle_stop(handle);
  87. return 0;
  88. }
  89. int uv_timer_again(uv_timer_t* handle) {
  90. if (handle->timer_cb == NULL)
  91. return UV_EINVAL;
  92. if (handle->repeat) {
  93. uv_timer_stop(handle);
  94. uv_timer_start(handle, handle->timer_cb, handle->repeat, handle->repeat);
  95. }
  96. return 0;
  97. }
  98. void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat) {
  99. handle->repeat = repeat;
  100. }
  101. uint64_t uv_timer_get_repeat(const uv_timer_t* handle) {
  102. return handle->repeat;
  103. }
  104. int uv__next_timeout(const uv_loop_t* loop) {
  105. const struct heap_node* heap_node;
  106. const uv_timer_t* handle;
  107. uint64_t diff;
  108. heap_node = heap_min(timer_heap(loop));
  109. if (heap_node == NULL)
  110. return -1; /* block indefinitely */
  111. handle = container_of(heap_node, uv_timer_t, heap_node);
  112. if (handle->timeout <= loop->time)
  113. return 0;
  114. diff = handle->timeout - loop->time;
  115. if (diff > INT_MAX)
  116. diff = INT_MAX;
  117. return (int) diff;
  118. }
  119. void uv__run_timers(uv_loop_t* loop) {
  120. struct heap_node* heap_node;
  121. uv_timer_t* handle;
  122. for (;;) {
  123. heap_node = heap_min(timer_heap(loop));
  124. if (heap_node == NULL)
  125. break;
  126. handle = container_of(heap_node, uv_timer_t, heap_node);
  127. if (handle->timeout > loop->time)
  128. break;
  129. uv_timer_stop(handle);
  130. uv_timer_again(handle);
  131. handle->timer_cb(handle);
  132. }
  133. }
  134. void uv__timer_close(uv_timer_t* handle) {
  135. uv_timer_stop(handle);
  136. }