| 1 |
use std::sync::{Arc, Mutex}; |
| 2 |
|
| 3 |
use crossterm_winapi::Semaphore; |
| 4 |
|
| 5 |
/// Allows to wake up the `WinApiPoll::poll()` method. |
| 6 |
#[derive(Clone, Debug)] |
| 7 |
pub(crate) struct Waker { |
| 8 |
inner: Arc<Mutex<Semaphore>>, |
| 9 |
} |
| 10 |
|
| 11 |
impl Waker { |
| 12 |
/// Creates a new waker. |
| 13 |
/// |
| 14 |
/// `Waker` is based on the `Semaphore`. You have to use the semaphore |
| 15 |
/// handle along with the `WaitForMultipleObjects`. |
| 16 |
pub(crate) fn new() -> std::io::Result<Self> { |
| 17 |
let inner = Semaphore::new()?; |
| 18 |
|
| 19 |
Ok(Self { |
| 20 |
inner: Arc::new(Mutex::new(inner)), |
| 21 |
}) |
| 22 |
} |
| 23 |
|
| 24 |
/// Wakes the `WaitForMultipleObjects`. |
| 25 |
pub(crate) fn wake(&self) -> std::io::Result<()> { |
| 26 |
self.inner.lock().unwrap().release()?; |
| 27 |
Ok(()) |
| 28 |
} |
| 29 |
|
| 30 |
/// Replaces the current semaphore with a new one allowing us to reuse the same `Waker`. |
| 31 |
pub(crate) fn reset(&self) -> std::io::Result<()> { |
| 32 |
*self.inner.lock().unwrap() = Semaphore::new()?; |
| 33 |
Ok(()) |
| 34 |
} |
| 35 |
|
| 36 |
/// Returns the semaphore associated with the waker. |
| 37 |
pub(crate) fn semaphore(&self) -> Semaphore { |
| 38 |
self.inner.lock().unwrap().clone() |
| 39 |
} |
| 40 |
} |