Skip to content

Commit a3c5584

Browse files
committed
add sync_cow and arc_swap
1 parent 6e1da95 commit a3c5584

File tree

6 files changed

+71
-2
lines changed

6 files changed

+71
-2
lines changed

special/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
arc-swap = "1.6.0"
910
async-lock = "2.5.0"
1011
async-oneshot = "0.5.0"
1112
async-weighted-semaphore = "0.2.1"
@@ -17,6 +18,7 @@ awaitgroup = "0.6.0"
1718
barrage = "0.2.3"
1819
catty = "0.1.5"
1920
concurrent-queue = "1.2.4"
21+
crossbeam-utils = "0.8.14"
2022
dashmap = "5.4.0"
2123
event-listener = "2.5.3"
2224
evmap = "10.0.2"
@@ -31,6 +33,7 @@ simple-mutex = "1.1.5"
3133
singleflight-async = "0.1.1"
3234
slab = "0.4.7"
3335
smol = "1.2.5"
36+
sync_cow = "0.1.1"
3437
tokio = { version = "1.21.2", features = ["full"] }
3538
triggered = "0.1.2"
3639
triple_buffer = "6.2.0"

special/src/arcswap.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
use arc_swap::ArcSwap;
3+
use std::sync::Arc;
4+
use crossbeam_utils::thread;
5+
6+
pub fn arc_swap_example() {
7+
let value = ArcSwap::from(Arc::new(5));
8+
thread::scope(|scope| {
9+
scope.spawn(|_| {
10+
let new_value = Arc::new(4);
11+
value.store(new_value);
12+
});
13+
for _ in 0..10 {
14+
scope.spawn(|_| {
15+
loop {
16+
let v = value.load();
17+
println!("value is {}", v);
18+
return;
19+
}
20+
});
21+
}
22+
}).unwrap()
23+
24+
}

special/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ mod queue;
77
mod scc_examples;
88
mod sema_examples;
99
mod singleflight_example;
10+
mod synccow;
11+
mod arcswap;
1012

1113
pub use oslock::*;
1214
pub use oneshots::*;
@@ -16,4 +18,6 @@ pub use notify::*;
1618
pub use queue::*;
1719
pub use scc_examples::*;
1820
pub use sema_examples::*;
19-
pub use singleflight_example::*;
21+
pub use singleflight_example::*;
22+
pub use synccow::*;
23+
pub use arcswap::*;

special/src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ fn main() {
5858
singleflight_example();
5959
async_singleflight_example();
6060

61+
sync_cow_example().unwrap();
62+
arc_swap_example();
63+
6164
}
6265

6366

special/src/singleflight_example.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use futures::future::join_all;
22
use singleflight_async::SingleFlight;
33
use std::sync::Arc;
4-
use std::time::Duration;
54

65
use async_singleflight::Group;
76

special/src/synccow.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use sync_cow::SyncCow;
2+
use std::sync::Arc;
3+
use std::any::Any;
4+
5+
6+
pub fn sync_cow_example() -> Result<(),Box<dyn Any + Send>> {
7+
let cow = Arc::new(SyncCow::new(5));
8+
9+
// Arc is only needed to pass the ref to the threads
10+
let cow_write_arc = cow.clone();
11+
let cow_read_arc = cow.clone();
12+
let cow_result_arc = cow.clone();
13+
14+
let writer = std::thread::spawn(move || {
15+
let cow = &*cow_write_arc; // unpack immediately to avoid Arc deref
16+
let mut val = 0;
17+
cow.edit(|x| {
18+
val = *x;
19+
*x = 4;
20+
});
21+
println!("Cow was {} when writing", val);
22+
});
23+
24+
let reader = std::thread::spawn(move || {
25+
let cow = &*cow_read_arc; // unpack immediately to avoid Arc deref
26+
println!("Cow was {} when reading", cow.read());
27+
});
28+
29+
writer.join()?;
30+
reader.join()?;
31+
32+
let cow = &*cow_result_arc; // unpack immediately to avoid Arc deref
33+
println!("Cow was {} when result", cow.read());
34+
35+
Ok(())
36+
}

0 commit comments

Comments
 (0)