@@ -3,7 +3,10 @@ use crossbeam_channel::Sender;
3
3
use log:: trace;
4
4
use std:: {
5
5
hash:: Hash ,
6
- sync:: { Arc , Mutex } ,
6
+ sync:: {
7
+ atomic:: { AtomicUsize , Ordering } ,
8
+ Arc , Mutex ,
9
+ } ,
7
10
} ;
8
11
use sync:: status:: StatusType ;
9
12
@@ -20,6 +23,7 @@ pub struct AsyncStatus {
20
23
current : Arc < Mutex < Request < u64 , Status > > > ,
21
24
last : Arc < Mutex < Status > > ,
22
25
sender : Sender < AsyncNotification > ,
26
+ pending : Arc < AtomicUsize > ,
23
27
}
24
28
25
29
impl AsyncStatus {
@@ -29,6 +33,7 @@ impl AsyncStatus {
29
33
current : Arc :: new ( Mutex :: new ( Request ( 0 , None ) ) ) ,
30
34
last : Arc :: new ( Mutex :: new ( Status :: default ( ) ) ) ,
31
35
sender,
36
+ pending : Arc :: new ( AtomicUsize :: new ( 0 ) ) ,
32
37
}
33
38
}
34
39
@@ -38,6 +43,11 @@ impl AsyncStatus {
38
43
last. clone ( )
39
44
}
40
45
46
+ ///
47
+ pub fn is_pending ( & self ) -> bool {
48
+ self . pending . load ( Ordering :: Relaxed ) > 0
49
+ }
50
+
41
51
///
42
52
pub fn fetch ( & mut self , request : u64 ) -> Option < Status > {
43
53
let hash_request = hash ( & request) ;
@@ -58,7 +68,10 @@ impl AsyncStatus {
58
68
let arc_current = Arc :: clone ( & self . current ) ;
59
69
let arc_last = Arc :: clone ( & self . last ) ;
60
70
let sender = self . sender . clone ( ) ;
71
+ let arc_pending = Arc :: clone ( & self . pending ) ;
61
72
rayon_core:: spawn ( move || {
73
+ arc_pending. fetch_add ( 1 , Ordering :: Relaxed ) ;
74
+
62
75
let res = Self :: get_status ( ) ;
63
76
trace ! ( "status fetched: {}" , hash( & res) ) ;
64
77
@@ -74,6 +87,8 @@ impl AsyncStatus {
74
87
* last = res;
75
88
}
76
89
90
+ arc_pending. fetch_sub ( 1 , Ordering :: Relaxed ) ;
91
+
77
92
sender
78
93
. send ( AsyncNotification :: Status )
79
94
. expect ( "error sending status" ) ;
0 commit comments