File tree 6 files changed +120
-13
lines changed
6 files changed +120
-13
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ edition = "2021"
8
8
[dependencies ]
9
9
async-lock = " 2.5.0"
10
10
async-oneshot = " 0.5.0"
11
+ async-weighted-semaphore = " 0.2.1"
12
+ async_singleflight = " 0.5.0"
11
13
atomic_float = " 0.1.0"
12
14
atomicbox = " 0.4.0"
13
15
atomig = " 0.4.0"
@@ -19,14 +21,17 @@ dashmap = "5.4.0"
19
21
event-listener = " 2.5.3"
20
22
evmap = " 10.0.2"
21
23
flurry = " 0.4.0"
24
+ futures = " 0.3.25"
22
25
oneshot = " 0.1.5"
23
26
portable-atomic = { version = " 0.3" , features =[" float" ] }
24
27
process_lock = " 0.1.0"
25
28
scc = " 0.11.1"
26
29
sharded-slab = " 0.1.4"
27
30
simple-mutex = " 1.1.5"
31
+ singleflight-async = " 0.1.1"
28
32
slab = " 0.4.7"
29
33
smol = " 1.2.5"
34
+ tokio = { version = " 1.21.2" , features = [" full" ] }
30
35
triggered = " 0.1.2"
31
36
triple_buffer = " 6.2.0"
32
37
try-lock = " 0.2.3"
Original file line number Diff line number Diff line change @@ -5,11 +5,15 @@ mod primitive;
5
5
mod notify;
6
6
mod queue;
7
7
mod scc_examples;
8
+ mod sema_examples;
9
+ mod singleflight_example;
8
10
9
11
pub use oslock:: * ;
10
12
pub use oneshots:: * ;
11
13
pub use map:: * ;
12
14
pub use primitive:: * ;
13
15
pub use notify:: * ;
14
16
pub use queue:: * ;
15
- pub use scc_examples:: * ;
17
+ pub use scc_examples:: * ;
18
+ pub use sema_examples:: * ;
19
+ pub use singleflight_example:: * ;
Original file line number Diff line number Diff line change @@ -26,7 +26,6 @@ fn main() {
26
26
async_lock_mutex ( ) ;
27
27
async_lock_rwlock ( ) ;
28
28
async_lock_barrier ( ) ;
29
- async_lock_semaphore ( ) ;
30
29
31
30
portable_atomic_i128 ( ) ;
32
31
portable_atomic_u128 ( ) ;
@@ -51,6 +50,14 @@ fn main() {
51
50
scc_hashset ( ) ;
52
51
scc_queue ( ) ;
53
52
53
+
54
+ async_lock_semaphore ( ) ;
55
+ async_weighted_semaphore_example ( ) ;
56
+ tokio_semaphore_example ( ) ;
57
+
58
+ singleflight_example ( ) ;
59
+ async_singleflight_example ( ) ;
60
+
54
61
}
55
62
56
63
Original file line number Diff line number Diff line change @@ -50,14 +50,3 @@ pub fn async_lock_barrier() {
50
50
}
51
51
} ) ;
52
52
}
53
-
54
- pub fn async_lock_semaphore ( ) {
55
- let s = Arc :: new ( Semaphore :: new ( 2 ) ) ;
56
-
57
- let _g1 = s. try_acquire_arc ( ) . unwrap ( ) ;
58
- let g2 = s. try_acquire_arc ( ) . unwrap ( ) ;
59
-
60
- assert ! ( s. try_acquire_arc( ) . is_none( ) ) ;
61
- drop ( g2) ;
62
- assert ! ( s. try_acquire_arc( ) . is_some( ) ) ;
63
- }
Original file line number Diff line number Diff line change
1
+ use futures:: pin_mut;
2
+ use futures:: poll;
3
+ use std:: sync:: Arc ;
4
+
5
+ pub fn tokio_semaphore_example ( ) {
6
+ let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
7
+
8
+ rt. block_on ( async {
9
+ let semaphore = Arc :: new ( tokio:: sync:: Semaphore :: new ( 3 ) ) ;
10
+ let mut join_handles = Vec :: new ( ) ;
11
+
12
+ for _ in 0 ..5 {
13
+ let permit = semaphore. clone ( ) . acquire_owned ( ) . await . unwrap ( ) ;
14
+ join_handles. push ( tokio:: spawn ( async move {
15
+ // perform task...
16
+ // explicitly own `permit` in the task
17
+ drop ( permit) ;
18
+ } ) ) ;
19
+ }
20
+
21
+ for handle in join_handles {
22
+ handle. await . unwrap ( ) ;
23
+ }
24
+ } ) ;
25
+ }
26
+
27
+ pub fn async_weighted_semaphore_example ( ) {
28
+ smol:: block_on ( async {
29
+ let sem = async_weighted_semaphore:: Semaphore :: new ( 1 ) ;
30
+ let a = sem. acquire ( 2 ) ;
31
+ let b = sem. acquire ( 1 ) ;
32
+ pin_mut ! ( a) ;
33
+ pin_mut ! ( b) ;
34
+ assert ! ( poll!( & mut a) . is_pending( ) ) ;
35
+ assert ! ( poll!( & mut b) . is_pending( ) ) ;
36
+
37
+ sem. release ( 1 ) ;
38
+ assert ! ( poll!( & mut a) . is_ready( ) ) ;
39
+ assert ! ( poll!( & mut b) . is_ready( ) ) ;
40
+ } ) ;
41
+ }
42
+
43
+ pub fn async_lock_semaphore ( ) {
44
+ let s = Arc :: new ( async_lock:: Semaphore :: new ( 2 ) ) ;
45
+
46
+ let _g1 = s. try_acquire_arc ( ) . unwrap ( ) ;
47
+ let g2 = s. try_acquire_arc ( ) . unwrap ( ) ;
48
+
49
+ assert ! ( s. try_acquire_arc( ) . is_none( ) ) ;
50
+ drop ( g2) ;
51
+ assert ! ( s. try_acquire_arc( ) . is_some( ) ) ;
52
+ }
Original file line number Diff line number Diff line change
1
+ use futures:: future:: join_all;
2
+ use singleflight_async:: SingleFlight ;
3
+ use std:: sync:: Arc ;
4
+ use std:: time:: Duration ;
5
+
6
+ use async_singleflight:: Group ;
7
+
8
+ pub fn singleflight_example ( ) {
9
+ smol:: block_on ( async {
10
+ let group = SingleFlight :: new ( ) ;
11
+ let mut futures = Vec :: new ( ) ;
12
+ for _ in 0 ..10 {
13
+ futures. push ( group. work ( "key" , || async {
14
+ println ! ( "will sleep to simulate async task" ) ;
15
+ smol:: Timer :: after ( std:: time:: Duration :: from_millis ( 100 ) ) . await ;
16
+ println ! ( "real task done" ) ;
17
+ "my-result"
18
+ } ) ) ;
19
+ }
20
+
21
+ for fut in futures. into_iter ( ) {
22
+ assert_eq ! ( fut. await , "my-result" ) ;
23
+ println ! ( "task finished" ) ;
24
+ }
25
+ } ) ;
26
+ }
27
+
28
+ const RES : usize = 7 ;
29
+
30
+ async fn expensive_fn ( ) -> Result < usize , ( ) > {
31
+ smol:: Timer :: after ( std:: time:: Duration :: from_millis ( 100 ) ) . await ;
32
+ Ok ( RES )
33
+ }
34
+
35
+ pub fn async_singleflight_example ( ) {
36
+ smol:: block_on ( async {
37
+ let g = Arc :: new ( Group :: < _ , ( ) > :: new ( ) ) ;
38
+ let mut handlers = Vec :: new ( ) ;
39
+ for _ in 0 ..10 {
40
+ let g = g. clone ( ) ;
41
+ handlers. push ( smol:: spawn ( async move {
42
+ let res = g. work ( "key" , expensive_fn ( ) ) . await . 0 ;
43
+ let r = res. unwrap ( ) ;
44
+ println ! ( "{}" , r) ;
45
+ } ) ) ;
46
+ }
47
+
48
+ join_all ( handlers) . await ;
49
+ } ) ;
50
+ }
You can’t perform that action at this time.
0 commit comments