threadteam 0.1.0

Scoped CPU thread-team placement token: a Pool and a non-Clone Exec with fork, for_ranges, external and broadcast; no Rayon type in the public API.
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Reserved awake members: acquisition, dispatch, nesting, policy, unwind.
#![cfg(feature = "parallel")]

use std::collections::HashSet;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Barrier, Mutex};
use std::thread::ThreadId;
use std::time::{Duration, Instant};
use threadteam::{Cancelled, Error, Exec, Pool, Refused, WaitOverride};

/// A hang is a failure: abort the whole process if the test body stalls.
fn watchdog() -> impl Drop {
    struct Stop(std::sync::mpsc::Sender<()>);
    impl Drop for Stop {
        fn drop(&mut self) {
            let _ = self.0.send(());
        }
    }
    let (tx, rx) = std::sync::mpsc::channel::<()>();
    std::thread::spawn(move || {
        if rx.recv_timeout(Duration::from_secs(45)).is_err() {
            eprintln!("watchdog: test stalled");
            std::process::exit(101);
        }
    });
    Stop(tx)
}

fn pool(n: usize) -> Pool {
    Pool::builder()
        .threads(n)
        .idle_spin(Duration::ZERO)
        .acquire_timeout(Duration::from_secs(5))
        .build()
        .unwrap()
}

/// A pool whose acquisitions tolerate a loaded test host.
fn builder(n: usize, spin: Duration) -> threadteam::PoolBuilder {
    Pool::builder()
        .threads(n)
        .idle_spin(spin)
        .acquire_timeout(Duration::from_secs(5))
}

fn wait_until(what: &str, mut cond: impl FnMut() -> bool) {
    let start = Instant::now();
    while !cond() {
        assert!(start.elapsed() < Duration::from_secs(10), "timeout: {what}");
        std::thread::sleep(Duration::from_millis(1));
    }
}

fn ids_of(team: threadteam::TeamExec<'_>) -> HashSet<ThreadId> {
    let ids = Mutex::new(HashSet::new());
    team.run(|_, _| {
        ids.lock().unwrap().insert(std::thread::current().id());
    });
    ids.into_inner().unwrap()
}

#[test]
fn every_participant_is_on_its_own_thread_and_the_caller_leads() {
    let _w = watchdog();
    let pool = pool(6);
    for budget in [2, 3, 6] {
        let mut exec = Exec::new(&pool, budget).unwrap();
        let me = std::thread::current().id();
        let leader = Mutex::new(None);
        let seen = Mutex::new(Vec::new());
        exec.team(|team| {
            assert_eq!(team.size().get(), budget);
            team.run_with(|p| {
                // All participants are in the section at the same time.
                p.barrier().unwrap();
                seen.lock().unwrap().push(std::thread::current().id());
                if p.index() == 0 {
                    *leader.lock().unwrap() = Some(std::thread::current().id());
                }
            });
        })
        .unwrap();
        let seen: HashSet<_> = seen.into_inner().unwrap().into_iter().collect();
        assert_eq!(seen.len(), budget);
        assert_eq!(leader.into_inner().unwrap(), Some(me));
        assert!(seen.contains(&me));
    }
}

#[test]
fn serial_and_budget_one_teams_run_inline_without_a_pool() {
    let _w = watchdog();
    let here = std::thread::current().id();
    let mut serial = Exec::serial();
    let n = serial
        .team(|team| {
            assert_eq!(team.size().get(), 1);
            team.run(|i, n| {
                assert_eq!((i, n), (0, 1));
                assert_eq!(std::thread::current().id(), here);
            });
            7
        })
        .unwrap();
    assert_eq!(n, 7);
    let pool = pool(2);
    let mut one = Exec::new(&pool, 1).unwrap();
    one.team(|team| assert_eq!(team.size().get(), 1)).unwrap();
    assert_eq!(pool.resident_members(), 0);
}

#[test]
fn sections_use_the_same_reserved_members_and_survive_a_long_serial_phase() {
    let _w = watchdog();
    // Spin time (20 ms) is far shorter than the serial phase (200 ms).
    let pool = builder(4, Duration::from_millis(20)).build().unwrap();
    let mut exec = Exec::new(&pool, 4).unwrap();
    exec.team(|mut team| {
        let first = ids_of(team.reborrow());
        assert_eq!(first.len(), 4);
        std::thread::sleep(Duration::from_millis(200));
        assert_eq!(
            pool.resident_members(),
            3,
            "members left during a serial phase"
        );
        let second = ids_of(team.reborrow());
        assert_eq!(first, second);
        // Again, after sections.
        std::thread::sleep(Duration::from_millis(100));
        assert_eq!(ids_of(team), first);
    })
    .unwrap();
}

#[test]
fn reserved_members_serve_no_unrelated_rayon_job() {
    let _w = watchdog();
    let pool = pool(3);
    let mut exec = Exec::new(&pool, 3).unwrap();
    exec.team(|mut team| {
        let members = ids_of(team.reborrow());
        assert_eq!(members.len(), 3);
        // One worker is free; jobs must run there, not on a reserved member.
        let job_threads = Mutex::new(HashSet::new());
        std::thread::scope(|s| {
            s.spawn(|| {
                Exec::new(&pool, 2).unwrap().fork(32, |_, _| {
                    job_threads
                        .lock()
                        .unwrap()
                        .insert(std::thread::current().id());
                    std::thread::sleep(Duration::from_micros(200));
                });
            });
        });
        let job_threads = job_threads.into_inner().unwrap();
        let me = std::thread::current().id();
        let member_threads: HashSet<_> = members.into_iter().filter(|t| *t != me).collect();
        assert!(
            job_threads.is_disjoint(&member_threads),
            "a reserved member served a job"
        );
        team.run(|_, _| {});
    })
    .unwrap();
}

#[test]
fn acquisition_refuses_while_a_running_job_holds_a_needed_worker() {
    let _w = watchdog();
    let pool = builder(3, Duration::ZERO)
        .acquire_timeout(Duration::from_millis(100))
        .build()
        .unwrap();
    let started = AtomicUsize::new(0);
    let release = AtomicBool::new(false);
    std::thread::scope(|s| {
        // A job that depends on something the leader does only *after* its
        // team call returns: waiting for it would deadlock.
        s.spawn(|| {
            Exec::new(&pool, 2).unwrap().fork(2, |_, _| {
                started.fetch_add(1, Ordering::SeqCst);
                while !release.load(Ordering::SeqCst) {
                    std::thread::sleep(Duration::from_millis(1));
                }
            });
        });
        wait_until("both workers busy", || started.load(Ordering::SeqCst) == 2);
        let mut exec = Exec::new(&pool, 3).unwrap(); // needs two members, one is free
        let t = Instant::now();
        let r = exec.team(|_| panic!("callback must not run"));
        assert_eq!(r.unwrap_err(), Refused::Unavailable);
        assert!(t.elapsed() < Duration::from_secs(2));
        // No tentative claim is left behind.
        wait_until("tentative claims released", || pool.resident_members() == 0);
        // The caller keeps a barrier-free fallback.
        let hits = AtomicUsize::new(0);
        exec.limit(2.try_into().unwrap())
            .for_ranges(4, 1.try_into().unwrap(), |r| {
                hits.fetch_add(r.len(), Ordering::Relaxed);
            });
        // (the pool is saturated by the blockers; the fallback ran inline-or-queued)
        release.store(true, Ordering::SeqCst);
    });
    // Once the job is gone the same request succeeds.
    let mut exec = Exec::new(&pool, 3).unwrap();
    assert_eq!(exec.team(|t| t.size().get()).unwrap(), 3);
}

#[test]
fn nested_child_teams_run_concurrently_on_distinct_threads() {
    let _w = watchdog();
    let pool = pool(8);
    let mut exec = Exec::new(&pool, 8).unwrap();
    // 8 participants in 3 leaf sections wait on one barrier: they must be 8 threads.
    let rendezvous = Barrier::new(8);
    let threads = Mutex::new(HashSet::new());
    exec.team(|team| {
        assert_eq!(team.size().get(), 8);
        team.split(&[3, 5], |c, child| {
            if c == 0 {
                assert_eq!(child.size().get(), 3);
                child.run(|_, _| {
                    threads.lock().unwrap().insert(std::thread::current().id());
                    rendezvous.wait();
                });
            } else {
                assert_eq!(child.size().get(), 5);
                child
                    .split(&[2, 3], |g, grand| {
                        assert_eq!(grand.size().get(), 2 + g);
                        grand.run(|_, _| {
                            threads.lock().unwrap().insert(std::thread::current().id());
                            rendezvous.wait();
                        });
                    })
                    .unwrap();
            }
        })
        .unwrap();
    })
    .unwrap();
    assert_eq!(threads.into_inner().unwrap().len(), 8);
    // Members came back to the parent's reservation and were released.
    wait_until("released", || pool.resident_members() == 0);
}

#[test]
fn split_with_leftover_members_and_even_fork() {
    let _w = watchdog();
    let pool = pool(6);
    let mut exec = Exec::new(&pool, 6).unwrap();
    exec.team(|mut team| {
        let sum = AtomicUsize::new(0);
        team.reborrow()
            .split(&[2, 1], |c, child| {
                child.run(|i, n| {
                    sum.fetch_add(1000 * c + 10 * n + i, Ordering::Relaxed);
                });
            })
            .unwrap();
        // c0: (20+0)+(20+1) = 41; c1: 1000+10+0 = 1010
        assert_eq!(sum.load(Ordering::Relaxed), 41 + 1010);
        // Members of the leftover set are still usable afterwards.
        assert_eq!(ids_of(team.reborrow()).len(), 6);
        let sizes = Mutex::new(Vec::new());
        team.fork(4, |c, child| {
            sizes.lock().unwrap().push((c, child.size().get()))
        });
        let mut sizes = sizes.into_inner().unwrap();
        sizes.sort();
        assert_eq!(sizes, vec![(0, 2), (1, 2), (2, 1), (3, 1)]);
    })
    .unwrap();
}

#[test]
fn invalid_splits_are_rejected_before_anything_runs() {
    let _w = watchdog();
    let pool = pool(4);
    let mut exec = Exec::new(&pool, 4).unwrap();
    exec.team(|mut team| {
        for bad in [&[][..], &[0, 4][..], &[3, 2][..], &[usize::MAX, 2][..]] {
            let r = team.reborrow().split(bad, |_, _| panic!("must not run"));
            assert_eq!(r.unwrap_err(), Error::InvalidSplit);
        }
        team.split(&[4], |_, c| assert_eq!(c.size().get(), 4))
            .unwrap();
    })
    .unwrap();
}

#[test]
fn scoped_policy_is_inherited_and_overridable_and_cannot_release_reservation() {
    let _w = watchdog();
    let pool = builder(4, Duration::from_millis(500)).build().unwrap();
    let base = pool.wait_policy();
    assert_eq!(base.idle_spin(), Duration::from_millis(500));
    let mut exec = Exec::new(&pool, 4).unwrap();
    exec.team_with(WaitOverride::new().idle_runs_rayon(true), |mut team| {
        let p = team.policy();
        assert!(p.idle_runs_rayon());
        assert_eq!(
            p.idle_spin(),
            Duration::from_millis(500),
            "unset field inherited"
        );
        team.reborrow()
            .split(&[2, 2], |c, child| {
                assert_eq!(child.policy(), p, "child inherits");
                if c == 0 {
                    let child =
                        child.with_wait(WaitOverride::new().idle_spin(Duration::from_millis(1)));
                    assert_eq!(child.policy().idle_spin(), Duration::from_millis(1));
                    assert!(child.policy().idle_runs_rayon());
                    child.fork(2, |_, grand| {
                        assert_eq!(grand.policy().idle_spin(), Duration::from_millis(1));
                    });
                } else {
                    // The sibling's override is not visible here.
                    assert_eq!(child.policy().idle_spin(), Duration::from_millis(500));
                }
            })
            .unwrap();
        // A child's 1 ms spin did not release the parent's members.
        std::thread::sleep(Duration::from_millis(50));
        assert_eq!(pool.resident_members(), 3);
        team.run(|_, _| {});
    })
    .unwrap();
    assert_eq!(pool.wait_policy(), base, "pool defaults are never mutated");
}

#[test]
fn idle_spin_governs_how_long_members_stay_awake_after_the_root_reservation() {
    let _w = watchdog();
    let pool = builder(4, Duration::from_secs(30)).build().unwrap();
    let mut exec = Exec::new(&pool, 4).unwrap();
    exec.team(|t| t.run(|_, _| {})).unwrap();
    assert_eq!(pool.resident_members(), 3, "default spin keeps them awake");
    // Warm reacquisition reuses the awake members.
    let again = Mutex::new(HashSet::new());
    exec.team(|t| *again.lock().unwrap() = ids_of(t)).unwrap();
    assert_eq!(again.into_inner().unwrap().len(), 4);
    assert_eq!(pool.resident_members(), 3);
    // A zero override sends them back as soon as this reservation ends.
    exec.team_with(WaitOverride::new().idle_spin(Duration::ZERO), |t| {
        t.run(|_, _| {})
    })
    .unwrap();
    wait_until("zero spin releases", || pool.resident_members() == 0);
}

#[test]
fn release_idle_waits_for_reservations_and_then_returns_members() {
    let _w = watchdog();
    let pool = builder(4, Duration::from_secs(30)).build().unwrap();
    let mut exec = Exec::new(&pool, 4).unwrap();
    exec.team(|mut team| {
        let before = ids_of(team.reborrow());
        pool.release_idle(); // inside a reservation: must not block or revoke
        std::thread::sleep(Duration::from_millis(30));
        assert_eq!(pool.resident_members(), 3, "live token revoked");
        assert_eq!(ids_of(team), before);
    })
    .unwrap();
    wait_until("released after the reservation", || {
        pool.resident_members() == 0
    });
    // Later teams are unaffected by the old request.
    exec.team(|t| t.run(|_, _| {})).unwrap();
    std::thread::sleep(Duration::from_millis(30));
    assert_eq!(pool.resident_members(), 3);
}

#[test]
fn idle_members_serve_rayon_only_when_the_policy_allows() {
    let _w = watchdog();
    let pool = builder(3, Duration::from_secs(30))
        .idle_runs_rayon(true)
        .build()
        .unwrap();
    let mut exec = Exec::new(&pool, 3).unwrap();
    exec.team(|t| t.run(|_, _| {})).unwrap();
    assert_eq!(pool.resident_members(), 2);
    // Three tasks wait for each other: needs all three workers, including
    // the two idle members, to take Rayon jobs.
    let b = Barrier::new(3);
    std::thread::scope(|s| {
        s.spawn(|| {
            Exec::new(&pool, 3).unwrap().fork(3, |_, _| {
                b.wait();
            });
        });
    });
    // And a team can still be taken afterwards.
    assert_eq!(exec.team(|t| t.size().get()).unwrap(), 3);
}

#[test]
fn a_member_panic_reaches_the_leader_after_all_shares_joined() {
    let _w = watchdog();
    let pool = pool(4);
    let mut exec = Exec::new(&pool, 4).unwrap();
    let finished = AtomicUsize::new(0);
    let r = catch_unwind(AssertUnwindSafe(|| {
        exec.team(|team| {
            team.run(|i, _| {
                if i == 2 {
                    panic!("member two failed");
                }
                std::thread::sleep(Duration::from_millis(30));
                finished.fetch_add(1, Ordering::SeqCst);
            });
        })
    }));
    let payload = r.expect_err("panic must propagate");
    assert_eq!(*payload.downcast::<&str>().unwrap(), "member two failed");
    assert_eq!(
        finished.load(Ordering::SeqCst),
        3,
        "shares not joined before unwinding"
    );
    // Reservation released; the pool is reusable.
    wait_until("released", || pool.resident_members() == 0);
    assert_eq!(exec.team(|t| ids_of(t).len()).unwrap(), 4);
}

#[test]
fn a_leader_panic_joins_the_members_first() {
    let _w = watchdog();
    let pool = pool(3);
    let mut exec = Exec::new(&pool, 3).unwrap();
    let finished = AtomicUsize::new(0);
    let r = catch_unwind(AssertUnwindSafe(|| {
        exec.team(|team| {
            team.run(|i, _| {
                if i == 0 {
                    panic!("leader failed");
                }
                std::thread::sleep(Duration::from_millis(30));
                finished.fetch_add(1, Ordering::SeqCst);
            });
        })
    }));
    assert!(r.is_err());
    assert_eq!(finished.load(Ordering::SeqCst), 2);
    assert_eq!(exec.team(|t| t.size().get()).unwrap(), 3);
}

#[test]
fn a_panic_cancels_barriers_so_peers_leave_instead_of_deadlocking() {
    let _w = watchdog();
    let pool = pool(5);
    for panicker in [0, 3] {
        let mut exec = Exec::new(&pool, 5).unwrap();
        let cancelled = AtomicUsize::new(0);
        let r = catch_unwind(AssertUnwindSafe(|| {
            exec.team(|team| {
                team.run_with(|p| {
                    if p.index() == panicker {
                        panic!("boom");
                    }
                    // The panicker never arrives: without cancellation this waits forever.
                    if p.barrier() == Err(Cancelled) {
                        cancelled.fetch_add(1, Ordering::SeqCst);
                    }
                });
            })
        }));
        assert!(r.is_err());
        assert_eq!(cancelled.load(Ordering::SeqCst), 4);
        // A fresh section after the failure has fresh barriers.
        exec.team(|team| {
            team.run_with(|p| {
                p.barrier().unwrap();
                p.barrier().unwrap();
            });
        })
        .unwrap();
    }
}

#[test]
fn a_child_panic_propagates_through_split_after_siblings_finish() {
    let _w = watchdog();
    let pool = pool(4);
    let mut exec = Exec::new(&pool, 4).unwrap();
    let done = AtomicUsize::new(0);
    let r = catch_unwind(AssertUnwindSafe(|| {
        exec.team(|team| {
            team.split(&[2, 2], |c, child| {
                child.run(|i, _| {
                    if c == 1 && i == 1 {
                        panic!("grandchild failed");
                    }
                    std::thread::sleep(Duration::from_millis(30));
                    done.fetch_add(1, Ordering::SeqCst);
                });
            })
            .unwrap();
        })
    }));
    let payload = r.expect_err("must propagate");
    assert_eq!(*payload.downcast::<&str>().unwrap(), "grandchild failed");
    assert_eq!(done.load(Ordering::SeqCst), 3);
    assert_eq!(exec.team(|t| t.size().get()).unwrap(), 4);
}

#[test]
fn a_panic_in_the_scope_callback_releases_the_reservation() {
    let _w = watchdog();
    let pool = pool(3);
    let mut exec = Exec::new(&pool, 3).unwrap();
    let r = catch_unwind(AssertUnwindSafe(|| {
        let _ = exec.team(|_team| panic!("scope failed"));
    }));
    assert!(r.is_err());
    wait_until("released", || pool.resident_members() == 0);
    assert_eq!(exec.team(|t| t.size().get()).unwrap(), 3);
}

#[test]
fn concurrent_teams_on_one_pool_use_disjoint_members() {
    let _w = watchdog();
    let pool = pool(8);
    let all = Mutex::new(Vec::new());
    let both_running = Barrier::new(2);
    std::thread::scope(|s| {
        for _ in 0..2 {
            s.spawn(|| {
                let mut exec = Exec::new(&pool, 4).unwrap();
                exec.team(|team| {
                    team.run(|i, _| {
                        if i == 0 {
                            both_running.wait();
                        }
                        all.lock().unwrap().push(std::thread::current().id());
                    });
                })
                .unwrap();
            });
        }
    });
    let all = all.into_inner().unwrap();
    let uniq: HashSet<_> = all.iter().copied().collect();
    assert_eq!(all.len(), 8);
    assert_eq!(uniq.len(), 8, "a thread served two teams at once");
}

#[test]
fn the_team_runs_from_a_pool_worker_with_members_from_the_others() {
    let _w = watchdog();
    let pool = pool(4);
    let mut exec = Exec::new(&pool, 3).unwrap();
    // The leader is itself a worker of the pool (inside a fork job).
    exec.reborrow().fork(1, |_, _| {});
    let ok = AtomicBool::new(false);
    std::thread::scope(|s| {
        s.spawn(|| {
            Exec::new(&pool, 2).unwrap().fork(2, |i, _| {
                if i == 0 {
                    let mut inner = Exec::new(&pool, 3).unwrap();
                    // Two other workers exist (4 - this one), so this may
                    // succeed or be refused if the sibling is busy; both are fine,
                    // neither may deadlock.
                    if let Ok(n) = inner.team(|t| ids_of(t).len()) {
                        assert_eq!(n, 3);
                        ok.store(true, Ordering::SeqCst);
                    }
                }
            });
        });
    });
    let _ = ok.load(Ordering::SeqCst);
}

#[test]
fn pool_drop_joins_idle_members() {
    let _w = watchdog();
    let pool = builder(4, Duration::from_secs(3600)).build().unwrap();
    let mut exec = Exec::new(&pool, 4).unwrap();
    exec.team(|t| t.run(|_, _| {})).unwrap();
    assert_eq!(pool.resident_members(), 3);
    let t = Instant::now();
    drop(exec);
    drop(pool); // must not wait for the hour-long spin
    assert!(t.elapsed() < Duration::from_secs(5));
}