@@ -9,11 +9,13 @@ use std::collections::HashMap;
9
9
use std:: rc:: Rc ;
10
10
use wasi:: io:: poll:: Pollable ;
11
11
12
- /// A key representing an entry into the poller
12
+ /// A key for a Pollable, which is an index into the Slab<Pollable> in Reactor.
13
13
#[ repr( transparent) ]
14
14
#[ derive( Debug , PartialEq , Eq , PartialOrd , Ord , Hash , Clone , Copy ) ]
15
15
pub ( crate ) struct EventKey ( pub ( crate ) usize ) ;
16
16
17
+ /// A Registration is a reference to the Reactor's owned Pollable. When the registration is
18
+ /// dropped, the reactor will drop the Pollable resource.
17
19
#[ derive( Debug , PartialEq , Eq , Hash ) ]
18
20
struct Registration {
19
21
key : EventKey ,
@@ -25,10 +27,13 @@ impl Drop for Registration {
25
27
}
26
28
}
27
29
30
+ /// An AsyncPollable is a reference counted Registration. It can be cloned, and used to create
31
+ /// as many WaitFor futures on a Pollable that the user needs.
28
32
#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
29
33
pub struct AsyncPollable ( Rc < Registration > ) ;
30
34
31
35
impl AsyncPollable {
36
+ /// Create a Future that waits for the Pollable's readiness.
32
37
pub fn wait_for ( & self ) -> WaitFor {
33
38
use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
34
39
static COUNTER : AtomicUsize = AtomicUsize :: new ( 0 ) ;
@@ -45,10 +50,13 @@ impl AsyncPollable {
45
50
46
51
#[ derive( Debug , PartialEq , Eq , Hash , Clone ) ]
47
52
struct Waitee {
53
+ /// This needs to be a reference counted registration, because it may outlive the AsyncPollable
54
+ /// &self that it was created from.
48
55
pollable : AsyncPollable ,
49
56
unique : usize ,
50
57
}
51
58
59
+ /// A Future that waits for the Pollable's readiness.
52
60
#[ must_use = "futures do nothing unless polled or .awaited" ]
53
61
#[ derive( Debug ) ]
54
62
pub struct WaitFor {
@@ -138,6 +146,8 @@ impl Reactor {
138
146
let mut targets = Vec :: with_capacity ( reactor. wakers . len ( ) ) ;
139
147
for waitee in reactor. wakers . keys ( ) {
140
148
let pollable_index = waitee. pollable . 0 . key ;
149
+ // FIXME: instead of storing the indexes, we can actually just stick the waker in here,
150
+ // and make the quadratic lookup at the end of this function into a linear lookup.
141
151
indexes. push ( pollable_index) ;
142
152
targets. push ( & reactor. pollables [ pollable_index. 0 ] ) ;
143
153
}
@@ -160,6 +170,7 @@ impl Reactor {
160
170
. into_iter ( )
161
171
. map ( |index| indexes[ index as usize ] ) ;
162
172
173
+ // FIXME this doesn't have to be quadratic.
163
174
for key in ready_keys {
164
175
for ( waitee, waker) in reactor. wakers . iter ( ) {
165
176
if waitee. pollable . 0 . key == key {
0 commit comments