1 | /* $Id: sched-win.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Scheduling, Win32.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /** @def WIN32_SCHED_ENABLED
|
---|
28 | * Enables the priority scheme. */
|
---|
29 | #define WIN32_SCHED_ENABLED
|
---|
30 |
|
---|
31 |
|
---|
32 | /*********************************************************************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *********************************************************************************************************************************/
|
---|
35 | #define LOG_GROUP RTLOGGROUP_THREAD
|
---|
36 | #include <Windows.h>
|
---|
37 |
|
---|
38 | #include <iprt/thread.h>
|
---|
39 | #include <iprt/log.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/err.h>
|
---|
42 | #include "internal/sched.h"
|
---|
43 | #include "internal/thread.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /*********************************************************************************************************************************
|
---|
47 | * Structures and Typedefs *
|
---|
48 | *********************************************************************************************************************************/
|
---|
49 | /**
|
---|
50 | * Configuration of one priority.
|
---|
51 | */
|
---|
52 | typedef struct
|
---|
53 | {
|
---|
54 | /** The priority. */
|
---|
55 | RTPROCPRIORITY enmPriority;
|
---|
56 | /** The name of this priority. */
|
---|
57 | const char *pszName;
|
---|
58 | /** The Win32 process priority class. If ANY_PROCESS_PRIORITY_CLASS the
|
---|
59 | * process priority class is left unchanged. */
|
---|
60 | DWORD dwProcessPriorityClass;
|
---|
61 | /** Array scheduler attributes corresponding to each of the thread types. */
|
---|
62 | struct
|
---|
63 | {
|
---|
64 | /** For sanity include the array index. */
|
---|
65 | RTTHREADTYPE enmType;
|
---|
66 | /** The Win32 thread priority. */
|
---|
67 | DWORD dwThreadPriority;
|
---|
68 | } aTypes[RTTHREADTYPE_END];
|
---|
69 | } PROCPRIORITY;
|
---|
70 |
|
---|
71 | /** Matches any process priority class. */
|
---|
72 | #define ANY_PROCESS_PRIORITY_CLASS (~0U)
|
---|
73 |
|
---|
74 |
|
---|
75 | /*********************************************************************************************************************************
|
---|
76 | * Global Variables *
|
---|
77 | *********************************************************************************************************************************/
|
---|
78 | /**
|
---|
79 | * Array of static priority configurations.
|
---|
80 | */
|
---|
81 | static const PROCPRIORITY g_aPriorities[] =
|
---|
82 | {
|
---|
83 | {
|
---|
84 | RTPROCPRIORITY_FLAT, "Flat", ANY_PROCESS_PRIORITY_CLASS,
|
---|
85 | {
|
---|
86 | { RTTHREADTYPE_INVALID, ~0 },
|
---|
87 | { RTTHREADTYPE_INFREQUENT_POLLER, THREAD_PRIORITY_NORMAL },
|
---|
88 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, THREAD_PRIORITY_NORMAL },
|
---|
89 | { RTTHREADTYPE_EMULATION, THREAD_PRIORITY_NORMAL },
|
---|
90 | { RTTHREADTYPE_DEFAULT, THREAD_PRIORITY_NORMAL },
|
---|
91 | { RTTHREADTYPE_GUI, THREAD_PRIORITY_NORMAL },
|
---|
92 | { RTTHREADTYPE_MAIN_WORKER, THREAD_PRIORITY_NORMAL },
|
---|
93 | { RTTHREADTYPE_VRDP_IO, THREAD_PRIORITY_NORMAL },
|
---|
94 | { RTTHREADTYPE_DEBUGGER, THREAD_PRIORITY_NORMAL },
|
---|
95 | { RTTHREADTYPE_MSG_PUMP, THREAD_PRIORITY_NORMAL },
|
---|
96 | { RTTHREADTYPE_IO, THREAD_PRIORITY_NORMAL },
|
---|
97 | { RTTHREADTYPE_TIMER, THREAD_PRIORITY_NORMAL }
|
---|
98 | }
|
---|
99 | },
|
---|
100 | {
|
---|
101 | RTPROCPRIORITY_LOW, "Low - Below Normal", BELOW_NORMAL_PRIORITY_CLASS,
|
---|
102 | {
|
---|
103 | { RTTHREADTYPE_INVALID, ~0 },
|
---|
104 | { RTTHREADTYPE_INFREQUENT_POLLER, THREAD_PRIORITY_LOWEST },
|
---|
105 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, THREAD_PRIORITY_BELOW_NORMAL },
|
---|
106 | { RTTHREADTYPE_EMULATION, THREAD_PRIORITY_NORMAL },
|
---|
107 | { RTTHREADTYPE_DEFAULT, THREAD_PRIORITY_NORMAL },
|
---|
108 | { RTTHREADTYPE_GUI, THREAD_PRIORITY_NORMAL },
|
---|
109 | { RTTHREADTYPE_MAIN_WORKER, THREAD_PRIORITY_NORMAL },
|
---|
110 | { RTTHREADTYPE_VRDP_IO, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
111 | { RTTHREADTYPE_DEBUGGER, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
112 | { RTTHREADTYPE_MSG_PUMP, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
113 | { RTTHREADTYPE_IO, THREAD_PRIORITY_HIGHEST },
|
---|
114 | { RTTHREADTYPE_TIMER, THREAD_PRIORITY_HIGHEST }
|
---|
115 | }
|
---|
116 | },
|
---|
117 | {
|
---|
118 | RTPROCPRIORITY_LOW, "Low", ANY_PROCESS_PRIORITY_CLASS,
|
---|
119 | {
|
---|
120 | { RTTHREADTYPE_INVALID, ~0 },
|
---|
121 | { RTTHREADTYPE_INFREQUENT_POLLER, THREAD_PRIORITY_LOWEST },
|
---|
122 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, THREAD_PRIORITY_LOWEST },
|
---|
123 | { RTTHREADTYPE_EMULATION, THREAD_PRIORITY_LOWEST },
|
---|
124 | { RTTHREADTYPE_DEFAULT, THREAD_PRIORITY_BELOW_NORMAL },
|
---|
125 | { RTTHREADTYPE_GUI, THREAD_PRIORITY_BELOW_NORMAL },
|
---|
126 | { RTTHREADTYPE_MAIN_WORKER, THREAD_PRIORITY_BELOW_NORMAL },
|
---|
127 | { RTTHREADTYPE_VRDP_IO, THREAD_PRIORITY_NORMAL },
|
---|
128 | { RTTHREADTYPE_DEBUGGER, THREAD_PRIORITY_NORMAL },
|
---|
129 | { RTTHREADTYPE_MSG_PUMP, THREAD_PRIORITY_NORMAL },
|
---|
130 | { RTTHREADTYPE_IO, THREAD_PRIORITY_NORMAL },
|
---|
131 | { RTTHREADTYPE_TIMER, THREAD_PRIORITY_NORMAL }
|
---|
132 | }
|
---|
133 | },
|
---|
134 | {
|
---|
135 | RTPROCPRIORITY_NORMAL, "Normal - Normal", NORMAL_PRIORITY_CLASS,
|
---|
136 | {
|
---|
137 | { RTTHREADTYPE_INVALID, ~0 },
|
---|
138 | { RTTHREADTYPE_INFREQUENT_POLLER, THREAD_PRIORITY_LOWEST },
|
---|
139 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, THREAD_PRIORITY_LOWEST },
|
---|
140 | { RTTHREADTYPE_EMULATION, THREAD_PRIORITY_BELOW_NORMAL },
|
---|
141 | { RTTHREADTYPE_DEFAULT, THREAD_PRIORITY_NORMAL },
|
---|
142 | { RTTHREADTYPE_GUI, THREAD_PRIORITY_NORMAL },
|
---|
143 | { RTTHREADTYPE_MAIN_WORKER, THREAD_PRIORITY_NORMAL },
|
---|
144 | { RTTHREADTYPE_VRDP_IO, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
145 | { RTTHREADTYPE_DEBUGGER, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
146 | { RTTHREADTYPE_MSG_PUMP, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
147 | { RTTHREADTYPE_IO, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
148 | { RTTHREADTYPE_TIMER, THREAD_PRIORITY_HIGHEST }
|
---|
149 | }
|
---|
150 | },
|
---|
151 | {
|
---|
152 | RTPROCPRIORITY_NORMAL, "Normal", ANY_PROCESS_PRIORITY_CLASS,
|
---|
153 | {
|
---|
154 | { RTTHREADTYPE_INVALID, ~0 },
|
---|
155 | { RTTHREADTYPE_INFREQUENT_POLLER, THREAD_PRIORITY_LOWEST },
|
---|
156 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, THREAD_PRIORITY_LOWEST },
|
---|
157 | { RTTHREADTYPE_EMULATION, THREAD_PRIORITY_BELOW_NORMAL },
|
---|
158 | { RTTHREADTYPE_DEFAULT, THREAD_PRIORITY_NORMAL },
|
---|
159 | { RTTHREADTYPE_GUI, THREAD_PRIORITY_NORMAL },
|
---|
160 | { RTTHREADTYPE_MAIN_WORKER, THREAD_PRIORITY_NORMAL },
|
---|
161 | { RTTHREADTYPE_VRDP_IO, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
162 | { RTTHREADTYPE_DEBUGGER, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
163 | { RTTHREADTYPE_MSG_PUMP, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
164 | { RTTHREADTYPE_IO, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
165 | { RTTHREADTYPE_TIMER, THREAD_PRIORITY_HIGHEST }
|
---|
166 | }
|
---|
167 | },
|
---|
168 | {
|
---|
169 | RTPROCPRIORITY_HIGH, "High - High", HIGH_PRIORITY_CLASS,
|
---|
170 | {
|
---|
171 | { RTTHREADTYPE_INVALID, ~0 },
|
---|
172 | { RTTHREADTYPE_INFREQUENT_POLLER, THREAD_PRIORITY_LOWEST },
|
---|
173 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, THREAD_PRIORITY_LOWEST },
|
---|
174 | { RTTHREADTYPE_EMULATION, THREAD_PRIORITY_BELOW_NORMAL },
|
---|
175 | { RTTHREADTYPE_DEFAULT, THREAD_PRIORITY_NORMAL },
|
---|
176 | { RTTHREADTYPE_GUI, THREAD_PRIORITY_NORMAL },
|
---|
177 | { RTTHREADTYPE_MAIN_WORKER, THREAD_PRIORITY_NORMAL },
|
---|
178 | { RTTHREADTYPE_VRDP_IO, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
179 | { RTTHREADTYPE_DEBUGGER, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
180 | { RTTHREADTYPE_MSG_PUMP, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
181 | { RTTHREADTYPE_IO, THREAD_PRIORITY_HIGHEST },
|
---|
182 | { RTTHREADTYPE_TIMER, THREAD_PRIORITY_HIGHEST }
|
---|
183 | }
|
---|
184 | },
|
---|
185 | {
|
---|
186 | RTPROCPRIORITY_HIGH, "High - Above Normal", ABOVE_NORMAL_PRIORITY_CLASS,
|
---|
187 | {
|
---|
188 | { RTTHREADTYPE_INVALID, ~0 },
|
---|
189 | { RTTHREADTYPE_INFREQUENT_POLLER, THREAD_PRIORITY_LOWEST },
|
---|
190 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, THREAD_PRIORITY_LOWEST },
|
---|
191 | { RTTHREADTYPE_EMULATION, THREAD_PRIORITY_BELOW_NORMAL },
|
---|
192 | { RTTHREADTYPE_DEFAULT, THREAD_PRIORITY_NORMAL },
|
---|
193 | { RTTHREADTYPE_GUI, THREAD_PRIORITY_NORMAL },
|
---|
194 | { RTTHREADTYPE_MAIN_WORKER, THREAD_PRIORITY_NORMAL },
|
---|
195 | { RTTHREADTYPE_VRDP_IO, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
196 | { RTTHREADTYPE_DEBUGGER, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
197 | { RTTHREADTYPE_MSG_PUMP, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
198 | { RTTHREADTYPE_IO, THREAD_PRIORITY_HIGHEST },
|
---|
199 | { RTTHREADTYPE_TIMER, THREAD_PRIORITY_HIGHEST }
|
---|
200 | }
|
---|
201 | },
|
---|
202 | {
|
---|
203 | RTPROCPRIORITY_HIGH, "High", ANY_PROCESS_PRIORITY_CLASS,
|
---|
204 | {
|
---|
205 | { RTTHREADTYPE_INVALID, ~0 },
|
---|
206 | { RTTHREADTYPE_INFREQUENT_POLLER, THREAD_PRIORITY_BELOW_NORMAL },
|
---|
207 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, THREAD_PRIORITY_NORMAL },
|
---|
208 | { RTTHREADTYPE_EMULATION, THREAD_PRIORITY_NORMAL },
|
---|
209 | { RTTHREADTYPE_DEFAULT, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
210 | { RTTHREADTYPE_GUI, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
211 | { RTTHREADTYPE_MAIN_WORKER, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
212 | { RTTHREADTYPE_VRDP_IO, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
213 | { RTTHREADTYPE_DEBUGGER, THREAD_PRIORITY_HIGHEST },
|
---|
214 | { RTTHREADTYPE_MSG_PUMP, THREAD_PRIORITY_HIGHEST },
|
---|
215 | { RTTHREADTYPE_IO, THREAD_PRIORITY_HIGHEST },
|
---|
216 | { RTTHREADTYPE_TIMER, THREAD_PRIORITY_HIGHEST }
|
---|
217 | }
|
---|
218 | }
|
---|
219 | };
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * The dynamic default priority configuration.
|
---|
223 | *
|
---|
224 | * This can be recalulated at runtime depending on what the
|
---|
225 | * system allow us to do. Presently we don't do this as it's
|
---|
226 | * generally not a bit issue on Win32 hosts.
|
---|
227 | */
|
---|
228 | static PROCPRIORITY g_aDefaultPriority =
|
---|
229 | {
|
---|
230 | RTPROCPRIORITY_LOW, "Default", ANY_PROCESS_PRIORITY_CLASS,
|
---|
231 | {
|
---|
232 | { RTTHREADTYPE_INVALID, ~0 },
|
---|
233 | { RTTHREADTYPE_INFREQUENT_POLLER, THREAD_PRIORITY_LOWEST },
|
---|
234 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, THREAD_PRIORITY_BELOW_NORMAL },
|
---|
235 | { RTTHREADTYPE_EMULATION, THREAD_PRIORITY_NORMAL },
|
---|
236 | { RTTHREADTYPE_DEFAULT, THREAD_PRIORITY_NORMAL },
|
---|
237 | { RTTHREADTYPE_GUI, THREAD_PRIORITY_NORMAL },
|
---|
238 | { RTTHREADTYPE_MAIN_WORKER, THREAD_PRIORITY_NORMAL },
|
---|
239 | { RTTHREADTYPE_VRDP_IO, THREAD_PRIORITY_NORMAL },
|
---|
240 | { RTTHREADTYPE_DEBUGGER, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
241 | { RTTHREADTYPE_MSG_PUMP, THREAD_PRIORITY_ABOVE_NORMAL },
|
---|
242 | { RTTHREADTYPE_IO, THREAD_PRIORITY_HIGHEST },
|
---|
243 | { RTTHREADTYPE_TIMER, THREAD_PRIORITY_HIGHEST }
|
---|
244 | }
|
---|
245 | };
|
---|
246 |
|
---|
247 |
|
---|
248 | /** Pointer to the current priority configuration. */
|
---|
249 | static const PROCPRIORITY *g_pProcessPriority = &g_aDefaultPriority;
|
---|
250 |
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Calculate the scheduling properties for all the threads in the default
|
---|
254 | * process priority, assuming the current thread have the type enmType.
|
---|
255 | *
|
---|
256 | * @returns iprt status code.
|
---|
257 | * @param enmType The thread type to be assumed for the current thread.
|
---|
258 | */
|
---|
259 | DECLHIDDEN(int) rtSchedNativeCalcDefaultPriority(RTTHREADTYPE enmType)
|
---|
260 | {
|
---|
261 | Assert(enmType > RTTHREADTYPE_INVALID && enmType < RTTHREADTYPE_END);
|
---|
262 | return VINF_SUCCESS;
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Validates and sets the process priority.
|
---|
268 | * This will check that all rtThreadNativeSetPriority() will success for all the
|
---|
269 | * thread types when applied to the current thread.
|
---|
270 | *
|
---|
271 | * @returns iprt status code.
|
---|
272 | * @param enmPriority The priority to validate and set.
|
---|
273 | * @remark Located in sched.
|
---|
274 | */
|
---|
275 | DECLHIDDEN(int) rtProcNativeSetPriority(RTPROCPRIORITY enmPriority)
|
---|
276 | {
|
---|
277 | Assert(enmPriority > RTPROCPRIORITY_INVALID && enmPriority < RTPROCPRIORITY_LAST);
|
---|
278 | return VINF_SUCCESS;
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Gets the win32 thread handle.
|
---|
284 | *
|
---|
285 | * @returns Valid win32 handle for the specified thread.
|
---|
286 | * @param pThread The thread.
|
---|
287 | */
|
---|
288 | DECLINLINE(HANDLE) rtThreadNativeGetHandle(PRTTHREADINT pThread)
|
---|
289 | {
|
---|
290 | if ((uintptr_t)pThread->Core.Key == GetCurrentThreadId())
|
---|
291 | return GetCurrentThread();
|
---|
292 | return (HANDLE)pThread->hThread;
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * Sets the priority of the thread according to the thread type
|
---|
298 | * and current process priority.
|
---|
299 | *
|
---|
300 | * The RTTHREADINT::enmType member has not yet been updated and will be updated by
|
---|
301 | * the caller on a successful return.
|
---|
302 | *
|
---|
303 | * @returns iprt status code.
|
---|
304 | * @param pThread The thread in question.
|
---|
305 | * @param enmType The thread type.
|
---|
306 | * @remark Located in sched.
|
---|
307 | */
|
---|
308 | DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
|
---|
309 | {
|
---|
310 | Assert(enmType > RTTHREADTYPE_INVALID && enmType < RTTHREADTYPE_END);
|
---|
311 | AssertMsg(g_pProcessPriority && g_pProcessPriority->aTypes[enmType].enmType == enmType,
|
---|
312 | ("enmType=%d entry=%d\n", enmType, g_pProcessPriority->aTypes[enmType].enmType));
|
---|
313 |
|
---|
314 | #ifdef WIN32_SCHED_ENABLED
|
---|
315 | if (SetThreadPriority(rtThreadNativeGetHandle(pThread), g_pProcessPriority->aTypes[enmType].dwThreadPriority))
|
---|
316 | return VINF_SUCCESS;
|
---|
317 |
|
---|
318 | DWORD dwLastError = GetLastError();
|
---|
319 | int rc = RTErrConvertFromWin32(dwLastError);
|
---|
320 | AssertMsgFailed(("SetThreadPriority(%p, %d) failed, dwLastError=%d rc=%Rrc\n",
|
---|
321 | rtThreadNativeGetHandle(pThread), g_pProcessPriority->aTypes[enmType].dwThreadPriority, dwLastError, rc));
|
---|
322 | return rc;
|
---|
323 | #else
|
---|
324 | return VINF_SUCCESS;
|
---|
325 | #endif
|
---|
326 | }
|
---|
327 |
|
---|