Skip to content

Commit

Permalink
Fix CAS for pause and resume
Browse files Browse the repository at this point in the history
Signed-off-by: Heinz N. Gies <[email protected]>
  • Loading branch information
Licenser committed Sep 15, 2023
1 parent 9eb11f5 commit 7767997
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/connectors/utils/quiescence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::errors::Result;
use crate::errors::{Error, Result};
use event_listener::Event;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -90,31 +90,35 @@ impl QuiescenceBeacon {

/// pause both reading and writing
pub fn pause(&mut self) -> Result<()> {
self.0
.state
.compare_exchange(
Inner::RUNNING,
Inner::PAUSED,
Ordering::AcqRel,
Ordering::Relaxed,
)
.map_err(|_| "failed to pause")?;
match self.0.state.compare_exchange(
Inner::RUNNING,
Inner::PAUSED,
Ordering::AcqRel,
Ordering::Relaxed,
) {
Ok(_) | Err(Inner::PAUSED | Inner::RUNNING) => Ok(()),
Err(Inner::STOP_ALL) => Err(Error::from("failed to pause from state STOP_ALL")),
Err(Inner::STOP_READING) => Err(Error::from("failed to pause from state STOP_READING")),
Err(e) => Err(Error::from(format!("Invalid state {e}"))),
}?;
Ok(())
}

/// Resume both reading and writing.
///
/// Has no effect if not currently paused.
pub fn resume(&mut self) -> Result<()> {
self.0
.state
.compare_exchange(
Inner::PAUSED,
Inner::RUNNING,
Ordering::AcqRel,
Ordering::Relaxed,
)
.map_err(|_| "failed to resume")?;
match self.0.state.compare_exchange(
Inner::PAUSED,
Inner::RUNNING,
Ordering::AcqRel,
Ordering::Relaxed,
) {
Ok(_) | Err(Inner::PAUSED | Inner::RUNNING) => Ok(()),
Ok(_) | Err(Inner::STOP_READING) => Err(Error::from("Can't resume from STOP_READING")),

Check failure on line 118 in src/connectors/utils/quiescence.rs

View workflow job for this annotation

GitHub Actions / clippy

unreachable pattern

error: unreachable pattern --> src/connectors/utils/quiescence.rs:118:13 | 118 | Ok(_) | Err(Inner::STOP_READING) => Err(Error::from("Can't resume from STOP_READING")), | ^^^^^ | note: the lint level is defined here --> src/lib.rs:17:9 | 17 | #![deny(warnings)] | ^^^^^^^^ = note: `#[deny(unreachable_patterns)]` implied by `#[deny(warnings)]`
Ok(_) | Err(Inner::STOP_ALL) => Err(Error::from("Can't resume from STOP_ALL")),

Check failure on line 119 in src/connectors/utils/quiescence.rs

View workflow job for this annotation

GitHub Actions / clippy

unreachable pattern

error: unreachable pattern --> src/connectors/utils/quiescence.rs:119:13 | 119 | Ok(_) | Err(Inner::STOP_ALL) => Err(Error::from("Can't resume from STOP_ALL")), | ^^^^^
Err(e) => Err(Error::from(format!("Invalid state {e}"))),
}?;

self.0.resume_event.notify(Self::MAX_LISTENERS); // we might have been paused, so notify here
Ok(())
Expand Down

0 comments on commit 7767997

Please sign in to comment.