Repository navigation
Expand file tree
/
Copy pathCompilePool.h
More file actions
76 lines (62 loc) · 2.41 KB
/
Copy pathCompilePool.h
File metadata and controls
76 lines (62 loc) · 2.41 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
68
69
70
71
72
73
74
75
76
#ifndef NATIVESCRIPT_COMMON_LOADER_COMPILE_POOL_H_
#define NATIVESCRIPT_COMMON_LOADER_COMPILE_POOL_H_
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <functional>
#include <mutex>
namespace nativescript::common::loader {
// The threads that read module files and run V8's background compiles
// (ScriptStreamingTask::Run, ConsumeCodeCacheTask::Run), shared by every
// isolate in the process. The background parser checks for stack overflow
// against V8's --stack-size below the point where Run() starts, so a thread
// with less stack than that overflows for real before the parser can report
// a RangeError: callers pass BackgroundCompileStackBytes.
class CompilePool {
public:
// `run` is false when the job is discarded instead of run. Either way it is
// called exactly once, unless Post returned false.
using Job = std::function<void(bool run)>;
// Never destroyed: its threads wait on it for the life of the process.
static CompilePool& Shared();
explicit CompilePool(unsigned maxThreads);
CompilePool(const CompilePool&) = delete;
CompilePool& operator=(const CompilePool&) = delete;
// Queues `job` for `owner`, starting a thread of `stackBytes` when every
// thread is busy and fewer than the maximum are running. False when no
// thread could be started; `job` is then destroyed without being called.
bool Post(const void* owner, size_t stackBytes, Job job);
// Calls every job `owner` still has queued with run == false, on the
// calling thread, outside the pool's lock.
void Discard(const void* owner);
private:
struct Entry {
const void* owner;
Job job;
};
bool StartThreadLocked(size_t stackBytes);
void ThreadMain();
std::mutex mutex_;
std::condition_variable available_;
std::deque<Entry> jobs_;
size_t idle_ = 0;
unsigned threads_ = 0;
const unsigned maxThreads_;
};
// Bumped each time a file load finishes on the pool, so a synchronous wait
// can sleep until there is something to finalize.
class FileJobSignal {
public:
uint64_t Generation();
void Notify();
// Returns when the generation differs from `seen` or after `timeout`.
void WaitForChange(uint64_t seen, std::chrono::milliseconds timeout);
private:
std::mutex mutex_;
std::condition_variable changed_;
uint64_t generation_ = 0;
};
} // namespace nativescript::common::loader
#endif // NATIVESCRIPT_COMMON_LOADER_COMPILE_POOL_H_