1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 #include "handle-inl.h"
36 /* The only event loop we support right now */
37 static uv_loop_t uv_default_loop_;
39 /* uv_once intialization guards */
40 static uv_once_t uv_init_guard_ = UV_ONCE_INIT;
41 static uv_once_t uv_default_loop_init_guard_ = UV_ONCE_INIT;
44 static void uv__crt_invalid_parameter_handler(const wchar_t* expression,
45 const wchar_t* function, const wchar_t * file, unsigned int line,
51 static void uv_init(void) {
52 /* Tell Windows that we will handle critical errors. */
53 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
54 SEM_NOOPENFILEERRORBOX);
56 /* Tell the CRT to not exit the application when an invalid parameter is */
57 /* passed. The main issue is that invalid FDs will trigger this behavior. */
58 #if !defined(__MINGW32__) || __MSVCRT_VERSION__ >= 0x800
59 _set_invalid_parameter_handler(uv__crt_invalid_parameter_handler);
62 /* Fetch winapi function pointers. This must be done first because other */
63 /* intialization code might need these function pointers to be loaded. */
66 /* Initialize winsock */
72 /* Initialize signal stuff */
75 /* Initialize console */
78 /* Initialize utilities */
83 static void uv_loop_init(uv_loop_t* loop) {
84 /* Create an I/O completion port */
85 loop->iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
86 if (loop->iocp == NULL) {
87 uv_fatal_error(GetLastError(), "CreateIoCompletionPort");
90 /* To prevent uninitialized memory access, loop->time must be intialized */
91 /* to zero before calling uv_update_time for the first time. */
93 loop->last_tick_count = 0;
96 QUEUE_INIT(&loop->handle_queue);
97 QUEUE_INIT(&loop->active_reqs);
98 loop->active_handles = 0;
100 loop->pending_reqs_tail = NULL;
102 loop->endgame_handles = NULL;
104 RB_INIT(&loop->timers);
106 loop->check_handles = NULL;
107 loop->prepare_handles = NULL;
108 loop->idle_handles = NULL;
110 loop->next_prepare_handle = NULL;
111 loop->next_check_handle = NULL;
112 loop->next_idle_handle = NULL;
114 memset(&loop->poll_peer_sockets, 0, sizeof loop->poll_peer_sockets);
116 loop->active_tcp_streams = 0;
117 loop->active_udp_streams = 0;
119 loop->timer_counter = 0;
122 loop->last_err = uv_ok_;
126 static void uv_default_loop_init(void) {
127 /* Initialize libuv itself first */
130 /* Initialize the main loop */
131 uv_loop_init(&uv_default_loop_);
135 void uv__once_init(void) {
136 uv_once(&uv_init_guard_, uv_init);
140 uv_loop_t* uv_default_loop(void) {
141 uv_once(&uv_default_loop_init_guard_, uv_default_loop_init);
142 return &uv_default_loop_;
146 uv_loop_t* uv_loop_new(void) {
149 /* Initialize libuv itself first */
152 loop = (uv_loop_t*)malloc(sizeof(uv_loop_t));
155 uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
163 void uv_loop_delete(uv_loop_t* loop) {
164 if (loop != &uv_default_loop_) {
166 for (i = 0; i < ARRAY_SIZE(loop->poll_peer_sockets); i++) {
167 SOCKET sock = loop->poll_peer_sockets[i];
168 if (sock != 0 && sock != INVALID_SOCKET) {
178 int uv_backend_fd(const uv_loop_t* loop) {
183 int uv_backend_timeout(const uv_loop_t* loop) {
188 static void uv_poll(uv_loop_t* loop, int block) {
189 DWORD bytes, timeout;
191 OVERLAPPED* overlapped;
195 timeout = uv_get_poll_timeout(loop);
200 GetQueuedCompletionStatus(loop->iocp,
207 /* Package was dequeued */
208 req = uv_overlapped_to_req(overlapped);
209 uv_insert_pending_req(loop, req);
210 } else if (GetLastError() != WAIT_TIMEOUT) {
212 uv_fatal_error(GetLastError(), "GetQueuedCompletionStatus");
214 /* We're sure that at least `timeout` milliseconds have expired, but */
215 /* this may not be reflected yet in the GetTickCount() return value. */
216 /* Therefore we ensure it's taken into account here. */
217 uv__time_forward(loop, timeout);
222 static void uv_poll_ex(uv_loop_t* loop, int block) {
226 OVERLAPPED_ENTRY overlappeds[128];
231 timeout = uv_get_poll_timeout(loop);
236 success = pGetQueuedCompletionStatusEx(loop->iocp,
238 ARRAY_SIZE(overlappeds),
244 for (i = 0; i < count; i++) {
245 /* Package was dequeued */
246 req = uv_overlapped_to_req(overlappeds[i].lpOverlapped);
247 uv_insert_pending_req(loop, req);
249 } else if (GetLastError() != WAIT_TIMEOUT) {
251 uv_fatal_error(GetLastError(), "GetQueuedCompletionStatusEx");
252 } else if (timeout > 0) {
253 /* We're sure that at least `timeout` milliseconds have expired, but */
254 /* this may not be reflected yet in the GetTickCount() return value. */
255 /* Therefore we ensure it's taken into account here. */
256 uv__time_forward(loop, timeout);
261 static int uv__loop_alive(uv_loop_t* loop) {
262 return loop->active_handles > 0 ||
263 !QUEUE_EMPTY(&loop->active_reqs) ||
264 loop->endgame_handles != NULL;
268 int uv_run(uv_loop_t *loop, uv_run_mode mode) {
270 void (*poll)(uv_loop_t* loop, int block);
272 if (pGetQueuedCompletionStatusEx)
277 if (!uv__loop_alive(loop))
280 r = uv__loop_alive(loop);
281 while (r != 0 && loop->stop_flag == 0) {
282 uv_update_time(loop);
283 uv_process_timers(loop);
285 /* Call idle callbacks if nothing to do. */
286 if (loop->pending_reqs_tail == NULL &&
287 loop->endgame_handles == NULL) {
288 uv_idle_invoke(loop);
291 uv_process_reqs(loop);
292 uv_process_endgames(loop);
294 uv_prepare_invoke(loop);
296 (*poll)(loop, loop->idle_handles == NULL &&
297 loop->pending_reqs_tail == NULL &&
298 loop->endgame_handles == NULL &&
300 (loop->active_handles > 0 ||
301 !QUEUE_EMPTY(&loop->active_reqs)) &&
302 !(mode & UV_RUN_NOWAIT));
304 uv_check_invoke(loop);
305 r = uv__loop_alive(loop);
306 if (mode & (UV_RUN_ONCE | UV_RUN_NOWAIT))
310 /* The if statement lets the compiler compile it to a conditional store.
311 * Avoids dirtying a cache line.
313 if (loop->stop_flag != 0)