Repository navigation
Expand file tree
/
Copy paththreads.h
More file actions
67 lines (58 loc) · 2.46 KB
/
Copy paththreads.h
File metadata and controls
67 lines (58 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// threads.h - a persistent thread pool for splitting a frame's big
// operations (convs, matrix products) across the CPU's cores.
//
// set_threads(10); // once, at startup
// pool().run(chunks, [&](int i) { ... }); // calls fn(0..chunks-1)
//
// Workers are created once and wait for work: creating threads per
// operation would cost more than many operations themselves. Chunks are
// handed out one at a time from a shared counter, so fast cores (Apple's
// performance cores) simply take more chunks than slow ones. The caller
// works on chunks too, and run() returns when every chunk is done.
//
// run() never allocates: the work is passed as a function pointer plus a
// context pointer, not a std::function (which may allocate on every call,
// and a frame makes ~300 calls).
//
// Waiting workers spin (with yield) so they react within microseconds;
// they use a core while waiting. Fine for generating audio; an idle-friendly
// mode can come with the real-time engine.
#pragma once
#include <atomic>
#include <thread>
#include <type_traits>
#include <vector>
namespace mrt2 {
class ThreadPool {
public:
explicit ThreadPool(int threads); // total threads, including the caller
~ThreadPool();
ThreadPool(const ThreadPool &) = delete;
ThreadPool &operator=(const ThreadPool &) = delete;
int size() const { return n_; }
// Call fn(i) for every i in [0, chunks), spread over all threads.
template <class F> void run(int chunks, F &&fn) {
using Fn = std::remove_reference_t<F>;
run_impl(chunks, [](void *ctx, int i) { (*static_cast<Fn *>(ctx))(i); }, (void *)&fn);
}
private:
void run_impl(int chunks, void (*call)(void *, int), void *ctx);
void work(); // take chunks until none are left
void worker_main();
int n_;
std::vector<std::thread> workers_;
std::atomic<int> generation_{0}; // bumped once per run(): wakes the workers
std::atomic<int> next_{0}; // next chunk to hand out
std::atomic<int> done_{0}; // workers finished with this run
std::atomic<bool> quit_{false};
void (*call_)(void *, int) = nullptr;
void *ctx_ = nullptr;
int chunks_ = 0;
};
// The pool used by ops, conv and the codec. Default: 1 thread (no workers).
void set_threads(int threads);
ThreadPool &pool();
// Ask the OS to run the calling thread on performance cores (Apple:
// QoS "user interactive"; elsewhere nothing to do).
void prefer_performance_cores();
} // namespace mrt2