docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
codegen_build_pool.h
Go to the documentation of this file.
1#pragma once
2#include <condition_variable>
3#include <list>
4#include <map>
5#include <memory>
6#include <queue>
7#include <thread>
8
14
15namespace docc::compile {
16
18public:
19 virtual ~CompileExecutor() = default;
20
21 virtual void add_compile_state(std::unique_ptr<CompileState> state) = 0;
22 virtual void await_compiles_finished() = 0;
23
24 virtual void for_each_src(std::function<void(CompileState&)> fn) = 0;
25
26 virtual bool is_parallel() = 0;
27};
28
30private:
31 std::vector<std::unique_ptr<CompileState>> srcs_;
32 std::mutex mutex_;
33 std::atomic_int outstanding_compiles_ = 0;
34
35 // Thread pool members
36 std::vector<std::thread> workers_;
37 // Tasks whose codegen still needs to run. Codegen is unconstrained, so a plain
38 // FIFO queue is enough; running it eagerly keeps gated compiles unblocked.
39 std::queue<CompileState*> codegen_queue_;
40 // Tasks that finished codegen and are waiting to compile (their compile may still
41 // be gated on lower codegen orders).
42 std::vector<CompileState*> compile_ready_;
43 // Number of states whose codegen has not finished yet, keyed by codegen_order
44 // (zero entries erased).
45 std::map<int, int> pending_codegen_by_order_;
46 std::mutex queue_mutex_;
47 std::condition_variable cv_;
48 std::condition_variable done_cv_;
49 bool stop_ = false;
50
51 void worker_loop();
52
53 // The following helpers must be called while holding queue_mutex_.
54 bool has_pending_codegen_below(int order) const;
55 bool has_ready_compile() const;
56 CompileState* take_ready_compile();
57
58public:
59 CodegenBuildPool(int num_threads);
61
62 void add_compile_state(std::unique_ptr<CompileState> state) override;
63 void await_compiles_finished() override;
64
65 void for_each_src(std::function<void(CompileState&)> fn) override;
66
67 bool is_parallel() override { return workers_.size() > 1; }
68};
69
70} // namespace docc::compile
bool is_parallel() override
Definition codegen_build_pool.h:67
void for_each_src(std::function< void(CompileState &)> fn) override
CodegenBuildPool(int num_threads)
void add_compile_state(std::unique_ptr< CompileState > state) override
void await_compiles_finished() override
Definition codegen_build_pool.h:17
virtual void for_each_src(std::function< void(CompileState &)> fn)=0
virtual void await_compiles_finished()=0
virtual ~CompileExecutor()=default
virtual void add_compile_state(std::unique_ptr< CompileState > state)=0
Definition codegen_compiler.h:11
Definition targets.h:8