JS8Call-Improved master
Loading...
Searching...
No Matches
optional_work.h
Go to the documentation of this file.
1
4#pragma once
5
6#include <algorithm>
7#include <array>
8#include <atomic>
9#include <chrono>
10#include <cstddef>
11#include <cstdint>
12
13namespace js8 {
14
16 public:
17 using Clock = std::chrono::steady_clock;
18 using Duration = std::chrono::nanoseconds;
19
20 void pending(bool value) noexcept {
21 m_pending.store(value, std::memory_order_release);
22 }
23
24 void nextReady(Clock::time_point when) noexcept {
25 m_nextReadyNs.store(
26 std::chrono::duration_cast<Duration>(when.time_since_epoch())
27 .count(),
28 std::memory_order_release);
29 }
30
31 bool hasDeadline() const noexcept {
32 return m_nextReadyNs.load(std::memory_order_acquire) != 0;
33 }
34
35 // No forecast is available in standalone decoders; their normal behavior
36 // remains unchanged. The application publishes a deadline every RX tick.
37 bool pressured(Duration reserve = Duration{0}) const noexcept {
38 if (m_pending.load(std::memory_order_acquire))
39 return true;
40 auto const deadline = m_nextReadyNs.load(std::memory_order_acquire);
41 if (deadline == 0)
42 return false;
43 auto const now = std::chrono::duration_cast<Duration>(
44 Clock::now().time_since_epoch())
45 .count();
46 return now >= deadline || reserve.count() >= deadline - now;
47 }
48
49 private:
50 std::atomic<bool> m_pending{false};
51 std::atomic<std::int64_t> m_nextReadyNs{0};
52};
53
54enum class OptionalStage : std::size_t { Deep, Rescue, Aided, Count };
55
56// Worker-owned stage estimates are updated from this device's actual elapsed
57// time; they are neither model-dependent nor shared between decoder threads.
58class OptionalWorkBudget {
59 public:
60 using Clock = OptionalWorkSignal::Clock;
61 using Duration = OptionalWorkSignal::Duration;
62 static constexpr std::size_t stages =
63 static_cast<std::size_t>(OptionalStage::Count);
64
65 OptionalWorkBudget(OptionalWorkSignal const *signal, Duration reserve,
66 std::array<Duration, stages> &estimates,
67 bool unknownMandatory = false)
68 : m_signal(signal), m_reserve(reserve), m_estimates(estimates),
69 m_unknownMandatory(unknownMandatory) {}
70
71 bool allow(OptionalStage stage) const {
72 if (m_signal == nullptr)
73 return true;
74 if (m_unknownMandatory && m_signal->pressured())
75 return false;
76 if (m_unknownMandatory && m_signal->hasDeadline())
77 return false;
78 return !m_signal->pressured(m_reserve +
79 m_estimates[static_cast<std::size_t>(stage)]);
80 }
81
82 bool stop() const {
83 return m_signal && m_signal->pressured(m_reserve);
84 }
85
86 void observe(OptionalStage stage, Clock::time_point started) {
87 auto const elapsed = std::chrono::duration_cast<Duration>(
88 Clock::now() - started);
89 auto &estimate = m_estimates[static_cast<std::size_t>(stage)];
90 // A decaying high-water estimate reacts quickly to slower hardware
91 // and slowly recovers when the receiver has spare capacity.
92 estimate = std::max(estimate * 9 / 10, elapsed + elapsed / 4);
93 }
94
95 private:
96 OptionalWorkSignal const *m_signal;
97 Duration m_reserve;
98 std::array<Duration, stages> &m_estimates;
99 bool m_unknownMandatory;
100};
101} // namespace js8
Definition optional_work.h:15
JS8 namespace for Kalman filter-based trackers.
Definition bp_math.h:12