-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathWorkQueue.h
More file actions
58 lines (48 loc) · 1.48 KB
/
WorkQueue.h
File metadata and controls
58 lines (48 loc) · 1.48 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
#pragma once
#include <arcana/threading/dispatcher.h>
#include <arcana/threading/task.h>
#include <napi/env.h>
#include <future>
#include <functional>
#include <exception>
namespace Babylon
{
class WorkQueue
{
public:
WorkQueue(std::function<void()> threadProcedure);
~WorkQueue();
template<typename CallableT>
void Append(CallableT callable)
{
// Manual dispatcher queueing requires a copyable CallableT, we use a shared pointer trick to make a
// copyable callable if necessary.
if constexpr (std::is_copy_constructible<CallableT>::value)
{
m_dispatcher.queue([this, callable = std::move(callable)]() {
Invoke(callable);
});
}
else
{
m_dispatcher.queue([this, callablePtr = std::make_shared<CallableT>(std::move(callable))]() {
Invoke(*callablePtr);
});
}
}
void Suspend();
void Resume();
void Run(Napi::Env);
private:
template<typename CallableT>
void Invoke(CallableT& callable)
{
callable(m_env.value());
}
std::optional<Napi::Env> m_env{};
std::optional<std::scoped_lock<std::mutex>> m_suspensionLock{};
arcana::cancellation_source m_cancelSource{};
arcana::manual_dispatcher<128> m_dispatcher{};
std::thread m_thread;
};
}