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.
19 #include "pbd/windows_mmcss.h"
21 #include "pbd/compose.h"
22 #include "pbd/debug.h"
24 #define DEBUG_THREADS(msg) DEBUG_TRACE (PBD::DEBUG::Threads, msg);
26 typedef HANDLE (WINAPI* AvSetMmThreadCharacteristicsA_t)(LPCSTR TaskName,
29 typedef BOOL (WINAPI* AvRevertMmThreadCharacteristics_t)(HANDLE AvrtHandle);
31 typedef BOOL (WINAPI* AvSetMmThreadPriority_t)(
32 HANDLE AvrtHandle, PBD::MMCSS::AVRT_PRIORITY Priority);
34 static HMODULE avrt_dll = NULL;
36 static AvSetMmThreadCharacteristicsA_t AvSetMmThreadCharacteristicsA = NULL;
37 static AvRevertMmThreadCharacteristics_t AvRevertMmThreadCharacteristics = NULL;
38 static AvSetMmThreadPriority_t AvSetMmThreadPriority = NULL;
47 if (avrt_dll != NULL) return true;
49 avrt_dll = LoadLibraryA ("avrt.dll");
51 if (avrt_dll == NULL) {
52 DEBUG_THREADS ("Unable to load avrt.dll\n");
55 bool unload_dll = false;
57 AvSetMmThreadCharacteristicsA =
58 (AvSetMmThreadCharacteristicsA_t)GetProcAddress (
59 avrt_dll, "AvSetMmThreadCharacteristicsA");
61 if (AvSetMmThreadCharacteristicsA == NULL) {
62 DEBUG_THREADS ("Unable to resolve AvSetMmThreadCharacteristicsA\n");
66 AvRevertMmThreadCharacteristics =
67 (AvRevertMmThreadCharacteristics_t)GetProcAddress (
68 avrt_dll, "AvRevertMmThreadCharacteristics");
70 if (AvRevertMmThreadCharacteristics == NULL) {
71 DEBUG_THREADS ("Unable to resolve AvRevertMmThreadCharacteristics\n");
75 AvSetMmThreadPriority = (AvSetMmThreadPriority_t)GetProcAddress (
76 avrt_dll, "AvSetMmThreadPriority");
78 if (AvSetMmThreadPriority == NULL) {
79 DEBUG_THREADS ("Unable to resolve AvSetMmThreadPriority\n");
85 "MMCSS Unable to resolve necessary symbols, unloading avrt.dll\n");
95 if (avrt_dll == NULL) return true;
97 if (FreeLibrary (avrt_dll) == 0) {
98 DEBUG_THREADS ("Unable to unload avrt.dll\n");
104 AvSetMmThreadCharacteristicsA = NULL;
105 AvRevertMmThreadCharacteristics = NULL;
106 AvSetMmThreadPriority = NULL;
112 set_thread_characteristics (const std::string& task_name, HANDLE* task_handle)
114 if (AvSetMmThreadCharacteristicsA == NULL) return false;
116 DWORD task_index_dummy = 0;
118 *task_handle = AvSetMmThreadCharacteristicsA(task_name.c_str(), &task_index_dummy);
120 if (*task_handle == 0) {
121 DEBUG_THREADS (string_compose ("Failed to set Thread Characteristics to %1\n",
123 DWORD error = GetLastError();
126 case ERROR_INVALID_TASK_INDEX:
127 DEBUG_THREADS("MMCSS: Invalid Task Index\n");
129 case ERROR_INVALID_TASK_NAME:
130 DEBUG_THREADS("MMCSS: Invalid Task Name\n");
132 case ERROR_PRIVILEGE_NOT_HELD:
133 DEBUG_THREADS("MMCSS: Privilege not held\n");
136 DEBUG_THREADS("MMCSS: Unknown error setting thread characteristics\n");
143 string_compose ("Set thread characteristics to %1\n", task_name));
149 revert_thread_characteristics (HANDLE task_handle)
151 if (AvRevertMmThreadCharacteristics == NULL) return false;
153 if (AvRevertMmThreadCharacteristics (task_handle) == 0) {
154 DEBUG_THREADS ("Failed to set revert thread characteristics\n");
158 DEBUG_THREADS ("Reverted thread characteristics\n");
164 set_thread_priority (HANDLE task_handle, AVRT_PRIORITY priority)
166 if (AvSetMmThreadPriority == NULL) return false;
168 if (AvSetMmThreadPriority (task_handle, priority) == 0) {
170 string_compose ("Failed to set thread priority %1\n", priority));
174 DEBUG_THREADS (string_compose ("Set thread priority to %1\n", priority));