struct uv_loop_s {
/* User data - use this for whatever. */
void* data;
/* Loop reference counting. */ //循环引用计数
unsigned int active_handles; /*活跃的handles个数*/
void* handle_queue[2]; /*handle双向环形队列头部标记*/
union {
void* unused;
unsigned int count; /*request个数,主要用于文件操作*/
} active_reqs;
/* Internal storage for future extensions. */ //用于扩展的内部存储
void* internal_fields;
/* Internal flag to signal loop stop. */
unsigned int stop_flag; //事件循环是否结束的标记。结束为1,否则为0
UV_LOOP_PRIVATE_FIELDS
};
#define UV_LOOP_PRIVATE_FIELDS \
/* The loop's I/O completion port I/O完成端口 */ \
HANDLE iocp; \
/* The current time according to the event loop. in msecs.事件循环的当前时间。以毫秒为单位 */ \
uint64_t time; \
/* Tail of a single-linked circular queue of pending reqs. If the queue */ \
/* is empty, tail_ is NULL. If there is only one item, */ \
/* tail_->next_req == tail_ */ \
uv_req_t* pending_reqs_tail; /*pending阶段单向环形队列队尾,如果队列为空,那么tail_是NULL,如果只有一个节点,那么tail_->next = tail_,由于是环形队列,所以只需记录队尾即可*/ \
/* Head of a single-linked list of closed handles */ \
uv_handle_t* endgame_handles; /*closing阶段单向队列队头,由uv_close产生*/ \
/* TODO(bnoordhuis) Stop heap-allocating |timer_heap| in libuv v2.x. */ \
void* timer_heap; /*保存定时器的小顶堆结构*/ \
/* Lists of active loop (prepare / check / idle) watchers */ \
uv_prepare_t* prepare_handles; \
uv_check_t* check_handles; \
uv_idle_t* idle_handles; \
/* This pointer will refer to the prepare/check/idle handle whose */ \
/* callback is scheduled to be called next. This is needed to allow */ \
/* safe removal from one of the lists above while that list being */ \
/* iterated over. 下面的指针将引用prepare/check/idle句柄,该句柄的回调将在*/ \
/*下一次循环时调用,但是需要在上述列表中安全的移除 */ \
uv_prepare_t* next_prepare_handle; \
uv_check_t* next_check_handle; \
uv_idle_t* next_idle_handle; \
/* This handle holds the peer sockets for the fast variant of uv_poll_t */ \
/* 此句柄用于保存uv_poll_t的快速变化的对等套接字*/ \
/* 轮询句柄用于监视文件描述符的可读性、可写性和断开连接,类似于poll(2)的目的。*/ \
SOCKET poll_peer_sockets[UV_MSAFD_PROVIDER_COUNT]; \
/* Counter to keep track of active tcp streams */ \
/* 跟踪活动tcp流的计数器 */ \
unsigned int active_tcp_streams; \
/* Counter to keep track of active udp streams */ \
/* 跟踪活动udp流的计数器 */ \
unsigned int active_udp_streams; \
/* Counter to started timer */ \
/* 管理定时器节点的ID,不断叠加 */ \
uint64_t timer_counter; \
/* Threadpool */ \
void* wq[2]; /*线程池的线程处理完任务后把对应的结构体插入到wq双向环形队列中*/ \
uv_mutex_t wq_mutex; /*互斥量,控制wq队列的互斥访问*/ \
uv_async_t wq_async; /*用于线程池和主线程通信*/
/* The abstract base class of all handles. */ //所有句柄的抽象基类,即所有事件类型都继承uv_handle_s
struct uv_handle_s {
UV_HANDLE_FIELDS
};
#define UV_HANDLE_FIELDS \
/* public */ \
void* data; /*用户数据*/ \
/* read-only 仅读*/ \
uv_loop_t* loop; \
uv_handle_type type; /*句柄类型*/ \
/* private */ \
uv_close_cb close_cb; /*句柄调用uv_close时的需要注册的close回调*/ \
void* handle_queue[2]; /*双向环形队列节点标记*/ \
union { \
int fd; /*文件描述符,绑定真实资源的索引*/ \
void* reserved[4]; \
} u; \
UV_HANDLE_PRIVATE_FIELDS \
#define UV_HANDLE_PRIVATE_FIELDS \
uv_handle_t* endgame_next; /*用于关联close阶段队列*/ \
unsigned int flags; /*handle的状态和标记*/
//持续请求用来做不同事件循环中(线程间通信)通信的,唯一一个有线程安全的模块,其他子模块
//也使用它,比较重要
struct uv_async_s {
UV_HANDLE_FIELDS
UV_ASYNC_PRIVATE_FIELDS
};
#define UV_ASYNC_PRIVATE_FIELDS \
struct uv_req_s async_req; \
uv_async_cb async_cb; /*异步事件触发时执行回调*/ \
/* char to avoid alignment issues */ \
char volatile async_sent; /*该标记位为1表示对应的事件已触发,可以理解为锁*/