timer.c
Go to the documentation of this file.
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 "uv-common.h"
23 #include "heap-inl.h"
24 
25 #include <assert.h>
26 #include <limits.h>
27 
28 
29 static struct heap *timer_heap(const uv_loop_t* loop) {
30 #ifdef _WIN32
31  return (struct heap*) loop->timer_heap;
32 #else
33  return (struct heap*) &loop->timer_heap;
34 #endif
35 }
36 
37 
38 static int timer_less_than(const struct heap_node* ha,
39  const struct heap_node* hb) {
40  const uv_timer_t* a;
41  const uv_timer_t* b;
42 
45 
46  if (a->timeout < b->timeout)
47  return 1;
48  if (b->timeout < a->timeout)
49  return 0;
50 
51  /* Compare start_id when both have the same timeout. start_id is
52  * allocated with loop->timer_counter in uv_timer_start().
53  */
54  return a->start_id < b->start_id;
55 }
56 
57 
59  uv__handle_init(loop, (uv_handle_t*)handle, UV_TIMER);
60  handle->timer_cb = NULL;
61  handle->repeat = 0;
62  return 0;
63 }
64 
65 
69  uint64_t repeat) {
70  uint64_t clamped_timeout;
71 
72  if (uv__is_closing(handle) || cb == NULL)
73  return UV_EINVAL;
74 
75  if (uv__is_active(handle))
77 
78  clamped_timeout = handle->loop->time + timeout;
79  if (clamped_timeout < timeout)
80  clamped_timeout = (uint64_t) -1;
81 
82  handle->timer_cb = cb;
83  handle->timeout = clamped_timeout;
84  handle->repeat = repeat;
85  /* start_id is the second index to be compared in timer_less_than() */
86  handle->start_id = handle->loop->timer_counter++;
87 
88  heap_insert(timer_heap(handle->loop),
89  (struct heap_node*) &handle->heap_node,
92 
93  return 0;
94 }
95 
96 
98  if (!uv__is_active(handle))
99  return 0;
100 
101  heap_remove(timer_heap(handle->loop),
102  (struct heap_node*) &handle->heap_node,
105 
106  return 0;
107 }
108 
109 
111  if (handle->timer_cb == NULL)
112  return UV_EINVAL;
113 
114  if (handle->repeat) {
116  uv_timer_start(handle, handle->timer_cb, handle->repeat, handle->repeat);
117  }
118 
119  return 0;
120 }
121 
122 
124  handle->repeat = repeat;
125 }
126 
127 
129  return handle->repeat;
130 }
131 
132 
134  const struct heap_node* heap_node;
135  const uv_timer_t* handle;
136  uint64_t diff;
137 
138  heap_node = heap_min(timer_heap(loop));
139  if (heap_node == NULL)
140  return -1; /* block indefinitely */
141 
143  if (handle->timeout <= loop->time)
144  return 0;
145 
146  diff = handle->timeout - loop->time;
147  if (diff > INT_MAX)
148  diff = INT_MAX;
149 
150  return (int) diff;
151 }
152 
153 
155  struct heap_node* heap_node;
157 
158  for (;;) {
159  heap_node = heap_min(timer_heap(loop));
160  if (heap_node == NULL)
161  break;
162 
164  if (handle->timeout > loop->time)
165  break;
166 
169  handle->timer_cb(handle);
170  }
171 }
172 
173 
176 }
uv__is_active
#define uv__is_active(h)
Definition: uv-common.h:235
uv__is_closing
#define uv__is_closing(h)
Definition: uv-common.h:238
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
uv_timer_set_repeat
void uv_timer_set_repeat(uv_timer_t *handle, uint64_t repeat)
Definition: timer.c:123
timer_heap
static struct heap * timer_heap(const uv_loop_t *loop)
Definition: timer.c:29
uv-common.h
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
container_of
#define container_of(ptr, type, member)
Definition: uv-common.h:57
uv__timer_close
void uv__timer_close(uv_timer_t *handle)
Definition: timer.c:174
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
heap-inl.h
bm_diff.diff
diff
Definition: bm_diff.py:274
heap_node
Definition: heap-inl.h:27
timer_less_than
static int timer_less_than(const struct heap_node *ha, const struct heap_node *hb)
Definition: timer.c:38
uv__next_timeout
int uv__next_timeout(const uv_loop_t *loop)
Definition: timer.c:133
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
uv_timer_s
Definition: uv.h:850
uv_timer_again
int uv_timer_again(uv_timer_t *handle)
Definition: timer.c:110
uv__handle_init
#define uv__handle_init(loop_, h, type_)
Definition: uv-common.h:284
uv_timer_init
int uv_timer_init(uv_loop_t *loop, uv_timer_t *handle)
Definition: timer.c:58
uv.h
uv_timer_cb
void(* uv_timer_cb)(uv_timer_t *handle)
Definition: uv.h:318
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_loop_s
Definition: uv.h:1767
uv__handle_start
#define uv__handle_start(h)
Definition: uv-common.h:241
uv_timer_get_repeat
uint64_t uv_timer_get_repeat(const uv_timer_t *handle)
Definition: timer.c:128
uv_timer_start
int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
Definition: timer.c:66
uv__handle_stop
#define uv__handle_stop(h)
Definition: uv-common.h:249
uv__run_timers
void uv__run_timers(uv_loop_t *loop)
Definition: timer.c:154
timeout
uv_timer_t timeout
Definition: libuv/docs/code/uvwget/main.c:9
heap
Definition: heap-inl.h:40
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
uv_timer_stop
int uv_timer_stop(uv_timer_t *handle)
Definition: timer.c:97


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:39