Job system module primal.jobs #16

Open
UnflavoredMeson/Primal_Plus_Plus
opened 2026-11-24 20:15:52 +00:00 by UnflavoredMeson · 0 comments
Owner

Why

Transforms, render preparation, command recording and content loading all need to run on more than one core. A job system is the one place threads are made; everything else submits work.

Decided

  • An own pool, no fibers, no library (the reasoning: Multithreading sections 5 and 8).
  • A C++ named module, primal.jobs (Engine/Core/Jobs.ixx + Jobs.cpp): the first module. import comes after every #include, and only in .cpp files (GCC 16 fails otherwise).
  • jobs::submit, counters and wait() (a waiting thread runs jobs), parallel_for; PRIMAL_JOBS=<n> sets the worker count, 0 runs every job on its caller.
  • ThreadSanitizer from here on: preset linux-tsan, ci.sh tsan, a CI job.
  • attach_thread() for a foreign thread only if the editor needs one (it likely doesn't: the C++ editor renders on the frame thread). UnflavoredMeson/Primal#72.
  • Spinning before sleeping is measured later (S2-03).

Read first

  • Multithreading, sections 3-5, 8, 9 (step 4: what changed from the design).
  • Preferences, "Modules".
  • Engine/Utilities/Threading.h on master (what it replaces).

Steps

  • 1. Write UnitTests/JobTests.cpp first: submit and wait, nested jobs, parallel_for over 0, 1 and 100,000 items, PRIMAL_JOBS=0.
  • 2. Write the module interface and a single-threaded implementation that passes.
  • 3. Add the worker threads and queues.
  • 4. Add the TSan preset and CI job; run the tests under it until clean.
  • 5. Add the module to both build systems (Engine/Modules.props for the other Windows projects).

Done when

  • JobTests pass on clang, GCC and MSVC, Debug and Release, and under TSan.
  • 0 warnings both OSes; tidy 0 (it needs the compiled module interfaces first).

Claude can help with

  • Reading TSan reports.
  • Reviewing the wait/wake logic, where lost wake-ups hide.

Learn

  • What a lost wake-up is and how a condition variable's predicate prevents it.
  • Why a waiting thread should run jobs instead of blocking.
## Why Transforms, render preparation, command recording and content loading all need to run on more than one core. A job system is the one place threads are made; everything else submits work. ## Decided - An own pool, no fibers, no library (the reasoning: [Multithreading](https://forgejo.unflavoredmeson.com/UnflavoredMeson/Primal_Plus_Plus/wiki/Multithreading) sections 5 and 8). - A C++ named module, `primal.jobs` (`Engine/Core/Jobs.ixx` + `Jobs.cpp`): the first module. `import` comes after every `#include`, and only in `.cpp` files (GCC 16 fails otherwise). - `jobs::submit`, counters and `wait()` (a waiting thread runs jobs), `parallel_for`; `PRIMAL_JOBS=<n>` sets the worker count, `0` runs every job on its caller. - ThreadSanitizer from here on: preset `linux-tsan`, `ci.sh tsan`, a CI job. - `attach_thread()` for a foreign thread only if the editor needs one (it likely doesn't: the C++ editor renders on the frame thread). `UnflavoredMeson/Primal#72`. - Spinning before sleeping is measured later (S2-03). ## Read first - [Multithreading](https://forgejo.unflavoredmeson.com/UnflavoredMeson/Primal_Plus_Plus/wiki/Multithreading), sections 3-5, 8, 9 (step 4: what changed from the design). - [Preferences](https://forgejo.unflavoredmeson.com/UnflavoredMeson/Primal_Plus_Plus/wiki/Preferences), "Modules". - `Engine/Utilities/Threading.h` on `master` (what it replaces). ## Steps - [ ] 1. Write `UnitTests/JobTests.cpp` first: submit and wait, nested jobs, `parallel_for` over 0, 1 and 100,000 items, `PRIMAL_JOBS=0`. - [ ] 2. Write the module interface and a single-threaded implementation that passes. - [ ] 3. Add the worker threads and queues. - [ ] 4. Add the TSan preset and CI job; run the tests under it until clean. - [ ] 5. Add the module to both build systems (`Engine/Modules.props` for the other Windows projects). ## Done when - [ ] JobTests pass on clang, GCC and MSVC, Debug and Release, and under TSan. - [ ] 0 warnings both OSes; tidy 0 (it needs the compiled module interfaces first). ## Claude can help with - Reading TSan reports. - Reviewing the wait/wake logic, where lost wake-ups hide. ## Learn - What a lost wake-up is and how a condition variable's predicate prevents it. - Why a waiting thread should run jobs instead of blocking.
Sign in to join this conversation.
No assignees
No project
No milestone
Blocks
#18 Job system: spin before sleeping (measured)
UnflavoredMeson/Primal_Plus_Plus
#27 Shader hot reload
UnflavoredMeson/Primal_Plus_Plus
#28 ContentTools on both OSes
UnflavoredMeson/Primal_Plus_Plus
Depends on
#6 Logging through primal::log
UnflavoredMeson/Primal_Plus_Plus
#11 The engine as one shared library
UnflavoredMeson/Primal_Plus_Plus
Notifications
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
UnflavoredMeson/Primal_Plus_Plus#16
No description provided.