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 
21 #include "uv.h"
22 #include "internal.h"
23 #include "heap-inl.h"
24 
25 #include <assert.h>
26 #include <limits.h>
27 
28 
timer_less_than(const struct heap_node * ha,const struct heap_node * hb)29 static int timer_less_than(const struct heap_node* ha,
30                            const struct heap_node* hb) {
31   const uv_timer_t* a;
32   const uv_timer_t* b;
33 
34   a = container_of(ha, uv_timer_t, heap_node);
35   b = container_of(hb, uv_timer_t, heap_node);
36 
37   if (a->timeout < b->timeout)
38     return 1;
39   if (b->timeout < a->timeout)
40     return 0;
41 
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 
50   return 0;
51 }
52 
53 
uv_timer_init(uv_loop_t * loop,uv_timer_t * handle)54 int uv_timer_init(uv_loop_t* loop, uv_timer_t* handle) {
55   uv__handle_init(loop, (uv_handle_t*)handle, UV_TIMER);
56   handle->timer_cb = NULL;
57   handle->repeat = 0;
58   return 0;
59 }
60 
61 
uv_timer_start(uv_timer_t * handle,uv_timer_cb cb,uint64_t timeout,uint64_t repeat)62 int uv_timer_start(uv_timer_t* handle,
63                    uv_timer_cb cb,
64                    uint64_t timeout,
65                    uint64_t repeat) {
66   uint64_t clamped_timeout;
67 
68   if (cb == NULL)
69     return UV_EINVAL;
70 
71   if (uv__is_active(handle))
72     uv_timer_stop(handle);
73 
74   clamped_timeout = handle->loop->time + timeout;
75   if (clamped_timeout < timeout)
76     clamped_timeout = (uint64_t) -1;
77 
78   handle->timer_cb = cb;
79   handle->timeout = clamped_timeout;
80   handle->repeat = repeat;
81   /* start_id is the second index to be compared in uv__timer_cmp() */
82   handle->start_id = handle->loop->timer_counter++;
83 
84   heap_insert((struct heap*) &handle->loop->timer_heap,
85               (struct heap_node*) &handle->heap_node,
86               timer_less_than);
87   uv__handle_start(handle);
88 
89   return 0;
90 }
91 
92 
uv_timer_stop(uv_timer_t * handle)93 int uv_timer_stop(uv_timer_t* handle) {
94   if (!uv__is_active(handle))
95     return 0;
96 
97   heap_remove((struct heap*) &handle->loop->timer_heap,
98               (struct heap_node*) &handle->heap_node,
99               timer_less_than);
100   uv__handle_stop(handle);
101 
102   return 0;
103 }
104 
105 
uv_timer_again(uv_timer_t * handle)106 int uv_timer_again(uv_timer_t* handle) {
107   if (handle->timer_cb == NULL)
108     return UV_EINVAL;
109 
110   if (handle->repeat) {
111     uv_timer_stop(handle);
112     uv_timer_start(handle, handle->timer_cb, handle->repeat, handle->repeat);
113   }
114 
115   return 0;
116 }
117 
118 
uv_timer_set_repeat(uv_timer_t * handle,uint64_t repeat)119 void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat) {
120   handle->repeat = repeat;
121 }
122 
123 
uv_timer_get_repeat(const uv_timer_t * handle)124 uint64_t uv_timer_get_repeat(const uv_timer_t* handle) {
125   return handle->repeat;
126 }
127 
128 
uv__next_timeout(const uv_loop_t * loop)129 int uv__next_timeout(const uv_loop_t* loop) {
130   const struct heap_node* heap_node;
131   const uv_timer_t* handle;
132   uint64_t diff;
133 
134   heap_node = heap_min((const struct heap*) &loop->timer_heap);
135   if (heap_node == NULL)
136     return -1; /* block indefinitely */
137 
138   handle = container_of(heap_node, uv_timer_t, heap_node);
139   if (handle->timeout <= loop->time)
140     return 0;
141 
142   diff = handle->timeout - loop->time;
143   if (diff > INT_MAX)
144     diff = INT_MAX;
145 
146   return diff;
147 }
148 
149 
uv__run_timers(uv_loop_t * loop)150 void uv__run_timers(uv_loop_t* loop) {
151   struct heap_node* heap_node;
152   uv_timer_t* handle;
153 
154   for (;;) {
155     heap_node = heap_min((struct heap*) &loop->timer_heap);
156     if (heap_node == NULL)
157       break;
158 
159     handle = container_of(heap_node, uv_timer_t, heap_node);
160     if (handle->timeout > loop->time)
161       break;
162 
163     uv_timer_stop(handle);
164     uv_timer_again(handle);
165     handle->timer_cb(handle);
166   }
167 }
168 
169 
uv__timer_close(uv_timer_t * handle)170 void uv__timer_close(uv_timer_t* handle) {
171   uv_timer_stop(handle);
172 }
173