@@ -29,7 +29,7 @@ use windows_sys::Win32::System::Pipes::{
2929 WaitNamedPipeW ,
3030} ;
3131use windows_sys:: Win32 :: System :: Threading :: {
32- CreateEventW , INFINITE , SetEvent , WaitForMultipleObjects , WaitForSingleObject ,
32+ CreateEventW , INFINITE , ResetEvent , SetEvent , WaitForMultipleObjects , WaitForSingleObject ,
3333} ;
3434
3535pub ( crate ) mod security;
@@ -39,8 +39,6 @@ use security::PrivateSecurity;
3939const BUFFER_BYTES : u32 = 64 * 1024 ;
4040const CONNECT_TIMEOUT : Duration = Duration :: from_secs ( 5 ) ;
4141const COMPLETION_TIMEOUT : Duration = Duration :: from_secs ( 2 ) ;
42- /// Interim idle poll until `wait` blocks on the pending connect and a stop event.
43- const POLL_INTERVAL : Duration = Duration :: from_millis ( 25 ) ;
4442
4543pub ( crate ) struct Listener {
4644 endpoint : Vec < u16 > ,
@@ -86,10 +84,25 @@ impl Listener {
8684 self . pending . operation . pipe . cancel ( ) ;
8785 }
8886
89- /// Windows keeps a 25 ms poll for now; the caller then polls `accept`.
90- pub fn wait ( & self , _wake : & Wake , listen : bool , timeout : Duration ) -> io:: Result < Readiness > {
91- thread:: sleep ( timeout. min ( POLL_INTERVAL ) ) ;
92- // `timed_out` stays false: Windows cannot tell, and the missed-wake detector must stay off.
87+ /// Sleeps until a client connects (the pending `ConnectNamedPipe`'s event), a wake, or
88+ /// `timeout`; the caller then polls `accept`. It slept 25 ms instead, and the server makes
89+ /// two connections per keystroke, so every echo in a terminal waited up to 50 ms for it.
90+ pub fn wait ( & self , wake : & Wake , listen : bool , timeout : Duration ) -> io:: Result < Readiness > {
91+ let listening = listen && !self . stopped ;
92+ if listening && self . pending . connected {
93+ return Ok ( Readiness {
94+ connection : true ,
95+ timed_out : false ,
96+ } ) ;
97+ }
98+ let mut handles = vec ! [ wake. event. as_raw_handle( ) ] ;
99+ if listening {
100+ handles. push ( self . pending . operation . event . as_raw_handle ( ) ) ;
101+ }
102+ let milliseconds = timeout. as_millis ( ) . min ( u128:: from ( INFINITE - 1 ) ) as u32 ;
103+ // SAFETY: both events are owned by live objects for the whole call.
104+ unsafe { WaitForMultipleObjects ( handles. len ( ) as u32 , handles. as_ptr ( ) , 0 , milliseconds) } ;
105+ // `timed_out` stays false: the missed-wake detector stays off, as before.
93106 Ok ( Readiness {
94107 connection : listen,
95108 timed_out : false ,
@@ -105,18 +118,31 @@ pub(crate) struct Readiness {
105118 pub timed_out : bool ,
106119}
107120
108- /// Windows keeps a 25 ms poll for now, so notify is a no-op.
109- pub ( crate ) struct Wake ;
121+ /// The accept loop's wake: a manual-reset event `Listener::wait` sleeps on beside the pending
122+ /// connection.
123+ pub ( crate ) struct Wake {
124+ event : OwnedHandle ,
125+ }
110126
111127impl Wake {
112128 pub fn new ( ) -> io:: Result < Self > {
113- Ok ( Self )
129+ Ok ( Self { event : event ( ) ? } )
114130 }
115131
116- pub fn notify ( & self ) { }
132+ pub fn notify ( & self ) {
133+ // SAFETY: the event is owned by self; signalling it is idempotent.
134+ unsafe { SetEvent ( self . event . as_raw_handle ( ) ) } ;
135+ }
117136
137+ /// Whether a wake was pending, clearing it.
118138 pub fn drain ( & self ) -> bool {
119- false
139+ // SAFETY: the event is owned by self; a zero timeout only queries it.
140+ let set = unsafe { WaitForSingleObject ( self . event . as_raw_handle ( ) , 0 ) } == WAIT_OBJECT_0 ;
141+ if set {
142+ // SAFETY: as above.
143+ unsafe { ResetEvent ( self . event . as_raw_handle ( ) ) } ;
144+ }
145+ set
120146 }
121147}
122148
@@ -616,6 +642,47 @@ mod tests {
616642 }
617643 }
618644
645+ #[ test]
646+ fn wake_drain_reports_and_clears_a_notify ( ) {
647+ let wake = Wake :: new ( ) . unwrap ( ) ;
648+ assert ! ( !wake. drain( ) ) ;
649+ for _ in 0 ..1_000 {
650+ wake. notify ( ) ;
651+ }
652+ assert ! ( wake. drain( ) ) ;
653+ assert ! ( !wake. drain( ) ) ;
654+ }
655+
656+ #[ test]
657+ fn listener_wait_ends_on_a_wake_or_a_connection_not_a_poll ( ) {
658+ let endpoint = endpoint ( ) ;
659+ let mut listener = Listener :: bind ( & endpoint) . unwrap ( ) ;
660+ let wake = Wake :: new ( ) . unwrap ( ) ;
661+
662+ // Nothing to report: it sleeps the whole timeout (it used to return after 25 ms).
663+ let started = Instant :: now ( ) ;
664+ listener
665+ . wait ( & wake, true , Duration :: from_millis ( 200 ) )
666+ . unwrap ( ) ;
667+ assert ! ( started. elapsed( ) >= Duration :: from_millis( 150 ) ) ;
668+
669+ wake. notify ( ) ;
670+ let started = Instant :: now ( ) ;
671+ listener. wait ( & wake, true , Duration :: from_secs ( 5 ) ) . unwrap ( ) ;
672+ assert ! ( started. elapsed( ) < Duration :: from_secs( 1 ) ) ;
673+ assert ! ( wake. drain( ) ) ;
674+
675+ let started = Instant :: now ( ) ;
676+ let connector = thread:: spawn ( move || {
677+ thread:: sleep ( Duration :: from_millis ( 100 ) ) ;
678+ Connection :: connect ( & endpoint) . unwrap ( )
679+ } ) ;
680+ let ready = listener. wait ( & wake, true , Duration :: from_secs ( 5 ) ) . unwrap ( ) ;
681+ assert ! ( started. elapsed( ) < Duration :: from_secs( 1 ) ) ;
682+ assert ! ( ready. connection) ;
683+ let _client = connector. join ( ) . unwrap ( ) ;
684+ assert ! ( listener. accept( ) . unwrap( ) . is_some( ) ) ;
685+ }
619686 #[ test]
620687 fn multiple_byte_stream_connections_and_namespace_ownership ( ) {
621688 let endpoint = endpoint ( ) ;
0 commit comments