Lines Matching refs:loop

93 static int uv__loops_add(uv_loop_t* loop) {
109 uv__loops[uv__loops_size] = loop;
121 static void uv__loops_remove(uv_loop_t* loop) {
129 if (uv__loops[loop_index] == loop)
132 /* If loop was not found, ignore */
167 uv_loop_t* loop;
171 loop = uv__loops[i];
172 assert(loop);
173 if (loop->iocp != INVALID_HANDLE_VALUE)
174 PostQueuedCompletionStatus(loop->iocp, 0, 0, NULL);
227 int uv_loop_init(uv_loop_t* loop) {
236 loop->iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
237 if (loop->iocp == NULL)
243 loop->internal_fields = lfields;
252 /* To prevent uninitialized memory access, loop->time must be initialized
255 loop->time = 0;
256 uv_update_time(loop);
258 uv__queue_init(&loop->wq);
259 uv__queue_init(&loop->handle_queue);
260 loop->active_reqs.count = 0;
261 loop->active_handles = 0;
263 loop->pending_reqs_tail = NULL;
265 loop->endgame_handles = NULL;
267 loop->timer_heap = timer_heap = uv__malloc(sizeof(*timer_heap));
275 loop->check_handles = NULL;
276 loop->prepare_handles = NULL;
277 loop->idle_handles = NULL;
279 loop->next_prepare_handle = NULL;
280 loop->next_check_handle = NULL;
281 loop->next_idle_handle = NULL;
283 memset(&loop->poll_peer_sockets, 0, sizeof loop->poll_peer_sockets);
285 loop->timer_counter = 0;
286 loop->stop_flag = 0;
288 err = uv_mutex_init(&loop->wq_mutex);
292 err = uv_async_init(loop, &loop->wq_async, uv__work_done);
296 uv__handle_unref(&loop->wq_async);
297 loop->wq_async.flags |= UV_HANDLE_INTERNAL;
299 err = uv__loops_add(loop);
303 loop->magic = UV_LOOP_MAGIC;
307 uv_mutex_destroy(&loop->wq_mutex);
311 loop->timer_heap = NULL;
318 loop->internal_fields = NULL;
319 CloseHandle(loop->iocp);
320 loop->iocp = INVALID_HANDLE_VALUE;
326 void uv_update_time(uv_loop_t* loop) {
328 assert(new_time >= loop->time);
329 loop->time = new_time;
338 void uv__loop_close(uv_loop_t* loop) {
342 uv__loops_remove(loop);
344 /* Close the async handle without needing an extra loop iteration.
350 loop->wq_async.async_sent = 0;
351 loop->wq_async.close_cb = NULL;
352 uv__handle_closing(&loop->wq_async);
353 uv__handle_close(&loop->wq_async);
355 for (i = 0; i < ARRAY_SIZE(loop->poll_peer_sockets); i++) {
356 SOCKET sock = loop->poll_peer_sockets[i];
361 uv_mutex_lock(&loop->wq_mutex);
362 assert(uv__queue_empty(&loop->wq) && "thread pool work queue not empty!");
363 assert(!uv__has_active_reqs(loop));
364 uv_mutex_unlock(&loop->wq_mutex);
365 uv_mutex_destroy(&loop->wq_mutex);
367 uv__free(loop->timer_heap);
368 loop->timer_heap = NULL;
370 lfields = uv__get_internal_fields(loop);
373 loop->internal_fields = NULL;
375 CloseHandle(loop->iocp);
376 loop->magic = ~UV_LOOP_MAGIC;
380 int uv__loop_configure(uv_loop_t* loop, uv_loop_option option, va_list ap) {
383 lfields = uv__get_internal_fields(loop);
393 int uv_backend_fd(const uv_loop_t* loop) {
398 int uv_loop_fork(uv_loop_t* loop) {
403 static int uv__loop_alive(const uv_loop_t* loop) {
404 return uv__has_active_handles(loop) ||
405 uv__has_active_reqs(loop) ||
406 loop->pending_reqs_tail != NULL ||
407 loop->endgame_handles != NULL;
411 int uv_loop_alive(const uv_loop_t* loop) {
412 return uv__loop_alive(loop);
416 int uv_backend_timeout(const uv_loop_t* loop) {
417 if (loop->stop_flag == 0 &&
418 /* uv__loop_alive(loop) && */
419 (uv__has_active_handles(loop) || uv__has_active_reqs(loop)) &&
420 loop->pending_reqs_tail == NULL &&
421 loop->idle_handles == NULL &&
422 loop->endgame_handles == NULL)
423 return uv__next_timeout(loop);
428 static void uv__poll_wine(uv_loop_t* loop, DWORD timeout) {
439 lfields = uv__get_internal_fields(loop);
440 timeout_time = loop->time + timeout;
452 * will return early if the loop isn't configured with UV_METRICS_IDLE_TIME.
455 uv__metrics_set_provider_entry_time(loop);
463 GetQueuedCompletionStatus(loop->iocp,
471 uv__metrics_inc_events_waiting(loop, 1);
476 /* Placed here because on success the loop will break whether there is an
478 * the timeout will be updated and the loop will run again. In either case
481 uv__metrics_update_idle_time(loop);
484 uv__metrics_inc_events(loop, 1);
488 uv__insert_pending_req(loop, req);
491 * so update the loop time here.
493 uv_update_time(loop);
501 uv_update_time(loop);
502 if (timeout_time > loop->time) {
503 timeout = (DWORD)(timeout_time - loop->time);
507 * loop cannot happen, the timeout is increased exponentially
519 static void uv__poll(uv_loop_t* loop, DWORD timeout) {
532 lfields = uv__get_internal_fields(loop);
533 timeout_time = loop->time + timeout;
547 * will return early if the loop isn't configured with UV_METRICS_IDLE_TIME.
550 uv__metrics_set_provider_entry_time(loop);
558 success = pGetQueuedCompletionStatusEx(loop->iocp,
570 /* Placed here because on success the loop will break whether there is an
572 * then the timeout will be updated and the loop will run again. In either
575 uv__metrics_update_idle_time(loop);
583 uv__metrics_inc_events(loop, 1);
585 uv__metrics_inc_events_waiting(loop, 1);
588 uv__insert_pending_req(loop, req);
593 * so update the loop time here.
595 uv_update_time(loop);
603 uv_update_time(loop);
604 if (timeout_time > loop->time) {
605 timeout = (DWORD)(timeout_time - loop->time);
609 * loop cannot happen, the timeout is increased exponentially
621 int uv_run(uv_loop_t *loop, uv_run_mode mode) {
626 r = uv__loop_alive(loop);
628 uv_update_time(loop);
631 * while loop for UV_RUN_DEFAULT. Otherwise timers only need to be executed
633 * execution order of the conceptual event loop. */
634 if (mode == UV_RUN_DEFAULT && r != 0 && loop->stop_flag == 0) {
635 uv_update_time(loop);
636 uv__run_timers(loop);
639 while (r != 0 && loop->stop_flag == 0) {
640 can_sleep = loop->pending_reqs_tail == NULL && loop->idle_handles == NULL;
642 uv__process_reqs(loop);
643 uv__idle_invoke(loop);
644 uv__prepare_invoke(loop);
648 timeout = uv_backend_timeout(loop);
650 uv__metrics_inc_loop_count(loop);
653 uv__poll(loop, timeout);
655 uv__poll_wine(loop, timeout);
658 * times to avoid loop starvation.*/
659 for (r = 0; r < 8 && loop->pending_reqs_tail != NULL; r++)
660 uv__process_reqs(loop);
667 uv__metrics_update_idle_time(loop);
669 uv__check_invoke(loop);
670 uv__process_endgames(loop);
672 uv_update_time(loop);
673 uv__run_timers(loop);
675 r = uv__loop_alive(loop);
683 if (loop->stop_flag != 0)
684 loop->stop_flag = 0;