Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/framework/global/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ target_sources(muse_global PRIVATE
runtime.h
translation.cpp
translation.h
timer.cpp
timer.h
progress.h
utils.cpp
Expand Down
201 changes: 201 additions & 0 deletions src/framework/global/timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore
* Music Composition & Notation
*
* Copyright (C) 2025 MuseScore Limited and others
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "timer.h"

#include <condition_variable>
#include <functional>
#include <queue>
#include <thread>
#include <atomic>
#include <mutex>

namespace muse {
class TimerScheduler
{
public:
using Clock = std::chrono::steady_clock;
using TimePoint = Clock::time_point;
using Duration = Clock::duration;
using Callback = std::function<void ()>;

static TimerScheduler& instance()
{
static TimerScheduler scheduler;
return scheduler;
}

struct TimerHandle {
std::atomic_bool active { true };
};

using TimerHandlePtr = std::shared_ptr<TimerHandle>;

TimerHandlePtr schedule(Duration delay, Callback callback, bool repeat = false)
{
auto handle = std::make_shared<TimerHandle>();

{
std::lock_guard lock(m_mutex);
m_queue.push({
Clock::now() + delay,
delay,
repeat,
std::move(callback),
handle
});
}

m_cv.notify_one();
return handle;
}

void cancel(const TimerHandlePtr& handle)
{
if (handle) {
handle->active = false;
}
m_cv.notify_one();
}

private:
TimerScheduler()
: m_running(true),
m_thread([this] { run(); })
{}

~TimerScheduler()
{
{
std::lock_guard lock(m_mutex);
m_running = false;
}
m_cv.notify_one();
m_thread.join();
}

struct Entry {
TimePoint time;
Duration interval;
bool repeat = false;
Callback callback;
TimerHandlePtr handle;

bool operator>(const Entry& other) const
{
return time > other.time;
}
};

void run()
{
std::unique_lock lock(m_mutex);

while (m_running) {
if (m_queue.empty()) {
m_cv.wait(lock);
continue;
}

auto& next = m_queue.top();
if (m_cv.wait_until(lock, next.time) == std::cv_status::timeout) {
auto entry = m_queue.top();
m_queue.pop();

if (entry.handle->active) {
lock.unlock();
entry.callback();
lock.lock();

if (entry.repeat && entry.handle->active) {
entry.time = Clock::now() + entry.interval;
m_queue.push(entry);
}
}
}
}
}

std::priority_queue<Entry,
std::vector<Entry>,
std::greater<> > m_queue;

bool m_running = false;
std::mutex m_mutex;
std::condition_variable m_cv;
std::thread m_thread;
};
}

using namespace muse;

Timer::Timer(time_t interval)
: m_interval(interval) {}

Timer::~Timer()
{
stop();
}

void Timer::onTimeout(const async::Asyncable* receiver, Callback callback)
{
m_notification.onNotify(receiver, std::move(callback), async::Asyncable::Mode::SetReplace);
}

void Timer::start()
{
if (m_handle) {
return;
}

m_started = std::chrono::steady_clock::now();

auto realHandle = TimerScheduler::instance().schedule(
m_interval,
[this]() { m_notification.notify(); },
true
);

m_handle = std::static_pointer_cast<void>(realHandle);
}

void Timer::stop()
{
if (!m_handle) {
return;
}

auto realHandle = std::static_pointer_cast<TimerScheduler::TimerHandle>(m_handle);
TimerScheduler::instance().cancel(realHandle);
m_handle = nullptr;
}

bool Timer::isActive() const
{
return m_handle != nullptr;
}

float Timer::secondsSinceStart() const
{
std::chrono::duration<float, std::ratio<1> > diff(std::chrono::steady_clock::now() - m_started);
return diff.count();
}
69 changes: 10 additions & 59 deletions src/framework/global/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "async/asyncable.h"

#include <chrono>
#include <thread>

namespace muse {
/*!
Expand All @@ -40,73 +39,25 @@ class Timer
public:
using time_t = std::chrono::microseconds;

explicit Timer(time_t interval)
: m_interval(interval) {}

~Timer()
{
stop();

if (m_thread.joinable()) {
m_thread.join();
}
}
explicit Timer(time_t interval);
~Timer();

Timer(const Timer&) = delete;
Timer& operator=(const Timer&) = delete;

template<typename Func>
void onTimeout(const async::Asyncable* receiver, Func function)
{
m_notification.onNotify(receiver, function, async::Asyncable::Mode::SetReplace);
}

void start()
{
if (m_active) {
return;
}

m_active = true;
m_started = std::chrono::steady_clock::now();
m_thread = std::thread([this]() { timerLoop(); });
}
using Callback = std::function<void ()>;
void onTimeout(const async::Asyncable* receiver, Callback callback);

void stop()
{
m_active = false;
}
void start();
void stop();

bool isActive() const
{
return m_active;
}

float secondsSinceStart() const
{
std::chrono::duration<float, std::ratio<1> > diff(std::chrono::steady_clock::now() - m_started);
return diff.count();
}
bool isActive() const;
float secondsSinceStart() const;

private:
void timerLoop()
{
std::this_thread::sleep_for(m_interval);
while (m_active) {
auto start = std::chrono::steady_clock::now();
m_notification.notify();
auto end = std::chrono::steady_clock::now();
if (m_interval > (end - start)) {
auto diff = m_interval - (end - start);
std::this_thread::sleep_for(diff);
}
}
}

time_t m_interval;
std::chrono::time_point<std::chrono::steady_clock> m_started;
std::atomic_bool m_active { false };
async::Notification m_notification;
std::thread m_thread;
std::chrono::time_point<std::chrono::steady_clock> m_started;
std::shared_ptr<void> m_handle;
};
}
1 change: 1 addition & 0 deletions src/framework/musesampler/internal/musesamplerwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ void MuseSamplerWrapper::prepareToPlay()
doCurrentSetPosition();

if (readyToPlay()) {
m_checkReadyToPlayTimer.reset();
return;
}

Expand Down
Loading