by gngshn gngshn@gmail.com
从这里开始我将从linux的角度来看看libuv的工作原理, 如果您希望看到libuv跨平台的实现方式, 那你可能要失望了. 因为这一系列文章都将从linux的角度来写.
我们知道如果需要使用libuv的event loop需要通过uv_default_loop
或者uv_loop_init
来获得一个loop, 前者也是会调用后者的. 所以我们来看看uv_loop_init
到底做了些什么, 我将在source code中进行直接写注释来描述相关字段的含义.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| int uv_loop_init(uv_loop_t* loop) { void* saved_data; int err;
uv__signal_global_once_init();
saved_data = loop->data; memset(loop, 0, sizeof(*loop)); loop->data = saved_data;
heap_init((struct heap*) &loop->timer_heap); QUEUE_INIT(&loop->wq); QUEUE_INIT(&loop->active_reqs); QUEUE_INIT(&loop->idle_handles); QUEUE_INIT(&loop->async_handles); QUEUE_INIT(&loop->check_handles); QUEUE_INIT(&loop->prepare_handles); QUEUE_INIT(&loop->handle_queue);
loop->nfds = 0; loop->watchers = NULL; loop->nwatchers = 0; QUEUE_INIT(&loop->pending_queue); QUEUE_INIT(&loop->watcher_queue);
loop->closing_handles = NULL; uv__update_time(loop); loop->async_io_watcher.fd = -1; loop->async_wfd = -1; loop->signal_pipefd[0] = -1; loop->signal_pipefd[1] = -1; loop->backend_fd = -1; loop->emfile_fd = -1;
loop->timer_counter = 0; loop->stop_flag = 0;
err = uv__platform_loop_init(loop); if (err) return err; err = uv_signal_init(loop, &loop->child_watcher); if (err) goto fail_signal_init;
uv__handle_unref(&loop->child_watcher); loop->child_watcher.flags |= UV__HANDLE_INTERNAL; QUEUE_INIT(&loop->process_handles);
err = uv_rwlock_init(&loop->cloexec_lock); if (err) goto fail_rwlock_init;
err = uv_mutex_init(&loop->wq_mutex); if (err) goto fail_mutex_init;
err = uv_async_init(loop, &loop->wq_async, uv__work_done); if (err) goto fail_async_init;
uv__handle_unref(&loop->wq_async); loop->wq_async.flags |= UV__HANDLE_INTERNAL;
return 0;
fail_async_init: uv_mutex_destroy(&loop->wq_mutex);
fail_mutex_init: uv_rwlock_destroy(&loop->cloexec_lock);
fail_rwlock_init: uv__signal_loop_cleanup(loop);
fail_signal_init: uv__platform_loop_delete(loop);
return err; }
|
以上就是libuv的event loop的初始化流程, 后续我将分模块来讲解libuv各个功能模块的实现原理.