Commit 7d81e3c
committed
Replace FTP pool barrier dispatch with per-worker condvar dispatch
Mirror of Phase 5.3 on the FTP side. Replaces broadcast mark_barrier/
done_barrier dispatch in the persistent thread pool with per-worker
(wake_mutex, wake_cond, wake_flag) and pool-level (done_mutex, done_cond,
workers_done_count). Main thread wakes only [1, adaptive_workers) helpers
and waits for that many done signals. Idle workers stay asleep.
Together with the previous controller-state commit (5ef3a61) and
the helper-extraction commit (12a3440), the FTP build now uses the
same random-walk controller logic as the GIL build via the shared
_PyGC_RandomWalkUpdate helper. The adaptive count actually takes effect
on FTP now: _PyGC_GetParallelWorkers returns pool->adaptive_workers, so
FTP collections scale per-collection like GIL.
Component changes:
- Include/internal/pycore_gc_barrier.h: add _PyGCBarrier_Resize. Safe
only when no one is waiting; used by the FTP dispatch to size
phase_barrier to adaptive_workers per dispatch (so internal multi-
phase work like update_refs init/compute only waits for active
participants).
- _PyGCWorkerState: add wake_mutex, wake_cond, wake_flag.
- _PyGCThreadPool: add done_mutex, done_cond, workers_done_count,
dispatch_in_progress. Drop mark_barrier and done_barrier (no longer
used for dispatch). Keep phase_barrier; it's now resized per dispatch.
- _PyGC_FTDispatchAndWait: new helper (mirror of GIL _PyGC_DispatchAndWait).
Resizes phase_barrier, resets done counter, wakes helpers, runs main's
share (worker 0), waits for helper completions. Has reentrancy guard
although FTP STW collections shouldn't ever be reentrant; the four call
sites assert reentrant == 0.
- thread_pool_worker_func: replace mark_barrier wait with per-worker
condvar wait; replace done_barrier wait with done_mutex/cond signal.
- thread_pool_do_work: removed the adaptive_workers early-return added
in the previous commit; no longer needed because idle workers don't
wake at all under condvar dispatch.
- _PyGC_ThreadPoolInit: initialise the new sync primitives, per-worker
condvars, drop mark/done barrier Init. Update error-path cleanup.
- _PyGC_ThreadPoolFini: wake all helpers via per-worker condvars (so
they see shutdown and exit), Fini per-worker mutex/cond and pool-level
done_mutex/done_cond. Drop mark/done barrier Fini.
- Four pool dispatch call sites (PropagateAliveWithPool,
ParallelUpdateRefsWithPool, ParallelMarkHeapWithPool,
ParallelScanHeapWithPool): replace mark_barrier + thread_pool_do_work(0)
+ done_barrier with _PyGC_FTDispatchAndWait(pool, adaptive_workers).
The ScanHeap merge loop now iterates adaptive_workers (only active
workers contribute results).
- gcmodule.c get_parallel_config (FTP branch): expose both num_workers
(configured max) and adaptive_workers (current); mirror of GIL build's
config API. Previously was inadvertently returning adaptive_workers as
num_workers via _PyGC_GetParallelWorkers, breaking the historical
meaning of the num_workers key.
- _PyGC_FTParallelGetStats: same num_workers/adaptive_workers split.
- _PyGC_GetParallelWorkers: now returns pool->adaptive_workers (the
earlier revert of this is now superseded by the dispatch refactor that
makes it safe to return the adaptive count).
- Loosen assertions in PropagateAliveWithPool and UpdateRefs/MarkHeap/
ScanHeap that previously required state->num_workers == pool->num_workers
to allow state->num_workers <= pool->num_workers.
Verified end-to-end:
- FTP smoke test (20 collections, 4 workers, enable/disable cycle): OK
- FTP parallel-GC suite: 177 tests pass, 7 skips
- FTP full CPython suite: 481 OK, 0 failures
- GIL full CPython suite: 479 OK, 0 failures (no regression)
The barrier mark_barrier and done_barrier struct fields can be cleaned
up in a follow-up (parallel to GIL Phase 5 cleanup that removed the
same fields from _PyParallelGCState).1 parent 9c77265 commit 7d81e3c
4 files changed
Lines changed: 239 additions & 100 deletions
File tree
- Include/internal
- Modules
- Python
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
100 | 116 | | |
101 | 117 | | |
102 | 118 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
273 | 273 | | |
274 | 274 | | |
275 | 275 | | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
276 | 284 | | |
277 | 285 | | |
278 | 286 | | |
| |||
328 | 336 | | |
329 | 337 | | |
330 | 338 | | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
335 | 353 | | |
336 | 354 | | |
337 | 355 | | |
338 | 356 | | |
339 | 357 | | |
340 | 358 | | |
341 | 359 | | |
342 | | - | |
| 360 | + | |
343 | 361 | | |
344 | 362 | | |
345 | 363 | | |
| |||
529 | 547 | | |
530 | 548 | | |
531 | 549 | | |
532 | | - | |
| 550 | + | |
| 551 | + | |
533 | 552 | | |
534 | | - | |
535 | | - | |
536 | | - | |
537 | | - | |
538 | | - | |
539 | | - | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
540 | 557 | | |
541 | | - | |
542 | | - | |
543 | | - | |
544 | | - | |
545 | | - | |
546 | | - | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
547 | 561 | | |
548 | 562 | | |
549 | 563 | | |
550 | 564 | | |
551 | 565 | | |
552 | 566 | | |
553 | 567 | | |
554 | | - | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
555 | 572 | | |
556 | 573 | | |
557 | 574 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
730 | 730 | | |
731 | 731 | | |
732 | 732 | | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
733 | 736 | | |
734 | | - | |
| 737 | + | |
735 | 738 | | |
736 | 739 | | |
737 | 740 | | |
| |||
740 | 743 | | |
741 | 744 | | |
742 | 745 | | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
743 | 756 | | |
744 | 757 | | |
745 | 758 | | |
| |||
0 commit comments