@webrtc-node/webrtc 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,96 @@
1
+ #pragma once
2
+
3
+ #include <condition_variable>
4
+ #include <cstddef>
5
+ #include <deque>
6
+ #include <functional>
7
+ #include <mutex>
8
+ #include <stdexcept>
9
+ #include <thread>
10
+ #include <utility>
11
+
12
+ namespace webrtc_node {
13
+
14
+ class SendQueue {
15
+ struct Job {
16
+ std::function<void()> run;
17
+ size_t bytes;
18
+ };
19
+ static constexpr size_t MAX_BYTES = 64 * 1024 * 1024;
20
+ static constexpr size_t MAX_JOBS = 65536;
21
+ std::mutex mutex;
22
+ std::condition_variable ready;
23
+ std::deque<Job> jobs;
24
+ std::thread worker;
25
+ size_t bufferedBytes = 0;
26
+ size_t bufferedJobs = 0;
27
+ bool stopped = false;
28
+
29
+ void CheckCapacityLocked(size_t bytes, bool control) const {
30
+ if (stopped)
31
+ throw std::runtime_error("Native send queue is stopped");
32
+ if (!control && (bytes > MAX_BYTES - bufferedBytes || bufferedJobs >= MAX_JOBS))
33
+ throw std::runtime_error("Native send queue is full");
34
+ }
35
+
36
+ void Run() {
37
+ for (;;) {
38
+ Job job;
39
+ {
40
+ std::unique_lock lock(mutex);
41
+ ready.wait(lock, [&]() { return stopped || !jobs.empty(); });
42
+ if (stopped)
43
+ return;
44
+ job = std::move(jobs.front());
45
+ jobs.pop_front();
46
+ }
47
+ job.run();
48
+ job.run = {};
49
+ {
50
+ std::lock_guard lock(mutex);
51
+ bufferedBytes -= job.bytes;
52
+ --bufferedJobs;
53
+ }
54
+ }
55
+ }
56
+
57
+ public:
58
+ ~SendQueue() { Stop(); }
59
+
60
+ void Enqueue(std::function<void()> run, size_t bytes, bool control = false) {
61
+ {
62
+ std::lock_guard lock(mutex);
63
+ CheckCapacityLocked(bytes, control);
64
+ if (!worker.joinable())
65
+ worker = std::thread([this]() { Run(); });
66
+ jobs.push_back({std::move(run), bytes});
67
+ bufferedBytes += bytes;
68
+ ++bufferedJobs;
69
+ }
70
+ ready.notify_one();
71
+ }
72
+
73
+ // The factory runs on the submitting thread after checking capacity.
74
+ template <typename Factory> void EnqueueProduced(size_t bytes, Factory factory) {
75
+ {
76
+ std::lock_guard lock(mutex);
77
+ CheckCapacityLocked(bytes, false);
78
+ }
79
+ Enqueue(factory(), bytes);
80
+ }
81
+
82
+ void Stop() {
83
+ std::deque<Job> discarded;
84
+ {
85
+ std::lock_guard lock(mutex);
86
+ stopped = true;
87
+ discarded.swap(jobs);
88
+ }
89
+ ready.notify_one();
90
+ discarded.clear();
91
+ if (worker.joinable())
92
+ worker.join();
93
+ }
94
+ };
95
+
96
+ } // namespace webrtc_node
@@ -0,0 +1,60 @@
1
+ #pragma once
2
+
3
+ #include <atomic>
4
+ #include <functional>
5
+ #include <memory>
6
+ #include <thread>
7
+ #include <utility>
8
+ #include <vector>
9
+
10
+ namespace webrtc_node {
11
+
12
+ // Scheduling and joining belong to the owning Node environment thread.
13
+ class TeardownTasks {
14
+ struct Task {
15
+ std::shared_ptr<std::atomic<bool>> finished;
16
+ std::thread thread;
17
+ };
18
+ std::vector<Task> tasks;
19
+
20
+ static void Execute(std::function<void()> &run) noexcept {
21
+ try {
22
+ run();
23
+ } catch (...) {
24
+ // Native handle destruction remains best-effort during shutdown.
25
+ }
26
+ }
27
+
28
+ public:
29
+ ~TeardownTasks() { Join(); }
30
+
31
+ void Start(std::function<void()> run) {
32
+ for (auto task = tasks.begin(); task != tasks.end();) {
33
+ if (!task->finished->load()) {
34
+ ++task;
35
+ continue;
36
+ }
37
+ task->thread.join();
38
+ task = tasks.erase(task);
39
+ }
40
+ auto finished = std::make_shared<std::atomic<bool>>(false);
41
+ tasks.push_back({finished, {}});
42
+ try {
43
+ tasks.back().thread = std::thread([run, finished]() mutable {
44
+ Execute(run);
45
+ finished->store(true);
46
+ });
47
+ } catch (...) {
48
+ tasks.pop_back();
49
+ Execute(run);
50
+ }
51
+ }
52
+
53
+ void Join() {
54
+ for (auto &task : tasks)
55
+ task.thread.join();
56
+ tasks.clear();
57
+ }
58
+ };
59
+
60
+ } // namespace webrtc_node