summaryrefslogtreecommitdiff
path: root/libs/pbd/windows_mmcss.cc
blob: 92fdea2fdca4d765932fddcbea650675f5c5fde5 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "pbd/windows_mmcss.h"

#include "pbd/compose.h"
#include "pbd/debug.h"

#define DEBUG_THREADS(msg) DEBUG_TRACE (PBD::DEBUG::Threads, msg);

typedef HANDLE (WINAPI* AvSetMmThreadCharacteristicsA_t)(LPCSTR TaskName,
                                                         LPDWORD TaskIndex);

typedef BOOL (WINAPI* AvRevertMmThreadCharacteristics_t)(HANDLE AvrtHandle);

typedef BOOL (WINAPI* AvSetMmThreadPriority_t)(
    HANDLE AvrtHandle, PBD::MMCSS::AVRT_PRIORITY Priority);

static HMODULE avrt_dll = NULL;

static AvSetMmThreadCharacteristicsA_t AvSetMmThreadCharacteristicsA = NULL;
static AvRevertMmThreadCharacteristics_t AvRevertMmThreadCharacteristics = NULL;
static AvSetMmThreadPriority_t AvSetMmThreadPriority = NULL;

namespace PBD {

namespace MMCSS {

bool
initialize ()
{
	if (avrt_dll != NULL) return true;

	avrt_dll = LoadLibraryA ("avrt.dll");

	if (avrt_dll == NULL) {
		DEBUG_THREADS ("Unable to load avrt.dll\n");
		return false;
	}
	bool unload_dll = false;

	AvSetMmThreadCharacteristicsA =
	    (AvSetMmThreadCharacteristicsA_t)GetProcAddress (
	        avrt_dll, "AvSetMmThreadCharacteristicsA");

	if (AvSetMmThreadCharacteristicsA == NULL) {
		DEBUG_THREADS ("Unable to resolve AvSetMmThreadCharacteristicsA\n");
		unload_dll = true;
	}

	AvRevertMmThreadCharacteristics =
	    (AvRevertMmThreadCharacteristics_t)GetProcAddress (
	        avrt_dll, "AvRevertMmThreadCharacteristics");

	if (AvRevertMmThreadCharacteristics == NULL) {
		DEBUG_THREADS ("Unable to resolve AvRevertMmThreadCharacteristics\n");
		unload_dll = true;
	}

	AvSetMmThreadPriority = (AvSetMmThreadPriority_t)GetProcAddress (
	    avrt_dll, "AvSetMmThreadPriority");

	if (AvSetMmThreadPriority == NULL) {
		DEBUG_THREADS ("Unable to resolve AvSetMmThreadPriority\n");
		unload_dll = true;
	}

	if (unload_dll) {
		DEBUG_THREADS (
		    "MMCSS Unable to resolve necessary symbols, unloading avrt.dll\n");
		deinitialize ();
	}

	return true;
}

bool
deinitialize ()
{
	if (avrt_dll == NULL) return true;

	if (FreeLibrary (avrt_dll) == 0) {
		DEBUG_THREADS ("Unable to unload avrt.dll\n");
		return false;
	}

	avrt_dll = NULL;

	AvSetMmThreadCharacteristicsA = NULL;
	AvRevertMmThreadCharacteristics = NULL;
	AvSetMmThreadPriority = NULL;

	return true;
}

bool
set_thread_characteristics (const std::string& task_name, HANDLE* task_handle)
{
	if (AvSetMmThreadCharacteristicsA == NULL) return false;

	DWORD task_index_dummy = 0;

	*task_handle = AvSetMmThreadCharacteristicsA(task_name.c_str(), &task_index_dummy);

	if (*task_handle == 0) {
		DEBUG_THREADS (string_compose ("Failed to set Thread Characteristics to %1\n",
		                               task_name));
		DWORD error = GetLastError();

		switch (error) {
		case ERROR_INVALID_TASK_INDEX:
			DEBUG_THREADS("MMCSS: Invalid Task Index\n");
			break;
		case ERROR_INVALID_TASK_NAME:
			DEBUG_THREADS("MMCSS: Invalid Task Name\n");
			break;
		case ERROR_PRIVILEGE_NOT_HELD:
			DEBUG_THREADS("MMCSS: Privilege not held\n");
			break;
		default:
			DEBUG_THREADS("MMCSS: Unknown error setting thread characteristics\n");
			break;
		}
		return false;
	}

	DEBUG_THREADS (
	    string_compose ("Set thread characteristics to %1\n", task_name));

	return true;
}

bool
revert_thread_characteristics (HANDLE task_handle)
{
	if (AvRevertMmThreadCharacteristics == NULL) return false;

	if (AvRevertMmThreadCharacteristics (task_handle) == 0) {
		DEBUG_THREADS ("Failed to set revert thread characteristics\n");
		return false;
	}

	DEBUG_THREADS ("Reverted thread characteristics\n");

	return true;
}

bool
set_thread_priority (HANDLE task_handle, AVRT_PRIORITY priority)
{
	if (AvSetMmThreadPriority == NULL) return false;

	if (AvSetMmThreadPriority (task_handle, priority) == 0) {
		DEBUG_THREADS (
		    string_compose ("Failed to set thread priority %1\n", priority));
		return false;
	}

	DEBUG_THREADS (string_compose ("Set thread priority to %1\n", priority));

	return true;
}

} // namespace MMCSS

} // namespace PBD