From 8820f6e922d3332d0fdd035b504897503d4cdf3a Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 11 Apr 2023 18:10:51 -0400 Subject: fix(npm): do not "npm install" when npm specifier happens to match package.json entry (#18660) --- cli/util/mod.rs | 1 + cli/util/sync.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 cli/util/sync.rs (limited to 'cli/util') diff --git a/cli/util/mod.rs b/cli/util/mod.rs index c3133cc10..61511679f 100644 --- a/cli/util/mod.rs +++ b/cli/util/mod.rs @@ -11,6 +11,7 @@ pub mod fs; pub mod logger; pub mod path; pub mod progress_bar; +pub mod sync; pub mod text_encoding; pub mod time; pub mod unix; diff --git a/cli/util/sync.rs b/cli/util/sync.rs new file mode 100644 index 000000000..6d136abc1 --- /dev/null +++ b/cli/util/sync.rs @@ -0,0 +1,35 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; + +/// Simplifies the use of an atomic boolean as a flag. +#[derive(Debug, Default)] +pub struct AtomicFlag(AtomicBool); + +impl AtomicFlag { + /// Raises the flag returning if the raise was successful. + pub fn raise(&self) -> bool { + !self.0.swap(true, Ordering::SeqCst) + } + + /// Gets if the flag is raised. + pub fn is_raised(&self) -> bool { + self.0.load(Ordering::SeqCst) + } +} + +#[cfg(test)] +mod test { + use super::AtomicFlag; + + #[test] + fn atomic_flag_raises() { + let flag = AtomicFlag::default(); + assert!(!flag.is_raised()); // false by default + assert!(flag.raise()); + assert!(flag.is_raised()); + assert!(!flag.raise()); + assert!(flag.is_raised()); + } +} -- cgit v1.2.3