Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
thread-pool.hh
Go to the documentation of this file.
1#pragma once
3
4#include "nix/util/error.hh"
5#include "nix/util/sync.hh"
6
7#include <boost/compat/move_only_function.hpp>
8
9#include <queue>
10#include <functional>
11#include <thread>
12#include <map>
13#include <atomic>
14
15namespace nix {
16
17MakeError(ThreadPoolShutDown, Error);
18
23class ThreadPool
24{
25public:
26
27 ThreadPool(size_t maxThreads = 0);
28
29 ~ThreadPool();
30
38 using work_t = boost::compat::move_only_function<void()>;
39
43 void enqueue(work_t t);
44
57 void process();
58
63 void shutdown();
64
65private:
66
67 size_t maxThreads;
68
69 struct State
70 {
71 std::queue<work_t> pending;
72 size_t active = 0;
73 std::exception_ptr exception;
74 std::vector<std::thread> workers;
75 bool draining = false;
76 };
77
78 std::atomic_bool quit{false};
79
80 Sync<State> state_;
81
82 std::condition_variable work;
83
84 void doWork(bool mainThread);
85};
86
92template<typename T>
93void processGraph(const std::set<T> & nodes, fun<std::set<T>(const T &)> getEdges, fun<void(const T &)> processNode)
94{
95 struct Graph
96 {
97 std::set<T> left;
98 std::map<T, std::set<T>> refs, rrefs;
99 };
100
101 Sync<Graph> graph_(Graph{nodes, {}, {}});
102
103 std::function<void(const T &)> worker;
104
105 /* Create pool last to ensure threads are stopped before other destructors
106 * run */
107 ThreadPool pool;
108
109 worker = [&](const T & node) {
110 {
111 auto graph(graph_.lock());
112 auto i = graph->refs.find(node);
113 if (i == graph->refs.end())
114 goto getRefs;
115 goto doWork;
116 }
117
118 getRefs: {
119 auto refs = getEdges(node);
120 refs.erase(node);
121
122 {
123 auto graph(graph_.lock());
124 for (auto & ref : refs)
125 if (graph->left.count(ref)) {
126 graph->refs[node].insert(ref);
127 graph->rrefs[ref].insert(node);
128 }
129 if (graph->refs[node].empty())
130 goto doWork;
131 }
132 }
133
134 return;
135
136 doWork:
137 processNode(node);
138
139 /* Enqueue work for all nodes that were waiting on this one
140 and have no unprocessed dependencies. */
141 {
142 auto graph(graph_.lock());
143 for (auto & rref : graph->rrefs[node]) {
144 auto & refs(graph->refs[rref]);
145 auto i = refs.find(node);
146 assert(i != refs.end());
147 refs.erase(i);
148 if (refs.empty())
149 pool.enqueue(std::bind(worker, rref));
150 }
151 graph->left.erase(node);
152 graph->refs.erase(node);
153 graph->rrefs.erase(node);
154 }
155 };
156
157 for (auto & node : nodes) {
158 try {
159 pool.enqueue(std::bind(worker, std::ref(node)));
160 } catch (ThreadPoolShutDown &) {
161 /* Stop if the thread pool is shutting down. It means a
162 previous work item threw an exception, so process()
163 below will rethrow it. */
164 break;
165 }
166 }
167
168 pool.process();
169
170 if (!graph_.lock()->left.empty())
171 throw Error("graph processing incomplete (cyclic reference?)");
172}
173
174} // namespace nix
Definition error.hh:310
Definition thread-pool.hh:24
void process()
Definition thread-pool.cc:58
boost::compat::move_only_function< void()> work_t
Definition thread-pool.hh:38
void shutdown()
Definition thread-pool.cc:26
void enqueue(work_t t)
Definition thread-pool.cc:46
Definition fun.hh:19
Definition ref.hh:58
This file defines two main structs/classes used in nix error handling.
void processGraph(const std::set< T > &nodes, fun< std::set< T >(const T &)> getEdges, fun< void(const T &)> processNode)
Definition thread-pool.hh:93