| 1 | //! Functionality that is only available for IOCP-based platforms. |
| 2 | |
| 3 | pub use crate::sys::CompletionPacket; |
| 4 | |
| 5 | use super::__private::PollerSealed; |
| 6 | use crate::{Event, PollMode, Poller}; |
| 7 | |
| 8 | use std::io; |
| 9 | use std::os::windows::io::{AsRawHandle, RawHandle}; |
| 10 | use std::os::windows::prelude::{AsHandle, BorrowedHandle}; |
| 11 | |
| 12 | /// Extension trait for the [`Poller`] type that provides functionality specific to IOCP-based |
| 13 | /// platforms. |
| 14 | /// |
| 15 | /// [`Poller`]: crate::Poller |
| 16 | pub trait PollerIocpExt: PollerSealed { |
| 17 | /// Post a new [`Event`] to the poller. |
| 18 | /// |
| 19 | /// # Examples |
| 20 | /// |
| 21 | /// ```rust |
| 22 | /// use polling::{Poller, Event, Events}; |
| 23 | /// use polling::os::iocp::{CompletionPacket, PollerIocpExt}; |
| 24 | /// |
| 25 | /// use std::thread; |
| 26 | /// use std::sync::Arc; |
| 27 | /// use std::time::Duration; |
| 28 | /// |
| 29 | /// # fn main() -> std::io::Result<()> { |
| 30 | /// // Spawn a thread to wake us up after 100ms. |
| 31 | /// let poller = Arc::new(Poller::new()?); |
| 32 | /// thread::spawn({ |
| 33 | /// let poller = poller.clone(); |
| 34 | /// move || { |
| 35 | /// let packet = CompletionPacket::new(Event::readable(0)); |
| 36 | /// thread::sleep(Duration::from_millis(100)); |
| 37 | /// poller.post(packet).unwrap(); |
| 38 | /// } |
| 39 | /// }); |
| 40 | /// |
| 41 | /// // Wait for the event. |
| 42 | /// let mut events = Events::new(); |
| 43 | /// poller.wait(&mut events, None)?; |
| 44 | /// |
| 45 | /// assert_eq!(events.len(), 1); |
| 46 | /// # Ok(()) } |
| 47 | /// ``` |
| 48 | fn post(&self, packet: CompletionPacket) -> io::Result<()>; |
| 49 | |
| 50 | /// Add a waitable handle to this poller. |
| 51 | /// |
| 52 | /// Some handles in Windows are "waitable", which means that they emit a "readiness" signal |
| 53 | /// after some event occurs. This function can be used to wait for such events to occur |
| 54 | /// on a handle. This function can be used in addition to regular socket polling. |
| 55 | /// |
| 56 | /// Waitable objects include the following: |
| 57 | /// |
| 58 | /// - Console inputs |
| 59 | /// - Waitable events |
| 60 | /// - Mutexes |
| 61 | /// - Processes |
| 62 | /// - Semaphores |
| 63 | /// - Threads |
| 64 | /// - Timer |
| 65 | /// |
| 66 | /// Once the object has been signalled, the poller will emit the `interest` event. |
| 67 | /// |
| 68 | /// # Safety |
| 69 | /// |
| 70 | /// The added handle must not be dropped before it is deleted. |
| 71 | /// |
| 72 | /// # Examples |
| 73 | /// |
| 74 | /// ```no_run |
| 75 | /// use polling::{Poller, Event, Events, PollMode}; |
| 76 | /// use polling::os::iocp::PollerIocpExt; |
| 77 | /// |
| 78 | /// use std::process::Command; |
| 79 | /// |
| 80 | /// // Spawn a new process. |
| 81 | /// let mut child = Command::new("echo") |
| 82 | /// .arg("Hello, world!") |
| 83 | /// .spawn() |
| 84 | /// .unwrap(); |
| 85 | /// |
| 86 | /// // Create a new poller. |
| 87 | /// let poller = Poller::new().unwrap(); |
| 88 | /// |
| 89 | /// // Add the child process to the poller. |
| 90 | /// unsafe { |
| 91 | /// poller.add_waitable(&child, Event::all(0), PollMode::Oneshot).unwrap(); |
| 92 | /// } |
| 93 | /// |
| 94 | /// // Wait for the child process to exit. |
| 95 | /// let mut events = Events::new(); |
| 96 | /// poller.wait(&mut events, None).unwrap(); |
| 97 | /// |
| 98 | /// assert_eq!(events.len(), 1); |
| 99 | /// assert_eq!(events.iter().next().unwrap(), Event::all(0)); |
| 100 | /// ``` |
| 101 | unsafe fn add_waitable( |
| 102 | &self, |
| 103 | handle: impl AsRawWaitable, |
| 104 | interest: Event, |
| 105 | mode: PollMode, |
| 106 | ) -> io::Result<()>; |
| 107 | |
| 108 | /// Modify an existing waitable handle. |
| 109 | /// |
| 110 | /// This function can be used to change the emitted event and/or mode of an existing waitable |
| 111 | /// handle. The handle must have been previously added to the poller using [`add_waitable`]. |
| 112 | /// |
| 113 | /// [`add_waitable`]: Self::add_waitable |
| 114 | /// |
| 115 | /// # Examples |
| 116 | /// |
| 117 | /// ```no_run |
| 118 | /// use polling::{Poller, Event, Events, PollMode}; |
| 119 | /// use polling::os::iocp::PollerIocpExt; |
| 120 | /// |
| 121 | /// use std::process::Command; |
| 122 | /// |
| 123 | /// // Spawn a new process. |
| 124 | /// let mut child = Command::new("echo") |
| 125 | /// .arg("Hello, world!") |
| 126 | /// .spawn() |
| 127 | /// .unwrap(); |
| 128 | /// |
| 129 | /// // Create a new poller. |
| 130 | /// let poller = Poller::new().unwrap(); |
| 131 | /// |
| 132 | /// // Add the child process to the poller. |
| 133 | /// unsafe { |
| 134 | /// poller.add_waitable(&child, Event::all(0), PollMode::Oneshot).unwrap(); |
| 135 | /// } |
| 136 | /// |
| 137 | /// // Wait for the child process to exit. |
| 138 | /// let mut events = Events::new(); |
| 139 | /// poller.wait(&mut events, None).unwrap(); |
| 140 | /// |
| 141 | /// assert_eq!(events.len(), 1); |
| 142 | /// assert_eq!(events.iter().next().unwrap(), Event::all(0)); |
| 143 | /// |
| 144 | /// // Modify the waitable handle. |
| 145 | /// poller.modify_waitable(&child, Event::readable(0), PollMode::Oneshot).unwrap(); |
| 146 | /// ``` |
| 147 | fn modify_waitable( |
| 148 | &self, |
| 149 | handle: impl AsWaitable, |
| 150 | interest: Event, |
| 151 | mode: PollMode, |
| 152 | ) -> io::Result<()>; |
| 153 | |
| 154 | /// Remove a waitable handle from this poller. |
| 155 | /// |
| 156 | /// This function can be used to remove a waitable handle from the poller. The handle must |
| 157 | /// have been previously added to the poller using [`add_waitable`]. |
| 158 | /// |
| 159 | /// [`add_waitable`]: Self::add_waitable |
| 160 | /// |
| 161 | /// # Examples |
| 162 | /// |
| 163 | /// ```no_run |
| 164 | /// use polling::{Poller, Event, Events, PollMode}; |
| 165 | /// use polling::os::iocp::PollerIocpExt; |
| 166 | /// |
| 167 | /// use std::process::Command; |
| 168 | /// |
| 169 | /// // Spawn a new process. |
| 170 | /// let mut child = Command::new("echo") |
| 171 | /// .arg("Hello, world!") |
| 172 | /// .spawn() |
| 173 | /// .unwrap(); |
| 174 | /// |
| 175 | /// // Create a new poller. |
| 176 | /// let poller = Poller::new().unwrap(); |
| 177 | /// |
| 178 | /// // Add the child process to the poller. |
| 179 | /// unsafe { |
| 180 | /// poller.add_waitable(&child, Event::all(0), PollMode::Oneshot).unwrap(); |
| 181 | /// } |
| 182 | /// |
| 183 | /// // Wait for the child process to exit. |
| 184 | /// let mut events = Events::new(); |
| 185 | /// poller.wait(&mut events, None).unwrap(); |
| 186 | /// |
| 187 | /// assert_eq!(events.len(), 1); |
| 188 | /// assert_eq!(events.iter().next().unwrap(), Event::all(0)); |
| 189 | /// |
| 190 | /// // Remove the waitable handle. |
| 191 | /// poller.remove_waitable(&child).unwrap(); |
| 192 | /// ``` |
| 193 | fn remove_waitable(&self, handle: impl AsWaitable) -> io::Result<()>; |
| 194 | } |
| 195 | |
| 196 | impl PollerIocpExt for Poller { |
| 197 | fn post(&self, packet: CompletionPacket) -> io::Result<()> { |
| 198 | self.poller.post(packet) |
| 199 | } |
| 200 | |
| 201 | unsafe fn add_waitable( |
| 202 | &self, |
| 203 | handle: impl AsRawWaitable, |
| 204 | event: Event, |
| 205 | mode: PollMode, |
| 206 | ) -> io::Result<()> { |
| 207 | self.poller |
| 208 | .add_waitable(handle.as_raw_handle(), event, mode) |
| 209 | } |
| 210 | |
| 211 | fn modify_waitable( |
| 212 | &self, |
| 213 | handle: impl AsWaitable, |
| 214 | interest: Event, |
| 215 | mode: PollMode, |
| 216 | ) -> io::Result<()> { |
| 217 | self.poller |
| 218 | .modify_waitable(handle.as_waitable().as_raw_handle(), interest, mode) |
| 219 | } |
| 220 | |
| 221 | fn remove_waitable(&self, handle: impl AsWaitable) -> io::Result<()> { |
| 222 | self.poller |
| 223 | .remove_waitable(handle.as_waitable().as_raw_handle()) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /// A type that represents a waitable handle. |
| 228 | pub trait AsRawWaitable { |
| 229 | /// Returns the raw handle of this waitable. |
| 230 | fn as_raw_handle(&self) -> RawHandle; |
| 231 | } |
| 232 | |
| 233 | impl AsRawWaitable for RawHandle { |
| 234 | fn as_raw_handle(&self) -> RawHandle { |
| 235 | *self |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | impl<T: AsRawHandle + ?Sized> AsRawWaitable for &T { |
| 240 | fn as_raw_handle(&self) -> RawHandle { |
| 241 | AsRawHandle::as_raw_handle(*self) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /// A type that represents a waitable handle. |
| 246 | pub trait AsWaitable: AsHandle { |
| 247 | /// Returns the raw handle of this waitable. |
| 248 | fn as_waitable(&self) -> BorrowedHandle<'_> { |
| 249 | self.as_handle() |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | impl<T: AsHandle + ?Sized> AsWaitable for T {} |