diff options
| author | zy <[email protected]> | 2023-08-17 10:54:34 +0000 |
|---|---|---|
| committer | zy <[email protected]> | 2023-08-17 10:54:34 +0000 |
| commit | 9b6de7062e421a3d50d6ff4114b3a3408e906da4 (patch) | |
| tree | f003f8be5617f746dde3f3f4230048121f84dcde | |
| parent | 903e822ad10befec177827f50e00ca0f53cb3e58 (diff) | |
更多注释..
| -rw-r--r-- | src/executor.rs | 10 | ||||
| -rw-r--r-- | src/reactor.rs | 16 |
2 files changed, 16 insertions, 10 deletions
diff --git a/src/executor.rs b/src/executor.rs index 268c625..9832d88 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -12,17 +12,19 @@ use futures::{future::LocalBoxFuture, Future, FutureExt}; use crate::reactor::Reactor; -// scoped_thread_local宏创建的 本地线程独享的变量 +// scoped_thread_local 宏创建的 本地线程独享的变量 // EX Executor 类型实例 scoped_tls::scoped_thread_local!(pub(crate) static EX: Executor); pub struct Executor { local_queue: TaskQueue, // 任务队列 - // 带一个 pub(crate) 限制 + // pub(crate) 作用域 + // Rc 和 RefCell: 多个所有者 且 可变 pub(crate) reactor: Rc<RefCell<Reactor>>, /// Make sure the type is `!Send` and `!Sync`. - /// 不会占用任何内存,但类型是 Rc<()>,编译时就确保不会实现 Send 和 Sync + /// 将 Executor 当作 Rc<()>, Rc 非线程安全,这样编译器就不会自动实现 Send 和 Sync + /// Send 和 Sync 跨线程才有, thread-per-core 不需要 _marker: PhantomData<Rc<()>>, } @@ -40,7 +42,7 @@ impl Executor { local_queue: TaskQueue::default(), reactor: Rc::new(RefCell::new(Reactor::default())), - _marker: PhantomData, + _marker: PhantomData, // 自动实现 Pin } } // 创建新 task diff --git a/src/reactor.rs b/src/reactor.rs index 9bec3e1..0e4f261 100644 --- a/src/reactor.rs +++ b/src/reactor.rs @@ -7,17 +7,21 @@ use std::{ use polling::{Event, Poller}; -#[inline] +#[inline] // 内联 pub(crate) fn get_reactor() -> Rc<RefCell<Reactor>> { - crate::executor::EX.with(|ex| ex.reactor.clone()) + // create: 当前根模块 + // executor: 模块名 + // EX 静态变量 + // reactor 本身是 Rc<RefCell<Reactor>>, 所以 clone 的是指向 实例 的引用 + crate::executor::EX.with(|ex| ex.reactor.clone()) // } #[derive(Debug)] pub struct Reactor { poller: Poller, // epoll 的包装 - waker_mapping: rustc_hash::FxHashMap<u64, Waker>, // token -> waker 的映射, slab 更加常见 + waker_mapping: rustc_hash::FxHashMap<u64, Waker>, // token(io事件) -> waker 的映射, slab 更加常见 - buffer: Vec<Event>, // 事件缓冲区 + buffer: Vec<Event>, // IO 事件缓冲区 } impl Reactor { @@ -63,13 +67,13 @@ impl Reactor { self.poller.modify(fd, event); // 添加监听 } - pub fn wait(&mut self) { + pub fn wait(&mut self) { // 等价于 epoll_wait 阻塞 println!("[reactor] waiting"); self.poller.wait(&mut self.buffer, None); // epoll_wait println!("[reactor] wait done"); // 已经有事件了 for i in 0..self.buffer.len() { // 遍历事件 buffer - let event = self.buffer.swap_remove(0); // 取出事件 + let event = self.buffer.swap_remove(0); // 队列删除并返回 if event.readable { // 可读事件 if let Some(waker) = self.waker_mapping.remove(&(event.key as u64 * 2)) { println!( |
