turnloop 0.1.0-alpha.3

An embeddable event-loop driver, turned by its host
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Lazily started process-wide bounded pool, with a completion port per loop.
use crate::{BlockingResult, Error, ErrorKind, Notifier, OpId, Payload, Result, queue::Queue};
#[cfg(not(target_arch = "wasm32"))]
use std::sync::atomic::AtomicUsize;
use std::{
    net::{SocketAddr, ToSocketAddrs},
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// Configuration fixed by the first process-wide blocking-pool submission.
pub struct PoolConfig {
    /// Number of lazily started process-wide blocking workers.
    pub threads: usize,
    /// Maximum queued blocking jobs before ResourceLimit is returned.
    pub queue_capacity: usize,
}
impl Default for PoolConfig {
    fn default() -> Self {
        Self {
            threads: 4,
            queue_capacity: 1024,
        }
    }
}
#[derive(Debug)]
/// Owned hostname and port for a pool-backed native address lookup.
pub struct DnsRequest {
    /// Hostname resolved by the native resolver.
    pub host: String,
    /// Port included in each resolved socket address.
    pub port: u16,
}
pub(crate) enum WorkOutput {
    ExternalWait(crate::WaitResult),
    #[cfg(not(target_arch = "wasm32"))]
    Fs(crate::fs::Reply),
    Blocking(Payload),
    Resolved(Vec<SocketAddr>),
}
pub(crate) struct WorkResult {
    pub op: OpId,
    pub result: Result<WorkOutput>,
}
pub(crate) struct WorkPort {
    queue: Queue<WorkResult>,
    notifier: Notifier,
    closed: AtomicBool,
}
impl WorkPort {
    pub fn new(capacity: usize, notifier: Notifier) -> Arc<Self> {
        Arc::new(Self {
            queue: Queue::new(capacity.max(2).next_power_of_two()),
            notifier,
            closed: AtomicBool::new(false),
        })
    }
    pub fn is_empty(&self) -> bool {
        self.queue.is_empty()
    }
    pub fn pop(&self) -> Option<WorkResult> {
        self.queue.pop()
    }
    pub fn close(&self) {
        self.closed.store(true, Ordering::Release);
    }
    // The owning agent is already collecting this result. Do not leave a stale
    // notification that would turn its next future deadline into a Now poll.
    #[cfg(target_arch = "wasm32")]
    pub(crate) fn complete_during_turn(&self, result: WorkResult) {
        if !self.closed.load(Ordering::Acquire) {
            assert!(
                self.queue.push(result).is_ok(),
                "local completion credit invariant"
            );
        }
    }
    pub(crate) fn complete(&self, result: WorkResult) {
        if self.closed.load(Ordering::Acquire) {
            return;
        }
        // One reserved core operation credit per job remains held until delivery.
        // Thus this separate queue (>= max_operations) cannot overflow.
        assert!(
            self.queue.push(result).is_ok(),
            "blocking completion credit invariant"
        );
        let _ = self.notifier.notify();
    }
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) trait ReusableWork: Send + Sync {
    fn run(&self);
}
#[cfg(not(target_arch = "wasm32"))]
mod native {
    use super::*;
    use std::{
        collections::VecDeque,
        panic::{AssertUnwindSafe, catch_unwind},
        sync::{Condvar, Mutex, OnceLock},
        thread,
    };
    struct Job {
        op: OpId,
        cancel: Arc<AtomicBool>,
        port: Arc<WorkPort>,
        f: Box<dyn FnOnce() -> Result<WorkOutput> + Send>,
    }
    enum Task {
        Boxed(Job),
        Reusable(Arc<dyn ReusableWork>),
    }
    struct State {
        jobs: Mutex<VecDeque<Task>>,
        // Queue capacity promised to accepted operations that will push later.
        // Modified only while holding `jobs`: queued + reserved <= queue_capacity.
        reserved: AtomicUsize,
        ready: Condvar,
        stopping: AtomicBool,
        /// Workers that finished thread startup; `start` returns only once all have.
        started: Mutex<usize>,
        started_changed: Condvar,
    }
    struct Pool {
        state: Arc<State>,
        config: PoolConfig,
    }
    static POOL: OnceLock<Result<Pool>> = OnceLock::new();
    fn start(config: PoolConfig) -> Result<Pool> {
        let state = Arc::new(State {
            jobs: Mutex::new(VecDeque::with_capacity(config.queue_capacity)),
            reserved: AtomicUsize::new(0),
            ready: Condvar::new(),
            stopping: AtomicBool::new(false),
            started: Mutex::new(0),
            started_changed: Condvar::new(),
        });
        let mut handles = Vec::with_capacity(config.threads);
        for i in 0..config.threads {
            let s = state.clone();
            let worker = thread::Builder::new()
                .name(format!("turnloop-blocking-{i}"))
                .spawn(move || {
                    // The runtime's per-thread setup (thread name, current-thread
                    // handle, TLS destructor lists; allocating on Windows) has run.
                    *s.started
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner) += 1;
                    s.started_changed.notify_all();
                    loop {
                        let job = {
                            let mut jobs = s
                                .jobs
                                .lock()
                                .unwrap_or_else(std::sync::PoisonError::into_inner);
                            while jobs.is_empty() && !s.stopping.load(Ordering::Acquire) {
                                jobs = s
                                    .ready
                                    .wait(jobs)
                                    .unwrap_or_else(std::sync::PoisonError::into_inner);
                            }
                            if s.stopping.load(Ordering::Acquire) {
                                return;
                            }
                            jobs.pop_front().expect("nonempty job queue")
                        };
                        let job = match job {
                            Task::Boxed(job) => job,
                            Task::Reusable(work) => {
                                work.run();
                                continue;
                            }
                        };
                        let result = if job.cancel.load(Ordering::Acquire) {
                            Err(Error::new(ErrorKind::Cancelled))
                        } else {
                            catch_unwind(AssertUnwindSafe(job.f))
                                .unwrap_or_else(|_| Err(Error::new(ErrorKind::Other)))
                        };
                        job.port.complete(WorkResult { op: job.op, result });
                    }
                });
            match worker {
                Ok(handle) => handles.push(handle),
                Err(e) => {
                    state.stopping.store(true, Ordering::Release);
                    state.ready.notify_all();
                    for h in handles {
                        let _ = h.join();
                    }
                    return Err(e.into());
                }
            }
        }
        // Return only after every worker finished starting. Thread startup runs
        // asynchronously and allocates on some platforms; it must not overlap the
        // first steady-state jobs of any loop.
        let mut started = state
            .started
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        while *started < config.threads {
            started = state
                .started_changed
                .wait(started)
                .unwrap_or_else(std::sync::PoisonError::into_inner);
        }
        drop(started);
        // Workers live for the process lifetime, shared by every submitting loop.
        Ok(Pool { state, config })
    }
    pub(super) fn submit(
        config: PoolConfig,
        op: OpId,
        cancel: Arc<AtomicBool>,
        port: Arc<WorkPort>,
        f: Box<dyn FnOnce() -> Result<WorkOutput> + Send>,
    ) -> Result<()> {
        if config.threads == 0 || config.queue_capacity == 0 {
            return Err(Error::new(ErrorKind::InvalidInput));
        }
        let pool = POOL
            .get_or_init(|| start(config))
            .as_ref()
            .map_err(|&e| e)?;
        if pool.config != config {
            return Err(Error::new(ErrorKind::InvalidInput));
        }
        let mut jobs = pool
            .state
            .jobs
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if jobs.len() + pool.state.reserved.load(Ordering::Relaxed) >= pool.config.queue_capacity {
            return Err(Error::new(ErrorKind::ResourceLimit));
        }
        jobs.push_back(Task::Boxed(Job {
            op,
            cancel,
            port,
            f,
        }));
        drop(jobs);
        pool.state.ready.notify_one();
        Ok(())
    }
    fn pool(config: PoolConfig) -> Result<&'static Pool> {
        if config.threads == 0 || config.queue_capacity == 0 {
            return Err(Error::new(ErrorKind::InvalidInput));
        }
        let pool = POOL
            .get_or_init(|| start(config))
            .as_ref()
            .map_err(|&e| e)?;
        if pool.config != config {
            return Err(Error::new(ErrorKind::InvalidInput));
        }
        Ok(pool)
    }
    fn lock(pool: &Pool) -> std::sync::MutexGuard<'_, VecDeque<Task>> {
        pool.state
            .jobs
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }
    #[cfg(any(turnloop_backend = "kqueue", turnloop_backend = "epoll"))]
    pub(crate) fn reusable(config: PoolConfig, work: Arc<dyn ReusableWork>) -> Result<()> {
        let pool = pool(config)?;
        let mut jobs = lock(pool);
        if jobs.len() + pool.state.reserved.load(Ordering::Relaxed) >= pool.config.queue_capacity {
            return Err(Error::new(ErrorKind::ResourceLimit));
        }
        jobs.push_back(Task::Reusable(work));
        pool.state.ready.notify_one();
        Ok(())
    }
    /// Promise one queue slot to an accepted operation (ResourceLimit when full).
    pub(crate) fn reserve(config: PoolConfig) -> Result<()> {
        let pool = pool(config)?;
        let jobs = lock(pool);
        let reserved = pool.state.reserved.load(Ordering::Relaxed);
        if jobs.len() + reserved >= pool.config.queue_capacity {
            return Err(Error::new(ErrorKind::ResourceLimit));
        }
        pool.state.reserved.store(reserved + 1, Ordering::Relaxed);
        Ok(())
    }
    /// Return an unused reservation (the operation ended before it was queued).
    pub(crate) fn unreserve() {
        let pool = POOL
            .get()
            .and_then(|p| p.as_ref().ok())
            .expect("reserved pool");
        let _jobs = lock(pool);
        let reserved = pool.state.reserved.load(Ordering::Relaxed);
        pool.state.reserved.store(reserved - 1, Ordering::Relaxed);
    }
    /// Queue reusable work into its reservation. Never fails and never grows the
    /// preallocated queue, because the reservation already counted this slot.
    pub(crate) fn push_reserved(work: Arc<dyn ReusableWork>) {
        let pool = POOL
            .get()
            .and_then(|p| p.as_ref().ok())
            .expect("reserved pool");
        let mut jobs = lock(pool);
        let reserved = pool.state.reserved.load(Ordering::Relaxed);
        pool.state.reserved.store(reserved - 1, Ordering::Relaxed);
        debug_assert!(jobs.len() < pool.config.queue_capacity);
        jobs.push_back(Task::Reusable(work));
        drop(jobs);
        pool.state.ready.notify_one();
    }
}
pub(crate) fn submit(
    config: PoolConfig,
    op: OpId,
    cancel: Arc<AtomicBool>,
    port: Arc<WorkPort>,
    f: Box<dyn FnOnce() -> Result<WorkOutput> + Send>,
) -> Result<()> {
    #[cfg(not(target_arch = "wasm32"))]
    {
        native::submit(config, op, cancel, port, f)
    }
    #[cfg(target_arch = "wasm32")]
    {
        let _ = (config, op, cancel, port, f);
        Err(Error::new(ErrorKind::Unsupported))
    }
}
pub(crate) fn blocking(
    f: impl FnOnce() -> BlockingResult + Send + 'static,
) -> Box<dyn FnOnce() -> Result<WorkOutput> + Send> {
    Box::new(move || f().map(WorkOutput::Blocking))
}
pub(crate) fn resolve(request: DnsRequest) -> Box<dyn FnOnce() -> Result<WorkOutput> + Send> {
    Box::new(move || {
        // std's native ToSocketAddrs implementation uses getaddrinfo (Unix) or
        // GetAddrInfoW (Windows). Only owned Rust data runs on the pool thread.
        let addresses: Vec<_> = (request.host.as_str(), request.port)
            .to_socket_addrs()
            .map_err(Error::from)?
            .collect();
        if addresses.is_empty() {
            return Err(Error::new(ErrorKind::NotFound));
        }
        Ok(WorkOutput::Resolved(addresses))
    })
}

#[cfg(all(test, loom))]
mod models {
    use super::*;
    use crate::{
        backend::Wake,
        sync::{AtomicUsize, Ordering as ModelOrdering},
    };
    struct WakeCounter(AtomicUsize);
    impl Wake for WakeCounter {
        fn wake(&self) -> Result<()> {
            self.0.fetch_add(1, ModelOrdering::Relaxed);
            Ok(())
        }
        fn syscall_count(&self) -> u64 {
            self.0.load(ModelOrdering::Relaxed) as u64
        }
    }
    #[test]
    fn pool_result_publication_races_loop_parking() {
        loom::model(|| {
            let wake = Arc::new(WakeCounter(AtomicUsize::new(0)));
            let notifier = Notifier::new(wake.clone());
            let port = WorkPort::new(2, notifier.clone());
            let worker = port.clone();
            let op = OpId {
                owner: 7,
                key: 1 << 32,
            };
            let t = loom::thread::spawn(move || {
                worker.complete(WorkResult {
                    op,
                    result: Ok(WorkOutput::Blocking(Payload::U64(42))),
                })
            });
            let parked = notifier.park();
            t.join().expect("pool worker");
            let completion = port.pop().expect("published completion");
            assert_eq!(completion.op, op);
            assert!(matches!(
                completion.result,
                Ok(WorkOutput::Blocking(Payload::U64(42)))
            ));
            assert!(port.pop().is_none());
            if parked {
                assert_eq!(wake.syscall_count(), 1);
            } else {
                assert!(notifier.begin());
            }
        });
    }
}

#[cfg(any(turnloop_backend = "kqueue", turnloop_backend = "epoll"))]
pub(crate) use native::reusable;
#[cfg(not(target_arch = "wasm32"))]
pub(crate) use native::{push_reserved, reserve, unreserve};