当前位置:网站首页>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 .
边栏推荐
- QDialog
- 英雄联盟轮播图手动轮播
- MapReduce工作机制
- Pytest parameterization some tips you don't know / pytest you don't know
- Advance Computer Network Review(1)——FatTree
- Activiti7工作流的使用
- Advanced Computer Network Review(3)——BBR
- Master slave replication of redis
- Redis之Geospatial
- The order of include header files and the difference between double quotation marks "and angle brackets < >
猜你喜欢
postman之参数化详解
Workflow - activiti7 environment setup
Reids之删除策略
QML type: locale, date
Activiti7工作流的使用
Selenium+pytest automated test framework practice
Lua script of redis
Advanced Computer Network Review(3)——BBR
[oc]- < getting started with UI> -- common controls - prompt dialog box and wait for the prompt (circle)
Servlet learning diary 8 - servlet life cycle and thread safety
随机推荐
[oc foundation framework] - < copy object copy >
What is MySQL? What is the learning path of MySQL
leetcode-14. Longest common prefix JS longitudinal scanning method
软件负载均衡和硬件负载均衡的选择
Redis分布式锁实现Redisson 15问
Redis cluster
基于B/S的影视创作论坛的设计与实现(附:源码 论文 sql文件 项目部署教程)
Redis core configuration
运维,放过监控-也放过自己吧
Mathematical modeling 2004b question (transmission problem)
Servlet learning diary 8 - servlet life cycle and thread safety
基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
Meituan Er Mian: why does redis have sentinels?
Global and Chinese market of bank smart cards 2022-2028: Research Report on technology, participants, trends, market size and share
[daily question] Porter (DFS / DP)
[three storage methods of graph] just use adjacency matrix to go out
Global and Chinese market of capacitive displacement sensors 2022-2028: Research Report on technology, participants, trends, market size and share
【shell脚本】——归档文件脚本
Kratos ares microservice framework (II)
Advance Computer Network Review(1)——FatTree