1
1
use core:: cell:: Cell ;
2
2
use core:: task:: Waker ;
3
3
4
- use crate :: blocking_mutex:: raw:: CriticalSectionRawMutex ;
4
+ use crate :: blocking_mutex:: raw:: { CriticalSectionRawMutex , RawMutex } ;
5
5
use crate :: blocking_mutex:: Mutex ;
6
6
7
7
/// Utility struct to register and wake a waker.
8
- pub struct AtomicWaker {
9
- waker : Mutex < CriticalSectionRawMutex , Cell < Option < Waker > > > ,
8
+ pub struct GenericAtomicWaker < M : RawMutex > {
9
+ waker : Mutex < M , Cell < Option < Waker > > > ,
10
10
}
11
11
12
- impl AtomicWaker {
12
+ impl < M : RawMutex > GenericAtomicWaker < M > {
13
13
/// Create a new `AtomicWaker`.
14
- pub const fn new ( ) -> Self {
14
+ pub const fn new ( mutex : M ) -> Self {
15
15
Self {
16
- waker : Mutex :: const_new ( CriticalSectionRawMutex :: new ( ) , Cell :: new ( None ) ) ,
16
+ waker : Mutex :: const_new ( mutex , Cell :: new ( None ) ) ,
17
17
}
18
18
}
19
19
20
20
/// Register a waker. Overwrites the previous waker, if any.
21
21
pub fn register ( & self , w : & Waker ) {
22
- critical_section:: with ( |cs| {
23
- let cell = self . waker . borrow ( cs) ;
22
+ self . waker . lock ( |cell| {
24
23
cell. set ( match cell. replace ( None ) {
25
24
Some ( w2) if ( w2. will_wake ( w) ) => Some ( w2) ,
26
25
_ => Some ( w. clone ( ) ) ,
@@ -30,12 +29,35 @@ impl AtomicWaker {
30
29
31
30
/// Wake the registered waker, if any.
32
31
pub fn wake ( & self ) {
33
- critical_section:: with ( |cs| {
34
- let cell = self . waker . borrow ( cs) ;
32
+ self . waker . lock ( |cell| {
35
33
if let Some ( w) = cell. replace ( None ) {
36
34
w. wake_by_ref ( ) ;
37
35
cell. set ( Some ( w) ) ;
38
36
}
39
37
} )
40
38
}
41
39
}
40
+
41
+ /// Utility struct to register and wake a waker.
42
+ pub struct AtomicWaker {
43
+ waker : GenericAtomicWaker < CriticalSectionRawMutex > ,
44
+ }
45
+
46
+ impl AtomicWaker {
47
+ /// Create a new `AtomicWaker`.
48
+ pub const fn new ( ) -> Self {
49
+ Self {
50
+ waker : GenericAtomicWaker :: new ( CriticalSectionRawMutex :: new ( ) ) ,
51
+ }
52
+ }
53
+
54
+ /// Register a waker. Overwrites the previous waker, if any.
55
+ pub fn register ( & self , w : & Waker ) {
56
+ self . waker . register ( w) ;
57
+ }
58
+
59
+ /// Wake the registered waker, if any.
60
+ pub fn wake ( & self ) {
61
+ self . waker . wake ( ) ;
62
+ }
63
+ }
0 commit comments