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 }; 11*91e9d4abSLoGin use system_error::SystemError; 1240609970SGnoCiYeH 1340609970SGnoCiYeH use crate::{ 1440609970SGnoCiYeH arch::sched::sched, 1540609970SGnoCiYeH filesystem::vfs::{ 1640609970SGnoCiYeH file::{File, FileMode}, 1740609970SGnoCiYeH FilePrivateData, IndexNode, Metadata, 1840609970SGnoCiYeH }, 1940609970SGnoCiYeH include::bindings::bindings::INT32_MAX, 2040609970SGnoCiYeH libs::{ 2140609970SGnoCiYeH rbtree::RBTree, 2240609970SGnoCiYeH rwlock::RwLock, 2340609970SGnoCiYeH spinlock::{SpinLock, SpinLockGuard}, 2440609970SGnoCiYeH wait_queue::WaitQueue, 2540609970SGnoCiYeH }, 2640609970SGnoCiYeH process::ProcessManager, 2740609970SGnoCiYeH time::{ 2840609970SGnoCiYeH timer::{next_n_us_timer_jiffies, Timer, WakeUpHelper}, 2940609970SGnoCiYeH TimeSpec, 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); 5540609970SGnoCiYeH pub fn new() -> Self { 5640609970SGnoCiYeH Self { 5740609970SGnoCiYeH epoll_wq: WaitQueue::INIT, 5840609970SGnoCiYeH ep_items: RBTree::new(), 5940609970SGnoCiYeH ready_list: LinkedList::new(), 6040609970SGnoCiYeH shutdown: AtomicBool::new(false), 6140609970SGnoCiYeH self_ref: None, 6240609970SGnoCiYeH } 6340609970SGnoCiYeH } 6440609970SGnoCiYeH } 6540609970SGnoCiYeH 6640609970SGnoCiYeH /// EpollItem表示的是Epoll所真正管理的对象 6740609970SGnoCiYeH /// 每当用户向Epoll添加描述符时都会注册一个新的EpollItem,EpollItem携带了一些被监听的描述符的必要信息 6840609970SGnoCiYeH #[derive(Debug)] 6940609970SGnoCiYeH pub struct EPollItem { 7040609970SGnoCiYeH /// 对应的Epoll 7140609970SGnoCiYeH epoll: Weak<SpinLock<EventPoll>>, 7240609970SGnoCiYeH /// 用户注册的事件 7340609970SGnoCiYeH event: RwLock<EPollEvent>, 7440609970SGnoCiYeH /// 监听的描述符 7540609970SGnoCiYeH fd: i32, 7640609970SGnoCiYeH /// 对应的文件 7740609970SGnoCiYeH file: Weak<SpinLock<File>>, 7840609970SGnoCiYeH } 7940609970SGnoCiYeH 8040609970SGnoCiYeH impl EPollItem { 8140609970SGnoCiYeH pub fn new( 8240609970SGnoCiYeH epoll: Weak<SpinLock<EventPoll>>, 8340609970SGnoCiYeH events: EPollEvent, 8440609970SGnoCiYeH fd: i32, 8540609970SGnoCiYeH file: Weak<SpinLock<File>>, 8640609970SGnoCiYeH ) -> Self { 8740609970SGnoCiYeH Self { 8840609970SGnoCiYeH epoll, 8940609970SGnoCiYeH event: RwLock::new(events), 9040609970SGnoCiYeH fd, 9140609970SGnoCiYeH file, 9240609970SGnoCiYeH } 9340609970SGnoCiYeH } 9440609970SGnoCiYeH 9540609970SGnoCiYeH pub fn epoll(&self) -> Weak<SpinLock<EventPoll>> { 9640609970SGnoCiYeH self.epoll.clone() 9740609970SGnoCiYeH } 9840609970SGnoCiYeH 9940609970SGnoCiYeH pub fn event(&self) -> &RwLock<EPollEvent> { 10040609970SGnoCiYeH &self.event 10140609970SGnoCiYeH } 10240609970SGnoCiYeH 10340609970SGnoCiYeH pub fn file(&self) -> Weak<SpinLock<File>> { 10440609970SGnoCiYeH self.file.clone() 10540609970SGnoCiYeH } 10640609970SGnoCiYeH 10740609970SGnoCiYeH pub fn fd(&self) -> i32 { 10840609970SGnoCiYeH self.fd 10940609970SGnoCiYeH } 11040609970SGnoCiYeH 11140609970SGnoCiYeH /// ## 通过epoll_item来执行绑定文件的poll方法,并获取到感兴趣的事件 11240609970SGnoCiYeH fn ep_item_poll(&self) -> EPollEventType { 11340609970SGnoCiYeH let file = self.file.upgrade(); 11440609970SGnoCiYeH if file.is_none() { 11540609970SGnoCiYeH return EPollEventType::empty(); 11640609970SGnoCiYeH } 11740609970SGnoCiYeH if let Ok(events) = file.unwrap().lock_irqsave().poll() { 11840609970SGnoCiYeH let events = events as u32 & self.event.read().events; 11940609970SGnoCiYeH return EPollEventType::from_bits_truncate(events); 12040609970SGnoCiYeH } 12140609970SGnoCiYeH return EPollEventType::empty(); 12240609970SGnoCiYeH } 12340609970SGnoCiYeH } 12440609970SGnoCiYeH 12540609970SGnoCiYeH /// ### Epoll文件的私有信息 12640609970SGnoCiYeH #[derive(Debug, Clone)] 12740609970SGnoCiYeH pub struct EPollPrivateData { 12840609970SGnoCiYeH epoll: LockedEventPoll, 12940609970SGnoCiYeH } 13040609970SGnoCiYeH 13140609970SGnoCiYeH /// ### 该结构体将Epoll加入文件系统 13240609970SGnoCiYeH #[derive(Debug)] 13340609970SGnoCiYeH pub struct EPollInode { 13440609970SGnoCiYeH epoll: LockedEventPoll, 13540609970SGnoCiYeH } 13640609970SGnoCiYeH 13740609970SGnoCiYeH impl EPollInode { 13840609970SGnoCiYeH pub fn new(epoll: LockedEventPoll) -> Arc<Self> { 13940609970SGnoCiYeH Arc::new(Self { epoll }) 14040609970SGnoCiYeH } 14140609970SGnoCiYeH } 14240609970SGnoCiYeH 14340609970SGnoCiYeH impl IndexNode for EPollInode { 14440609970SGnoCiYeH fn read_at( 14540609970SGnoCiYeH &self, 14640609970SGnoCiYeH _offset: usize, 14740609970SGnoCiYeH _len: usize, 14840609970SGnoCiYeH _buf: &mut [u8], 14940609970SGnoCiYeH _data: &mut crate::filesystem::vfs::FilePrivateData, 150*91e9d4abSLoGin ) -> Result<usize, SystemError> { 15140609970SGnoCiYeH Err(SystemError::ENOSYS) 15240609970SGnoCiYeH } 15340609970SGnoCiYeH 15440609970SGnoCiYeH fn write_at( 15540609970SGnoCiYeH &self, 15640609970SGnoCiYeH _offset: usize, 15740609970SGnoCiYeH _len: usize, 15840609970SGnoCiYeH _buf: &[u8], 15940609970SGnoCiYeH _data: &mut crate::filesystem::vfs::FilePrivateData, 160*91e9d4abSLoGin ) -> Result<usize, SystemError> { 16140609970SGnoCiYeH Err(SystemError::ENOSYS) 16240609970SGnoCiYeH } 16340609970SGnoCiYeH 164*91e9d4abSLoGin fn poll(&self) -> Result<usize, SystemError> { 16540609970SGnoCiYeH // 需要实现epoll嵌套epoll时,需要实现这里 16640609970SGnoCiYeH todo!() 16740609970SGnoCiYeH } 16840609970SGnoCiYeH 16940609970SGnoCiYeH fn fs(&self) -> Arc<dyn crate::filesystem::vfs::FileSystem> { 17040609970SGnoCiYeH todo!() 17140609970SGnoCiYeH } 17240609970SGnoCiYeH 17340609970SGnoCiYeH fn as_any_ref(&self) -> &dyn core::any::Any { 17440609970SGnoCiYeH self 17540609970SGnoCiYeH } 17640609970SGnoCiYeH 177*91e9d4abSLoGin fn list(&self) -> Result<Vec<alloc::string::String>, SystemError> { 17840609970SGnoCiYeH Err(SystemError::ENOSYS) 17940609970SGnoCiYeH } 18040609970SGnoCiYeH 18140609970SGnoCiYeH fn metadata(&self) -> Result<Metadata, SystemError> { 18240609970SGnoCiYeH Ok(Metadata::default()) 18340609970SGnoCiYeH } 18440609970SGnoCiYeH 18540609970SGnoCiYeH fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 18640609970SGnoCiYeH // 释放资源 18740609970SGnoCiYeH let mut epoll = self.epoll.0.lock_irqsave(); 18840609970SGnoCiYeH 18940609970SGnoCiYeH // 唤醒epoll上面等待的所有进程 19040609970SGnoCiYeH epoll.shutdown.store(true, Ordering::SeqCst); 19140609970SGnoCiYeH epoll.ep_wake_all(); 19240609970SGnoCiYeH 19340609970SGnoCiYeH let fds = epoll.ep_items.keys().cloned().collect::<Vec<_>>(); 19440609970SGnoCiYeH 19540609970SGnoCiYeH // 清理红黑树里面的epitems 19640609970SGnoCiYeH for fd in fds { 19740609970SGnoCiYeH let file = ProcessManager::current_pcb() 19840609970SGnoCiYeH .fd_table() 19940609970SGnoCiYeH .read() 20040609970SGnoCiYeH .get_file_by_fd(fd); 20140609970SGnoCiYeH 20240609970SGnoCiYeH if file.is_some() { 20340609970SGnoCiYeH file.unwrap() 20440609970SGnoCiYeH .lock_irqsave() 20540609970SGnoCiYeH .remove_epoll(&Arc::downgrade(&self.epoll.0))?; 20640609970SGnoCiYeH } 20740609970SGnoCiYeH 20840609970SGnoCiYeH epoll.ep_items.remove(&fd); 20940609970SGnoCiYeH } 21040609970SGnoCiYeH 21140609970SGnoCiYeH Ok(()) 21240609970SGnoCiYeH } 21340609970SGnoCiYeH 21440609970SGnoCiYeH fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> { 21540609970SGnoCiYeH Ok(()) 21640609970SGnoCiYeH } 21740609970SGnoCiYeH } 21840609970SGnoCiYeH 21940609970SGnoCiYeH impl EventPoll { 22040609970SGnoCiYeH /// ## 创建epoll对象 22140609970SGnoCiYeH /// 22240609970SGnoCiYeH /// ### 参数 22340609970SGnoCiYeH /// - flags: 创建的epoll文件的FileMode 22440609970SGnoCiYeH /// 22540609970SGnoCiYeH /// ### 返回值 22640609970SGnoCiYeH /// - 成功则返回Ok(fd),否则返回Err 22740609970SGnoCiYeH pub fn do_create_epoll(flags: FileMode) -> Result<usize, SystemError> { 22840609970SGnoCiYeH if !flags.difference(FileMode::O_CLOEXEC).is_empty() { 22940609970SGnoCiYeH return Err(SystemError::EINVAL); 23040609970SGnoCiYeH } 23140609970SGnoCiYeH 23240609970SGnoCiYeH // 创建epoll 23340609970SGnoCiYeH let epoll = LockedEventPoll(Arc::new(SpinLock::new(EventPoll::new()))); 23440609970SGnoCiYeH epoll.0.lock_irqsave().self_ref = Some(Arc::downgrade(&epoll.0)); 23540609970SGnoCiYeH 23640609970SGnoCiYeH // 创建epoll的inode对象 23740609970SGnoCiYeH let epoll_inode = EPollInode::new(epoll.clone()); 23840609970SGnoCiYeH 23940609970SGnoCiYeH let mut ep_file = File::new( 24040609970SGnoCiYeH epoll_inode, 24140609970SGnoCiYeH FileMode::O_RDWR | (flags & FileMode::O_CLOEXEC), 24240609970SGnoCiYeH )?; 24340609970SGnoCiYeH 24440609970SGnoCiYeH // 设置ep_file的FilePrivateData 24540609970SGnoCiYeH ep_file.private_data = FilePrivateData::EPoll(EPollPrivateData { epoll }); 24640609970SGnoCiYeH 24740609970SGnoCiYeH let current_pcb = ProcessManager::current_pcb(); 24840609970SGnoCiYeH let fd_table = current_pcb.fd_table(); 24940609970SGnoCiYeH let mut fd_table_guard = fd_table.write(); 25040609970SGnoCiYeH 25140609970SGnoCiYeH let fd = fd_table_guard.alloc_fd(ep_file, None)?; 25240609970SGnoCiYeH 25340609970SGnoCiYeH Ok(fd as usize) 25440609970SGnoCiYeH } 25540609970SGnoCiYeH 25640609970SGnoCiYeH /// ## epoll_ctl的具体实现 25740609970SGnoCiYeH /// 25840609970SGnoCiYeH /// 根据不同的op对epoll文件进行增删改 25940609970SGnoCiYeH /// 26040609970SGnoCiYeH /// ### 参数 26140609970SGnoCiYeH /// - epfd: 操作的epoll文件描述符 26240609970SGnoCiYeH /// - op: 对应的操作 26340609970SGnoCiYeH /// - fd: 操作对应的文件描述符 26440609970SGnoCiYeH /// - epds: 从用户态传入的event,若op为EpollCtlAdd,则对应注册的监听事件,若op为EPollCtlMod,则对应更新的事件,删除操作不涉及此字段 26540609970SGnoCiYeH /// - nonblock: 定义这次操作是否为非阻塞(有可能其他地方占有EPoll的锁) 26640609970SGnoCiYeH pub fn do_epoll_ctl( 26740609970SGnoCiYeH epfd: i32, 26840609970SGnoCiYeH op: EPollCtlOption, 26940609970SGnoCiYeH fd: i32, 27040609970SGnoCiYeH epds: &mut EPollEvent, 27140609970SGnoCiYeH nonblock: bool, 27240609970SGnoCiYeH ) -> Result<usize, SystemError> { 27340609970SGnoCiYeH let current_pcb = ProcessManager::current_pcb(); 27440609970SGnoCiYeH let fd_table = current_pcb.fd_table(); 27540609970SGnoCiYeH let fd_table_guard = fd_table.read(); 27640609970SGnoCiYeH 27740609970SGnoCiYeH // 获取epoll和对应fd指向的文件 27840609970SGnoCiYeH let ep_file = fd_table_guard 27940609970SGnoCiYeH .get_file_by_fd(epfd) 28040609970SGnoCiYeH .ok_or(SystemError::EBADF)?; 28140609970SGnoCiYeH let dst_file = fd_table_guard 28240609970SGnoCiYeH .get_file_by_fd(fd) 28340609970SGnoCiYeH .ok_or(SystemError::EBADF)?; 28440609970SGnoCiYeH 28540609970SGnoCiYeH // 检查是否允许 EPOLLWAKEUP 28640609970SGnoCiYeH if op != EPollCtlOption::EpollCtlDel { 28740609970SGnoCiYeH epds.events &= !EPollEventType::EPOLLWAKEUP.bits(); 28840609970SGnoCiYeH } 28940609970SGnoCiYeH 29040609970SGnoCiYeH let events = EPollEventType::from_bits_truncate(epds.events); 29140609970SGnoCiYeH 29240609970SGnoCiYeH // 检查获取到的两个文件的正确性 29340609970SGnoCiYeH // 首先是不能自己嵌套自己 29440609970SGnoCiYeH // 然后ep_file必须是epoll文件 29540609970SGnoCiYeH if Arc::ptr_eq(&ep_file, &dst_file) || !Self::is_epoll_file(&ep_file) { 29640609970SGnoCiYeH return Err(SystemError::EINVAL); 29740609970SGnoCiYeH } 29840609970SGnoCiYeH 29940609970SGnoCiYeH if op != EPollCtlOption::EpollCtlDel && events.contains(EPollEventType::EPOLLEXCLUSIVE) { 30040609970SGnoCiYeH // epoll独占模式下不允许EpollCtlMod 30140609970SGnoCiYeH if op == EPollCtlOption::EpollCtlMod { 30240609970SGnoCiYeH return Err(SystemError::EINVAL); 30340609970SGnoCiYeH } 30440609970SGnoCiYeH 30540609970SGnoCiYeH // 不支持嵌套的独占唤醒 30640609970SGnoCiYeH if op == EPollCtlOption::EpollCtlAdd && Self::is_epoll_file(&dst_file) 30740609970SGnoCiYeH || !events 30840609970SGnoCiYeH .difference(EPollEventType::EPOLLEXCLUSIVE_OK_BITS) 30940609970SGnoCiYeH .is_empty() 31040609970SGnoCiYeH { 31140609970SGnoCiYeH return Err(SystemError::EINVAL); 31240609970SGnoCiYeH } 31340609970SGnoCiYeH } 31440609970SGnoCiYeH 31540609970SGnoCiYeH // 从FilePrivateData获取到epoll 31640609970SGnoCiYeH if let FilePrivateData::EPoll(epoll_data) = &ep_file.lock_irqsave().private_data { 31740609970SGnoCiYeH let mut epoll_guard = { 31840609970SGnoCiYeH if nonblock { 31940609970SGnoCiYeH // 如果设置非阻塞,则尝试获取一次锁 32040609970SGnoCiYeH if let Ok(guard) = epoll_data.epoll.0.try_lock_irqsave() { 32140609970SGnoCiYeH guard 32240609970SGnoCiYeH } else { 32340609970SGnoCiYeH return Err(SystemError::EAGAIN_OR_EWOULDBLOCK); 32440609970SGnoCiYeH } 32540609970SGnoCiYeH } else { 32640609970SGnoCiYeH epoll_data.epoll.0.lock_irqsave() 32740609970SGnoCiYeH } 32840609970SGnoCiYeH }; 32940609970SGnoCiYeH 33040609970SGnoCiYeH if op == EPollCtlOption::EpollCtlAdd { 33140609970SGnoCiYeH // TODO: 循环检查是否为epoll嵌套epoll的情况,如果是则需要检测其深度 33240609970SGnoCiYeH // 这里是需要一种检测算法的,但是目前未考虑epoll嵌套epoll的情况,所以暂时未实现 33340609970SGnoCiYeH // Linux算法:https://code.dragonos.org.cn/xref/linux-6.1.9/fs/eventpoll.c?r=&mo=56953&fi=2057#2133 33440609970SGnoCiYeH if Self::is_epoll_file(&dst_file) { 33540609970SGnoCiYeH todo!(); 33640609970SGnoCiYeH } 33740609970SGnoCiYeH } 33840609970SGnoCiYeH 33940609970SGnoCiYeH let ep_item = epoll_guard.ep_items.get(&fd); 34040609970SGnoCiYeH match op { 34140609970SGnoCiYeH EPollCtlOption::EpollCtlAdd => { 34240609970SGnoCiYeH // 如果已经存在,则返回错误 34340609970SGnoCiYeH if ep_item.is_some() { 34440609970SGnoCiYeH return Err(SystemError::EEXIST); 34540609970SGnoCiYeH } 34640609970SGnoCiYeH // 设置epoll 34740609970SGnoCiYeH let epitem = Arc::new(EPollItem::new( 34840609970SGnoCiYeH Arc::downgrade(&epoll_data.epoll.0), 34940609970SGnoCiYeH *epds, 35040609970SGnoCiYeH fd, 35140609970SGnoCiYeH Arc::downgrade(&dst_file), 35240609970SGnoCiYeH )); 35340609970SGnoCiYeH Self::ep_insert(&mut epoll_guard, dst_file, epitem)?; 35440609970SGnoCiYeH } 35540609970SGnoCiYeH EPollCtlOption::EpollCtlDel => { 35640609970SGnoCiYeH // 不存在则返回错误 35740609970SGnoCiYeH if ep_item.is_none() { 35840609970SGnoCiYeH return Err(SystemError::ENOENT); 35940609970SGnoCiYeH } 36040609970SGnoCiYeH // 删除 36140609970SGnoCiYeH Self::ep_remove(&mut epoll_guard, fd, Some(dst_file))?; 36240609970SGnoCiYeH } 36340609970SGnoCiYeH EPollCtlOption::EpollCtlMod => { 36440609970SGnoCiYeH // 不存在则返回错误 36540609970SGnoCiYeH if ep_item.is_none() { 36640609970SGnoCiYeH return Err(SystemError::ENOENT); 36740609970SGnoCiYeH } 36840609970SGnoCiYeH let ep_item = ep_item.unwrap().clone(); 36940609970SGnoCiYeH if ep_item.event.read().events & EPollEventType::EPOLLEXCLUSIVE.bits() != 0 { 37040609970SGnoCiYeH epds.events |= 37140609970SGnoCiYeH EPollEventType::EPOLLERR.bits() | EPollEventType::EPOLLHUP.bits(); 37240609970SGnoCiYeH 37340609970SGnoCiYeH Self::ep_modify(&mut epoll_guard, ep_item, &epds)?; 37440609970SGnoCiYeH } 37540609970SGnoCiYeH } 37640609970SGnoCiYeH } 37740609970SGnoCiYeH } 37840609970SGnoCiYeH 37940609970SGnoCiYeH Ok(0) 38040609970SGnoCiYeH } 38140609970SGnoCiYeH 38240609970SGnoCiYeH /// ## epoll_wait的具体实现 38340609970SGnoCiYeH pub fn do_epoll_wait( 38440609970SGnoCiYeH epfd: i32, 38540609970SGnoCiYeH epoll_event: &mut [EPollEvent], 38640609970SGnoCiYeH max_events: i32, 38740609970SGnoCiYeH timespec: Option<TimeSpec>, 38840609970SGnoCiYeH ) -> Result<usize, SystemError> { 38940609970SGnoCiYeH let current_pcb = ProcessManager::current_pcb(); 39040609970SGnoCiYeH let fd_table = current_pcb.fd_table(); 39140609970SGnoCiYeH let fd_table_guard = fd_table.read(); 39240609970SGnoCiYeH 39340609970SGnoCiYeH // 获取epoll文件 39440609970SGnoCiYeH let ep_file = fd_table_guard 39540609970SGnoCiYeH .get_file_by_fd(epfd) 39640609970SGnoCiYeH .ok_or(SystemError::EBADF)?; 39740609970SGnoCiYeH 39840609970SGnoCiYeH drop(fd_table_guard); 39940609970SGnoCiYeH 40040609970SGnoCiYeH // 确保是epoll file 40140609970SGnoCiYeH if !Self::is_epoll_file(&ep_file) { 40240609970SGnoCiYeH return Err(SystemError::EINVAL); 40340609970SGnoCiYeH } 40440609970SGnoCiYeH 40540609970SGnoCiYeH // 从epoll文件获取到epoll 40640609970SGnoCiYeH let mut epolldata = None; 40740609970SGnoCiYeH if let FilePrivateData::EPoll(epoll_data) = &ep_file.lock_irqsave().private_data { 40840609970SGnoCiYeH epolldata = Some(epoll_data.clone()) 40940609970SGnoCiYeH } 41040609970SGnoCiYeH if epolldata.is_some() { 41140609970SGnoCiYeH let epoll_data = epolldata.unwrap(); 41240609970SGnoCiYeH let epoll = epoll_data.epoll.clone(); 41340609970SGnoCiYeH let epoll_guard = epoll.0.lock_irqsave(); 41440609970SGnoCiYeH 41540609970SGnoCiYeH let mut timeout = false; 41640609970SGnoCiYeH if timespec.is_some() { 41740609970SGnoCiYeH let timespec = timespec.unwrap(); 41840609970SGnoCiYeH if !(timespec.tv_sec > 0 || timespec.tv_nsec > 0) { 41940609970SGnoCiYeH // 非阻塞情况 42040609970SGnoCiYeH timeout = true; 42140609970SGnoCiYeH } 42240609970SGnoCiYeH } 42340609970SGnoCiYeH // 判断epoll上有没有就绪事件 42440609970SGnoCiYeH let mut available = epoll_guard.ep_events_available(); 42540609970SGnoCiYeH drop(epoll_guard); 42640609970SGnoCiYeH loop { 42740609970SGnoCiYeH if available { 42840609970SGnoCiYeH // 如果有就绪的事件,则直接返回就绪事件 42940609970SGnoCiYeH return Self::ep_send_events(epoll.clone(), epoll_event, max_events); 43040609970SGnoCiYeH } 43140609970SGnoCiYeH 43240609970SGnoCiYeH if epoll.0.lock_irqsave().shutdown.load(Ordering::SeqCst) { 43340609970SGnoCiYeH // 如果已经关闭 43440609970SGnoCiYeH return Err(SystemError::EBADF); 43540609970SGnoCiYeH } 43640609970SGnoCiYeH 43740609970SGnoCiYeH // 如果超时 43840609970SGnoCiYeH if timeout { 43940609970SGnoCiYeH return Ok(0); 44040609970SGnoCiYeH } 44140609970SGnoCiYeH 44240609970SGnoCiYeH // 自旋等待一段时间 44340609970SGnoCiYeH available = { 44440609970SGnoCiYeH let mut ret = false; 44540609970SGnoCiYeH for _ in 0..50 { 44640609970SGnoCiYeH if let Ok(guard) = epoll.0.try_lock_irqsave() { 44740609970SGnoCiYeH if guard.ep_events_available() { 44840609970SGnoCiYeH ret = true; 44940609970SGnoCiYeH break; 45040609970SGnoCiYeH } 45140609970SGnoCiYeH } 45240609970SGnoCiYeH } 45340609970SGnoCiYeH // 最后再次不使用try_lock尝试 45440609970SGnoCiYeH if !ret { 45540609970SGnoCiYeH ret = epoll.0.lock_irqsave().ep_events_available(); 45640609970SGnoCiYeH } 45740609970SGnoCiYeH ret 45840609970SGnoCiYeH }; 45940609970SGnoCiYeH 46040609970SGnoCiYeH if available { 46140609970SGnoCiYeH continue; 46240609970SGnoCiYeH } 46340609970SGnoCiYeH 46440609970SGnoCiYeH // 如果有未处理的信号则返回错误 46540609970SGnoCiYeH if current_pcb.sig_info().sig_pending().signal().bits() != 0 { 46640609970SGnoCiYeH return Err(SystemError::EINTR); 46740609970SGnoCiYeH } 46840609970SGnoCiYeH 46940609970SGnoCiYeH // 还未等待到事件发生,则睡眠 47040609970SGnoCiYeH // 注册定时器 47140609970SGnoCiYeH let mut timer = None; 47240609970SGnoCiYeH if timespec.is_some() { 47340609970SGnoCiYeH let timespec = timespec.unwrap(); 47440609970SGnoCiYeH let handle = WakeUpHelper::new(current_pcb.clone()); 47540609970SGnoCiYeH let jiffies = next_n_us_timer_jiffies( 47640609970SGnoCiYeH (timespec.tv_sec * 1000000 + timespec.tv_nsec / 1000) as u64, 47740609970SGnoCiYeH ); 47840609970SGnoCiYeH let inner = Timer::new(handle, jiffies); 47940609970SGnoCiYeH inner.activate(); 48040609970SGnoCiYeH timer = Some(inner); 48140609970SGnoCiYeH } 48240609970SGnoCiYeH let guard = epoll.0.lock_irqsave(); 48340609970SGnoCiYeH unsafe { guard.epoll_wq.sleep_without_schedule() }; 48440609970SGnoCiYeH drop(guard); 48540609970SGnoCiYeH sched(); 48640609970SGnoCiYeH // 被唤醒后,检查是否有事件可读 48740609970SGnoCiYeH available = epoll.0.lock_irqsave().ep_events_available(); 48840609970SGnoCiYeH if timer.is_some() { 48940609970SGnoCiYeH if timer.as_ref().unwrap().timeout() { 49040609970SGnoCiYeH // 超时 49140609970SGnoCiYeH timeout = true; 49240609970SGnoCiYeH } else { 49340609970SGnoCiYeH // 未超时,则取消计时器 49440609970SGnoCiYeH timer.unwrap().cancel(); 49540609970SGnoCiYeH } 49640609970SGnoCiYeH } 49740609970SGnoCiYeH } 49840609970SGnoCiYeH } else { 49940609970SGnoCiYeH panic!("An epoll file does not have the corresponding private information"); 50040609970SGnoCiYeH } 50140609970SGnoCiYeH } 50240609970SGnoCiYeH 50340609970SGnoCiYeH /// ## 将已经准备好的事件拷贝到用户空间 50440609970SGnoCiYeH /// 50540609970SGnoCiYeH /// ### 参数 50640609970SGnoCiYeH /// - epoll: 对应的epoll 50740609970SGnoCiYeH /// - user_event: 用户空间传入的epoll_event地址,因为内存对其问题,所以这里需要直接操作地址 50840609970SGnoCiYeH /// - max_events: 处理的最大事件数量 50940609970SGnoCiYeH fn ep_send_events( 51040609970SGnoCiYeH epoll: LockedEventPoll, 51140609970SGnoCiYeH user_event: &mut [EPollEvent], 51240609970SGnoCiYeH max_events: i32, 51340609970SGnoCiYeH ) -> Result<usize, SystemError> { 51440609970SGnoCiYeH let mut ep_guard = epoll.0.lock_irqsave(); 51540609970SGnoCiYeH let mut res: usize = 0; 51640609970SGnoCiYeH 51740609970SGnoCiYeH // 在水平触发模式下,需要将epitem再次加入队列,在下次循环再次判断是否还有事件 51840609970SGnoCiYeH // (所以边缘触发的效率会高于水平触发,但是水平触发某些情况下能够使得更迅速地向用户反馈) 51940609970SGnoCiYeH let mut push_back = Vec::new(); 52040609970SGnoCiYeH while let Some(epitem) = ep_guard.ready_list.pop_front() { 52140609970SGnoCiYeH if res >= max_events as usize { 52240609970SGnoCiYeH push_back.push(epitem); 52340609970SGnoCiYeH break; 52440609970SGnoCiYeH } 52540609970SGnoCiYeH let ep_events = EPollEventType::from_bits_truncate(epitem.event.read().events); 52640609970SGnoCiYeH 52740609970SGnoCiYeH // 再次poll获取事件(为了防止水平触发一直加入队列) 52840609970SGnoCiYeH let revents = epitem.ep_item_poll(); 52940609970SGnoCiYeH if revents.is_empty() { 53040609970SGnoCiYeH continue; 53140609970SGnoCiYeH } 53240609970SGnoCiYeH 53340609970SGnoCiYeH // 构建触发事件结构体 53440609970SGnoCiYeH let event = EPollEvent { 53540609970SGnoCiYeH events: revents.bits, 53640609970SGnoCiYeH data: epitem.event.read().data, 53740609970SGnoCiYeH }; 53840609970SGnoCiYeH 53940609970SGnoCiYeH // 这里是需要判断下一个写入的位置是否为空指针 54040609970SGnoCiYeH 54140609970SGnoCiYeH // TODO:这里有可能会出现事件丢失的情况 54240609970SGnoCiYeH // 如果用户传入的数组长度小于传入的max_event,到这里时如果已经到数组最大长度,但是未到max_event 54340609970SGnoCiYeH // 会出现的问题是我们会把这个数据写入到后面的内存中,用户无法在传入的数组中拿到事件,而且写脏数据到了后面一片内存,导致事件丢失 54440609970SGnoCiYeH // 出现这个问题的几率比较小,首先是因为用户的使用不规范,后因为前面地址校验是按照max_event来校验的,只会在两块内存连着分配时出现,但是也是需要考虑的 54540609970SGnoCiYeH 54640609970SGnoCiYeH // 以下的写法判断并无意义,只是记一下错误处理 54740609970SGnoCiYeH // offset += core::mem::size_of::<EPollEvent>(); 54840609970SGnoCiYeH // if offset >= max_offset { 54940609970SGnoCiYeH // // 当前指向的地址已为空,则把epitem放回队列 55040609970SGnoCiYeH // ep_guard.ready_list.push_back(epitem.clone()); 55140609970SGnoCiYeH // if res == 0 { 55240609970SGnoCiYeH // // 一个都未写入成功,表明用户传进的地址就是有问题的 55340609970SGnoCiYeH // return Err(SystemError::EFAULT); 55440609970SGnoCiYeH // } 55540609970SGnoCiYeH // } 55640609970SGnoCiYeH 55740609970SGnoCiYeH // 拷贝到用户空间 55840609970SGnoCiYeH user_event[res] = event; 55940609970SGnoCiYeH // 记数加一 56040609970SGnoCiYeH res += 1; 56140609970SGnoCiYeH 56240609970SGnoCiYeH // crate::kdebug!("ep send {event:?}"); 56340609970SGnoCiYeH 56440609970SGnoCiYeH if ep_events.contains(EPollEventType::EPOLLONESHOT) { 56540609970SGnoCiYeH let mut event_writer = epitem.event.write(); 56640609970SGnoCiYeH let new_event = event_writer.events & EPollEventType::EP_PRIVATE_BITS.bits; 56740609970SGnoCiYeH event_writer.set_events(new_event); 56840609970SGnoCiYeH } else if !ep_events.contains(EPollEventType::EPOLLET) { 56940609970SGnoCiYeH push_back.push(epitem); 57040609970SGnoCiYeH } 57140609970SGnoCiYeH } 57240609970SGnoCiYeH 57340609970SGnoCiYeH for item in push_back { 57440609970SGnoCiYeH ep_guard.ep_add_ready(item); 57540609970SGnoCiYeH } 57640609970SGnoCiYeH 57740609970SGnoCiYeH Ok(res) 57840609970SGnoCiYeH } 57940609970SGnoCiYeH 58040609970SGnoCiYeH // ### 查看文件是否为epoll文件 58140609970SGnoCiYeH fn is_epoll_file(file: &Arc<SpinLock<File>>) -> bool { 58240609970SGnoCiYeH if let FilePrivateData::EPoll(_) = file.lock_irqsave().private_data { 58340609970SGnoCiYeH return true; 58440609970SGnoCiYeH } 58540609970SGnoCiYeH return false; 58640609970SGnoCiYeH } 58740609970SGnoCiYeH 58840609970SGnoCiYeH fn ep_insert( 58940609970SGnoCiYeH epoll_guard: &mut SpinLockGuard<EventPoll>, 59040609970SGnoCiYeH dst_file: Arc<SpinLock<File>>, 59140609970SGnoCiYeH epitem: Arc<EPollItem>, 59240609970SGnoCiYeH ) -> Result<(), SystemError> { 59340609970SGnoCiYeH if Self::is_epoll_file(&dst_file) { 59440609970SGnoCiYeH return Err(SystemError::ENOSYS); 59540609970SGnoCiYeH // TODO:现在的实现先不考虑嵌套其它类型的文件(暂时只针对socket),这里的嵌套指epoll/select/poll 59640609970SGnoCiYeH } 59740609970SGnoCiYeH 59840609970SGnoCiYeH let test_poll = dst_file.lock_irqsave().poll(); 59940609970SGnoCiYeH if test_poll.is_err() { 60040609970SGnoCiYeH if test_poll.unwrap_err() == SystemError::EOPNOTSUPP_OR_ENOTSUP { 60140609970SGnoCiYeH // 如果目标文件不支持poll 60240609970SGnoCiYeH return Err(SystemError::ENOSYS); 60340609970SGnoCiYeH } 60440609970SGnoCiYeH } 60540609970SGnoCiYeH 60640609970SGnoCiYeH epoll_guard.ep_items.insert(epitem.fd, epitem.clone()); 60740609970SGnoCiYeH 60840609970SGnoCiYeH // 检查文件是否已经有事件发生 60940609970SGnoCiYeH let event = epitem.ep_item_poll(); 61040609970SGnoCiYeH if !event.is_empty() { 61140609970SGnoCiYeH // 加入到就绪队列 61240609970SGnoCiYeH epoll_guard.ep_add_ready(epitem.clone()); 61340609970SGnoCiYeH 61440609970SGnoCiYeH epoll_guard.ep_wake_one(); 61540609970SGnoCiYeH } 61640609970SGnoCiYeH 61740609970SGnoCiYeH // TODO: 嵌套epoll? 61840609970SGnoCiYeH 61940609970SGnoCiYeH // 这个标志是用与电源管理相关,暂时不支持 62040609970SGnoCiYeH if epitem.event.read().events & EPollEventType::EPOLLWAKEUP.bits() != 0 { 62140609970SGnoCiYeH return Err(SystemError::ENOSYS); 62240609970SGnoCiYeH } 62340609970SGnoCiYeH 62440609970SGnoCiYeH dst_file.lock_irqsave().add_epoll(epitem.clone())?; 62540609970SGnoCiYeH Ok(()) 62640609970SGnoCiYeH } 62740609970SGnoCiYeH 62840609970SGnoCiYeH pub fn ep_remove( 62940609970SGnoCiYeH epoll: &mut SpinLockGuard<EventPoll>, 63040609970SGnoCiYeH fd: i32, 63140609970SGnoCiYeH dst_file: Option<Arc<SpinLock<File>>>, 63240609970SGnoCiYeH ) -> Result<(), SystemError> { 63340609970SGnoCiYeH if dst_file.is_some() { 63440609970SGnoCiYeH let dst_file = dst_file.unwrap(); 63540609970SGnoCiYeH let mut file_guard = dst_file.lock_irqsave(); 63640609970SGnoCiYeH 63740609970SGnoCiYeH file_guard.remove_epoll(epoll.self_ref.as_ref().unwrap())?; 63840609970SGnoCiYeH } 63940609970SGnoCiYeH 64040609970SGnoCiYeH let epitem = epoll.ep_items.remove(&fd).unwrap(); 64140609970SGnoCiYeH 64240609970SGnoCiYeH let _ = epoll 64340609970SGnoCiYeH .ready_list 64440609970SGnoCiYeH .extract_if(|item| Arc::ptr_eq(item, &epitem)); 64540609970SGnoCiYeH 64640609970SGnoCiYeH Ok(()) 64740609970SGnoCiYeH } 64840609970SGnoCiYeH 64940609970SGnoCiYeH /// ## 修改已经注册的监听事件 65040609970SGnoCiYeH /// 65140609970SGnoCiYeH /// ### 参数 65240609970SGnoCiYeH /// - epoll_guard: EventPoll的锁 65340609970SGnoCiYeH /// - epitem: 需要修改的描述符对应的epitem 65440609970SGnoCiYeH /// - event: 新的事件 65540609970SGnoCiYeH fn ep_modify( 65640609970SGnoCiYeH epoll_guard: &mut SpinLockGuard<EventPoll>, 65740609970SGnoCiYeH epitem: Arc<EPollItem>, 65840609970SGnoCiYeH event: &EPollEvent, 65940609970SGnoCiYeH ) -> Result<(), SystemError> { 66040609970SGnoCiYeH let mut epi_event_guard = epitem.event.write(); 66140609970SGnoCiYeH 66240609970SGnoCiYeH // 修改epitem 66340609970SGnoCiYeH epi_event_guard.events = event.events; 66440609970SGnoCiYeH epi_event_guard.data = event.data; 66540609970SGnoCiYeH 66640609970SGnoCiYeH drop(epi_event_guard); 66740609970SGnoCiYeH // 修改后检查文件是否已经有感兴趣事件发生 66840609970SGnoCiYeH let event = epitem.ep_item_poll(); 66940609970SGnoCiYeH if !event.is_empty() { 67040609970SGnoCiYeH epoll_guard.ep_add_ready(epitem.clone()); 67140609970SGnoCiYeH 67240609970SGnoCiYeH epoll_guard.ep_wake_one(); 67340609970SGnoCiYeH } 67440609970SGnoCiYeH // TODO:处理EPOLLWAKEUP,目前不支持 67540609970SGnoCiYeH 67640609970SGnoCiYeH Ok(()) 67740609970SGnoCiYeH } 67840609970SGnoCiYeH 67940609970SGnoCiYeH /// ### 判断epoll是否有就绪item 68040609970SGnoCiYeH pub fn ep_events_available(&self) -> bool { 68140609970SGnoCiYeH !self.ready_list.is_empty() 68240609970SGnoCiYeH } 68340609970SGnoCiYeH 68440609970SGnoCiYeH /// ### 将epitem加入到就绪队列,如果为重复添加则忽略 68540609970SGnoCiYeH pub fn ep_add_ready(&mut self, epitem: Arc<EPollItem>) { 68640609970SGnoCiYeH let ret = self.ready_list.iter().find(|epi| Arc::ptr_eq(epi, &epitem)); 68740609970SGnoCiYeH 68840609970SGnoCiYeH if ret.is_none() { 68940609970SGnoCiYeH self.ready_list.push_back(epitem); 69040609970SGnoCiYeH } 69140609970SGnoCiYeH } 69240609970SGnoCiYeH 69340609970SGnoCiYeH /// ### 判断该epoll上是否有进程在等待 69440609970SGnoCiYeH pub fn ep_has_waiter(&self) -> bool { 69540609970SGnoCiYeH self.epoll_wq.len() != 0 69640609970SGnoCiYeH } 69740609970SGnoCiYeH 69840609970SGnoCiYeH /// ### 唤醒所有在epoll上等待的进程 69940609970SGnoCiYeH pub fn ep_wake_all(&self) { 70040609970SGnoCiYeH self.epoll_wq.wakeup_all(None); 70140609970SGnoCiYeH } 70240609970SGnoCiYeH 70340609970SGnoCiYeH /// ### 唤醒所有在epoll上等待的首个进程 70440609970SGnoCiYeH pub fn ep_wake_one(&self) { 70540609970SGnoCiYeH self.epoll_wq.wakeup(None); 70640609970SGnoCiYeH } 70740609970SGnoCiYeH } 70840609970SGnoCiYeH 70940609970SGnoCiYeH /// 与C兼容的Epoll事件结构体 71040609970SGnoCiYeH #[derive(Copy, Clone, Default)] 71140609970SGnoCiYeH #[repr(packed)] 71240609970SGnoCiYeH pub struct EPollEvent { 71340609970SGnoCiYeH /// 表示触发的事件 71440609970SGnoCiYeH events: u32, 71540609970SGnoCiYeH /// 内核态不使用该字段,该字段由用户态自由使用,在事件发生时内核将会原样返回 71640609970SGnoCiYeH data: u64, 71740609970SGnoCiYeH } 71840609970SGnoCiYeH 71940609970SGnoCiYeH impl Debug for EPollEvent { 72040609970SGnoCiYeH fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 72140609970SGnoCiYeH let events = self.events; 72240609970SGnoCiYeH let u64 = self.data; 72340609970SGnoCiYeH f.debug_struct("epoll_event") 72440609970SGnoCiYeH .field("events", &events) 72540609970SGnoCiYeH .field("data", &u64) 72640609970SGnoCiYeH .finish() 72740609970SGnoCiYeH } 72840609970SGnoCiYeH } 72940609970SGnoCiYeH 73040609970SGnoCiYeH impl EPollEvent { 73140609970SGnoCiYeH pub fn set_events(&mut self, events: u32) { 73240609970SGnoCiYeH self.events = events; 73340609970SGnoCiYeH } 73440609970SGnoCiYeH 73540609970SGnoCiYeH pub fn events(&self) -> u32 { 73640609970SGnoCiYeH self.events 73740609970SGnoCiYeH } 73840609970SGnoCiYeH } 73940609970SGnoCiYeH 74040609970SGnoCiYeH /// ## epoll_ctl函数的参数 74140609970SGnoCiYeH #[derive(Debug, PartialEq)] 74240609970SGnoCiYeH pub enum EPollCtlOption { 74340609970SGnoCiYeH /// 注册新的文件描述符到epfd 74440609970SGnoCiYeH EpollCtlAdd, 74540609970SGnoCiYeH /// 将对应的文件描述符从epfd中删除 74640609970SGnoCiYeH EpollCtlDel, 74740609970SGnoCiYeH /// 修改已经注册的文件描述符的监听事件 74840609970SGnoCiYeH EpollCtlMod, 74940609970SGnoCiYeH } 75040609970SGnoCiYeH 75140609970SGnoCiYeH impl EPollCtlOption { 75240609970SGnoCiYeH pub fn from_op_num(op: usize) -> Result<Self, SystemError> { 75340609970SGnoCiYeH match op { 75440609970SGnoCiYeH 1 => Ok(Self::EpollCtlAdd), 75540609970SGnoCiYeH 2 => Ok(Self::EpollCtlDel), 75640609970SGnoCiYeH 3 => Ok(Self::EpollCtlMod), 75740609970SGnoCiYeH _ => Err(SystemError::EINVAL), 75840609970SGnoCiYeH } 75940609970SGnoCiYeH } 76040609970SGnoCiYeH } 76140609970SGnoCiYeH 76240609970SGnoCiYeH bitflags! { 76340609970SGnoCiYeH #[allow(dead_code)] 76440609970SGnoCiYeH pub struct EPollEventType: u32 { 76540609970SGnoCiYeH /// 对应的描述符有新的数据可读时会触发 76640609970SGnoCiYeH const EPOLLIN = 0x00000001; 76740609970SGnoCiYeH /// 对应的描述符有紧急数据可读时会触发 76840609970SGnoCiYeH const EPOLLPRI = 0x00000002; 76940609970SGnoCiYeH /// 对应的描述符可以写入数据时会触发 77040609970SGnoCiYeH const EPOLLOUT = 0x00000004; 77140609970SGnoCiYeH /// 对应的描述符发生错误时会触发 77240609970SGnoCiYeH const EPOLLERR = 0x00000008; 77340609970SGnoCiYeH /// 对应的描述符被挂断(连接关闭)时会触发 77440609970SGnoCiYeH const EPOLLHUP = 0x00000010; 77540609970SGnoCiYeH /// 对应的描述符不是一个有效的文件描述符时会触发 77640609970SGnoCiYeH const EPOLLNVAL = 0x00000020; 77740609970SGnoCiYeH /// 普通数据可读,类似于`EPOLLIN` 77840609970SGnoCiYeH const EPOLLRDNORM = 0x00000040; 77940609970SGnoCiYeH /// 优先级带外数据可读 78040609970SGnoCiYeH const EPOLLRDBAND = 0x00000080; 78140609970SGnoCiYeH /// 普通数据可写,类似于'EPOLLOUT' 78240609970SGnoCiYeH const EPOLLWRNORM = 0x00000100; 78340609970SGnoCiYeH /// 优先级带外数据可写 78440609970SGnoCiYeH const EPOLLWRBAND = 0x00000200; 78540609970SGnoCiYeH /// 通过消息队列收到消息时会触 78640609970SGnoCiYeH const EPOLLMSG = 0x00000400; 78740609970SGnoCiYeH /// 对应的描述符被挂断(连接关闭)的一端发送了 FIN 时会触发(读关闭) 78840609970SGnoCiYeH const EPOLLRDHUP = 0x00002000; 78940609970SGnoCiYeH 79040609970SGnoCiYeH /// 以下为额外选项 79140609970SGnoCiYeH /// 79240609970SGnoCiYeH /// 特定选项,用于异步 I/O,目前未实现 79340609970SGnoCiYeH const EPOLL_URING_WAKE = 1u32 << 27; 79440609970SGnoCiYeH /// 设置epoll为独占模式 79540609970SGnoCiYeH const EPOLLEXCLUSIVE = 1u32 << 28; 79640609970SGnoCiYeH /// 允许在系统挂起时唤醒 epoll,通常用于通过 eventfd 或 timerfd 唤醒 epoll,(通常与电源管理相关,未实现) 79740609970SGnoCiYeH const EPOLLWAKEUP = 1u32 << 29; 79840609970SGnoCiYeH /// 表示只监听一次事件,之后需要重新添加 79940609970SGnoCiYeH const EPOLLONESHOT = 1u32 << 30; 80040609970SGnoCiYeH 80140609970SGnoCiYeH /// 启用边缘触发模式(即只有下次触发事件时才会通过epoll_wait返回), 80240609970SGnoCiYeH /// 对应为水平触发(默认),水平触发模式下若这次未处理完数据,那epoll还会将其加入自己的就绪队列 80340609970SGnoCiYeH const EPOLLET = 1u32 << 31; 80440609970SGnoCiYeH 80540609970SGnoCiYeH /// 以下为组合码 80640609970SGnoCiYeH const EPOLLINOUT_BITS = Self::EPOLLIN.bits | Self::EPOLLOUT.bits; 80740609970SGnoCiYeH const EPOLLEXCLUSIVE_OK_BITS = 80840609970SGnoCiYeH Self::EPOLLINOUT_BITS.bits 80940609970SGnoCiYeH | Self::EPOLLERR.bits 81040609970SGnoCiYeH | Self::EPOLLHUP.bits 81140609970SGnoCiYeH | Self::EPOLLWAKEUP.bits 81240609970SGnoCiYeH | Self::EPOLLET.bits 81340609970SGnoCiYeH | Self::EPOLLEXCLUSIVE.bits; 81440609970SGnoCiYeH 81540609970SGnoCiYeH const EP_PRIVATE_BITS = 81640609970SGnoCiYeH Self::EPOLLWAKEUP.bits 81740609970SGnoCiYeH | Self::EPOLLONESHOT.bits 81840609970SGnoCiYeH | Self::EPOLLET.bits 81940609970SGnoCiYeH | Self::EPOLLEXCLUSIVE.bits; 82040609970SGnoCiYeH 82140609970SGnoCiYeH /// 表示epoll已经被释放,但是在目前的设计中未用到 82240609970SGnoCiYeH const POLLFREE = 0x4000; 82340609970SGnoCiYeH } 82440609970SGnoCiYeH } 825