// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® export module Crafter.Build:Jobs; import std; export namespace Crafter { // The threads build steps run on: a fixed number of workers taking jobs // from one queue, so that no more steps run at once than there are // workers, whichever builds they belong to. Process() is the pool every // build in this process shares, with a worker per hardware thread. // // A job is one step of a build: one compiler, linker or archiver run, one // shader or asset compiled in process. The pool bounds the steps that run // at once, not the threads a step starts for itself, as lld does for a // link and the compressor of Crafter.Asset for an asset. // // A job never waits for another job. Whatever waits does so on a // thread that is not a worker: a Build() for the builds of its // dependencies, a step for the steps it comes after (JobGroup holds it // back instead). A worker that waited for a job still in the queue would // keep from it the very thread it needs, and with every worker waiting // like that nothing would run again. // // Before this, Build() started a thread for every stale shader, asset // to compress, C, CUDA and C++ unit, and nothing bounded how many of // them ran at once. class JobPool { public: // At most `workers` jobs at once, at least one. The threads are // started as jobs need them: a build with two stale files starts two. explicit JobPool(std::uint32_t workers); // Runs what is still queued, then joins the workers. Nothing may be // handing it jobs any more: every group on it is gone by then. ~JobPool(); JobPool(const JobPool&) = delete; JobPool& operator=(const JobPool&) = delete; // The pool of this process: one worker per hardware thread // (std::thread::hardware_concurrency), and one where that is not // known. Never destroyed, so that its workers are not joined while // the statics the last job may still be using are being destroyed. static JobPool& Process(); std::uint32_t Workers() const; // Runs `work` on a worker and returns when it has finished; what it // throws is thrown here. For a step that stands by itself, as // RunBuildCommand uses it for a link. Called on a worker of this // pool it runs `work` there and then, since that thread is the one // the step would have occupied. void Run(const std::function& work); private: friend class JobGroup; // Queues `job`, which must not throw. Throws only when the pool has // no thread and cannot start one, and then `job` is not queued. void Submit(std::function job); void Work(); const std::uint32_t limit; std::mutex mutex; std::condition_variable wake; std::deque> queue; std::vector threads; // Workers waiting for a job. A job queued while one waits is its to // take, so a thread is only started for a job beyond those. std::uint32_t idle = 0; bool stopping = false; }; // The jobs of one piece of work, such as the build of a configuration, // on the workers of a pool. A job starts once every job it was added // after has finished, so the order steps need (a unit after the BMIs it // imports) costs no thread while it is waited for. // // The first job to fail fails the group: no job of it starts after that, // the ones already running finish, and Wait() returns that first error. // Fail() does the same for work done outside the jobs, so one group // holds the outcome of everything a build runs at once. class JobGroup { public: using Job = std::size_t; // Throws std::logic_error on a worker of `pool`: a job that waited // for jobs of its own would occupy a worker they need (see JobPool). explicit JobGroup(JobPool& pool = JobPool::Process()); // Keeps what has not started from starting and waits for what is // running, so a job never outlives what it was given by reference, // whichever way the scope that made the group is left. ~JobGroup(); JobGroup(const JobGroup&) = delete; JobGroup& operator=(const JobGroup&) = delete; // Adds a job, counted as a Progress::Task under `label` from now // until it has finished or been skipped. It starts once every job in // `after` has finished, at once when there is none; `after` names // jobs of this group, so a job can only come after ones added before // it and no two can wait for each other. `work` returns what went // wrong, empty when nothing did; an exception it throws is its error // too. It runs on a worker: it must not wait for other jobs, and it // must own what it uses or use only what outlives the group. Job Add(std::string label, std::function work, std::span after = {}); // Fails the group with `error`, as a job returning it would. Without // effect on a group that has already failed. void Fail(std::string error); bool Failed() const; // Returns once every job added so far has finished or been skipped: // the first error, empty when there was none. std::string Wait(); private: struct Node; void Start(Node& node); void Execute(Node& node); JobPool& pool; mutable std::mutex mutex; std::condition_variable settled; // Owned through pointers: a job on a worker holds on to its node // while Add() grows the vector. std::vector> nodes; std::size_t unfinished = 0; bool failed = false; std::string error; }; }