Skip to content

Commit

Permalink
Reorganize types in signal
Browse files Browse the repository at this point in the history
Created public `AtomicSignalSet` type. Replaces the old internal
`AtomicSignalMask`.

Removed internal `SignalMask` type and moved all operations onto
`SignalSet`.

Changed `SignalSet::with` back to not taking a `bool` and added
`SignalSet::without`. Using `SignalSet::setting` matches the old
behavior.

Changed `remove_{first,last}` to not return the `Signal` and made
`pop_{first,last}` to match the old behavior.
  • Loading branch information
nvzqz committed Feb 26, 2020
1 parent 8d3ec4c commit 01cd650
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 218 deletions.
2 changes: 1 addition & 1 deletion src/once/signal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn register_signal(signal: Signal) -> io::Result<RegisteredSignal> {
let table = table::Table::global();

// Set the flag before waking up the reading end.
table.caught.enable(signal, Ordering::SeqCst);
table.caught.insert(signal, Ordering::SeqCst);
table.entry(signal).load_writer(Ordering::SeqCst).wake();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/once/signal/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Future for SignalOnce {
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let table = Table::global();

if table.caught.contains(self.signal, Ordering::SeqCst) {
if table.caught.load(Ordering::SeqCst).contains(self.signal) {
return Poll::Ready(());
}

Expand Down
6 changes: 5 additions & 1 deletion src/once/signal/signal_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ impl Future for SignalSetOnce {
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let table = Table::global();

if table.caught.contains_any(self.signals.0, Ordering::SeqCst) {
if table
.caught
.load(Ordering::SeqCst)
.contains_any(self.signals)
{
return Poll::Ready(());
}

Expand Down
10 changes: 5 additions & 5 deletions src/once/signal/table.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use crate::{
signal::{signal_mask::AtomicSignalMask, Signal, SignalArray},
signal::{AtomicSignalSet, Signal, SignalArray},
unix::pipe::Writer,
};
use std::sync::atomic::{AtomicI32, Ordering};

pub(crate) struct Table {
pub registered: AtomicSignalMask,
pub caught: AtomicSignalMask,
pub registered: AtomicSignalSet,
pub caught: AtomicSignalSet,
entries: SignalArray<Entry>,
}

impl Table {
#[inline]
pub fn global() -> &'static Self {
static GLOBAL: Table = Table {
registered: AtomicSignalMask::empty(),
caught: AtomicSignalMask::empty(),
registered: AtomicSignalSet::new(),
caught: AtomicSignalSet::new(),
entries: [Entry::EMPTY; Signal::NUM],
};
&GLOBAL
Expand Down
11 changes: 5 additions & 6 deletions src/signal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@

#![cfg_attr(not(unix), allow(warnings))]

pub(crate) mod signal_mask;
pub(crate) mod signal_set;
mod set;

// Declare this after `signal_set` so that `SignalSet` methods inside can come
// after the initial `impl`.
pub(crate) mod signal;
// Declare this after `set` so that `SignalSet` methods inside can come after
// the initial `impl`.
mod signal;

pub use {
set::{AtomicSignalSet, SignalSet, SignalSetIter},
signal::Signal,
signal_set::{SignalSet, SignalSetIter},
};

/// An array suitable for indexing with a [`Signal`] without bounds checks.
Expand Down
50 changes: 50 additions & 0 deletions src/signal/set/atomic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::sync::atomic::{AtomicU32, Ordering};

use super::SignalSet;

/// A set of signals with atomic operations.
pub struct AtomicSignalSet(AtomicU32);

impl From<SignalSet> for AtomicSignalSet {
#[inline]
fn from(signals: SignalSet) -> Self {
Self::from_signal_set(signals)
}
}

impl AtomicSignalSet {
/// Creates a new, empty signal set.
#[inline]
pub const fn new() -> Self {
Self::from_signal_set(SignalSet::new())
}

/// Creates a new atomic signal set from `signals`.
#[inline]
pub const fn from_signal_set(signals: SignalSet) -> Self {
Self(AtomicU32::new(signals.0))
}

/// Atomically loads the inner `SignalSet` using `ordering`.
#[inline]
#[must_use]
pub fn load(&self, ordering: Ordering) -> SignalSet {
SignalSet(self.0.load(ordering))
}

/// Atomically stores the `signals` in `self` using `ordering`.
#[inline]
pub fn store<S: Into<SignalSet>>(&self, signals: S, ordering: Ordering) {
self.0.store(signals.into().0, ordering);
}

/// Atomically inserts `signals` into `self` using `ordering`.
#[inline]
pub fn insert<S: Into<SignalSet>>(
&self,
signals: S,
ordering: Ordering,
) -> SignalSet {
SignalSet(self.0.fetch_or(signals.into().0, ordering))
}
}

0 comments on commit 01cd650

Please sign in to comment.