mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
1b355d8a87
Introduces a `SyncReadAsyncWriteLock` to make it harder to write to the npm resolution without first waiting async in a queue. For the npm resolution, reading synchronously is fine, but when updating, someone should wait async, clone the data, then write the data at the end back.
35 lines
822 B
Rust
35 lines
822 B
Rust
// Copyright 2018-2024 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::*;
|
|
|
|
#[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());
|
|
}
|
|
}
|