|
| 1 | +//! Functionality that is only available on Windows. |
| 2 | +
|
| 3 | +use crate::reactor::{Reactor, Readable, Registration}; |
| 4 | +use crate::Async; |
| 5 | + |
| 6 | +use std::convert::TryFrom; |
| 7 | +use std::future::Future; |
| 8 | +use std::io::{self, Result}; |
| 9 | +use std::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle, OwnedHandle, RawHandle}; |
| 10 | +use std::pin::Pin; |
| 11 | +use std::task::{Context, Poll}; |
| 12 | + |
| 13 | +/// A waitable handle registered in the reactor. |
| 14 | +/// |
| 15 | +/// Some handles in Windows are “waitable”, which means that they emit a “readiness” signal after some event occurs. This function can be used to wait for such events to occur on a handle. This function can be used in addition to regular socket polling. |
| 16 | +/// |
| 17 | +/// Waitable objects include the following: |
| 18 | +/// |
| 19 | +/// - Console inputs |
| 20 | +/// - Waitable events |
| 21 | +/// - Mutexes |
| 22 | +/// - Processes |
| 23 | +/// - Semaphores |
| 24 | +/// - Threads |
| 25 | +/// - Timer |
| 26 | +/// |
| 27 | +/// This structure can be used to wait for any of these objects to become ready. |
| 28 | +/// |
| 29 | +/// ## Implementation |
| 30 | +/// |
| 31 | +/// The current implementation waits on the handle by registering it in the application-global |
| 32 | +/// Win32 threadpool. However, in the futur it may be possible to migrate to an implementation |
| 33 | +/// on Windows 10 that uses a mechanism similar to [`MsgWaitForMultipleObjectsEx`]. |
| 34 | +/// |
| 35 | +/// [`MsgWaitForMultipleObjectsEx`]: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-msgwaitformultipleobjectsex |
| 36 | +/// |
| 37 | +/// ## Caveats |
| 38 | +/// |
| 39 | +/// Read the documentation for the [`Async`](crate::Async) type for more information regarding the |
| 40 | +/// abilities and caveats with using this type. |
| 41 | +#[derive(Debug)] |
| 42 | +pub struct Waitable<T>(Async<T>); |
| 43 | + |
| 44 | +impl<T> AsRef<T> for Waitable<T> { |
| 45 | + fn as_ref(&self) -> &T { |
| 46 | + self.0.as_ref() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl<T: AsHandle> Waitable<T> { |
| 51 | + /// Create a new [`Waitable`] around a waitable handle. |
| 52 | + /// |
| 53 | + /// # Examples |
| 54 | + /// |
| 55 | + /// ```no_run |
| 56 | + /// use std::process::Command; |
| 57 | + /// use async_io::os::windows::Waitable; |
| 58 | + /// |
| 59 | + /// // Create a new process to wait for. |
| 60 | + /// let mut child = Command::new("sleep").arg("5").spawn().unwrap(); |
| 61 | + /// |
| 62 | + /// // Wrap the process in an `Async` object that waits for it to exit. |
| 63 | + /// let process = Waitable::new(child).unwrap(); |
| 64 | + /// |
| 65 | + /// // Wait for the process to exit. |
| 66 | + /// # async_io::block_on(async { |
| 67 | + /// process.ready().await.unwrap(); |
| 68 | + /// # }); |
| 69 | + /// ``` |
| 70 | + pub fn new(handle: T) -> Result<Self> { |
| 71 | + Ok(Self(Async { |
| 72 | + source: Reactor::get() |
| 73 | + .insert_io(unsafe { Registration::new_waitable(handle.as_handle()) })?, |
| 74 | + io: Some(handle), |
| 75 | + })) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<T: AsRawHandle> AsRawHandle for Waitable<T> { |
| 80 | + fn as_raw_handle(&self) -> RawHandle { |
| 81 | + self.get_ref().as_raw_handle() |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl<T: AsHandle> AsHandle for Waitable<T> { |
| 86 | + fn as_handle(&self) -> BorrowedHandle<'_> { |
| 87 | + self.get_ref().as_handle() |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl<T: AsHandle + From<OwnedHandle>> TryFrom<OwnedHandle> for Waitable<T> { |
| 92 | + type Error = io::Error; |
| 93 | + |
| 94 | + fn try_from(handle: OwnedHandle) -> Result<Self> { |
| 95 | + Self::new(handle.into()) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl<T: Into<OwnedHandle>> TryFrom<Waitable<T>> for OwnedHandle { |
| 100 | + type Error = io::Error; |
| 101 | + |
| 102 | + fn try_from(value: Waitable<T>) -> std::result::Result<Self, Self::Error> { |
| 103 | + value.into_inner().map(|handle| handle.into()) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl<T> Waitable<T> { |
| 108 | + /// Get a reference to the inner handle. |
| 109 | + pub fn get_ref(&self) -> &T { |
| 110 | + self.0.get_ref() |
| 111 | + } |
| 112 | + |
| 113 | + /// Get a mutable reference to the inner handle. |
| 114 | + /// |
| 115 | + /// # Safety |
| 116 | + /// |
| 117 | + /// The underlying I/O source must not be dropped or moved out using this function. |
| 118 | + pub unsafe fn get_mut(&mut self) -> &mut T { |
| 119 | + self.0.get_mut() |
| 120 | + } |
| 121 | + |
| 122 | + /// Consumes the [`Waitable`], returning the inner handle. |
| 123 | + pub fn into_inner(self) -> Result<T> { |
| 124 | + self.0.into_inner() |
| 125 | + } |
| 126 | + |
| 127 | + /// Waits until the [`Waitable`] object is ready. |
| 128 | + /// |
| 129 | + /// This method completes when the underlying [`Waitable`] object has completed. See the documentation |
| 130 | + /// for the [`Waitable`] object for more information. |
| 131 | + /// |
| 132 | + /// # Examples |
| 133 | + /// |
| 134 | + /// ```no_run |
| 135 | + /// use std::process::Command; |
| 136 | + /// use async_io::os::windows::Waitable; |
| 137 | + /// |
| 138 | + /// # futures_lite::future::block_on(async { |
| 139 | + /// let child = Command::new("sleep").arg("5").spawn()?; |
| 140 | + /// let process = Waitable::new(child)?; |
| 141 | + /// |
| 142 | + /// // Wait for the process to exit. |
| 143 | + /// process.ready().await?; |
| 144 | + /// # std::io::Result::Ok(()) }); |
| 145 | + /// ``` |
| 146 | + pub fn ready(&self) -> Ready<'_, T> { |
| 147 | + Ready(self.0.readable()) |
| 148 | + } |
| 149 | + |
| 150 | + /// Polls the I/O handle for readiness. |
| 151 | + /// |
| 152 | + /// When this method returns [`Poll::Ready`], that means that the OS has delivered a notification |
| 153 | + /// that the underlying [`Waitable`] object is ready. See the documentation for the [`Waitable`] |
| 154 | + /// object for more information. |
| 155 | + /// |
| 156 | + /// # Caveats |
| 157 | + /// |
| 158 | + /// Two different tasks should not call this method concurrently. Otherwise, conflicting tasks |
| 159 | + /// will just keep waking each other in turn, thus wasting CPU time. |
| 160 | + /// |
| 161 | + /// # Examples |
| 162 | + /// |
| 163 | + /// ```no_run |
| 164 | + /// use std::process::Command; |
| 165 | + /// use async_io::os::windows::Waitable; |
| 166 | + /// use futures_lite::future; |
| 167 | + /// |
| 168 | + /// # futures_lite::future::block_on(async { |
| 169 | + /// let child = Command::new("sleep").arg("5").spawn()?; |
| 170 | + /// let process = Waitable::new(child)?; |
| 171 | + /// |
| 172 | + /// // Wait for the process to exit. |
| 173 | + /// future::poll_fn(|cx| process.poll_ready(cx)).await?; |
| 174 | + /// # std::io::Result::Ok(()) }); |
| 175 | + /// ``` |
| 176 | + pub fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<()>> { |
| 177 | + self.0.poll_readable(cx) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +/// Future for [`Filter::ready`]. |
| 182 | +#[must_use = "futures do nothing unless you `.await` or poll them"] |
| 183 | +#[derive(Debug)] |
| 184 | +pub struct Ready<'a, T>(Readable<'a, T>); |
| 185 | + |
| 186 | +impl<T> Future for Ready<'_, T> { |
| 187 | + type Output = Result<()>; |
| 188 | + |
| 189 | + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 190 | + Pin::new(&mut self.0).poll(cx) |
| 191 | + } |
| 192 | +} |
0 commit comments