2 * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "pbd/compose.h"
25 typedef HANDLE (WINAPI* AvSetMmThreadCharacteristicsA_t)(LPCSTR TaskName,
28 typedef BOOL (WINAPI* AvRevertMmThreadCharacteristics_t)(HANDLE AvrtHandle);
30 typedef BOOL (WINAPI* AvSetMmThreadPriority_t)(
31 HANDLE AvrtHandle, mmcss::AVRT_PRIORITY Priority);
33 static HMODULE avrt_dll = NULL;
35 static AvSetMmThreadCharacteristicsA_t AvSetMmThreadCharacteristicsA = NULL;
36 static AvRevertMmThreadCharacteristics_t AvRevertMmThreadCharacteristics = NULL;
37 static AvSetMmThreadPriority_t AvSetMmThreadPriority = NULL;
45 if (avrt_dll != NULL) return true;
47 avrt_dll = LoadLibraryA ("avrt.dll");
49 if (avrt_dll == NULL) {
50 DEBUG_THREADS ("Unable to load avrt.dll\n");
53 bool unload_dll = false;
55 AvSetMmThreadCharacteristicsA =
56 (AvSetMmThreadCharacteristicsA_t)GetProcAddress (
57 avrt_dll, "AvSetMmThreadCharacteristicsA");
59 if (AvSetMmThreadCharacteristicsA == NULL) {
60 DEBUG_THREADS ("Unable to resolve AvSetMmThreadCharacteristicsA\n");
64 AvRevertMmThreadCharacteristics =
65 (AvRevertMmThreadCharacteristics_t)GetProcAddress (
66 avrt_dll, "AvRevertMmThreadCharacteristics");
68 if (AvRevertMmThreadCharacteristics == NULL) {
69 DEBUG_THREADS ("Unable to resolve AvRevertMmThreadCharacteristics\n");
73 AvSetMmThreadPriority = (AvSetMmThreadPriority_t)GetProcAddress (
74 avrt_dll, "AvSetMmThreadPriority");
76 if (AvSetMmThreadPriority == NULL) {
77 DEBUG_THREADS ("Unable to resolve AvSetMmThreadPriority\n");
83 "MMCSS Unable to resolve necessary symbols, unloading avrt.dll\n");
93 if (avrt_dll == NULL) return true;
95 if (FreeLibrary (avrt_dll) == 0) {
96 DEBUG_THREADS ("Unable to unload avrt.dll\n");
102 AvSetMmThreadCharacteristicsA = NULL;
103 AvRevertMmThreadCharacteristics = NULL;
104 AvSetMmThreadPriority = NULL;
110 set_thread_characteristics (const std::string& task_name, HANDLE* task_handle)
112 if (AvSetMmThreadCharacteristicsA == NULL) return false;
114 DWORD task_index_dummy = 0;
116 *task_handle = AvSetMmThreadCharacteristicsA(task_name.c_str(), &task_index_dummy);
118 if (*task_handle == 0) {
119 DEBUG_THREADS (string_compose ("Failed to set Thread Characteristics to %1\n",
121 DWORD error = GetLastError();
124 case ERROR_INVALID_TASK_INDEX:
125 DEBUG_THREADS("MMCSS: Invalid Task Index\n");
127 case ERROR_INVALID_TASK_NAME:
128 DEBUG_THREADS("MMCSS: Invalid Task Name\n");
130 case ERROR_PRIVILEGE_NOT_HELD:
131 DEBUG_THREADS("MMCSS: Privilege not held\n");
134 DEBUG_THREADS("MMCSS: Unknown error setting thread characteristics\n");
141 string_compose ("Set thread characteristics to %1\n", task_name));
147 revert_thread_characteristics (HANDLE task_handle)
149 if (AvRevertMmThreadCharacteristics == NULL) return false;
151 if (AvRevertMmThreadCharacteristics (task_handle) == 0) {
152 DEBUG_THREADS ("Failed to set revert thread characteristics\n");
156 DEBUG_THREADS ("Reverted thread characteristics\n");
162 set_thread_priority (HANDLE task_handle, AVRT_PRIORITY priority)
164 if (AvSetMmThreadPriority == NULL) return false;
166 if (AvSetMmThreadPriority (task_handle, priority) == 0) {
168 string_compose ("Failed to set thread priority %1\n", priority));
172 DEBUG_THREADS (string_compose ("Set thread priority to %1\n", priority));