|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: GPL-3.0-only |
| 3 | + * MuseScore-CLA-applies |
| 4 | + * |
| 5 | + * MuseScore |
| 6 | + * Music Composition & Notation |
| 7 | + * |
| 8 | + * Copyright (C) 2025 MuseScore Limited and others |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License version 3 as |
| 12 | + * published by the Free Software Foundation. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | + |
| 23 | +#include "timer.h" |
| 24 | + |
| 25 | +#include <condition_variable> |
| 26 | +#include <functional> |
| 27 | +#include <queue> |
| 28 | +#include <thread> |
| 29 | +#include <atomic> |
| 30 | +#include <mutex> |
| 31 | + |
| 32 | +namespace muse { |
| 33 | +class TimerScheduler |
| 34 | +{ |
| 35 | +public: |
| 36 | + using Clock = std::chrono::steady_clock; |
| 37 | + using TimePoint = Clock::time_point; |
| 38 | + using Duration = Clock::duration; |
| 39 | + using Callback = std::function<void ()>; |
| 40 | + |
| 41 | + static TimerScheduler& instance() |
| 42 | + { |
| 43 | + static TimerScheduler scheduler; |
| 44 | + return scheduler; |
| 45 | + } |
| 46 | + |
| 47 | + struct TimerHandle { |
| 48 | + std::atomic_bool active { true }; |
| 49 | + }; |
| 50 | + |
| 51 | + using TimerHandlePtr = std::shared_ptr<TimerHandle>; |
| 52 | + |
| 53 | + TimerHandlePtr schedule(Duration delay, Callback callback, bool repeat = false) |
| 54 | + { |
| 55 | + auto handle = std::make_shared<TimerHandle>(); |
| 56 | + |
| 57 | + { |
| 58 | + std::lock_guard lock(m_mutex); |
| 59 | + m_queue.push({ |
| 60 | + Clock::now() + delay, |
| 61 | + delay, |
| 62 | + repeat, |
| 63 | + std::move(callback), |
| 64 | + handle |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + m_cv.notify_one(); |
| 69 | + return handle; |
| 70 | + } |
| 71 | + |
| 72 | + void cancel(const TimerHandlePtr& handle) |
| 73 | + { |
| 74 | + if (handle) { |
| 75 | + handle->active = false; |
| 76 | + } |
| 77 | + m_cv.notify_one(); |
| 78 | + } |
| 79 | + |
| 80 | +private: |
| 81 | + TimerScheduler() |
| 82 | + : m_running(true), |
| 83 | + m_thread([this] { run(); }) |
| 84 | + {} |
| 85 | + |
| 86 | + ~TimerScheduler() |
| 87 | + { |
| 88 | + { |
| 89 | + std::lock_guard lock(m_mutex); |
| 90 | + m_running = false; |
| 91 | + } |
| 92 | + m_cv.notify_one(); |
| 93 | + m_thread.join(); |
| 94 | + } |
| 95 | + |
| 96 | + struct Entry { |
| 97 | + TimePoint time; |
| 98 | + Duration interval; |
| 99 | + bool repeat = false; |
| 100 | + Callback callback; |
| 101 | + TimerHandlePtr handle; |
| 102 | + |
| 103 | + bool operator>(const Entry& other) const |
| 104 | + { |
| 105 | + return time > other.time; |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + void run() |
| 110 | + { |
| 111 | + std::unique_lock lock(m_mutex); |
| 112 | + |
| 113 | + while (m_running) { |
| 114 | + if (m_queue.empty()) { |
| 115 | + m_cv.wait(lock); |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + auto& next = m_queue.top(); |
| 120 | + if (m_cv.wait_until(lock, next.time) == std::cv_status::timeout) { |
| 121 | + auto entry = m_queue.top(); |
| 122 | + m_queue.pop(); |
| 123 | + |
| 124 | + if (entry.handle->active) { |
| 125 | + lock.unlock(); |
| 126 | + entry.callback(); |
| 127 | + lock.lock(); |
| 128 | + |
| 129 | + if (entry.repeat && entry.handle->active) { |
| 130 | + entry.time = Clock::now() + entry.interval; |
| 131 | + m_queue.push(entry); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + std::priority_queue<Entry, |
| 139 | + std::vector<Entry>, |
| 140 | + std::greater<> > m_queue; |
| 141 | + |
| 142 | + bool m_running = false; |
| 143 | + std::mutex m_mutex; |
| 144 | + std::condition_variable m_cv; |
| 145 | + std::thread m_thread; |
| 146 | +}; |
| 147 | +} |
| 148 | + |
| 149 | +using namespace muse; |
| 150 | + |
| 151 | +Timer::Timer(time_t interval) |
| 152 | + : m_interval(interval) {} |
| 153 | + |
| 154 | +Timer::~Timer() |
| 155 | +{ |
| 156 | + stop(); |
| 157 | +} |
| 158 | + |
| 159 | +void Timer::onTimeout(const async::Asyncable* receiver, Callback callback) |
| 160 | +{ |
| 161 | + m_notification.onNotify(receiver, std::move(callback), async::Asyncable::Mode::SetReplace); |
| 162 | +} |
| 163 | + |
| 164 | +void Timer::start() |
| 165 | +{ |
| 166 | + if (m_handle) { |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + m_started = std::chrono::steady_clock::now(); |
| 171 | + |
| 172 | + auto realHandle = TimerScheduler::instance().schedule( |
| 173 | + m_interval, |
| 174 | + [this]() { m_notification.notify(); }, |
| 175 | + true |
| 176 | + ); |
| 177 | + |
| 178 | + m_handle = std::static_pointer_cast<void>(realHandle); |
| 179 | +} |
| 180 | + |
| 181 | +void Timer::stop() |
| 182 | +{ |
| 183 | + if (!m_handle) { |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + auto realHandle = std::static_pointer_cast<TimerScheduler::TimerHandle>(m_handle); |
| 188 | + TimerScheduler::instance().cancel(realHandle); |
| 189 | + m_handle = nullptr; |
| 190 | +} |
| 191 | + |
| 192 | +bool Timer::isActive() const |
| 193 | +{ |
| 194 | + return m_handle != nullptr; |
| 195 | +} |
| 196 | + |
| 197 | +float Timer::secondsSinceStart() const |
| 198 | +{ |
| 199 | + std::chrono::duration<float, std::ratio<1> > diff(std::chrono::steady_clock::now() - m_started); |
| 200 | + return diff.count(); |
| 201 | +} |
0 commit comments