|
28 | 28 | #include "handle-inl.h" |
29 | 29 |
|
30 | 30 |
|
31 | | -void uv_update_time(uv_loop_t* loop) { |
32 | | - DWORD ticks; |
33 | | - ULARGE_INTEGER time; |
34 | | - |
35 | | - ticks = GetTickCount(); |
| 31 | +/* The number of milliseconds in one second. */ |
| 32 | +#define UV__MILLISEC 1000 |
36 | 33 |
|
37 | | - time.QuadPart = loop->time; |
38 | 34 |
|
39 | | - /* GetTickCount() can conceivably wrap around, so when the current tick |
40 | | - * count is lower than the last tick count, we'll assume it has wrapped. |
41 | | - * uv_poll must make sure that the timer can never overflow more than |
42 | | - * once between two subsequent uv_update_time calls. |
43 | | - */ |
44 | | - time.LowPart = ticks; |
45 | | - if (ticks < loop->last_tick_count) |
46 | | - time.HighPart++; |
47 | | - |
48 | | - /* Remember the last tick count. */ |
49 | | - loop->last_tick_count = ticks; |
50 | | - |
51 | | - /* The GetTickCount() resolution isn't too good. Sometimes it'll happen |
52 | | - * that GetQueuedCompletionStatus() or GetQueuedCompletionStatusEx() has |
53 | | - * waited for a couple of ms but this is not reflected in the GetTickCount |
54 | | - * result yet. Therefore whenever GetQueuedCompletionStatus times out |
55 | | - * we'll add the number of ms that it has waited to the current loop time. |
56 | | - * When that happened the loop time might be a little ms farther than what |
57 | | - * we've just computed, and we shouldn't update the loop time. |
58 | | - */ |
59 | | - if (loop->time < time.QuadPart) |
60 | | - loop->time = time.QuadPart; |
| 35 | +void uv_update_time(uv_loop_t* loop) { |
| 36 | + uint64_t new_time = uv__hrtime(UV__MILLISEC); |
| 37 | + if (new_time > loop->time) { |
| 38 | + loop->time = new_time; |
| 39 | + } |
61 | 40 | } |
62 | 41 |
|
63 | | - |
64 | 42 | void uv__time_forward(uv_loop_t* loop, uint64_t msecs) { |
65 | 43 | loop->time += msecs; |
66 | 44 | } |
@@ -191,16 +169,9 @@ DWORD uv__next_timeout(const uv_loop_t* loop) { |
191 | 169 | timer = RB_MIN(uv_timer_tree_s, &((uv_loop_t*)loop)->timers); |
192 | 170 | if (timer) { |
193 | 171 | delta = timer->due - loop->time; |
194 | | - if (delta >= UINT_MAX >> 1) { |
195 | | - /* A timeout value of UINT_MAX means infinite, so that's no good. But |
196 | | - * more importantly, there's always the risk that GetTickCount wraps. |
197 | | - * uv_update_time can detect this, but we must make sure that the |
198 | | - * tick counter never overflows twice between two subsequent |
199 | | - * uv_update_time calls. We do this by never sleeping more than half |
200 | | - * the time it takes to wrap the counter - which is huge overkill, |
201 | | - * but hey, it's not so bad to wake up every 25 days. |
202 | | - */ |
203 | | - return UINT_MAX >> 1; |
| 172 | + if (delta >= UINT_MAX - 1) { |
| 173 | + /* A timeout value of UINT_MAX means infinite, so that's no good. */ |
| 174 | + return UINT_MAX - 1; |
204 | 175 | } else if (delta < 0) { |
205 | 176 | /* Negative timeout values are not allowed */ |
206 | 177 | return 0; |
|
0 commit comments