VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win32/sched-win32.cpp@ 5285

Last change on this file since 5285 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

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