forked from musescore/muse_framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudiotaskscheduler.h
More file actions
97 lines (87 loc) · 3.3 KB
/
Copy pathaudiotaskscheduler.h
File metadata and controls
97 lines (87 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore
* Music Composition & Notation
*
* Copyright (C) 2026 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/>.
*/
#pragma once
#include "audioworkgroup.h"
#include "realtimethreadpool.h"
#include "iaudiodriver.h"
#include "iaudiotaskscheduler.h"
#include "audiosanitizer.h"
#include "global/async/asyncable.h"
#include "global/log.h"
#include <mutex>
namespace muse::audio {
class AudioTaskScheduler : public IAudioTaskScheduler, public muse::async::Asyncable
{
constexpr static const char* threadpoolName = "audio_realtime_thread";
public:
void submitRealtimeTasksAndWait(const std::vector<Task>& tasks) override
{
std::lock_guard lock(m_threadPoolMutex);
for (const auto& task : tasks) {
IF_ASSERT_FAILED(m_threadPool->enqueue(task)) {
task();
}
}
m_threadPool->participateAndWait();
}
void setAudioDriver(const IAudioDriverPtr& audioDriver)
{
if (!audioDriver) {
return;
}
setWorkgroup(audioDriver->getAudioWorkGroup());
audioDriver->currentWorkgroupChanged().onNotify(
this, [this, audioDriverWeak = std::weak_ptr<IAudioDriver>(audioDriver)]() {
if (auto audioDriver = audioDriverWeak.lock()) {
setWorkgroup(audioDriver->getAudioWorkGroup());
}
});
}
private:
void setWorkgroup(const AudioWorkGroup& workGroup)
{
std::lock_guard lock(m_threadPoolMutex);
ensureThreadPoolSize(workGroup);
m_threadPool->setAudioWorkgroup(workGroup);
}
int getIdealThreadCount(AudioWorkGroup workGroup) const
{
int bestThreadHint = std::thread::hardware_concurrency();
if (workGroup.getProvider() != nullptr) {
bestThreadHint = workGroup.getMaxParallelThreadCount();
}
constexpr int hardwareToRealtimeRatio = 2; // This is a heuristic value. The optimal value may vary depending on the workload and system.
return bestThreadHint > 0 ? static_cast<int>(bestThreadHint / hardwareToRealtimeRatio) : 1;
}
void ensureThreadPoolSize(const AudioWorkGroup& currentWorkGroup)
{
auto idealWorkerCount = getIdealThreadCount(currentWorkGroup);
std::lock_guard lock(m_threadPoolMutex);
if (m_threadPool->getNumberOfWorkers() != idealWorkerCount) {
m_threadPool = std::make_unique<RealtimeThreadPool>(threadpoolName, idealWorkerCount);
AudioSanitizer::setMixerThreads(m_threadPool->threadIdSet());
}
}
std::unique_ptr<RealtimeThreadPool> m_threadPool{ std::make_unique<RealtimeThreadPool>(threadpoolName, getIdealThreadCount({})) };
std::recursive_mutex m_threadPoolMutex;
};
}