| @@ -6,10 +6,12 @@ |
| 6 | 6 | //! When nothing can run, the scheduler polls the reactor (see `sched`), |
| 7 | 7 | //! then retries the operation. |
| 8 | 8 | //! - Sockets are registered once, edge-triggered, for reading and writing. |
| 9 | | −//! A wake-up may be spurious; the retry then parks again. An event that |
| 10 | | −//! arrives before the actor parks (another thread polled) is kept in |
| 11 | | −//! the scheduler (`Shared::io_wait`), and the actor retries instead of |
| 12 | | −//! parking. |
| 9 | +//! A wake-up may be spurious; the retry then parks again. Every actor |
| 10 | +//! waiting on a socket is woken by its events. An operation reads the |
| 11 | +//! reactor's event count before it tries ([`io_events`]); if an event |
| 12 | +//! came before the actor parks (another thread polled), the count moved, |
| 13 | +//! and the actor retries instead of parking (`Shared::io_wait`, issue |
| 14 | +//! #221). |
| 13 | 15 | //! - A kill wakes a parked actor, which unwinds. |
| 14 | 16 | //! |
| 15 | 17 | //! Results are `Result`s (`Ok` 0, `Error` 1) with the error text. Data |
| @@ -34,7 +36,7 @@ use crate::heap::drop_value; |
| 34 | 36 | use crate::int::saturating; |
| 35 | 37 | use crate::otp::Wait; |
| 36 | 38 | use crate::resource::{tl_resource_get, tl_resource_new}; |
| 37 | | −use crate::sched::{Shared, park, this}; |
| 39 | +use crate::sched::{park, this}; |
| 38 | 40 | use crate::values::{bytes_from, bytes_of, error, ok, text}; |
| 39 | 41 | |
| 40 | 42 | enum Socket { |
| @@ -47,8 +49,6 @@ enum Socket { |
| 47 | 49 | struct Handle { |
| 48 | 50 | socket: Socket, |
| 49 | 51 | token: Token, |
| 50 | | − /// The scheduler, which outlives every actor and so every socket. |
| 51 | | − sched: *const Shared, |
| 52 | 52 | /// Given to a TLS stream, with the token (`upgrade`). |
| 53 | 53 | upgraded: AtomicBool, |
| 54 | 54 | } |
| @@ -58,11 +58,7 @@ unsafe extern "C" fn finalize(p: *mut std::ffi::c_void) { |
| 58 | 58 | // (and removes it from the reactor). Finalizers never run under the |
| 59 | 59 | // scheduler lock. |
| 60 | 60 | unsafe { |
| 61 | | − let handle = Box::from_raw(p.cast::<Handle>()); |
| 62 | | − // An upgraded socket's token is the TLS stream's now. |
| 63 | | − if !handle.upgraded.load(Ordering::Acquire) { |
| 64 | | − (*handle.sched).io_closed(handle.token); |
| 65 | | − } |
| 61 | + drop(Box::from_raw(p.cast::<Handle>())); |
| 66 | 62 | } |
| 67 | 63 | } |
| 68 | 64 | |
| @@ -90,7 +86,6 @@ unsafe fn wrap(ctx: *mut Ctx, mut socket: Socket) -> Result<Value, String> { |
| 90 | 86 | let handle = Box::into_raw(Box::new(Handle { |
| 91 | 87 | socket, |
| 92 | 88 | token, |
| 93 | | − sched, |
| 94 | 89 | upgraded: AtomicBool::new(false), |
| 95 | 90 | })); |
| 96 | 91 | Ok(tl_resource_new(ctx, handle.cast(), Some(finalize), true)) |
| @@ -112,24 +107,36 @@ fn stream(h: &Handle) -> Result<&TcpStream, &'static str> { |
| 112 | 107 | } |
| 113 | 108 | } |
| 114 | 109 | |
| 110 | +/// The reactor's event count, read before an operation that may find |
| 111 | +/// `WouldBlock`, for [`wait_for`]. |
| 112 | +/// |
| 113 | +/// # Safety |
| 114 | +/// `ctx` must be the running actor's context. |
| 115 | +pub(crate) unsafe fn io_events(ctx: *mut Ctx) -> u64 { |
| 116 | + // SAFETY: forwarded caller guarantee. |
| 117 | + unsafe { this(ctx).1.io_events() } |
| 118 | +} |
| 119 | + |
| 115 | 120 | /// Park until the socket's token fires (or the actor is stopped: false). |
| 121 | +/// `seen` is the event count read before the operation that blocked |
| 122 | +/// ([`io_events`]): if it moved since, this returns at once, to retry. |
| 116 | 123 | /// |
| 117 | 124 | /// # Safety |
| 118 | 125 | /// `ctx` must be the running actor's context. |
| 119 | | −pub(crate) unsafe fn wait_for(ctx: *mut Ctx, token: Token) -> bool { |
| 126 | +pub(crate) unsafe fn wait_for(ctx: *mut Ctx, token: Token, seen: u64) -> bool { |
| 120 | 127 | // SAFETY: forwarded caller guarantee. |
| 121 | 128 | unsafe { |
| 122 | 129 | let (a, sched) = this(ctx); |
| 123 | | − // Waiting first, then registered: an event in between is kept as |
| 124 | | − // readiness, which `io_wait` reports. |
| 130 | + // Waiting first, then registered: an event in between moves the |
| 131 | + // count, which `io_wait` checks. |
| 125 | 132 | a.with_proc(|p| p.park_as(Wait::Io)); |
| 126 | | − if !sched.io_wait(token, a.pid) { |
| 133 | + if !sched.io_wait(token, a.pid, seen) { |
| 127 | 134 | a.with_proc(|p| p.wait = Wait::Ready); |
| 128 | | − return true; // Ready meanwhile: retry. |
| 135 | + return true; // An event meanwhile: retry. |
| 129 | 136 | } |
| 130 | 137 | let v = park(ctx); |
| 131 | 138 | drop_value(ctx, v); |
| 132 | | − sched.io_unwait(token); |
| 139 | + sched.io_unwait(token, a.pid); |
| 133 | 140 | (*ctx).yielding == YIELD_NONE |
| 134 | 141 | } |
| 135 | 142 | } |
| @@ -183,6 +190,7 @@ pub unsafe extern "C" fn tl_net_accept(ctx: *mut Ctx, listener: Value) -> Value |
| 183 | 190 | let Socket::Listener(l) = &h.socket else { |
| 184 | 191 | return error(ctx, "accept on a connection"); |
| 185 | 192 | }; |
| 193 | + let seen = io_events(ctx); |
| 186 | 194 | match l.accept() { |
| 187 | 195 | Ok((stream, _)) => { |
| 188 | 196 | return match wrap(ctx, Socket::Stream(Arc::new(stream))) { |
| @@ -191,7 +199,7 @@ pub unsafe extern "C" fn tl_net_accept(ctx: *mut Ctx, listener: Value) -> Value |
| 191 | 199 | }; |
| 192 | 200 | } |
| 193 | 201 | Err(e) if e.kind() == ErrorKind::WouldBlock => { |
| 194 | | − if !wait_for(ctx, h.token) { |
| 202 | + if !wait_for(ctx, h.token, seen) { |
| 195 | 203 | return UNIT; |
| 196 | 204 | } |
| 197 | 205 | } |
| @@ -246,11 +254,12 @@ unsafe fn connect_to(ctx: *mut Ctx, addr: SocketAddr) -> Result<Value, Option<St |
| 246 | 254 | let Socket::Stream(s) = &h.socket else { |
| 247 | 255 | unreachable!() |
| 248 | 256 | }; |
| 257 | + let seen = io_events(ctx); |
| 249 | 258 | let failed = match s.peer_addr() { |
| 250 | 259 | Ok(_) => return Ok(r), |
| 251 | 260 | Err(e) if e.kind() == ErrorKind::NotConnected => match s.take_error() { |
| 252 | 261 | Ok(Some(e)) | Err(e) => Some(Some(e.to_string())), |
| 253 | | − Ok(None) if !wait_for(ctx, h.token) => Some(None), |
| 262 | + Ok(None) if !wait_for(ctx, h.token, seen) => Some(None), |
| 254 | 263 | Ok(None) => None, |
| 255 | 264 | }, |
| 256 | 265 | Err(e) => Some(Some(e.to_string())), |
| @@ -281,10 +290,11 @@ pub unsafe extern "C" fn tl_net_read(ctx: *mut Ctx, conn: Value, max: Value) -> |
| 281 | 290 | Ok(s) => s, |
| 282 | 291 | Err(e) => return error(ctx, e), |
| 283 | 292 | }; |
| 293 | + let seen = io_events(ctx); |
| 284 | 294 | match s.read(&mut buf) { |
| 285 | 295 | Ok(n) => return ok(ctx, bytes_from(ctx, &buf[..n])), |
| 286 | 296 | Err(e) if e.kind() == ErrorKind::WouldBlock => { |
| 287 | | − if !wait_for(ctx, h.token) { |
| 297 | + if !wait_for(ctx, h.token, seen) { |
| 288 | 298 | return UNIT; |
| 289 | 299 | } |
| 290 | 300 | } |
| @@ -332,11 +342,12 @@ unsafe fn write_all(ctx: *mut Ctx, conn: Value, data: Value) -> Value { |
| 332 | 342 | Ok(s) => s, |
| 333 | 343 | Err(e) => return error(ctx, e), |
| 334 | 344 | }; |
| 345 | + let seen = io_events(ctx); |
| 335 | 346 | match s.write(&bytes[sent..]) { |
| 336 | 347 | Ok(0) => return error(ctx, "connection closed"), |
| 337 | 348 | Ok(n) => sent += n, |
| 338 | 349 | Err(e) if e.kind() == ErrorKind::WouldBlock => { |
| 339 | | − if !wait_for(ctx, h.token) { |
| 350 | + if !wait_for(ctx, h.token, seen) { |
| 340 | 351 | return UNIT; |
| 341 | 352 | } |
| 342 | 353 | } |
| @@ -366,14 +377,12 @@ pub unsafe extern "C" fn tl_net_close(_ctx: *mut Ctx, conn: Value) -> Value { |
| 366 | 377 | const UPGRADED: &str = "this connection was upgraded to TLS: use the TLS stream"; |
| 367 | 378 | |
| 368 | 379 | /// Give `conn`'s socket and token to a TLS stream (`tls.rs`): the |
| 369 | | −/// connection keeps the socket only to shut it down. The scheduler too. |
| 380 | +/// connection keeps the socket only to shut it down. |
| 370 | 381 | /// |
| 371 | 382 | /// # Safety |
| 372 | 383 | /// `conn` must be a live socket resource. |
| 373 | 384 | #[cfg_attr(not(feature = "tls"), allow(dead_code))] |
| 374 | | −pub(crate) unsafe fn upgrade( |
| 375 | | − conn: Value, |
| 376 | | −) -> Result<(Arc<TcpStream>, Token, *const Shared), String> { |
| 385 | +pub(crate) unsafe fn upgrade(conn: Value) -> Result<(Arc<TcpStream>, Token), String> { |
| 377 | 386 | // SAFETY: caller guarantees a socket resource. |
| 378 | 387 | let h = unsafe { handle(conn) }; |
| 379 | 388 | let Socket::Stream(s) = &h.socket else { |
| @@ -383,5 +392,5 @@ pub(crate) unsafe fn upgrade( |
| 383 | 392 | if h.upgraded.swap(true, Ordering::AcqRel) { |
| 384 | 393 | return Err("this connection is already a TLS stream's".into()); |
| 385 | 394 | } |
| 386 | | − Ok((s.clone(), h.token, h.sched)) |
| 395 | + Ok((s.clone(), h.token)) |
| 387 | 396 | } |