@@ -68,8 +68,8 @@ extern crate alloc;
68
68
#[ cfg( feature = "std" ) ]
69
69
extern crate std;
70
70
71
- mod list ;
72
- mod sync ;
71
+ # [ path = "no_std.rs" ]
72
+ mod sys ;
73
73
74
74
use alloc:: sync:: Arc ;
75
75
@@ -88,83 +88,6 @@ use std::panic::{RefUnwindSafe, UnwindSafe};
88
88
#[ cfg( feature = "std" ) ]
89
89
use std:: time:: { Duration , Instant } ;
90
90
91
- #[ cfg( feature = "std" ) ]
92
- use parking:: Unparker ;
93
-
94
- /// An asynchronous waker or thread unparker that can be used to notify a task or thread.
95
- #[ derive( Debug ) ]
96
- enum Task {
97
- /// A waker that can be used to notify a task.
98
- Waker ( Waker ) ,
99
-
100
- /// An unparker that can be used to notify a thread.
101
- #[ cfg( feature = "std" ) ]
102
- Thread ( Unparker ) ,
103
- }
104
-
105
- impl Task {
106
- fn as_task_ref ( & self ) -> TaskRef < ' _ > {
107
- match self {
108
- Self :: Waker ( waker) => TaskRef :: Waker ( waker) ,
109
- #[ cfg( feature = "std" ) ]
110
- Self :: Thread ( unparker) => TaskRef :: Unparker ( unparker) ,
111
- }
112
- }
113
-
114
- /// Notifies the task or thread.
115
- fn wake ( self ) {
116
- match self {
117
- Task :: Waker ( waker) => waker. wake ( ) ,
118
- #[ cfg( feature = "std" ) ]
119
- Task :: Thread ( unparker) => {
120
- unparker. unpark ( ) ;
121
- }
122
- }
123
- }
124
- }
125
-
126
- impl PartialEq for Task {
127
- fn eq ( & self , other : & Self ) -> bool {
128
- self . as_task_ref ( ) . will_wake ( other. as_task_ref ( ) )
129
- }
130
- }
131
-
132
- /// A reference to a task.
133
- #[ derive( Debug , Clone , Copy ) ]
134
- enum TaskRef < ' a > {
135
- /// A waker that wakes up a future.
136
- Waker ( & ' a Waker ) ,
137
-
138
- /// An unparker that wakes up a thread.
139
- #[ cfg( feature = "std" ) ]
140
- Unparker ( & ' a parking:: Unparker ) ,
141
- }
142
-
143
- impl TaskRef < ' _ > {
144
- /// Tells if this task will wake up the other task.
145
- fn will_wake ( self , other : Self ) -> bool {
146
- match ( self , other) {
147
- ( Self :: Waker ( a) , Self :: Waker ( b) ) => a. will_wake ( b) ,
148
- #[ cfg( feature = "std" ) ]
149
- ( Self :: Unparker ( _) , Self :: Unparker ( _) ) => {
150
- // TODO: Use unreleased will_unpark API.
151
- false
152
- }
153
- #[ cfg( feature = "std" ) ]
154
- _ => false ,
155
- }
156
- }
157
-
158
- /// Converts this task reference to a task by cloning.
159
- fn into_task ( self ) -> Task {
160
- match self {
161
- Self :: Waker ( waker) => Task :: Waker ( waker. clone ( ) ) ,
162
- #[ cfg( feature = "std" ) ]
163
- Self :: Unparker ( unparker) => Task :: Thread ( unparker. clone ( ) ) ,
164
- }
165
- }
166
- }
167
-
168
91
/// Inner state of [`Event`].
169
92
struct Inner {
170
93
/// The number of notified entries, or `usize::MAX` if all of them have been notified.
@@ -173,15 +96,15 @@ struct Inner {
173
96
notified : AtomicUsize ,
174
97
175
98
/// Inner queue of event listeners.
176
- list : list :: List ,
99
+ list : sys :: List ,
177
100
}
178
101
179
102
impl Inner {
180
103
/// Create a new `Inner`.
181
104
fn new ( ) -> Self {
182
105
Self {
183
106
notified : AtomicUsize :: new ( core:: usize:: MAX ) ,
184
- list : list :: List :: new ( ) ,
107
+ list : sys :: List :: new ( ) ,
185
108
}
186
109
}
187
110
}
@@ -255,7 +178,7 @@ impl Event {
255
178
// Register the listener.
256
179
let mut listener = EventListener {
257
180
inner : unsafe { Arc :: clone ( & ManuallyDrop :: new ( Arc :: from_raw ( inner) ) ) } ,
258
- state : list :: Listener :: Discarded ,
181
+ state : sys :: Listener :: Discarded ,
259
182
} ;
260
183
261
184
listener. inner . insert ( & mut listener. state ) ;
@@ -525,7 +448,7 @@ pub struct EventListener {
525
448
inner : Arc < Inner > ,
526
449
527
450
/// The current state of the listener.
528
- state : list :: Listener ,
451
+ state : sys :: Listener ,
529
452
}
530
453
531
454
#[ cfg( feature = "std" ) ]
@@ -784,6 +707,80 @@ impl State {
784
707
}
785
708
}
786
709
710
+ /// An asynchronous waker or thread unparker that can be used to notify a task or thread.
711
+ #[ derive( Debug ) ]
712
+ enum Task {
713
+ /// A waker that can be used to notify a task.
714
+ Waker ( Waker ) ,
715
+
716
+ /// An unparker that can be used to notify a thread.
717
+ #[ cfg( feature = "std" ) ]
718
+ Unparker ( parking:: Unparker ) ,
719
+ }
720
+
721
+ impl Task {
722
+ fn as_task_ref ( & self ) -> TaskRef < ' _ > {
723
+ match self {
724
+ Self :: Waker ( waker) => TaskRef :: Waker ( waker) ,
725
+ #[ cfg( feature = "std" ) ]
726
+ Self :: Unparker ( unparker) => TaskRef :: Unparker ( unparker) ,
727
+ }
728
+ }
729
+
730
+ /// Notifies the task or thread.
731
+ fn wake ( self ) {
732
+ match self {
733
+ Task :: Waker ( waker) => waker. wake ( ) ,
734
+ #[ cfg( feature = "std" ) ]
735
+ Task :: Unparker ( unparker) => {
736
+ unparker. unpark ( ) ;
737
+ }
738
+ }
739
+ }
740
+ }
741
+
742
+ impl PartialEq for Task {
743
+ fn eq ( & self , other : & Self ) -> bool {
744
+ self . as_task_ref ( ) . will_wake ( other. as_task_ref ( ) )
745
+ }
746
+ }
747
+
748
+ /// A reference to a task.
749
+ #[ derive( Debug , Clone , Copy ) ]
750
+ enum TaskRef < ' a > {
751
+ /// A waker that wakes up a future.
752
+ Waker ( & ' a Waker ) ,
753
+
754
+ /// An unparker that wakes up a thread.
755
+ #[ cfg( feature = "std" ) ]
756
+ Unparker ( & ' a parking:: Unparker ) ,
757
+ }
758
+
759
+ impl TaskRef < ' _ > {
760
+ /// Tells if this task will wake up the other task.
761
+ fn will_wake ( self , other : Self ) -> bool {
762
+ match ( self , other) {
763
+ ( Self :: Waker ( a) , Self :: Waker ( b) ) => a. will_wake ( b) ,
764
+ #[ cfg( feature = "std" ) ]
765
+ ( Self :: Unparker ( _) , Self :: Unparker ( _) ) => {
766
+ // TODO: Use unreleased will_unpark API.
767
+ false
768
+ }
769
+ #[ cfg( feature = "std" ) ]
770
+ _ => false ,
771
+ }
772
+ }
773
+
774
+ /// Converts this task reference to a task by cloning.
775
+ fn into_task ( self ) -> Task {
776
+ match self {
777
+ Self :: Waker ( waker) => Task :: Waker ( waker. clone ( ) ) ,
778
+ #[ cfg( feature = "std" ) ]
779
+ Self :: Unparker ( unparker) => Task :: Unparker ( unparker. clone ( ) ) ,
780
+ }
781
+ }
782
+ }
783
+
787
784
/// Equivalent to `atomic::fence(Ordering::SeqCst)`, but in some cases faster.
788
785
#[ inline]
789
786
fn full_fence ( ) {
@@ -811,3 +808,30 @@ fn full_fence() {
811
808
atomic:: fence ( Ordering :: SeqCst ) ;
812
809
}
813
810
}
811
+
812
+ /// Synchronization primitive implementation.
813
+ mod sync {
814
+ pub ( super ) use alloc:: sync:: Arc ;
815
+ pub ( super ) use core:: cell;
816
+ pub ( super ) use core:: sync:: atomic;
817
+
818
+ pub ( super ) trait WithMut {
819
+ type Output ;
820
+
821
+ fn with_mut < F , R > ( & mut self , f : F ) -> R
822
+ where
823
+ F : FnOnce ( & mut Self :: Output ) -> R ;
824
+ }
825
+
826
+ impl < T > WithMut for atomic:: AtomicPtr < T > {
827
+ type Output = * mut T ;
828
+
829
+ #[ inline]
830
+ fn with_mut < F , R > ( & mut self , f : F ) -> R
831
+ where
832
+ F : FnOnce ( & mut Self :: Output ) -> R ,
833
+ {
834
+ f ( self . get_mut ( ) )
835
+ }
836
+ }
837
+ }
0 commit comments