|
| 1 | +#include "TimeoutDispatcher.h" |
| 2 | + |
| 3 | +#include <cassert> |
| 4 | + |
| 5 | +namespace Babylon::Polyfills::Internal |
| 6 | +{ |
| 7 | + namespace |
| 8 | + { |
| 9 | + using TimePoint = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>; |
| 10 | + |
| 11 | + TimePoint Now() |
| 12 | + { |
| 13 | + return std::chrono::time_point_cast<std::chrono::microseconds, std::chrono::steady_clock>(std::chrono::steady_clock::now()); |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + struct TimeoutDispatcher::Timeout |
| 18 | + { |
| 19 | + TimeoutId id; |
| 20 | + |
| 21 | + // Make this non-shared when JsRuntime::Dispatch supports it. |
| 22 | + std::shared_ptr<Napi::FunctionReference> function; |
| 23 | + |
| 24 | + TimePoint time; |
| 25 | + |
| 26 | + Timeout(TimeoutId id, std::shared_ptr<Napi::FunctionReference> function, TimePoint time) |
| 27 | + : id{id} |
| 28 | + , function{std::move(function)} |
| 29 | + , time{time} |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + Timeout(const Timeout&) = delete; |
| 34 | + Timeout(Timeout&&) = delete; |
| 35 | + }; |
| 36 | + |
| 37 | + TimeoutDispatcher::TimeoutDispatcher(Babylon::JsRuntime& runtime) |
| 38 | + : m_runtime{runtime} |
| 39 | + , m_thread{std::thread{&TimeoutDispatcher::ThreadFunction, this}} |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | + TimeoutDispatcher::~TimeoutDispatcher() |
| 44 | + { |
| 45 | + { |
| 46 | + std::unique_lock<std::mutex> lk{m_mutex}; |
| 47 | + m_idMap.clear(); |
| 48 | + m_timeMap.clear(); |
| 49 | + } |
| 50 | + |
| 51 | + m_shutdown = true; |
| 52 | + m_condVariable.notify_one(); |
| 53 | + m_thread.join(); |
| 54 | + } |
| 55 | + |
| 56 | + TimeoutDispatcher::TimeoutId TimeoutDispatcher::Dispatch(std::shared_ptr<Napi::FunctionReference> function, std::chrono::milliseconds delay) |
| 57 | + { |
| 58 | + if (delay.count() < 0) |
| 59 | + { |
| 60 | + delay = std::chrono::milliseconds{0}; |
| 61 | + } |
| 62 | + |
| 63 | + std::unique_lock<std::mutex> lk{m_mutex}; |
| 64 | + |
| 65 | + const auto id = NextTimeoutId(); |
| 66 | + const auto earliestTime = m_timeMap.empty() ? TimePoint::max() |
| 67 | + : m_timeMap.cbegin()->second->time; |
| 68 | + const auto time = Now() + delay; |
| 69 | + const auto result = m_idMap.insert({id, std::make_unique<Timeout>(id, std::move(function), time)}); |
| 70 | + m_timeMap.insert({time, result.first->second.get()}); |
| 71 | + |
| 72 | + if (time <= earliestTime) |
| 73 | + { |
| 74 | + m_runtime.Dispatch([this](Napi::Env) { |
| 75 | + m_condVariable.notify_one(); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + return id; |
| 80 | + } |
| 81 | + |
| 82 | + void TimeoutDispatcher::Clear(TimeoutId id) |
| 83 | + { |
| 84 | + std::unique_lock<std::mutex> lk{m_mutex}; |
| 85 | + const auto itId = m_idMap.find(id); |
| 86 | + if (itId != m_idMap.end()) |
| 87 | + { |
| 88 | + const auto& timeout = itId->second; |
| 89 | + const auto timeRange = m_timeMap.equal_range(timeout->time); |
| 90 | + |
| 91 | + assert(timeRange.first != m_timeMap.end() && "m_idMap and m_timeMap are out of sync"); |
| 92 | + |
| 93 | + for (auto itTime = timeRange.first; itTime != timeRange.second; itTime++) |
| 94 | + { |
| 95 | + if (itTime->second->id == id) |
| 96 | + { |
| 97 | + m_timeMap.erase(itTime); |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + m_idMap.erase(itId); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + TimeoutDispatcher::TimeoutId TimeoutDispatcher::NextTimeoutId() |
| 107 | + { |
| 108 | + while (true) |
| 109 | + { |
| 110 | + ++m_lastTimeoutId; |
| 111 | + |
| 112 | + if (m_lastTimeoutId <= 0) |
| 113 | + { |
| 114 | + m_lastTimeoutId = 1; |
| 115 | + } |
| 116 | + |
| 117 | + if (m_idMap.find(m_lastTimeoutId) == m_idMap.end()) |
| 118 | + { |
| 119 | + return m_lastTimeoutId; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + void TimeoutDispatcher::ThreadFunction() |
| 125 | + { |
| 126 | + while (!m_shutdown) |
| 127 | + { |
| 128 | + std::unique_lock<std::mutex> lk{m_mutex}; |
| 129 | + TimePoint nextTimePoint{}; |
| 130 | + |
| 131 | + while (!m_timeMap.empty()) |
| 132 | + { |
| 133 | + nextTimePoint = m_timeMap.begin()->second->time; |
| 134 | + if (nextTimePoint <= Now()) |
| 135 | + { |
| 136 | + break; |
| 137 | + } |
| 138 | + |
| 139 | + m_condVariable.wait_until(lk, nextTimePoint); |
| 140 | + } |
| 141 | + |
| 142 | + while (!m_timeMap.empty() && m_timeMap.begin()->second->time == nextTimePoint) |
| 143 | + { |
| 144 | + const auto* timeout = m_timeMap.begin()->second; |
| 145 | + CallFunction(timeout->function); |
| 146 | + m_timeMap.erase(m_timeMap.begin()); |
| 147 | + m_idMap.erase(timeout->id); |
| 148 | + } |
| 149 | + |
| 150 | + while (!m_shutdown && m_timeMap.empty()) |
| 151 | + { |
| 152 | + m_condVariable.wait(lk); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + void TimeoutDispatcher::CallFunction(std::shared_ptr<Napi::FunctionReference> function) |
| 158 | + { |
| 159 | + if (function) |
| 160 | + { |
| 161 | + m_runtime.Dispatch([function = std::move(function)](Napi::Env) |
| 162 | + { function->Call({}); }); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments