use std::collections::VecDeque;
use std::io;
use std::num::NonZeroUsize;
use std::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, OwnedHandle};
use std::ptr;
use std::sync::{Arc, Condvar, Mutex, OnceLock, Weak};
use std::time::{Duration, Instant};
use windows_sys::Win32::Foundation::{DUPLICATE_SAME_ACCESS, DuplicateHandle, FALSE, TRUE};
use windows_sys::Win32::System::Threading::{
CreateEventW, GetCurrentProcess, ResetEvent, SetEvent,
};
use crate::directory::{FaultDetail, VolumeIdentity};
use crate::notify::{Change, DesyncCause};
use crate::retry::{FaultOperation, WatchMode};
pub const DEFAULT_BOUND: NonZeroUsize = match NonZeroUsize::new(1024) {
Some(bound) => bound,
None => unreachable!(),
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct WatchId(u64);
impl WatchId {
#[must_use]
pub fn from_raw(value: u64) -> Self {
Self(value)
}
#[must_use]
pub fn get(self) -> u64 {
self.0
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Outcome {
Subscribed,
Establishing,
Failed {
detail: FaultDetail,
},
Cancelled,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Notification {
Batch {
watch: WatchId,
changes: Vec<Change>,
},
Desync {
watch: WatchId,
cause: DesyncCause,
},
Completion {
watch: WatchId,
outcome: Outcome,
},
Suspended {
watch: WatchId,
},
Resumed {
watch: WatchId,
},
Established {
watch: WatchId,
mode: WatchMode,
},
RetryQuestion {
watch: WatchId,
operation: FaultOperation,
detail: FaultDetail,
},
VolumeChanged {
watch: WatchId,
previous: VolumeIdentity,
current: VolumeIdentity,
},
}
impl Notification {
#[must_use]
pub fn watch(&self) -> WatchId {
match self {
Notification::Batch { watch, .. }
| Notification::Desync { watch, .. }
| Notification::Completion { watch, .. }
| Notification::Suspended { watch }
| Notification::Resumed { watch }
| Notification::Established { watch, .. }
| Notification::RetryQuestion { watch, .. }
| Notification::VolumeChanged { watch, .. } => *watch,
}
}
}
struct Shared {
items: Mutex<State>,
arrived: Condvar,
doorbell: OnceLock<OwnedHandle>,
}
pub(crate) trait Resume: Send + Sync {
fn resume(&self);
}
struct Entry {
notification: EntryNotification,
_standing_hold: Option<StandingHold>,
}
enum EntryNotification {
Plain(Notification),
Standing,
}
impl Entry {
fn plain(notification: Notification) -> Self {
Self {
notification: EntryNotification::Plain(notification),
_standing_hold: None,
}
}
fn standing(hold: StandingHold) -> Self {
Self {
notification: EntryNotification::Standing,
_standing_hold: Some(hold),
}
}
fn into_notification(self) -> Notification {
match self.notification {
EntryNotification::Plain(notification) => notification,
EntryNotification::Standing => {
let hold = self
._standing_hold
.as_ref()
.expect("a Standing entry always carries a StandingHold");
lock(&hold.state)
.queued
.clone()
.expect("a Standing entry's queued value is set for as long as it is undrained")
}
}
}
}
struct State {
queue: VecDeque<Entry>,
capacity: usize,
reserved: usize,
latched: VecDeque<WatchId>,
senders: usize,
resumers: Vec<Weak<dyn Resume>>,
}
impl State {
fn free(&self) -> usize {
self.capacity - self.queue.len() - self.reserved
}
fn best_effort_room(&self) -> usize {
self.free().saturating_sub(self.latched.len())
}
fn pending(&self) -> bool {
!self.queue.is_empty() || !self.latched.is_empty() || self.senders == 0
}
fn flush_latched(&mut self) {
while self.free() > 0 {
let Some(watch) = self.latched.pop_front() else {
break;
};
self.queue.push_back(Entry::plain(Notification::Desync {
watch,
cause: DesyncCause::QueueFull,
}));
}
}
fn latch(&mut self, watch: WatchId) {
if !self.latched.contains(&watch) {
self.latched.push_back(watch);
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[must_use = "a latched notification was lost, which M3.7 responds to by not re-arming"]
pub enum Delivery {
Queued,
Latched,
}
pub struct Sender {
shared: Arc<Shared>,
}
impl Clone for Sender {
fn clone(&self) -> Self {
let mut state = lock(&self.shared.items);
state.senders += 1;
drop(state);
Self {
shared: Arc::clone(&self.shared),
}
}
}
impl Sender {
pub fn send(&self, notification: Notification) -> Delivery {
let watch = notification.watch();
let delivery = {
let mut state = lock(&self.shared.items);
state.flush_latched();
let delivery = if state.free() > 0 {
state.queue.push_back(Entry::plain(notification));
Delivery::Queued
} else {
state.latch(watch);
Delivery::Latched
};
refresh_doorbell(&self.shared, &state);
delivery
};
self.shared.arrived.notify_all();
delivery
}
pub fn reserve(&self) -> Option<Reservation> {
{
let mut state = lock(&self.shared.items);
if state.free() == 0 {
return None;
}
state.reserved += 1;
}
Some(Reservation {
sender: self.clone(),
used: false,
})
}
pub(crate) fn reserve_standing(&self) -> Option<StandingSlot> {
{
let mut state = lock(&self.shared.items);
if state.free() == 0 {
return None;
}
state.reserved += 1;
}
Some(StandingSlot {
sender: self.clone(),
state: Arc::new(Mutex::new(StandingState {
slot_alive: true,
in_flight: false,
queued: None,
})),
})
}
pub fn has_room(&self) -> bool {
lock(&self.shared.items).best_effort_room() > 0
}
pub(crate) fn register_resume(&self, who: &Arc<impl Resume + 'static>) {
lock(&self.shared.items)
.resumers
.push(Arc::downgrade(who) as Weak<dyn Resume>);
}
}
pub struct Reservation {
sender: Sender,
used: bool,
}
impl Reservation {
pub fn send(mut self, notification: Notification) {
{
let mut state = lock(&self.sender.shared.items);
state.flush_latched();
state.reserved -= 1;
state.queue.push_back(Entry::plain(notification));
refresh_doorbell(&self.sender.shared, &state);
}
self.used = true;
self.sender.shared.arrived.notify_all();
}
}
impl Drop for Reservation {
fn drop(&mut self) {
if !self.used {
let resumers = {
let mut state = lock(&self.sender.shared.items);
state.reserved -= 1;
freed_resumers(&mut state, true)
};
prod(resumers);
}
}
}
struct StandingState {
slot_alive: bool,
in_flight: bool,
queued: Option<Notification>,
}
pub(crate) struct StandingSlot {
sender: Sender,
state: Arc<Mutex<StandingState>>,
}
impl StandingSlot {
pub(crate) fn send(&self, notification: Notification) {
{
let mut state = lock(&self.sender.shared.items);
let mut standing = lock(&self.state);
if standing.in_flight {
standing.queued = Some(notification);
} else {
standing.in_flight = true;
standing.queued = Some(notification);
drop(standing);
state.flush_latched();
state.reserved -= 1;
let hold = StandingHold {
shared: Arc::downgrade(&self.sender.shared),
state: Arc::clone(&self.state),
resolved: false,
};
state.queue.push_back(Entry::standing(hold));
refresh_doorbell(&self.sender.shared, &state);
}
}
self.sender.shared.arrived.notify_all();
}
}
impl Drop for StandingSlot {
fn drop(&mut self) {
let mut state = lock(&self.sender.shared.items);
let mut standing = lock(&self.state);
standing.slot_alive = false;
if standing.in_flight {
return;
}
drop(standing);
state.reserved -= 1;
let resumers = freed_resumers(&mut state, true);
drop(state);
prod(resumers);
}
}
impl std::fmt::Debug for StandingSlot {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StandingSlot").finish_non_exhaustive()
}
}
struct StandingHold {
shared: Weak<Shared>,
state: Arc<Mutex<StandingState>>,
resolved: bool,
}
impl Drop for StandingHold {
fn drop(&mut self) {
if self.resolved {
return;
}
if self.shared.upgrade().is_none() {
return;
}
assert!(
std::thread::panicking(),
"a StandingHold outlived its entry with the queue still alive: a \
discard path must release the reservation under the `items` lock it \
already holds, as `take` does"
);
}
}
impl std::fmt::Debug for Reservation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Reservation").finish_non_exhaustive()
}
}
impl Drop for Sender {
fn drop(&mut self) {
let mut state = lock(&self.shared.items);
state.senders -= 1;
let last = state.senders == 0;
if last {
refresh_doorbell(&self.shared, &state);
}
drop(state);
if last {
self.shared.arrived.notify_all();
}
}
}
impl std::fmt::Debug for Sender {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Sender").finish_non_exhaustive()
}
}
pub struct Receiver {
shared: Arc<Shared>,
}
impl Receiver {
#[must_use]
pub fn try_recv(&self) -> Option<Notification> {
let (entry, resumers) = {
let mut state = lock(&self.shared.items);
let taken = take(&mut state);
refresh_doorbell(&self.shared, &state);
let resumers = freed_resumers(&mut state, taken.is_some());
(taken, resumers)
};
prod(resumers);
entry.map(Entry::into_notification)
}
#[must_use]
pub fn recv(&self) -> Option<Notification> {
let mut state = lock(&self.shared.items);
loop {
if let Some(entry) = take(&mut state) {
refresh_doorbell(&self.shared, &state);
let resumers = freed_resumers(&mut state, true);
drop(state);
prod(resumers);
return Some(entry.into_notification());
}
if state.senders == 0 {
return None;
}
state = self
.shared
.arrived
.wait(state)
.unwrap_or_else(|poison| poison.into_inner());
}
}
#[must_use]
pub fn recv_timeout(&self, timeout: Duration) -> Option<Notification> {
let deadline = Instant::now() + timeout;
let mut state = lock(&self.shared.items);
loop {
if let Some(entry) = take(&mut state) {
refresh_doorbell(&self.shared, &state);
let resumers = freed_resumers(&mut state, true);
drop(state);
prod(resumers);
return Some(entry.into_notification());
}
if state.senders == 0 {
return None;
}
let remaining = deadline.checked_duration_since(Instant::now())?;
let (next, _) = self
.shared
.arrived
.wait_timeout(state, remaining)
.unwrap_or_else(|poison| poison.into_inner());
state = next;
}
}
pub fn doorbell(&self) -> io::Result<BorrowedHandle<'_>> {
self.ensure_doorbell()?;
Ok(self
.shared
.doorbell
.get()
.expect("the doorbell was just created")
.as_handle())
}
pub fn doorbell_owned(&self) -> io::Result<OwnedHandle> {
duplicate(self.doorbell()?)
}
fn ensure_doorbell(&self) -> io::Result<()> {
if self.shared.doorbell.get().is_some() {
return Ok(());
}
let state = lock(&self.shared.items);
if self.shared.doorbell.get().is_none() {
let event = create_event(state.pending())?;
let _ = self.shared.doorbell.set(event);
}
Ok(())
}
#[must_use]
pub fn is_disconnected(&self) -> bool {
lock(&self.shared.items).senders == 0
}
#[must_use]
pub fn len(&self) -> usize {
lock(&self.shared.items).queue.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[must_use]
pub fn has_pending(&self) -> bool {
lock(&self.shared.items).pending()
}
#[must_use]
pub fn latched(&self) -> usize {
lock(&self.shared.items).latched.len()
}
#[must_use]
pub fn capacity(&self) -> usize {
lock(&self.shared.items).capacity
}
}
fn take(state: &mut State) -> Option<Entry> {
if let Some(mut entry) = state.queue.pop_front() {
if matches!(entry.notification, EntryNotification::Standing) {
let hold = entry
._standing_hold
.as_mut()
.expect("a Standing entry always carries a StandingHold");
let notification = {
let mut standing = lock(&hold.state);
standing.in_flight = false;
let notification = standing.queued.take().expect(
"a Standing entry's queued value is set for as long as it is undrained",
);
if standing.slot_alive {
state.reserved += 1;
}
notification
};
hold.resolved = true;
entry.notification = EntryNotification::Plain(notification);
}
return Some(entry);
}
state.latched.pop_front().map(|watch| {
Entry::plain(Notification::Desync {
watch,
cause: DesyncCause::QueueFull,
})
})
}
impl std::fmt::Debug for Receiver {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let state = lock(&self.shared.items);
f.debug_struct("Receiver")
.field("queued", &state.queue.len())
.field("capacity", &state.capacity)
.field("latched", &state.latched.len())
.field("disconnected", &(state.senders == 0))
.finish_non_exhaustive()
}
}
#[cfg(test)]
pub(crate) fn channel() -> (Sender, Receiver) {
channel_with_bound(DEFAULT_BOUND)
}
pub fn channel_with_bound(bound: NonZeroUsize) -> (Sender, Receiver) {
let shared = Arc::new(Shared {
items: Mutex::new(State {
queue: VecDeque::new(),
capacity: bound.get(),
reserved: 0,
latched: VecDeque::new(),
senders: 1,
resumers: Vec::new(),
}),
arrived: Condvar::new(),
doorbell: OnceLock::new(),
});
(
Sender {
shared: Arc::clone(&shared),
},
Receiver { shared },
)
}
fn freed_resumers(state: &mut State, took_one: bool) -> Vec<Arc<dyn Resume>> {
if !took_one || state.resumers.is_empty() || state.best_effort_room() != 1 {
return Vec::new();
}
let mut live = Vec::with_capacity(state.resumers.len());
let mut awake = Vec::new();
for weak in state.resumers.drain(..) {
if let Some(producer) = weak.upgrade() {
live.push(Arc::downgrade(&producer));
awake.push(producer);
}
}
state.resumers = live;
awake
}
fn prod(resumers: Vec<Arc<dyn Resume>>) {
for producer in resumers {
producer.resume();
}
}
fn lock<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
mutex.lock().unwrap_or_else(|poison| poison.into_inner())
}
fn refresh_doorbell(shared: &Shared, state: &State) {
let Some(doorbell) = shared.doorbell.get() else {
return;
};
let raw = doorbell.as_raw_handle();
unsafe {
if state.pending() {
SetEvent(raw);
} else {
ResetEvent(raw);
}
}
}
fn create_event(signalled: bool) -> io::Result<OwnedHandle> {
let raw = unsafe {
CreateEventW(
ptr::null(),
TRUE,
if signalled { TRUE } else { FALSE },
ptr::null(),
)
};
if raw.is_null() {
return Err(io::Error::last_os_error());
}
Ok(unsafe { OwnedHandle::from_raw_handle(raw) })
}
fn duplicate(handle: BorrowedHandle<'_>) -> io::Result<OwnedHandle> {
let mut duplicated = ptr::null_mut();
let ok = unsafe {
DuplicateHandle(
GetCurrentProcess(),
handle.as_raw_handle(),
GetCurrentProcess(),
&raw mut duplicated,
0,
FALSE,
DUPLICATE_SAME_ACCESS,
)
};
if ok == 0 {
return Err(io::Error::last_os_error());
}
Ok(unsafe { OwnedHandle::from_raw_handle(duplicated) })
}
#[cfg(test)]
mod tests;