当前位置:网站首页>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 .
边栏推荐
- 基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)
- 有软件负载均衡,也有硬件负载均衡,选择哪个?
- What is an R-value reference and what is the difference between it and an l-value?
- Global and Chinese market for annunciator panels 2022-2028: Research Report on technology, participants, trends, market size and share
- Redis之持久化实操(Linux版)
- Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
- Global and Chinese market of AVR series microcontrollers 2022-2028: Research Report on technology, participants, trends, market size and share
- Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)
- Go redis initialization connection
- QML type: locale, date
猜你喜欢

Advanced Computer Network Review(3)——BBR

Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges

Redis之发布订阅

I-BERT
![[shell script] - archive file script](/img/50/1bef6576902890dfd5771500414876.png)
[shell script] - archive file script

Servlet learning diary 7 -- servlet forwarding and redirection

Advance Computer Network Review(1)——FatTree

Advanced Computer Network Review(3)——BBR

Intel distiller Toolkit - Quantitative implementation 1

Advance Computer Network Review(1)——FatTree
随机推荐
CSP salary calculation
Advanced Computer Network Review(4)——Congestion Control of MPTCP
A convolution substitution of attention mechanism
基于B/S的影视创作论坛的设计与实现(附:源码 论文 sql文件 项目部署教程)
YARN组织架构
不同的数据驱动代码执行相同的测试场景
How to intercept the string correctly (for example, intercepting the stock in operation by applying the error information)
Multivariate cluster analysis
Redis之发布订阅
Master slave replication of redis
什么是MySQL?MySql的学习之路是怎样的
Kratos战神微服务框架(三)
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
Advanced Computer Network Review(4)——Congestion Control of MPTCP
面渣逆袭:Redis连环五十二问,图文详解,这下面试稳了
Mathematical modeling 2004b question (transmission problem)
[three storage methods of graph] just use adjacency matrix to go out
软件负载均衡和硬件负载均衡的选择
Heap (priority queue) topic
018.有效的回文