xref: /DragonOS/kernel/src/net/event_poll/mod.rs (revision 2eab6dd743e94a86a685f1f3c01e599adf86610a)
140609970SGnoCiYeH use core::{
240609970SGnoCiYeH     fmt::Debug,
340609970SGnoCiYeH     sync::atomic::{AtomicBool, Ordering},
440609970SGnoCiYeH };
540609970SGnoCiYeH 
640609970SGnoCiYeH use alloc::{
740609970SGnoCiYeH     collections::LinkedList,
840609970SGnoCiYeH     sync::{Arc, Weak},
940609970SGnoCiYeH     vec::Vec,
1040609970SGnoCiYeH };
1191e9d4abSLoGin use system_error::SystemError;
1240609970SGnoCiYeH 
1340609970SGnoCiYeH use crate::{
1440609970SGnoCiYeH     filesystem::vfs::{
1540609970SGnoCiYeH         file::{File, FileMode},
1640609970SGnoCiYeH         FilePrivateData, IndexNode, Metadata,
1740609970SGnoCiYeH     },
1840609970SGnoCiYeH     include::bindings::bindings::INT32_MAX,
1940609970SGnoCiYeH     libs::{
2040609970SGnoCiYeH         rbtree::RBTree,
2140609970SGnoCiYeH         rwlock::RwLock,
2240609970SGnoCiYeH         spinlock::{SpinLock, SpinLockGuard},
2340609970SGnoCiYeH         wait_queue::WaitQueue,
2440609970SGnoCiYeH     },
2540609970SGnoCiYeH     process::ProcessManager,
26f0c87a89SGnoCiYeH     sched::{schedule, SchedMode},
2740609970SGnoCiYeH     time::{
2840609970SGnoCiYeH         timer::{next_n_us_timer_jiffies, Timer, WakeUpHelper},
296fc066acSJomo         PosixTimeSpec,
3040609970SGnoCiYeH     },
3140609970SGnoCiYeH };
3240609970SGnoCiYeH 
3340609970SGnoCiYeH pub mod syscall;
3440609970SGnoCiYeH 
3540609970SGnoCiYeH #[derive(Debug, Clone)]
3640609970SGnoCiYeH pub struct LockedEventPoll(Arc<SpinLock<EventPoll>>);
3740609970SGnoCiYeH 
3840609970SGnoCiYeH /// 内核的Epoll对象结构体,当用户创建一个Epoll时,内核就会创建一个该类型对象
3940609970SGnoCiYeH /// 它对应一个epfd
4040609970SGnoCiYeH #[derive(Debug)]
4140609970SGnoCiYeH pub struct EventPoll {
4240609970SGnoCiYeH     /// epoll_wait用到的等待队列
4340609970SGnoCiYeH     epoll_wq: WaitQueue,
4440609970SGnoCiYeH     /// 维护所有添加进来的socket的红黑树
4540609970SGnoCiYeH     ep_items: RBTree<i32, Arc<EPollItem>>,
4640609970SGnoCiYeH     /// 接收就绪的描述符列表
4740609970SGnoCiYeH     ready_list: LinkedList<Arc<EPollItem>>,
4840609970SGnoCiYeH     /// 是否已经关闭
4940609970SGnoCiYeH     shutdown: AtomicBool,
5040609970SGnoCiYeH     self_ref: Option<Weak<SpinLock<EventPoll>>>,
5140609970SGnoCiYeH }
5240609970SGnoCiYeH 
5340609970SGnoCiYeH impl EventPoll {
5440609970SGnoCiYeH     pub const EP_MAX_EVENTS: u32 = INT32_MAX / (core::mem::size_of::<EPollEvent>() as u32);
5552bcb59eSGnoCiYeH     /// 用于获取inode中的epitem队列
5652bcb59eSGnoCiYeH     pub const ADD_EPOLLITEM: u32 = 0x7965;
5740609970SGnoCiYeH     pub fn new() -> Self {
5840609970SGnoCiYeH         Self {
59b5b571e0SLoGin             epoll_wq: WaitQueue::default(),
6040609970SGnoCiYeH             ep_items: RBTree::new(),
6140609970SGnoCiYeH             ready_list: LinkedList::new(),
6240609970SGnoCiYeH             shutdown: AtomicBool::new(false),
6340609970SGnoCiYeH             self_ref: None,
6440609970SGnoCiYeH         }
6540609970SGnoCiYeH     }
6640609970SGnoCiYeH }
6740609970SGnoCiYeH 
68b5b571e0SLoGin impl Default for EventPoll {
69b5b571e0SLoGin     fn default() -> Self {
70b5b571e0SLoGin         Self::new()
71b5b571e0SLoGin     }
72b5b571e0SLoGin }
73b5b571e0SLoGin 
7440609970SGnoCiYeH /// EpollItem表示的是Epoll所真正管理的对象
7540609970SGnoCiYeH /// 每当用户向Epoll添加描述符时都会注册一个新的EpollItem,EpollItem携带了一些被监听的描述符的必要信息
7640609970SGnoCiYeH #[derive(Debug)]
7740609970SGnoCiYeH pub struct EPollItem {
7840609970SGnoCiYeH     /// 对应的Epoll
7940609970SGnoCiYeH     epoll: Weak<SpinLock<EventPoll>>,
8040609970SGnoCiYeH     /// 用户注册的事件
8140609970SGnoCiYeH     event: RwLock<EPollEvent>,
8240609970SGnoCiYeH     /// 监听的描述符
8340609970SGnoCiYeH     fd: i32,
8440609970SGnoCiYeH     /// 对应的文件
85dfe53cf0SGnoCiYeH     file: Weak<File>,
8640609970SGnoCiYeH }
8740609970SGnoCiYeH 
8840609970SGnoCiYeH impl EPollItem {
8940609970SGnoCiYeH     pub fn new(
9040609970SGnoCiYeH         epoll: Weak<SpinLock<EventPoll>>,
9140609970SGnoCiYeH         events: EPollEvent,
9240609970SGnoCiYeH         fd: i32,
93dfe53cf0SGnoCiYeH         file: Weak<File>,
9440609970SGnoCiYeH     ) -> Self {
9540609970SGnoCiYeH         Self {
9640609970SGnoCiYeH             epoll,
9740609970SGnoCiYeH             event: RwLock::new(events),
9840609970SGnoCiYeH             fd,
9940609970SGnoCiYeH             file,
10040609970SGnoCiYeH         }
10140609970SGnoCiYeH     }
10240609970SGnoCiYeH 
10340609970SGnoCiYeH     pub fn epoll(&self) -> Weak<SpinLock<EventPoll>> {
10440609970SGnoCiYeH         self.epoll.clone()
10540609970SGnoCiYeH     }
10640609970SGnoCiYeH 
10740609970SGnoCiYeH     pub fn event(&self) -> &RwLock<EPollEvent> {
10840609970SGnoCiYeH         &self.event
10940609970SGnoCiYeH     }
11040609970SGnoCiYeH 
111dfe53cf0SGnoCiYeH     pub fn file(&self) -> Weak<File> {
11240609970SGnoCiYeH         self.file.clone()
11340609970SGnoCiYeH     }
11440609970SGnoCiYeH 
11540609970SGnoCiYeH     pub fn fd(&self) -> i32 {
11640609970SGnoCiYeH         self.fd
11740609970SGnoCiYeH     }
11840609970SGnoCiYeH 
11940609970SGnoCiYeH     /// ## 通过epoll_item来执行绑定文件的poll方法,并获取到感兴趣的事件
12040609970SGnoCiYeH     fn ep_item_poll(&self) -> EPollEventType {
12140609970SGnoCiYeH         let file = self.file.upgrade();
12240609970SGnoCiYeH         if file.is_none() {
12340609970SGnoCiYeH             return EPollEventType::empty();
12440609970SGnoCiYeH         }
125dfe53cf0SGnoCiYeH         if let Ok(events) = file.unwrap().poll() {
12640609970SGnoCiYeH             let events = events as u32 & self.event.read().events;
12740609970SGnoCiYeH             return EPollEventType::from_bits_truncate(events);
12840609970SGnoCiYeH         }
12940609970SGnoCiYeH         return EPollEventType::empty();
13040609970SGnoCiYeH     }
13140609970SGnoCiYeH }
13240609970SGnoCiYeH 
13340609970SGnoCiYeH /// ### Epoll文件的私有信息
13440609970SGnoCiYeH #[derive(Debug, Clone)]
13540609970SGnoCiYeH pub struct EPollPrivateData {
13640609970SGnoCiYeH     epoll: LockedEventPoll,
13740609970SGnoCiYeH }
13840609970SGnoCiYeH 
13940609970SGnoCiYeH /// ### 该结构体将Epoll加入文件系统
14040609970SGnoCiYeH #[derive(Debug)]
14140609970SGnoCiYeH pub struct EPollInode {
14240609970SGnoCiYeH     epoll: LockedEventPoll,
14340609970SGnoCiYeH }
14440609970SGnoCiYeH 
14540609970SGnoCiYeH impl EPollInode {
14640609970SGnoCiYeH     pub fn new(epoll: LockedEventPoll) -> Arc<Self> {
14740609970SGnoCiYeH         Arc::new(Self { epoll })
14840609970SGnoCiYeH     }
14940609970SGnoCiYeH }
15040609970SGnoCiYeH 
15140609970SGnoCiYeH impl IndexNode for EPollInode {
15240609970SGnoCiYeH     fn read_at(
15340609970SGnoCiYeH         &self,
15440609970SGnoCiYeH         _offset: usize,
15540609970SGnoCiYeH         _len: usize,
15640609970SGnoCiYeH         _buf: &mut [u8],
157dfe53cf0SGnoCiYeH         _data: SpinLockGuard<FilePrivateData>,
15891e9d4abSLoGin     ) -> Result<usize, SystemError> {
15940609970SGnoCiYeH         Err(SystemError::ENOSYS)
16040609970SGnoCiYeH     }
16140609970SGnoCiYeH 
16240609970SGnoCiYeH     fn write_at(
16340609970SGnoCiYeH         &self,
16440609970SGnoCiYeH         _offset: usize,
16540609970SGnoCiYeH         _len: usize,
16640609970SGnoCiYeH         _buf: &[u8],
167dfe53cf0SGnoCiYeH         _data: SpinLockGuard<FilePrivateData>,
16891e9d4abSLoGin     ) -> Result<usize, SystemError> {
16940609970SGnoCiYeH         Err(SystemError::ENOSYS)
17040609970SGnoCiYeH     }
17140609970SGnoCiYeH 
1725e948c56SGnoCiYeH     fn poll(&self, _private_data: &FilePrivateData) -> Result<usize, SystemError> {
17340609970SGnoCiYeH         // 需要实现epoll嵌套epoll时,需要实现这里
17440609970SGnoCiYeH         todo!()
17540609970SGnoCiYeH     }
17640609970SGnoCiYeH 
17740609970SGnoCiYeH     fn fs(&self) -> Arc<dyn crate::filesystem::vfs::FileSystem> {
17840609970SGnoCiYeH         todo!()
17940609970SGnoCiYeH     }
18040609970SGnoCiYeH 
18140609970SGnoCiYeH     fn as_any_ref(&self) -> &dyn core::any::Any {
18240609970SGnoCiYeH         self
18340609970SGnoCiYeH     }
18440609970SGnoCiYeH 
18591e9d4abSLoGin     fn list(&self) -> Result<Vec<alloc::string::String>, SystemError> {
18640609970SGnoCiYeH         Err(SystemError::ENOSYS)
18740609970SGnoCiYeH     }
18840609970SGnoCiYeH 
18940609970SGnoCiYeH     fn metadata(&self) -> Result<Metadata, SystemError> {
19040609970SGnoCiYeH         Ok(Metadata::default())
19140609970SGnoCiYeH     }
19240609970SGnoCiYeH 
193dfe53cf0SGnoCiYeH     fn close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError> {
19440609970SGnoCiYeH         // 释放资源
19540609970SGnoCiYeH         let mut epoll = self.epoll.0.lock_irqsave();
19640609970SGnoCiYeH 
19740609970SGnoCiYeH         // 唤醒epoll上面等待的所有进程
19840609970SGnoCiYeH         epoll.shutdown.store(true, Ordering::SeqCst);
19940609970SGnoCiYeH         epoll.ep_wake_all();
20040609970SGnoCiYeH 
20140609970SGnoCiYeH         let fds = epoll.ep_items.keys().cloned().collect::<Vec<_>>();
20240609970SGnoCiYeH 
20340609970SGnoCiYeH         // 清理红黑树里面的epitems
20440609970SGnoCiYeH         for fd in fds {
20540609970SGnoCiYeH             let file = ProcessManager::current_pcb()
20640609970SGnoCiYeH                 .fd_table()
20740609970SGnoCiYeH                 .read()
20840609970SGnoCiYeH                 .get_file_by_fd(fd);
20940609970SGnoCiYeH 
21040609970SGnoCiYeH             if file.is_some() {
211dfe53cf0SGnoCiYeH                 file.unwrap().remove_epoll(&Arc::downgrade(&self.epoll.0))?;
21240609970SGnoCiYeH             }
21340609970SGnoCiYeH 
21440609970SGnoCiYeH             epoll.ep_items.remove(&fd);
21540609970SGnoCiYeH         }
21640609970SGnoCiYeH 
21740609970SGnoCiYeH         Ok(())
21840609970SGnoCiYeH     }
21940609970SGnoCiYeH 
220dfe53cf0SGnoCiYeH     fn open(
221dfe53cf0SGnoCiYeH         &self,
222dfe53cf0SGnoCiYeH         _data: SpinLockGuard<FilePrivateData>,
223dfe53cf0SGnoCiYeH         _mode: &FileMode,
224dfe53cf0SGnoCiYeH     ) -> Result<(), SystemError> {
22540609970SGnoCiYeH         Ok(())
22640609970SGnoCiYeH     }
22740609970SGnoCiYeH }
22840609970SGnoCiYeH 
22940609970SGnoCiYeH impl EventPoll {
23040609970SGnoCiYeH     /// ## 创建epoll对象
23140609970SGnoCiYeH     ///
23240609970SGnoCiYeH     /// ### 参数
23340609970SGnoCiYeH     /// - flags: 创建的epoll文件的FileMode
23440609970SGnoCiYeH     ///
23540609970SGnoCiYeH     /// ### 返回值
23640609970SGnoCiYeH     /// - 成功则返回Ok(fd),否则返回Err
23740609970SGnoCiYeH     pub fn do_create_epoll(flags: FileMode) -> Result<usize, SystemError> {
23840609970SGnoCiYeH         if !flags.difference(FileMode::O_CLOEXEC).is_empty() {
23940609970SGnoCiYeH             return Err(SystemError::EINVAL);
24040609970SGnoCiYeH         }
24140609970SGnoCiYeH 
24240609970SGnoCiYeH         // 创建epoll
24340609970SGnoCiYeH         let epoll = LockedEventPoll(Arc::new(SpinLock::new(EventPoll::new())));
24440609970SGnoCiYeH         epoll.0.lock_irqsave().self_ref = Some(Arc::downgrade(&epoll.0));
24540609970SGnoCiYeH 
24640609970SGnoCiYeH         // 创建epoll的inode对象
24740609970SGnoCiYeH         let epoll_inode = EPollInode::new(epoll.clone());
24840609970SGnoCiYeH 
24940609970SGnoCiYeH         let mut ep_file = File::new(
25040609970SGnoCiYeH             epoll_inode,
25140609970SGnoCiYeH             FileMode::O_RDWR | (flags & FileMode::O_CLOEXEC),
25240609970SGnoCiYeH         )?;
25340609970SGnoCiYeH 
25440609970SGnoCiYeH         // 设置ep_file的FilePrivateData
255dfe53cf0SGnoCiYeH         ep_file.private_data = SpinLock::new(FilePrivateData::EPoll(EPollPrivateData { epoll }));
25640609970SGnoCiYeH 
25740609970SGnoCiYeH         let current_pcb = ProcessManager::current_pcb();
25840609970SGnoCiYeH         let fd_table = current_pcb.fd_table();
25940609970SGnoCiYeH         let mut fd_table_guard = fd_table.write();
26040609970SGnoCiYeH 
26140609970SGnoCiYeH         let fd = fd_table_guard.alloc_fd(ep_file, None)?;
26240609970SGnoCiYeH 
26340609970SGnoCiYeH         Ok(fd as usize)
26440609970SGnoCiYeH     }
26540609970SGnoCiYeH 
26640609970SGnoCiYeH     /// ## epoll_ctl的具体实现
26740609970SGnoCiYeH     ///
26840609970SGnoCiYeH     /// 根据不同的op对epoll文件进行增删改
26940609970SGnoCiYeH     ///
27040609970SGnoCiYeH     /// ### 参数
27140609970SGnoCiYeH     /// - epfd: 操作的epoll文件描述符
27240609970SGnoCiYeH     /// - op: 对应的操作
27340609970SGnoCiYeH     /// - fd: 操作对应的文件描述符
27440609970SGnoCiYeH     /// - epds: 从用户态传入的event,若op为EpollCtlAdd,则对应注册的监听事件,若op为EPollCtlMod,则对应更新的事件,删除操作不涉及此字段
27540609970SGnoCiYeH     /// - nonblock: 定义这次操作是否为非阻塞(有可能其他地方占有EPoll的锁)
27640609970SGnoCiYeH     pub fn do_epoll_ctl(
27740609970SGnoCiYeH         epfd: i32,
27840609970SGnoCiYeH         op: EPollCtlOption,
27940609970SGnoCiYeH         fd: i32,
28040609970SGnoCiYeH         epds: &mut EPollEvent,
28140609970SGnoCiYeH         nonblock: bool,
28240609970SGnoCiYeH     ) -> Result<usize, SystemError> {
28340609970SGnoCiYeH         let current_pcb = ProcessManager::current_pcb();
28440609970SGnoCiYeH         let fd_table = current_pcb.fd_table();
28540609970SGnoCiYeH         let fd_table_guard = fd_table.read();
28640609970SGnoCiYeH 
28740609970SGnoCiYeH         // 获取epoll和对应fd指向的文件
28840609970SGnoCiYeH         let ep_file = fd_table_guard
28940609970SGnoCiYeH             .get_file_by_fd(epfd)
29040609970SGnoCiYeH             .ok_or(SystemError::EBADF)?;
29140609970SGnoCiYeH         let dst_file = fd_table_guard
29240609970SGnoCiYeH             .get_file_by_fd(fd)
29340609970SGnoCiYeH             .ok_or(SystemError::EBADF)?;
29440609970SGnoCiYeH 
29540609970SGnoCiYeH         // 检查是否允许 EPOLLWAKEUP
296b5b571e0SLoGin         if op != EPollCtlOption::Del {
29740609970SGnoCiYeH             epds.events &= !EPollEventType::EPOLLWAKEUP.bits();
29840609970SGnoCiYeH         }
29940609970SGnoCiYeH 
30040609970SGnoCiYeH         let events = EPollEventType::from_bits_truncate(epds.events);
30140609970SGnoCiYeH 
30240609970SGnoCiYeH         // 检查获取到的两个文件的正确性
30340609970SGnoCiYeH         // 首先是不能自己嵌套自己
30440609970SGnoCiYeH         // 然后ep_file必须是epoll文件
30540609970SGnoCiYeH         if Arc::ptr_eq(&ep_file, &dst_file) || !Self::is_epoll_file(&ep_file) {
30640609970SGnoCiYeH             return Err(SystemError::EINVAL);
30740609970SGnoCiYeH         }
30840609970SGnoCiYeH 
309b5b571e0SLoGin         if op != EPollCtlOption::Del && events.contains(EPollEventType::EPOLLEXCLUSIVE) {
31040609970SGnoCiYeH             // epoll独占模式下不允许EpollCtlMod
311b5b571e0SLoGin             if op == EPollCtlOption::Mod {
31240609970SGnoCiYeH                 return Err(SystemError::EINVAL);
31340609970SGnoCiYeH             }
31440609970SGnoCiYeH 
31540609970SGnoCiYeH             // 不支持嵌套的独占唤醒
316b5b571e0SLoGin             if op == EPollCtlOption::Add && Self::is_epoll_file(&dst_file)
31740609970SGnoCiYeH                 || !events
31840609970SGnoCiYeH                     .difference(EPollEventType::EPOLLEXCLUSIVE_OK_BITS)
31940609970SGnoCiYeH                     .is_empty()
32040609970SGnoCiYeH             {
32140609970SGnoCiYeH                 return Err(SystemError::EINVAL);
32240609970SGnoCiYeH             }
32340609970SGnoCiYeH         }
32440609970SGnoCiYeH 
32540609970SGnoCiYeH         // 从FilePrivateData获取到epoll
326dfe53cf0SGnoCiYeH         if let FilePrivateData::EPoll(epoll_data) = &*ep_file.private_data.lock() {
32740609970SGnoCiYeH             let mut epoll_guard = {
32840609970SGnoCiYeH                 if nonblock {
32940609970SGnoCiYeH                     // 如果设置非阻塞,则尝试获取一次锁
33040609970SGnoCiYeH                     if let Ok(guard) = epoll_data.epoll.0.try_lock_irqsave() {
33140609970SGnoCiYeH                         guard
33240609970SGnoCiYeH                     } else {
33340609970SGnoCiYeH                         return Err(SystemError::EAGAIN_OR_EWOULDBLOCK);
33440609970SGnoCiYeH                     }
33540609970SGnoCiYeH                 } else {
33640609970SGnoCiYeH                     epoll_data.epoll.0.lock_irqsave()
33740609970SGnoCiYeH                 }
33840609970SGnoCiYeH             };
33940609970SGnoCiYeH 
340b5b571e0SLoGin             if op == EPollCtlOption::Add {
34140609970SGnoCiYeH                 // TODO: 循环检查是否为epoll嵌套epoll的情况,如果是则需要检测其深度
34240609970SGnoCiYeH                 // 这里是需要一种检测算法的,但是目前未考虑epoll嵌套epoll的情况,所以暂时未实现
34340609970SGnoCiYeH                 // Linux算法:https://code.dragonos.org.cn/xref/linux-6.1.9/fs/eventpoll.c?r=&mo=56953&fi=2057#2133
34440609970SGnoCiYeH                 if Self::is_epoll_file(&dst_file) {
34540609970SGnoCiYeH                     todo!();
34640609970SGnoCiYeH                 }
34740609970SGnoCiYeH             }
34840609970SGnoCiYeH 
34940609970SGnoCiYeH             let ep_item = epoll_guard.ep_items.get(&fd);
35040609970SGnoCiYeH             match op {
351b5b571e0SLoGin                 EPollCtlOption::Add => {
35240609970SGnoCiYeH                     // 如果已经存在,则返回错误
35340609970SGnoCiYeH                     if ep_item.is_some() {
35440609970SGnoCiYeH                         return Err(SystemError::EEXIST);
35540609970SGnoCiYeH                     }
35640609970SGnoCiYeH                     // 设置epoll
35740609970SGnoCiYeH                     let epitem = Arc::new(EPollItem::new(
35840609970SGnoCiYeH                         Arc::downgrade(&epoll_data.epoll.0),
35940609970SGnoCiYeH                         *epds,
36040609970SGnoCiYeH                         fd,
36140609970SGnoCiYeH                         Arc::downgrade(&dst_file),
36240609970SGnoCiYeH                     ));
36340609970SGnoCiYeH                     Self::ep_insert(&mut epoll_guard, dst_file, epitem)?;
36440609970SGnoCiYeH                 }
365b5b571e0SLoGin                 EPollCtlOption::Del => {
36640609970SGnoCiYeH                     // 不存在则返回错误
36740609970SGnoCiYeH                     if ep_item.is_none() {
36840609970SGnoCiYeH                         return Err(SystemError::ENOENT);
36940609970SGnoCiYeH                     }
37040609970SGnoCiYeH                     // 删除
37140609970SGnoCiYeH                     Self::ep_remove(&mut epoll_guard, fd, Some(dst_file))?;
37240609970SGnoCiYeH                 }
373b5b571e0SLoGin                 EPollCtlOption::Mod => {
37440609970SGnoCiYeH                     // 不存在则返回错误
37540609970SGnoCiYeH                     if ep_item.is_none() {
37640609970SGnoCiYeH                         return Err(SystemError::ENOENT);
37740609970SGnoCiYeH                     }
37840609970SGnoCiYeH                     let ep_item = ep_item.unwrap().clone();
37940609970SGnoCiYeH                     if ep_item.event.read().events & EPollEventType::EPOLLEXCLUSIVE.bits() != 0 {
38040609970SGnoCiYeH                         epds.events |=
38140609970SGnoCiYeH                             EPollEventType::EPOLLERR.bits() | EPollEventType::EPOLLHUP.bits();
38240609970SGnoCiYeH 
383b5b571e0SLoGin                         Self::ep_modify(&mut epoll_guard, ep_item, epds)?;
38440609970SGnoCiYeH                     }
38540609970SGnoCiYeH                 }
38640609970SGnoCiYeH             }
38740609970SGnoCiYeH         }
38840609970SGnoCiYeH 
38940609970SGnoCiYeH         Ok(0)
39040609970SGnoCiYeH     }
39140609970SGnoCiYeH 
39240609970SGnoCiYeH     /// ## epoll_wait的具体实现
39340609970SGnoCiYeH     pub fn do_epoll_wait(
39440609970SGnoCiYeH         epfd: i32,
39540609970SGnoCiYeH         epoll_event: &mut [EPollEvent],
39640609970SGnoCiYeH         max_events: i32,
3976fc066acSJomo         timespec: Option<PosixTimeSpec>,
39840609970SGnoCiYeH     ) -> Result<usize, SystemError> {
39940609970SGnoCiYeH         let current_pcb = ProcessManager::current_pcb();
40040609970SGnoCiYeH         let fd_table = current_pcb.fd_table();
40140609970SGnoCiYeH         let fd_table_guard = fd_table.read();
40240609970SGnoCiYeH 
40340609970SGnoCiYeH         // 获取epoll文件
40440609970SGnoCiYeH         let ep_file = fd_table_guard
40540609970SGnoCiYeH             .get_file_by_fd(epfd)
40640609970SGnoCiYeH             .ok_or(SystemError::EBADF)?;
40740609970SGnoCiYeH 
40840609970SGnoCiYeH         drop(fd_table_guard);
40940609970SGnoCiYeH 
41040609970SGnoCiYeH         // 确保是epoll file
41140609970SGnoCiYeH         if !Self::is_epoll_file(&ep_file) {
41240609970SGnoCiYeH             return Err(SystemError::EINVAL);
41340609970SGnoCiYeH         }
41440609970SGnoCiYeH 
41540609970SGnoCiYeH         // 从epoll文件获取到epoll
41640609970SGnoCiYeH         let mut epolldata = None;
417dfe53cf0SGnoCiYeH         if let FilePrivateData::EPoll(epoll_data) = &*ep_file.private_data.lock() {
41840609970SGnoCiYeH             epolldata = Some(epoll_data.clone())
41940609970SGnoCiYeH         }
420b5b571e0SLoGin         if let Some(epoll_data) = epolldata {
42140609970SGnoCiYeH             let epoll = epoll_data.epoll.clone();
42240609970SGnoCiYeH             let epoll_guard = epoll.0.lock_irqsave();
42340609970SGnoCiYeH 
42440609970SGnoCiYeH             let mut timeout = false;
425b5b571e0SLoGin             if let Some(timespec) = timespec {
42640609970SGnoCiYeH                 if !(timespec.tv_sec > 0 || timespec.tv_nsec > 0) {
42740609970SGnoCiYeH                     // 非阻塞情况
42840609970SGnoCiYeH                     timeout = true;
42940609970SGnoCiYeH                 }
43040609970SGnoCiYeH             }
43140609970SGnoCiYeH             // 判断epoll上有没有就绪事件
43240609970SGnoCiYeH             let mut available = epoll_guard.ep_events_available();
43340609970SGnoCiYeH             drop(epoll_guard);
43440609970SGnoCiYeH             loop {
43540609970SGnoCiYeH                 if available {
43640609970SGnoCiYeH                     // 如果有就绪的事件,则直接返回就绪事件
43740609970SGnoCiYeH                     return Self::ep_send_events(epoll.clone(), epoll_event, max_events);
43840609970SGnoCiYeH                 }
43940609970SGnoCiYeH 
44040609970SGnoCiYeH                 if epoll.0.lock_irqsave().shutdown.load(Ordering::SeqCst) {
44140609970SGnoCiYeH                     // 如果已经关闭
44240609970SGnoCiYeH                     return Err(SystemError::EBADF);
44340609970SGnoCiYeH                 }
44440609970SGnoCiYeH 
44540609970SGnoCiYeH                 // 如果超时
44640609970SGnoCiYeH                 if timeout {
44740609970SGnoCiYeH                     return Ok(0);
44840609970SGnoCiYeH                 }
44940609970SGnoCiYeH 
45040609970SGnoCiYeH                 // 自旋等待一段时间
45140609970SGnoCiYeH                 available = {
45240609970SGnoCiYeH                     let mut ret = false;
45340609970SGnoCiYeH                     for _ in 0..50 {
45440609970SGnoCiYeH                         if let Ok(guard) = epoll.0.try_lock_irqsave() {
45540609970SGnoCiYeH                             if guard.ep_events_available() {
45640609970SGnoCiYeH                                 ret = true;
45740609970SGnoCiYeH                                 break;
45840609970SGnoCiYeH                             }
45940609970SGnoCiYeH                         }
46040609970SGnoCiYeH                     }
46140609970SGnoCiYeH                     // 最后再次不使用try_lock尝试
46240609970SGnoCiYeH                     if !ret {
46340609970SGnoCiYeH                         ret = epoll.0.lock_irqsave().ep_events_available();
46440609970SGnoCiYeH                     }
46540609970SGnoCiYeH                     ret
46640609970SGnoCiYeH                 };
46740609970SGnoCiYeH 
46840609970SGnoCiYeH                 if available {
46940609970SGnoCiYeH                     continue;
47040609970SGnoCiYeH                 }
47140609970SGnoCiYeH 
47240609970SGnoCiYeH                 // 如果有未处理的信号则返回错误
47352bcb59eSGnoCiYeH                 if current_pcb.sig_info_irqsave().sig_pending().signal().bits() != 0 {
47440609970SGnoCiYeH                     return Err(SystemError::EINTR);
47540609970SGnoCiYeH                 }
47640609970SGnoCiYeH 
47740609970SGnoCiYeH                 // 还未等待到事件发生,则睡眠
47840609970SGnoCiYeH                 // 注册定时器
47940609970SGnoCiYeH                 let mut timer = None;
480b5b571e0SLoGin                 if let Some(timespec) = timespec {
48140609970SGnoCiYeH                     let handle = WakeUpHelper::new(current_pcb.clone());
48240609970SGnoCiYeH                     let jiffies = next_n_us_timer_jiffies(
48340609970SGnoCiYeH                         (timespec.tv_sec * 1000000 + timespec.tv_nsec / 1000) as u64,
48440609970SGnoCiYeH                     );
48540609970SGnoCiYeH                     let inner = Timer::new(handle, jiffies);
48640609970SGnoCiYeH                     inner.activate();
48740609970SGnoCiYeH                     timer = Some(inner);
48840609970SGnoCiYeH                 }
48940609970SGnoCiYeH                 let guard = epoll.0.lock_irqsave();
49040609970SGnoCiYeH                 unsafe { guard.epoll_wq.sleep_without_schedule() };
49140609970SGnoCiYeH                 drop(guard);
492f0c87a89SGnoCiYeH                 schedule(SchedMode::SM_NONE);
49340609970SGnoCiYeH                 // 被唤醒后,检查是否有事件可读
49440609970SGnoCiYeH                 available = epoll.0.lock_irqsave().ep_events_available();
495b5b571e0SLoGin                 if let Some(timer) = timer {
496b5b571e0SLoGin                     if timer.as_ref().timeout() {
49740609970SGnoCiYeH                         // 超时
49840609970SGnoCiYeH                         timeout = true;
49940609970SGnoCiYeH                     } else {
50040609970SGnoCiYeH                         // 未超时,则取消计时器
501b5b571e0SLoGin                         timer.cancel();
50240609970SGnoCiYeH                     }
50340609970SGnoCiYeH                 }
50440609970SGnoCiYeH             }
50540609970SGnoCiYeH         } else {
50640609970SGnoCiYeH             panic!("An epoll file does not have the corresponding private information");
50740609970SGnoCiYeH         }
50840609970SGnoCiYeH     }
50940609970SGnoCiYeH 
51040609970SGnoCiYeH     /// ## 将已经准备好的事件拷贝到用户空间
51140609970SGnoCiYeH     ///
51240609970SGnoCiYeH     /// ### 参数
51340609970SGnoCiYeH     /// - epoll: 对应的epoll
51440609970SGnoCiYeH     /// - user_event: 用户空间传入的epoll_event地址,因为内存对其问题,所以这里需要直接操作地址
51540609970SGnoCiYeH     /// - max_events: 处理的最大事件数量
51640609970SGnoCiYeH     fn ep_send_events(
51740609970SGnoCiYeH         epoll: LockedEventPoll,
51840609970SGnoCiYeH         user_event: &mut [EPollEvent],
51940609970SGnoCiYeH         max_events: i32,
52040609970SGnoCiYeH     ) -> Result<usize, SystemError> {
52140609970SGnoCiYeH         let mut ep_guard = epoll.0.lock_irqsave();
52240609970SGnoCiYeH         let mut res: usize = 0;
52340609970SGnoCiYeH 
52440609970SGnoCiYeH         // 在水平触发模式下,需要将epitem再次加入队列,在下次循环再次判断是否还有事件
52540609970SGnoCiYeH         // (所以边缘触发的效率会高于水平触发,但是水平触发某些情况下能够使得更迅速地向用户反馈)
52640609970SGnoCiYeH         let mut push_back = Vec::new();
52740609970SGnoCiYeH         while let Some(epitem) = ep_guard.ready_list.pop_front() {
52840609970SGnoCiYeH             if res >= max_events as usize {
52940609970SGnoCiYeH                 push_back.push(epitem);
53040609970SGnoCiYeH                 break;
53140609970SGnoCiYeH             }
53240609970SGnoCiYeH             let ep_events = EPollEventType::from_bits_truncate(epitem.event.read().events);
53340609970SGnoCiYeH 
53440609970SGnoCiYeH             // 再次poll获取事件(为了防止水平触发一直加入队列)
53540609970SGnoCiYeH             let revents = epitem.ep_item_poll();
53640609970SGnoCiYeH             if revents.is_empty() {
53740609970SGnoCiYeH                 continue;
53840609970SGnoCiYeH             }
53940609970SGnoCiYeH 
54040609970SGnoCiYeH             // 构建触发事件结构体
54140609970SGnoCiYeH             let event = EPollEvent {
54240609970SGnoCiYeH                 events: revents.bits,
54340609970SGnoCiYeH                 data: epitem.event.read().data,
54440609970SGnoCiYeH             };
54540609970SGnoCiYeH 
54640609970SGnoCiYeH             // 这里是需要判断下一个写入的位置是否为空指针
54740609970SGnoCiYeH 
54840609970SGnoCiYeH             // TODO:这里有可能会出现事件丢失的情况
54940609970SGnoCiYeH             // 如果用户传入的数组长度小于传入的max_event,到这里时如果已经到数组最大长度,但是未到max_event
55040609970SGnoCiYeH             // 会出现的问题是我们会把这个数据写入到后面的内存中,用户无法在传入的数组中拿到事件,而且写脏数据到了后面一片内存,导致事件丢失
55140609970SGnoCiYeH             // 出现这个问题的几率比较小,首先是因为用户的使用不规范,后因为前面地址校验是按照max_event来校验的,只会在两块内存连着分配时出现,但是也是需要考虑的
55240609970SGnoCiYeH 
55340609970SGnoCiYeH             // 以下的写法判断并无意义,只是记一下错误处理
55440609970SGnoCiYeH             // offset += core::mem::size_of::<EPollEvent>();
55540609970SGnoCiYeH             // if offset >= max_offset {
55640609970SGnoCiYeH             //     // 当前指向的地址已为空,则把epitem放回队列
55740609970SGnoCiYeH             //     ep_guard.ready_list.push_back(epitem.clone());
55840609970SGnoCiYeH             //     if res == 0 {
55940609970SGnoCiYeH             //         // 一个都未写入成功,表明用户传进的地址就是有问题的
56040609970SGnoCiYeH             //         return Err(SystemError::EFAULT);
56140609970SGnoCiYeH             //     }
56240609970SGnoCiYeH             // }
56340609970SGnoCiYeH 
56440609970SGnoCiYeH             // 拷贝到用户空间
56540609970SGnoCiYeH             user_event[res] = event;
56640609970SGnoCiYeH             // 记数加一
56740609970SGnoCiYeH             res += 1;
56840609970SGnoCiYeH 
569*2eab6dd7S曾俊             // crate::debug!("ep send {event:?}");
57040609970SGnoCiYeH 
57140609970SGnoCiYeH             if ep_events.contains(EPollEventType::EPOLLONESHOT) {
57240609970SGnoCiYeH                 let mut event_writer = epitem.event.write();
57340609970SGnoCiYeH                 let new_event = event_writer.events & EPollEventType::EP_PRIVATE_BITS.bits;
57440609970SGnoCiYeH                 event_writer.set_events(new_event);
57540609970SGnoCiYeH             } else if !ep_events.contains(EPollEventType::EPOLLET) {
57640609970SGnoCiYeH                 push_back.push(epitem);
57740609970SGnoCiYeH             }
57840609970SGnoCiYeH         }
57940609970SGnoCiYeH 
58040609970SGnoCiYeH         for item in push_back {
58140609970SGnoCiYeH             ep_guard.ep_add_ready(item);
58240609970SGnoCiYeH         }
58340609970SGnoCiYeH 
58440609970SGnoCiYeH         Ok(res)
58540609970SGnoCiYeH     }
58640609970SGnoCiYeH 
58740609970SGnoCiYeH     // ### 查看文件是否为epoll文件
588dfe53cf0SGnoCiYeH     fn is_epoll_file(file: &Arc<File>) -> bool {
589dfe53cf0SGnoCiYeH         if let FilePrivateData::EPoll(_) = *file.private_data.lock() {
59040609970SGnoCiYeH             return true;
59140609970SGnoCiYeH         }
59240609970SGnoCiYeH         return false;
59340609970SGnoCiYeH     }
59440609970SGnoCiYeH 
59540609970SGnoCiYeH     fn ep_insert(
59640609970SGnoCiYeH         epoll_guard: &mut SpinLockGuard<EventPoll>,
597dfe53cf0SGnoCiYeH         dst_file: Arc<File>,
59840609970SGnoCiYeH         epitem: Arc<EPollItem>,
59940609970SGnoCiYeH     ) -> Result<(), SystemError> {
60040609970SGnoCiYeH         if Self::is_epoll_file(&dst_file) {
60140609970SGnoCiYeH             return Err(SystemError::ENOSYS);
60240609970SGnoCiYeH             // TODO:现在的实现先不考虑嵌套其它类型的文件(暂时只针对socket),这里的嵌套指epoll/select/poll
60340609970SGnoCiYeH         }
60440609970SGnoCiYeH 
605dfe53cf0SGnoCiYeH         let test_poll = dst_file.poll();
606b5b571e0SLoGin         if test_poll.is_err() && test_poll.unwrap_err() == SystemError::EOPNOTSUPP_OR_ENOTSUP {
60740609970SGnoCiYeH             // 如果目标文件不支持poll
60840609970SGnoCiYeH             return Err(SystemError::ENOSYS);
60940609970SGnoCiYeH         }
61040609970SGnoCiYeH 
61140609970SGnoCiYeH         epoll_guard.ep_items.insert(epitem.fd, epitem.clone());
61240609970SGnoCiYeH 
61340609970SGnoCiYeH         // 检查文件是否已经有事件发生
61440609970SGnoCiYeH         let event = epitem.ep_item_poll();
61540609970SGnoCiYeH         if !event.is_empty() {
61640609970SGnoCiYeH             // 加入到就绪队列
61740609970SGnoCiYeH             epoll_guard.ep_add_ready(epitem.clone());
61840609970SGnoCiYeH 
61940609970SGnoCiYeH             epoll_guard.ep_wake_one();
62040609970SGnoCiYeH         }
62140609970SGnoCiYeH 
62240609970SGnoCiYeH         // TODO: 嵌套epoll?
62340609970SGnoCiYeH 
62440609970SGnoCiYeH         // 这个标志是用与电源管理相关,暂时不支持
62540609970SGnoCiYeH         if epitem.event.read().events & EPollEventType::EPOLLWAKEUP.bits() != 0 {
62640609970SGnoCiYeH             return Err(SystemError::ENOSYS);
62740609970SGnoCiYeH         }
62840609970SGnoCiYeH 
629dfe53cf0SGnoCiYeH         dst_file.add_epoll(epitem.clone())?;
63040609970SGnoCiYeH         Ok(())
63140609970SGnoCiYeH     }
63240609970SGnoCiYeH 
63340609970SGnoCiYeH     pub fn ep_remove(
63440609970SGnoCiYeH         epoll: &mut SpinLockGuard<EventPoll>,
63540609970SGnoCiYeH         fd: i32,
636dfe53cf0SGnoCiYeH         dst_file: Option<Arc<File>>,
63740609970SGnoCiYeH     ) -> Result<(), SystemError> {
638b5b571e0SLoGin         if let Some(dst_file) = dst_file {
639dfe53cf0SGnoCiYeH             dst_file.remove_epoll(epoll.self_ref.as_ref().unwrap())?;
64040609970SGnoCiYeH         }
64140609970SGnoCiYeH 
64240609970SGnoCiYeH         let epitem = epoll.ep_items.remove(&fd).unwrap();
64340609970SGnoCiYeH 
64440609970SGnoCiYeH         let _ = epoll
64540609970SGnoCiYeH             .ready_list
64640609970SGnoCiYeH             .extract_if(|item| Arc::ptr_eq(item, &epitem));
64740609970SGnoCiYeH 
64840609970SGnoCiYeH         Ok(())
64940609970SGnoCiYeH     }
65040609970SGnoCiYeH 
65140609970SGnoCiYeH     /// ## 修改已经注册的监听事件
65240609970SGnoCiYeH     ///
65340609970SGnoCiYeH     /// ### 参数
65440609970SGnoCiYeH     /// - epoll_guard: EventPoll的锁
65540609970SGnoCiYeH     /// - epitem: 需要修改的描述符对应的epitem
65640609970SGnoCiYeH     /// - event: 新的事件
65740609970SGnoCiYeH     fn ep_modify(
65840609970SGnoCiYeH         epoll_guard: &mut SpinLockGuard<EventPoll>,
65940609970SGnoCiYeH         epitem: Arc<EPollItem>,
66040609970SGnoCiYeH         event: &EPollEvent,
66140609970SGnoCiYeH     ) -> Result<(), SystemError> {
66240609970SGnoCiYeH         let mut epi_event_guard = epitem.event.write();
66340609970SGnoCiYeH 
66440609970SGnoCiYeH         // 修改epitem
66540609970SGnoCiYeH         epi_event_guard.events = event.events;
66640609970SGnoCiYeH         epi_event_guard.data = event.data;
66740609970SGnoCiYeH 
66840609970SGnoCiYeH         drop(epi_event_guard);
66940609970SGnoCiYeH         // 修改后检查文件是否已经有感兴趣事件发生
67040609970SGnoCiYeH         let event = epitem.ep_item_poll();
67140609970SGnoCiYeH         if !event.is_empty() {
67240609970SGnoCiYeH             epoll_guard.ep_add_ready(epitem.clone());
67340609970SGnoCiYeH 
67440609970SGnoCiYeH             epoll_guard.ep_wake_one();
67540609970SGnoCiYeH         }
67640609970SGnoCiYeH         // TODO:处理EPOLLWAKEUP,目前不支持
67740609970SGnoCiYeH 
67840609970SGnoCiYeH         Ok(())
67940609970SGnoCiYeH     }
68040609970SGnoCiYeH 
68140609970SGnoCiYeH     /// ### 判断epoll是否有就绪item
68240609970SGnoCiYeH     pub fn ep_events_available(&self) -> bool {
68340609970SGnoCiYeH         !self.ready_list.is_empty()
68440609970SGnoCiYeH     }
68540609970SGnoCiYeH 
68640609970SGnoCiYeH     /// ### 将epitem加入到就绪队列,如果为重复添加则忽略
68740609970SGnoCiYeH     pub fn ep_add_ready(&mut self, epitem: Arc<EPollItem>) {
68840609970SGnoCiYeH         let ret = self.ready_list.iter().find(|epi| Arc::ptr_eq(epi, &epitem));
68940609970SGnoCiYeH 
69040609970SGnoCiYeH         if ret.is_none() {
69140609970SGnoCiYeH             self.ready_list.push_back(epitem);
69240609970SGnoCiYeH         }
69340609970SGnoCiYeH     }
69440609970SGnoCiYeH 
69540609970SGnoCiYeH     /// ### 判断该epoll上是否有进程在等待
69640609970SGnoCiYeH     pub fn ep_has_waiter(&self) -> bool {
69740609970SGnoCiYeH         self.epoll_wq.len() != 0
69840609970SGnoCiYeH     }
69940609970SGnoCiYeH 
70040609970SGnoCiYeH     /// ### 唤醒所有在epoll上等待的进程
70140609970SGnoCiYeH     pub fn ep_wake_all(&self) {
70240609970SGnoCiYeH         self.epoll_wq.wakeup_all(None);
70340609970SGnoCiYeH     }
70440609970SGnoCiYeH 
70540609970SGnoCiYeH     /// ### 唤醒所有在epoll上等待的首个进程
70640609970SGnoCiYeH     pub fn ep_wake_one(&self) {
70740609970SGnoCiYeH         self.epoll_wq.wakeup(None);
70840609970SGnoCiYeH     }
7095e948c56SGnoCiYeH 
7105e948c56SGnoCiYeH     /// ### epoll的回调,支持epoll的文件有事件到来时直接调用该方法即可
7115e948c56SGnoCiYeH     pub fn wakeup_epoll(
712840045afSLoGin         epitems: &SpinLock<LinkedList<Arc<EPollItem>>>,
7135e948c56SGnoCiYeH         pollflags: EPollEventType,
7145e948c56SGnoCiYeH     ) -> Result<(), SystemError> {
7155e948c56SGnoCiYeH         let mut epitems_guard = epitems.try_lock_irqsave()?;
7165e948c56SGnoCiYeH         // 一次只取一个,因为一次也只有一个进程能拿到对应文件的��
7175e948c56SGnoCiYeH         if let Some(epitem) = epitems_guard.pop_front() {
7185e948c56SGnoCiYeH             let epoll = epitem.epoll().upgrade().unwrap();
7195e948c56SGnoCiYeH             let mut epoll_guard = epoll.try_lock()?;
7205e948c56SGnoCiYeH             let binding = epitem.clone();
7215e948c56SGnoCiYeH             let event_guard = binding.event().read();
7225e948c56SGnoCiYeH             let ep_events = EPollEventType::from_bits_truncate(event_guard.events());
7235e948c56SGnoCiYeH 
7245e948c56SGnoCiYeH             // 检查事件合理性以及是否有感兴趣的事件
7255e948c56SGnoCiYeH             if !(ep_events
7265e948c56SGnoCiYeH                 .difference(EPollEventType::EP_PRIVATE_BITS)
7275e948c56SGnoCiYeH                 .is_empty()
7285e948c56SGnoCiYeH                 || pollflags.difference(ep_events).is_empty())
7295e948c56SGnoCiYeH             {
7305e948c56SGnoCiYeH                 // TODO: 未处理pm相关
7315e948c56SGnoCiYeH 
7325e948c56SGnoCiYeH                 // 首先将就绪的epitem加入等待队列
7335e948c56SGnoCiYeH                 epoll_guard.ep_add_ready(epitem.clone());
7345e948c56SGnoCiYeH 
7355e948c56SGnoCiYeH                 if epoll_guard.ep_has_waiter() {
7365e948c56SGnoCiYeH                     if ep_events.contains(EPollEventType::EPOLLEXCLUSIVE)
7375e948c56SGnoCiYeH                         && !pollflags.contains(EPollEventType::POLLFREE)
7385e948c56SGnoCiYeH                     {
7395e948c56SGnoCiYeH                         // 避免惊群
7405e948c56SGnoCiYeH                         epoll_guard.ep_wake_one();
7415e948c56SGnoCiYeH                     } else {
7425e948c56SGnoCiYeH                         epoll_guard.ep_wake_all();
7435e948c56SGnoCiYeH                     }
7445e948c56SGnoCiYeH                 }
7455e948c56SGnoCiYeH             }
7465e948c56SGnoCiYeH 
7475e948c56SGnoCiYeH             epitems_guard.push_back(epitem);
7485e948c56SGnoCiYeH         }
7495e948c56SGnoCiYeH         Ok(())
7505e948c56SGnoCiYeH     }
75140609970SGnoCiYeH }
75240609970SGnoCiYeH 
75340609970SGnoCiYeH /// 与C兼容的Epoll事件结构体
75440609970SGnoCiYeH #[derive(Copy, Clone, Default)]
75540609970SGnoCiYeH #[repr(packed)]
75640609970SGnoCiYeH pub struct EPollEvent {
75740609970SGnoCiYeH     /// 表示触发的事件
75840609970SGnoCiYeH     events: u32,
75940609970SGnoCiYeH     /// 内核态不使用该字段,该字段由用户态自由使用,在事件发生时内核将会原样返回
76040609970SGnoCiYeH     data: u64,
76140609970SGnoCiYeH }
76240609970SGnoCiYeH 
76340609970SGnoCiYeH impl Debug for EPollEvent {
76440609970SGnoCiYeH     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
76540609970SGnoCiYeH         let events = self.events;
76640609970SGnoCiYeH         let u64 = self.data;
76740609970SGnoCiYeH         f.debug_struct("epoll_event")
76840609970SGnoCiYeH             .field("events", &events)
76940609970SGnoCiYeH             .field("data", &u64)
77040609970SGnoCiYeH             .finish()
77140609970SGnoCiYeH     }
77240609970SGnoCiYeH }
77340609970SGnoCiYeH 
77440609970SGnoCiYeH impl EPollEvent {
77540609970SGnoCiYeH     pub fn set_events(&mut self, events: u32) {
77640609970SGnoCiYeH         self.events = events;
77740609970SGnoCiYeH     }
77840609970SGnoCiYeH 
77940609970SGnoCiYeH     pub fn events(&self) -> u32 {
78040609970SGnoCiYeH         self.events
78140609970SGnoCiYeH     }
78240609970SGnoCiYeH }
78340609970SGnoCiYeH 
78440609970SGnoCiYeH /// ## epoll_ctl函数的参数
78540609970SGnoCiYeH #[derive(Debug, PartialEq)]
78640609970SGnoCiYeH pub enum EPollCtlOption {
78740609970SGnoCiYeH     /// 注册新的文件描述符到epfd
788b5b571e0SLoGin     Add,
78940609970SGnoCiYeH     /// 将对应的文件描述符从epfd中删除
790b5b571e0SLoGin     Del,
79140609970SGnoCiYeH     /// 修改已经注册的文件描述符的监听事件
792b5b571e0SLoGin     Mod,
79340609970SGnoCiYeH }
79440609970SGnoCiYeH 
79540609970SGnoCiYeH impl EPollCtlOption {
79640609970SGnoCiYeH     pub fn from_op_num(op: usize) -> Result<Self, SystemError> {
79740609970SGnoCiYeH         match op {
798b5b571e0SLoGin             1 => Ok(Self::Add),
799b5b571e0SLoGin             2 => Ok(Self::Del),
800b5b571e0SLoGin             3 => Ok(Self::Mod),
80140609970SGnoCiYeH             _ => Err(SystemError::EINVAL),
80240609970SGnoCiYeH         }
80340609970SGnoCiYeH     }
80440609970SGnoCiYeH }
80540609970SGnoCiYeH 
80640609970SGnoCiYeH bitflags! {
80740609970SGnoCiYeH     #[allow(dead_code)]
80840609970SGnoCiYeH     pub struct EPollEventType: u32 {
80940609970SGnoCiYeH         /// 对应的描述符有新的数据可读时会触发
81040609970SGnoCiYeH         const EPOLLIN = 0x00000001;
81140609970SGnoCiYeH         /// 对应的描述符有紧急数据可读时会触发
81240609970SGnoCiYeH         const EPOLLPRI = 0x00000002;
81340609970SGnoCiYeH         /// 对应的描述符可以写入数据时会触发
81440609970SGnoCiYeH         const EPOLLOUT = 0x00000004;
81540609970SGnoCiYeH         /// 对应的描述符发生错误时会触发
81640609970SGnoCiYeH         const EPOLLERR = 0x00000008;
81740609970SGnoCiYeH         /// 对应的描述符被挂断(连接关闭)时会触发
81840609970SGnoCiYeH         const EPOLLHUP = 0x00000010;
81940609970SGnoCiYeH         /// 对应的描述符不是一个有效的文件描述符时会触发
82040609970SGnoCiYeH         const EPOLLNVAL = 0x00000020;
82140609970SGnoCiYeH         /// 普通数据可读,类似于`EPOLLIN`
82240609970SGnoCiYeH         const EPOLLRDNORM = 0x00000040;
82340609970SGnoCiYeH         /// 优先级带外数据可读
82440609970SGnoCiYeH         const EPOLLRDBAND = 0x00000080;
82540609970SGnoCiYeH         /// 普通数据可写,类似于'EPOLLOUT'
82640609970SGnoCiYeH         const EPOLLWRNORM = 0x00000100;
82740609970SGnoCiYeH         /// 优先级带外数据可写
82840609970SGnoCiYeH         const EPOLLWRBAND = 0x00000200;
82940609970SGnoCiYeH         /// 通过消息队列收到消息时会触
83040609970SGnoCiYeH         const EPOLLMSG = 0x00000400;
83140609970SGnoCiYeH         /// 对应的描述符被挂断(连接关闭)的一端发送了 FIN 时会触发(读关闭)
83240609970SGnoCiYeH         const EPOLLRDHUP = 0x00002000;
83340609970SGnoCiYeH 
83440609970SGnoCiYeH         /// 以下为额外选项
83540609970SGnoCiYeH         ///
83640609970SGnoCiYeH         /// 特定选项,用于异步 I/O,目前未实现
83740609970SGnoCiYeH         const EPOLL_URING_WAKE = 1u32 << 27;
83840609970SGnoCiYeH         /// 设置epoll为独占模式
83940609970SGnoCiYeH         const EPOLLEXCLUSIVE = 1u32 << 28;
84040609970SGnoCiYeH         ///  允许在系统挂起时唤醒 epoll,通常用于通过 eventfd 或 timerfd 唤醒 epoll,(通常与电源管理相关,未实现)
84140609970SGnoCiYeH         const EPOLLWAKEUP = 1u32 << 29;
84240609970SGnoCiYeH         /// 表示只监听一次事件,之后需要重新添加
84340609970SGnoCiYeH         const EPOLLONESHOT = 1u32 << 30;
84440609970SGnoCiYeH 
84540609970SGnoCiYeH         /// 启用边缘触发模式(即只有下次触发事件时才会通过epoll_wait返回),
84640609970SGnoCiYeH         /// 对应为水平触发(默认),水平触发模式下若这次未处理完数据,那epoll还会将其加入自己的就绪队列
84740609970SGnoCiYeH         const EPOLLET = 1u32 << 31;
84840609970SGnoCiYeH 
84940609970SGnoCiYeH         /// 以下为组合码
85040609970SGnoCiYeH         const EPOLLINOUT_BITS = Self::EPOLLIN.bits | Self::EPOLLOUT.bits;
85140609970SGnoCiYeH         const EPOLLEXCLUSIVE_OK_BITS =
85240609970SGnoCiYeH             Self::EPOLLINOUT_BITS.bits
85340609970SGnoCiYeH             | Self::EPOLLERR.bits
85440609970SGnoCiYeH             | Self::EPOLLHUP.bits
85540609970SGnoCiYeH             | Self::EPOLLWAKEUP.bits
85640609970SGnoCiYeH             | Self::EPOLLET.bits
85740609970SGnoCiYeH             | Self::EPOLLEXCLUSIVE.bits;
85840609970SGnoCiYeH 
85940609970SGnoCiYeH         const EP_PRIVATE_BITS =
86040609970SGnoCiYeH             Self::EPOLLWAKEUP.bits
86140609970SGnoCiYeH             | Self::EPOLLONESHOT.bits
86240609970SGnoCiYeH             | Self::EPOLLET.bits
86340609970SGnoCiYeH             | Self::EPOLLEXCLUSIVE.bits;
86440609970SGnoCiYeH 
86540609970SGnoCiYeH         /// 表示epoll已经被释放,但是在目前的设计中未用到
86640609970SGnoCiYeH         const POLLFREE = 0x4000;
86740609970SGnoCiYeH     }
86840609970SGnoCiYeH }
869