We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
本篇文章主要对uv_handle_t结构体进行展开介绍
uv_handle_t
直接从uv.h切入,很容易便能找到uv_handle_t结构体的声明:
typedef struct uv_handle_s uv_handle_t;
在uv_handle_s的定义中有如下注释:
uv_handle_s
/* The abstract base class of all handles. */ /* 所有句柄的抽象基类 */ struct uv_handle_s { UV_HANDLE_FIELDS };
意即uv_##name##_t全部继承自uv_handle_t,让我们详细展开UV_HANDLE_FIELDS一窥究竟:
uv_##name##_t
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; \ void* handle_queue[2]; \ union { \ int fd; \ void* reserved[4]; \ } u; \ UV_HANDLE_PRIVATE_FIELDS \
简单对内部的数据结构做下介绍:
UV_##NAME##
handle->type
UV_HANDLE_PRIVATE_FIELDS展开代码如下(unix):
UV_HANDLE_PRIVATE_FIELDS
#define UV_HANDLE_PRIVATE_FIELDS \ uv_handle_t* next_closing; \ unsigned int flags; \
loop->closing_handles
void uv__make_close_pending(uv_handle_t* handle) { assert(handle->flags & UV_HANDLE_CLOSING); assert(!(handle->flags & UV_HANDLE_CLOSED)); handle->next_closing = handle->loop->closing_handles; handle->loop->closing_handles = handle; }
在初始化时,会把handle->next_closing以及loop->closing_handles 全部置空。通过这段代码,可以清晰的看到在关闭句柄的时候,只需要判断handle->next_closing是否为null就可以得知所有句柄是否已经全部关闭。
handle->next_closing
在这里我们以uv_poll_s结构体为例,来解读一下libuv如何使用宏实现的类似继承的操作:
uv_poll_s
struct uv_poll_s { UV_HANDLE_FIELDS uv_poll_cb poll_cb; UV_POLL_PRIVATE_FIELDS };
在uv_poll_s数据结构中,第一个使用的宏便是UV_HANDLE_FIELDS宏,其次才为uv_poll_s的私有宏。所有的uv_##name##_t均包含了uv_handle_t的结构体变量,所以任何的uv_##name##_t都可以转换为uv_handle_t。
至此,整个uv_handle_t就介绍完了。
by 小菜
The text was updated successfully, but these errors were encountered:
No branches or pull requests
uv_handle_t声明
直接从uv.h切入,很容易便能找到
uv_handle_t
结构体的声明:uv_handle_s的定义
在
uv_handle_s
的定义中有如下注释:意即
uv_##name##_t
全部继承自uv_handle_t
,让我们详细展开UV_HANDLE_FIELDS
一窥究竟:简单对内部的数据结构做下介绍:
uv_##name##_t
强相关,对应的uv_##name##_t
的type为UV_##NAME##
。这种情况下可以通过handle->type
很容易判断出来uv_handle_t
子类的类型。uv_handle_t
的指针UV_HANDLE_PRIVATE_FIELDS
展开代码如下(unix):loop->closing_handles
形成一个链表结构,从而方便删除:在初始化时,会把
handle->next_closing
以及loop->closing_handles
全部置空。通过这段代码,可以清晰的看到在关闭句柄的时候,只需要判断handle->next_closing
是否为null就可以得知所有句柄是否已经全部关闭。继承的实现
在这里我们以
uv_poll_s
结构体为例,来解读一下libuv如何使用宏实现的类似继承的操作:在
uv_poll_s
数据结构中,第一个使用的宏便是UV_HANDLE_FIELDS
宏,其次才为uv_poll_s
的私有宏。所有的uv_##name##_t
均包含了uv_handle_t
的结构体变量,所以任何的uv_##name##_t
都可以转换为uv_handle_t
。至此,整个
uv_handle_t
就介绍完了。by 小菜
The text was updated successfully, but these errors were encountered: