LCOV - code coverage report
Current view: top level - muse/framework/audio/common - audiotaskscheduler.h Coverage Total Hit
Test: filtered_coverage.info Lines: 57.1 % 35 20
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-CLA-applies
       4              :  *
       5              :  * MuseScore
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2026 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              : #pragma once
      24              : 
      25              : #include "muse_framework_config.h"
      26              : 
      27              : #include "audioworkgroup.h"
      28              : #ifdef MUSE_THREADS_SUPPORT
      29              : #include "realtimethreadpool.h"
      30              : #endif
      31              : #include "iaudiodriver.h"
      32              : #include "iaudiotaskscheduler.h"
      33              : #include "audiosanitizer.h"
      34              : #include "global/async/asyncable.h"
      35              : #include "global/log.h"
      36              : #include <mutex>
      37              : 
      38              : namespace muse::audio {
      39              : #ifdef MUSE_THREADS_SUPPORT
      40              : class AudioTaskScheduler : public IAudioTaskScheduler, public muse::async::Asyncable
      41              : {
      42              :     constexpr static const char* threadpoolName = "audio_rt"; // The name must be no more than 12 characters.
      43              : public:
      44            0 :     void submitRealtimeTasksAndWait(const std::vector<Task>& tasks) override
      45              :     {
      46            0 :         std::lock_guard lock(m_threadPoolMutex);
      47            0 :         for (const auto& task : tasks) {
      48            0 :             IF_ASSERT_FAILED(m_threadPool->enqueue(task)) {
      49            0 :                 task();
      50              :             }
      51              :         }
      52            0 :         m_threadPool->participateAndWait();
      53            0 :     }
      54              : 
      55           27 :     void setAudioDriver(const IAudioDriverPtr& audioDriver)
      56              :     {
      57           27 :         if (!audioDriver) {
      58            0 :             return;
      59              :         }
      60           27 :         setWorkgroup(audioDriver->getAudioWorkGroup());
      61           81 :         audioDriver->currentWorkgroupChanged().onNotify(
      62           54 :             this, [this, audioDriverWeak = std::weak_ptr<IAudioDriver>(audioDriver)]() {
      63            0 :             if (auto audioDriver = audioDriverWeak.lock()) {
      64            0 :                 setWorkgroup(audioDriver->getAudioWorkGroup());
      65            0 :             }
      66            0 :         });
      67              :     }
      68              : 
      69              : private:
      70              : 
      71           27 :     void setWorkgroup(const AudioWorkGroup& workGroup)
      72              :     {
      73           27 :         std::lock_guard lock(m_threadPoolMutex);
      74           27 :         ensureThreadPoolSize(workGroup);
      75           27 :         m_threadPool->setAudioWorkgroup(workGroup);
      76           27 :     }
      77              : 
      78           51 :     int getIdealThreadCount(AudioWorkGroup workGroup) const
      79              :     {
      80           51 :         unsigned int bestThreadHint = std::thread::hardware_concurrency();
      81           51 :         if (workGroup.getProvider() != nullptr) {
      82            0 :             bestThreadHint = static_cast<unsigned int>(workGroup.getMaxParallelThreadCount());
      83              :         }
      84           51 :         constexpr unsigned int hardwareToRealtimeRatio = 2; // This is a heuristic value. The optimal value may vary depending on the workload and system.
      85           51 :         return bestThreadHint > 0 ? bestThreadHint / hardwareToRealtimeRatio : 1;
      86              :     }
      87              : 
      88           27 :     void ensureThreadPoolSize(const AudioWorkGroup& currentWorkGroup)
      89              :     {
      90           27 :         auto idealWorkerCount = getIdealThreadCount(currentWorkGroup);
      91           27 :         std::lock_guard lock(m_threadPoolMutex);
      92           27 :         if (m_threadPool->getNumberOfWorkers() != idealWorkerCount) {
      93            0 :             m_threadPool = std::make_unique<RealtimeThreadPool>(threadpoolName, idealWorkerCount);
      94            0 :             AudioSanitizer::setMixerThreads(m_threadPool->threadIdSet());
      95              :         }
      96           27 :     }
      97              : 
      98              :     std::unique_ptr<RealtimeThreadPool> m_threadPool{ std::make_unique<RealtimeThreadPool>(threadpoolName, getIdealThreadCount({})) };
      99              : 
     100              :     std::recursive_mutex m_threadPoolMutex;
     101              : };
     102              : #else
     103              : //! NOTE Without threads support the tasks are executed sequentially in the calling thread
     104              : class AudioTaskScheduler : public IAudioTaskScheduler
     105              : {
     106              : public:
     107              :     void submitRealtimeTasksAndWait(const std::vector<Task>& tasks) override
     108              :     {
     109              :         for (const auto& task : tasks) {
     110              :             task();
     111              :         }
     112              :     }
     113              : 
     114              :     void setAudioDriver(const IAudioDriverPtr&) {}
     115              : };
     116              : #endif // MUSE_THREADS_SUPPORT
     117              : }
        

Generated by: LCOV version 2.0-1