VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/sched-win.cpp@ 85112

Last change on this file since 85112 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.9 KB
Line 
1/* $Id: sched-win.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - Scheduling, Win32.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 <iprt/win/windows.h>
37
38#include <iprt/thread.h>
39#include <iprt/log.h>
40#include <iprt/assert.h>
41#include <iprt/errcore.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 */
52typedef 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 int iThreadPriority;
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 */
81static 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 */
228static 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. */
249static 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 */
259DECLHIDDEN(int) rtSchedNativeCalcDefaultPriority(RTTHREADTYPE enmType)
260{
261 Assert(enmType > RTTHREADTYPE_INVALID && enmType < RTTHREADTYPE_END); RT_NOREF_PV(enmType);
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 */
275DECLHIDDEN(int) rtProcNativeSetPriority(RTPROCPRIORITY enmPriority)
276{
277 Assert(enmPriority > RTPROCPRIORITY_INVALID && enmPriority < RTPROCPRIORITY_LAST); RT_NOREF_PV(enmPriority);
278
279 if (enmPriority == RTPROCPRIORITY_DEFAULT)
280 {
281 g_pProcessPriority = &g_aDefaultPriority;
282 return VINF_SUCCESS;
283 }
284
285 for (size_t i = 0; i < RT_ELEMENTS(g_aPriorities); i++)
286 if ( g_aPriorities[i].enmPriority == enmPriority
287 && g_aPriorities[i].dwProcessPriorityClass == ANY_PROCESS_PRIORITY_CLASS)
288 {
289 g_pProcessPriority = &g_aPriorities[i];
290 return VINF_SUCCESS;
291 }
292
293 AssertFailedReturn(VERR_INTERNAL_ERROR);
294}
295
296
297/**
298 * Gets the win32 thread handle.
299 *
300 * @returns Valid win32 handle for the specified thread.
301 * @param pThread The thread.
302 */
303DECLINLINE(HANDLE) rtThreadNativeGetHandle(PRTTHREADINT pThread)
304{
305 if ((uintptr_t)pThread->Core.Key == GetCurrentThreadId())
306 return GetCurrentThread();
307 return (HANDLE)pThread->hThread;
308}
309
310
311/**
312 * Sets the priority of the thread according to the thread type
313 * and current process priority.
314 *
315 * The RTTHREADINT::enmType member has not yet been updated and will be updated by
316 * the caller on a successful return.
317 *
318 * @returns iprt status code.
319 * @param pThread The thread in question.
320 * @param enmType The thread type.
321 * @remark Located in sched.
322 */
323DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
324{
325 Assert(enmType > RTTHREADTYPE_INVALID && enmType < RTTHREADTYPE_END);
326 AssertMsg(g_pProcessPriority && g_pProcessPriority->aTypes[enmType].enmType == enmType,
327 ("enmType=%d entry=%d\n", enmType, g_pProcessPriority->aTypes[enmType].enmType));
328
329#ifdef WIN32_SCHED_ENABLED
330 HANDLE hThread = rtThreadNativeGetHandle(pThread);
331 if ( hThread == NULL /* No handle for alien threads. */
332 || SetThreadPriority(hThread, g_pProcessPriority->aTypes[enmType].iThreadPriority))
333 return VINF_SUCCESS;
334
335 DWORD dwLastError = GetLastError();
336 int rc = RTErrConvertFromWin32(dwLastError);
337 AssertMsgFailed(("SetThreadPriority(%p, %d) failed, dwLastError=%d rc=%Rrc\n",
338 rtThreadNativeGetHandle(pThread), g_pProcessPriority->aTypes[enmType].iThreadPriority, dwLastError, rc));
339 return rc;
340#else
341 return VINF_SUCCESS;
342#endif
343}
344
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette