Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
background_job.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <atomic>
7#include <condition_variable>
8#include <cstddef>
9#include <cstdint>
10#include <deque>
11#include <functional>
12#include <memory>
13#include <mutex>
14#include <optional>
15#include <string>
16#include <thread>
17#include <vector>
18
19namespace pulp::runtime {
20
22 low = 0,
23 normal = 1,
24 high = 2,
25};
26
28 std::uint64_t completed = 0;
29 std::uint64_t total = 0;
30 std::string label;
31};
32
34 public:
36 std::function<void(BackgroundJobProgress)> progress);
37
38 bool is_cancelled() const;
40 return cancellation_;
41 }
43
44 private:
45 CancelToken cancellation_;
46 std::function<void(BackgroundJobProgress)> progress_;
47};
48
49using BackgroundJobFunction = std::function<void(BackgroundJobContext&)>;
50
52public:
54
55 void cancel() const;
56 bool valid() const;
57 bool is_cancelled() const;
58 bool is_finished() const;
59 void wait() const;
60 std::optional<BackgroundJobProgress> latest_progress() const;
61
62private:
63 struct State {
64 State() : token(source.token()) {}
65
66 mutable std::mutex mutex;
67 std::condition_variable cv;
68 CancelSource source;
69 CancelToken token;
70 std::optional<BackgroundJobProgress> progress;
71 bool started = false;
72 bool finished = false;
73 };
74
75 explicit BackgroundJobHandle(std::shared_ptr<State> state);
76
77 std::shared_ptr<State> state_;
78
80};
81
86
88public:
91
94
97 void cancel_all();
98 void wait_all();
99 std::size_t pending_count() const;
100
101private:
102 struct QueuedJob {
103 BackgroundJobOptions options;
105 std::shared_ptr<BackgroundJobHandle::State> state;
106 std::uint64_t sequence = 0;
107 };
108
109 void worker_loop();
110 void prune_finished_jobs_locked();
111 static bool has_higher_priority(const QueuedJob& a, const QueuedJob& b);
112
113 mutable std::mutex mutex_;
114 std::condition_variable cv_;
115 std::deque<QueuedJob> queue_;
116 std::vector<std::shared_ptr<BackgroundJobHandle::State>> all_jobs_;
117 bool stopping_ = false;
118 std::thread worker_;
119};
120
126
131
141template <typename T, std::size_t RetireCapacity>
143 static_assert(RetireCapacity > 0, "RetireCapacity must be > 0");
144
145public:
148
151
152 const T* get() const noexcept {
153 return current_.load(std::memory_order_acquire);
154 }
155
156 [[nodiscard]] bool publish(std::unique_ptr<const T> next) {
158 publish_with_status(std::move(next)));
159 }
160
162 std::unique_ptr<const T> next) {
164
165 auto previous = std::move(active_);
166 if (previous && !retired_.try_push(std::move(previous))) {
167 active_ = std::move(previous);
168 retire_overflow_count_.fetch_add(1, std::memory_order_relaxed);
170 }
171
172 const T* next_ptr = next.get();
173 active_ = std::move(next);
174 current_.store(next_ptr, std::memory_order_release);
176 }
177
178 std::size_t reclaim_retired(std::size_t max_to_reclaim = RetireCapacity) {
179 std::size_t reclaimed = 0;
180 while (reclaimed < max_to_reclaim) {
181 auto retired = retired_.try_pop();
182 if (!retired) break;
183 ++reclaimed;
184 }
185 return reclaimed;
186 }
187
188 std::size_t retired_count_approx() const {
189 return retired_.size_approx();
190 }
191
192 std::uint64_t retire_overflow_count() const {
193 return retire_overflow_count_.load(std::memory_order_relaxed);
194 }
195
196private:
197 std::atomic<const T*> current_{nullptr};
198 std::unique_ptr<const T> active_;
199 SpscQueue<std::unique_ptr<const T>, RetireCapacity> retired_;
200 std::atomic<std::uint64_t> retire_overflow_count_{0};
201};
202
203} // namespace pulp::runtime
Definition background_job.hpp:33
const CancelToken & cancellation_token() const
Definition background_job.hpp:39
BackgroundJobContext(CancelToken cancellation, std::function< void(BackgroundJobProgress)> progress)
void publish_progress(BackgroundJobProgress progress) const
Definition background_job.hpp:51
friend class BackgroundJobService
Definition background_job.hpp:79
std::optional< BackgroundJobProgress > latest_progress() const
BackgroundJobService(const BackgroundJobService &)=delete
BackgroundJobHandle submit(BackgroundJobOptions options, BackgroundJobFunction job)
BackgroundJobService & operator=(const BackgroundJobService &)=delete
Move-only owner of a cancellation state.
Definition cancel.hpp:126
Definition cancel.hpp:77
RealtimeResourceSlotPublishStatus publish_with_status(std::unique_ptr< const T > next)
Definition background_job.hpp:161
RealtimeResourceSlot & operator=(const RealtimeResourceSlot &)=delete
const T * get() const noexcept
Definition background_job.hpp:152
std::size_t retired_count_approx() const
Definition background_job.hpp:188
std::size_t reclaim_retired(std::size_t max_to_reclaim=RetireCapacity)
Definition background_job.hpp:178
RealtimeResourceSlot(const RealtimeResourceSlot &)=delete
bool publish(std::unique_ptr< const T > next)
Definition background_job.hpp:156
std::uint64_t retire_overflow_count() const
Definition background_job.hpp:192
~RealtimeResourceSlot()
Definition background_job.hpp:147
Definition generated_event_source.hpp:33
RealtimeResourceSlotPublishStatus
Definition background_job.hpp:121
@ Published
Definition background_job.hpp:122
@ RejectedEmptyResource
Definition background_job.hpp:123
@ RejectedRetireQueueFull
Definition background_job.hpp:124
std::function< void(BackgroundJobContext &)> BackgroundJobFunction
Definition background_job.hpp:49
constexpr bool realtime_resource_slot_publish_succeeded(RealtimeResourceSlotPublishStatus status) noexcept
Definition background_job.hpp:127
BackgroundJobPriority
Definition background_job.hpp:21
@ low
Definition background_job.hpp:22
@ high
Definition background_job.hpp:24
@ normal
Definition background_job.hpp:23
Definition host_transport_projector.hpp:8
SequencerStateChannel is the audio-safe transport for a plugin's structured NON-parameter state: the ...
Definition audio_device_manager.hpp:48
Definition background_job.hpp:82
BackgroundJobPriority priority
Definition background_job.hpp:84
std::string name
Definition background_job.hpp:83
Definition background_job.hpp:27
std::uint64_t total
Definition background_job.hpp:29
std::string label
Definition background_job.hpp:30
std::uint64_t completed
Definition background_job.hpp:28