LCOV - code coverage report
Current view: top level - muse/framework/global/concurrency - threadutils.h Coverage Total Hit
Test: filtered_coverage.info Lines: 63.6 % 22 14
Test Date: 2026-11-05 04:07:02

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2024 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              : #ifdef MUSE_THREADS_SUPPORT
      28              : 
      29              : #include <thread>
      30              : 
      31              : #ifdef Q_OS_WIN
      32              : #include <windows.h>
      33              : #else
      34              : #include <pthread.h>
      35              : #endif
      36              : 
      37              : namespace muse {
      38              : enum class ThreadPriority {
      39              :     Low,
      40              :     Medium,
      41              :     High
      42              : };
      43              : 
      44           48 : static bool setThreadPriority(std::thread& thread, ThreadPriority priority)
      45              : {
      46              : #ifdef Q_OS_WIN
      47              :     int winPriority = 0;
      48              :     switch (priority) {
      49              :     case ThreadPriority::Low:
      50              :         winPriority = THREAD_PRIORITY_LOWEST;
      51              :         break;
      52              :     case ThreadPriority::Medium:
      53              :         winPriority = THREAD_PRIORITY_NORMAL;
      54              :         break;
      55              :     case ThreadPriority::High:
      56              :         winPriority = THREAD_PRIORITY_HIGHEST;
      57              :         break;
      58              :     }
      59              : 
      60              :     if (!SetThreadPriority(thread.native_handle(), winPriority)) {
      61              :         return false;
      62              :     }
      63              : #else
      64           48 :     pthread_t pthread = thread.native_handle();
      65              :     struct sched_param param;
      66           48 :     int policy = 0;
      67           48 :     int error = pthread_getschedparam(pthread, &policy, &param);
      68           48 :     if (error != 0) {
      69            0 :         return false;
      70              :     }
      71              : 
      72           48 :     int minP = sched_get_priority_min(policy);
      73           48 :     int maxP = sched_get_priority_max(policy);
      74           48 :     switch (priority) {
      75            0 :     case ThreadPriority::Low:
      76            0 :         param.sched_priority = minP;
      77            0 :         break;
      78            0 :     case ThreadPriority::Medium:
      79            0 :         param.sched_priority = minP + (maxP - minP) / 2;
      80            0 :         break;
      81           48 :     case ThreadPriority::High:
      82           48 :         param.sched_priority = maxP;
      83           48 :         break;
      84              :     }
      85              : 
      86           48 :     error = pthread_setschedparam(pthread, policy, &param);
      87           48 :     if (error != 0) {
      88            0 :         return false;
      89              :     }
      90              : #endif
      91              : 
      92           48 :     return true;
      93              : }
      94              : }
      95              : 
      96              : #endif // MUSE_THREADS_SUPPORT
        

Generated by: LCOV version 2.0-1