执行器和系统 IO
在The Future
Trait 的上一章节中,我们讨论了这个 Future 在套接字上,执行异步读取的示例:
pub struct SocketRead<'a> {
socket: &'a Socket,
}
impl SimpleFuture for SocketRead<'_> {
type Output = Vec<u8>;
fn poll(&mut self, wake: fn()) -> Poll<Self::Output> {
if self.socket.has_data_to_read() {
// The socket has data-- read it into a buffer and return it.
Poll::Ready(self.socket.read_buf())
} else {
// The socket does not yet have data.
//
// Arrange for `wake` to be called once data is available.
// When data becomes available, `wake` will be called, and the
// user of this `Future` will know to call `poll` again and
// receive data.
self.socket.set_readable_callback(wake);
Poll::Pending
}
}
}
这个 Future 将读取套接字上的可用数据,如果没有可用数据,它将交还给 executor,要求在套接字再次变得可读时,唤醒这个任务。但是,根据此示例尚不清楚,这个Socket
类型是怎么实现的,尤其是set_readable_callback
函数是如何工作的。我们如何安排lw.wake()
,在一旦套接字变得可读时,就被调用?一种选择是,让一个线程不断检查socket
是否可读,在适当的时候调用wake()
。但是,这将是非常低效的,需要为每个阻塞的 IO Future 使用一个单独的线程。这将大大降低我们异步代码的效率。
实际上,此问题是通过与 IO-感知系统阻塞原语交互来解决。例如,epoll
在 Linux 上,kqueue
在 FreeBSD 和 Mac OS 上,在 Windows 上为 IOCP,以及 Fuchsia 的port
(所有这些都通过跨平台的 Rust 箱子mio
揭露)。这些原语都允许一个线程,在多个异步 IO 事件上阻塞,并在事件的其中一个完成后返回。实际上,这些 API 通常如下所示:
struct IoBlocker {
...
}
struct Event {
// An ID uniquely identifying the event that occurred and was listened for.
id: usize,
// A set of signals to wait for, or which occurred.
signals: Signals,
}
impl IoBlocker {
/// Create a new collection of asynchronous IO events to block on.
fn new() -> Self { ... }
/// Express an interest in a particular IO event.
fn add_io_event_interest(
&self,
/// The object on which the event will occur
io_object: &IoObject,
/// A set of signals that may appear on the `io_object` for
/// which an event should be triggered, paired with
/// an ID to give to events that result from this interest.
event: Event,
) { ... }
/// Block until one of the events occurs.
fn block(&self) -> Event { ... }
}
let mut io_blocker = IoBlocker::new();
io_blocker.add_io_event_interest(
&socket_1,
Event { id: 1, signals: READABLE },
);
io_blocker.add_io_event_interest(
&socket_2,
Event { id: 2, signals: READABLE | WRITABLE },
);
let event = io_blocker.block();
// prints e.g. "Socket 1 is now READABLE" if socket one became readable.
println!("Socket {:?} is now {:?}", event.id, event.signals);
Future executor 可以使用这些原语来提供异步 IO 对象(例如套接字),这些对象可以配置,在发生特定 IO 事件时,运行的回调。在我们上面例子的SocketRead
情况下Socket::set_readable_callback
函数可能类似于以下伪代码:
impl Socket {
fn set_readable_callback(&self, waker: Waker) {
// `local_executor` is a reference to the local executor.
// this could be provided at creation of the socket, but in practice
// many executor implementations pass it down through thread local
// storage for convenience.
let local_executor = self.local_executor;
// Unique ID for this IO object.
let id = self.id;
// Store the local waker in the executor's map so that it can be called
// once the IO event arrives.
local_executor.event_map.insert(id, waker);
local_executor.add_io_event_interest(
&self.socket_file_descriptor,
Event { id, signals: READABLE },
);
}
}
现在,我们只有一个 executor 线程,该线程可以接收任何 IO 事件,并将 IO 事件分配给相应的Waker
,这将唤醒相应的任务,从而使 executor 在返回以检查更多 IO 事件之前,可以驱使更多任务驶向完成,(且该循环会继续...)。