VirtualBox

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

Last change on this file since 93115 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.2 KB
Line 
1/* $Id: sched-win.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * IPRT - Scheduling, Win32.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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
266DECLHIDDEN(int) rtProcNativeSetPriority(RTPROCPRIORITY enmPriority)
267{
268 Assert(enmPriority > RTPROCPRIORITY_INVALID && enmPriority < RTPROCPRIORITY_LAST); RT_NOREF_PV(enmPriority);
269
270 if (enmPriority == RTPROCPRIORITY_DEFAULT)
271 {
272 g_pProcessPriority = &g_aDefaultPriority;
273 return VINF_SUCCESS;
274 }
275
276 for (size_t i = 0; i < RT_ELEMENTS(g_aPriorities); i++)
277 if ( g_aPriorities[i].enmPriority == enmPriority
278 && g_aPriorities[i].dwProcessPriorityClass == ANY_PROCESS_PRIORITY_CLASS)
279 {
280 g_pProcessPriority = &g_aPriorities[i];
281 return VINF_SUCCESS;
282 }
283
284 AssertFailedReturn(VERR_INTERNAL_ERROR);
285}
286
287
288/**
289 * Gets the win32 thread handle.
290 *
291 * @returns Valid win32 handle for the specified thread.
292 * @param pThread The thread.
293 */
294DECLINLINE(HANDLE) rtThreadNativeGetHandle(PRTTHREADINT pThread)
295{
296 if ((uintptr_t)pThread->Core.Key == GetCurrentThreadId())
297 return GetCurrentThread();
298 return (HANDLE)pThread->hThread;
299}
300
301
302DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
303{
304 Assert(enmType > RTTHREADTYPE_INVALID && enmType < RTTHREADTYPE_END);
305 AssertMsg(g_pProcessPriority && g_pProcessPriority->aTypes[enmType].enmType == enmType,
306 ("enmType=%d entry=%d\n", enmType, g_pProcessPriority->aTypes[enmType].enmType));
307
308#ifdef WIN32_SCHED_ENABLED
309 HANDLE hThread = rtThreadNativeGetHandle(pThread);
310 if ( hThread == NULL /* No handle for alien threads. */
311 || SetThreadPriority(hThread, g_pProcessPriority->aTypes[enmType].iThreadPriority))
312 return VINF_SUCCESS;
313
314 DWORD dwLastError = GetLastError();
315 int rc = RTErrConvertFromWin32(dwLastError);
316 AssertMsgFailed(("SetThreadPriority(%p, %d) failed, dwLastError=%d rc=%Rrc\n",
317 rtThreadNativeGetHandle(pThread), g_pProcessPriority->aTypes[enmType].iThreadPriority, dwLastError, rc));
318 return rc;
319#else
320 return VINF_SUCCESS;
321#endif
322}
323
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