当前位置:网站首页>Basic concepts of libuv
Basic concepts of libuv
2022-07-06 09:29:00 【What are you afraid of? The truth is infinite】
libuv It's a cross platform library , Is around event driven asynchrony I/O Model to design .
This library not only provides different I/O polling Abstraction of mechanism ,handles and streams The mechanism also provides a higher level for sockets And other entities . This library also provides cross platform files I/O Operation and thread function .
1) At the bottom of the socket Related operations are epoll/kqueue and event ports, These things are based on unix like Systematic , In these systems specific API A layer is abstracted above uv_io_t Structure is used to provide a unified unix-like On the platform socket And event interface .
2)epoll ->linux kernel ;kqueue->freeBSD 4.1 after ;event ports-> It seems that it is also used in linux The mechanism on .
3)IOCP by windows Unique asynchrony on I/O Event model ,libuv Also supportive . That is libuv Better than libev The place of .
4) Besides libuv It also supports thread pools .
5) Above the above underlying abstraction ,libuv Provides a higher level of abstraction , Provides Abstract TCP/UDP/TTY/PIPE Such as the interface .
6) Files are provided on top of these interfaces I/O operation ,DNS Operation etc. .
7) Users can implement their own applications based on all the above abstract interfaces .
Handle and request
libuv It provides users with two ways and event loop Working together , One is a handle (handle) One is to ask (request).
Handle represents a long-standing object , These objects can perform specific operations when they are active . for example : A preparation (prepare) When the handle is active, it can call its callback once in each loop . One TCP The handle of the server will call its connection callback function every time there is a new connection .
request (request) Generally, it represents short-term operation . These operations can be used on handles . Write requests are used to write data on the handle ; There are some exceptions , for instance getaddrinfo The request does not need a handle, but is executed directly in the loop .
I/O loop
I/O The cycle, or event cycle, is the whole libuv The core of .I/O The loop establishes all IO The execution environment of the operation ,I/O The loop will be bound to a thread . We can run multiple time cycles , As long as each one runs on a different thread .libuv The event loop No Thread safe , So all that contain event loops API And handles are not thread safe .
Event loops follow the most common single threaded asynchronous I/O Method : all I/O Or network operation in non blocking socket On the implementation , This socket Will use platform based group poll Mechanism : stay linux Upper use epoll, stay OSX And others BSD On the platform kqueue, stay sunOS Upper use event ports, stay windows Upper use IOCP. As part of the loop iteration , The loop will block to wait socket Upper I/O Activities , These activities have been added to socket In practice , Once these conditions are met , that socket Your state will change , So that the loop is no longer blocked , And the handle can also be read 、 Write and implement other expectations I/O operation .
In order to better understand how the event loop operation is carried out , The figure below shows all stages of a loop iteration .
Circulating “now” Is updated to the current time . The event loop caches the current number of time ticks at the beginning of the loop in order to reduce the number of time-related system calls .
If the loop is alive Of , Then it shows that an iteration has begun , Otherwise, the loop will exit immediately . that , When is a cycle considered alive What about ? The answer is if a loop includes active and referenced handles (active and ref`d handles), Active request or closing handle , Then this cycle is considered alive (alive).
Due The timer runs .
The pending callback is called .
Free (idle) Handle callback is called .
Get ready (prepare) The callback handle is called .
Calculation poll Overtime .
Circulation obstruction .
call check Handle .
call close Handle .
special case function .
One cycle iteration ends .
libuv Using thread pool technology makes asynchronous files I/O The operation is called possible , But for the Internet IO Can only be executed in a single thread , namely loop In the thread of .
边栏推荐
- Global and Chinese market of electronic tubes 2022-2028: Research Report on technology, participants, trends, market size and share
- What is MySQL? What is the learning path of MySQL
- Workflow - activiti7 environment setup
- What is an R-value reference and what is the difference between it and an l-value?
- [daily question] Porter (DFS / DP)
- Go redis initialization connection
- [shell script] use menu commands to build scripts for creating folders in the cluster
- 英雄联盟轮播图手动轮播
- QML control type: Popup
- Withdrawal of wechat applet (enterprise payment to change)
猜你喜欢
LeetCode41——First Missing Positive——hashing in place & swap
Design and implementation of online shopping system based on Web (attached: source code paper SQL file)
Redis geospatial
I-BERT
Improved deep embedded clustering with local structure preservation (Idec)
Redis cluster
Withdrawal of wechat applet (enterprise payment to change)
Workflow - activiti7 environment setup
Redis之Bitmap
Redis' performance indicators and monitoring methods
随机推荐
Redis之五大基础数据结构深入、应用场景
Global and Chinese markets for small seed seeders 2022-2028: Research Report on technology, participants, trends, market size and share
Post training quantification of bminf
[daily question] Porter (DFS / DP)
Full stack development of quartz distributed timed task scheduling cluster
不同的数据驱动代码执行相同的测试场景
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
为什么要数据分层
美团二面:为什么 Redis 会有哨兵?
Advanced Computer Network Review(5)——COPE
【shell脚本】——归档文件脚本
Detailed explanation of cookies and sessions
Redis之Geospatial
068.查找插入位置--二分查找
[Yu Yue education] reference materials of complex variable function and integral transformation of Shenyang University of Technology
英雄联盟轮播图自动轮播
AcWing 2456. Notepad
Global and Chinese market of AVR series microcontrollers 2022-2028: Research Report on technology, participants, trends, market size and share
Redis之Bitmap
LeetCode41——First Missing Positive——hashing in place & swap