1 | /* $Id: sched-os2.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Scheduling, OS/2
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 | /** @def OS2_SCHED_ENABLED
|
---|
38 | * Enables the priority scheme. */
|
---|
39 | #define OS2_SCHED_ENABLED
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * Header Files *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 | #define LOG_GROUP RTLOGGROUP_THREAD
|
---|
46 | #define INCL_BASE
|
---|
47 | #define INCL_ERRORS
|
---|
48 | #include <os2.h>
|
---|
49 |
|
---|
50 | #include <iprt/thread.h>
|
---|
51 | #include <iprt/log.h>
|
---|
52 | #include <iprt/assert.h>
|
---|
53 | #include <iprt/errcore.h>
|
---|
54 | #include "internal/sched.h"
|
---|
55 | #include "internal/thread.h"
|
---|
56 |
|
---|
57 |
|
---|
58 | /*********************************************************************************************************************************
|
---|
59 | * Structures and Typedefs *
|
---|
60 | *********************************************************************************************************************************/
|
---|
61 | /**
|
---|
62 | * Configuration of one priority.
|
---|
63 | */
|
---|
64 | typedef struct
|
---|
65 | {
|
---|
66 | /** The priority. */
|
---|
67 | RTPROCPRIORITY enmPriority;
|
---|
68 | /** The name of this priority. */
|
---|
69 | const char *pszName;
|
---|
70 | /** Array scheduler attributes corresponding to each of the thread types. */
|
---|
71 | struct
|
---|
72 | {
|
---|
73 | /** For sanity include the array index. */
|
---|
74 | RTTHREADTYPE enmType;
|
---|
75 | /** The OS/2 priority class. */
|
---|
76 | ULONG ulClass;
|
---|
77 | /** The OS/2 priority delta. */
|
---|
78 | ULONG ulDelta;
|
---|
79 | } aTypes[RTTHREADTYPE_END];
|
---|
80 | } PROCPRIORITY;
|
---|
81 |
|
---|
82 | /** Matches any process priority class. */
|
---|
83 | #define ANY_PROCESS_PRIORITY_CLASS (~0U)
|
---|
84 |
|
---|
85 |
|
---|
86 | /*********************************************************************************************************************************
|
---|
87 | * Global Variables *
|
---|
88 | *********************************************************************************************************************************/
|
---|
89 | /**
|
---|
90 | * Array of static priority configurations.
|
---|
91 | */
|
---|
92 | static const PROCPRIORITY g_aPriorities[] =
|
---|
93 | {
|
---|
94 | {
|
---|
95 | RTPROCPRIORITY_FLAT, "Flat",
|
---|
96 | {
|
---|
97 | { RTTHREADTYPE_INVALID, ~0, ~0 },
|
---|
98 | { RTTHREADTYPE_INFREQUENT_POLLER, PRTYC_REGULAR, 0 },
|
---|
99 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, PRTYC_REGULAR, 0 },
|
---|
100 | { RTTHREADTYPE_EMULATION, PRTYC_REGULAR, 0 },
|
---|
101 | { RTTHREADTYPE_DEFAULT, PRTYC_REGULAR, 0 },
|
---|
102 | { RTTHREADTYPE_GUI, PRTYC_REGULAR, 0 },
|
---|
103 | { RTTHREADTYPE_MAIN_WORKER, PRTYC_REGULAR, 0 },
|
---|
104 | { RTTHREADTYPE_VRDP_IO, PRTYC_REGULAR, 0 },
|
---|
105 | { RTTHREADTYPE_DEBUGGER, PRTYC_REGULAR, 0 },
|
---|
106 | { RTTHREADTYPE_MSG_PUMP, PRTYC_REGULAR, 0 },
|
---|
107 | { RTTHREADTYPE_IO, PRTYC_REGULAR, 0 },
|
---|
108 | { RTTHREADTYPE_TIMER, PRTYC_REGULAR, 0 }
|
---|
109 | }
|
---|
110 | },
|
---|
111 | {
|
---|
112 | RTPROCPRIORITY_LOW, "Low",
|
---|
113 | {
|
---|
114 | { RTTHREADTYPE_INVALID, ~0 },
|
---|
115 | { RTTHREADTYPE_INFREQUENT_POLLER, PRTYC_IDLETIME, 0 },
|
---|
116 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, PRTYC_IDLETIME, 0 },
|
---|
117 | { RTTHREADTYPE_EMULATION, PRTYC_IDLETIME, 0 },
|
---|
118 | { RTTHREADTYPE_DEFAULT, PRTYC_IDLETIME, 30 },
|
---|
119 | { RTTHREADTYPE_GUI, PRTYC_IDLETIME, 30 },
|
---|
120 | { RTTHREADTYPE_MAIN_WORKER, PRTYC_IDLETIME, 30 },
|
---|
121 | { RTTHREADTYPE_VRDP_IO, PRTYC_REGULAR, 0 },
|
---|
122 | { RTTHREADTYPE_DEBUGGER, PRTYC_REGULAR, 0 },
|
---|
123 | { RTTHREADTYPE_MSG_PUMP, PRTYC_REGULAR, 0 },
|
---|
124 | { RTTHREADTYPE_IO, PRTYC_REGULAR, 0 },
|
---|
125 | { RTTHREADTYPE_TIMER, PRTYC_REGULAR, 0 }
|
---|
126 | }
|
---|
127 | },
|
---|
128 | {
|
---|
129 | RTPROCPRIORITY_NORMAL, "Normal",
|
---|
130 | {
|
---|
131 | { RTTHREADTYPE_INVALID, ~0 },
|
---|
132 | { RTTHREADTYPE_INFREQUENT_POLLER, PRTYC_IDLETIME, 30 },
|
---|
133 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, PRTYC_IDLETIME, 31 },
|
---|
134 | { RTTHREADTYPE_EMULATION, PRTYC_REGULAR, 0 },
|
---|
135 | { RTTHREADTYPE_DEFAULT, PRTYC_REGULAR, 5 },
|
---|
136 | { RTTHREADTYPE_GUI, PRTYC_REGULAR, 10 },
|
---|
137 | { RTTHREADTYPE_MAIN_WORKER, PRTYC_REGULAR, 12 },
|
---|
138 | { RTTHREADTYPE_VRDP_IO, PRTYC_REGULAR, 15 },
|
---|
139 | { RTTHREADTYPE_DEBUGGER, PRTYC_REGULAR, 20 },
|
---|
140 | { RTTHREADTYPE_MSG_PUMP, PRTYC_REGULAR, 25 },
|
---|
141 | { RTTHREADTYPE_IO, PRTYC_FOREGROUNDSERVER, 5 },
|
---|
142 | { RTTHREADTYPE_TIMER, PRTYC_TIMECRITICAL, 0 }
|
---|
143 | }
|
---|
144 | },
|
---|
145 | {
|
---|
146 | RTPROCPRIORITY_HIGH, "High",
|
---|
147 | {
|
---|
148 | { RTTHREADTYPE_INVALID, ~0 },
|
---|
149 | { RTTHREADTYPE_INFREQUENT_POLLER, PRTYC_IDLETIME, 30 },
|
---|
150 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, PRTYC_REGULAR, 0 },
|
---|
151 | { RTTHREADTYPE_EMULATION, PRTYC_REGULAR, 0 },
|
---|
152 | { RTTHREADTYPE_DEFAULT, PRTYC_REGULAR, 15 },
|
---|
153 | { RTTHREADTYPE_GUI, PRTYC_REGULAR, 20 },
|
---|
154 | { RTTHREADTYPE_MAIN_WORKER, PRTYC_REGULAR, 25 },
|
---|
155 | { RTTHREADTYPE_VRDP_IO, PRTYC_REGULAR, 30 },
|
---|
156 | { RTTHREADTYPE_DEBUGGER, PRTYC_TIMECRITICAL, 2 },
|
---|
157 | { RTTHREADTYPE_MSG_PUMP, PRTYC_TIMECRITICAL, 3 },
|
---|
158 | { RTTHREADTYPE_IO, PRTYC_TIMECRITICAL, 4 },
|
---|
159 | { RTTHREADTYPE_TIMER, PRTYC_TIMECRITICAL, 5 }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | };
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * The dynamic default priority configuration.
|
---|
166 | *
|
---|
167 | * This can be recalulated at runtime depending on what the
|
---|
168 | * system allow us to do. Presently we don't do this as it's
|
---|
169 | * generally not a bit issue on OS/2 hosts.
|
---|
170 | */
|
---|
171 | static PROCPRIORITY g_aDefaultPriority =
|
---|
172 | {
|
---|
173 | RTPROCPRIORITY_LOW, "Default",
|
---|
174 | {
|
---|
175 | { RTTHREADTYPE_INVALID, ~0 },
|
---|
176 | { RTTHREADTYPE_INFREQUENT_POLLER, PRTYC_IDLETIME, 30 },
|
---|
177 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, PRTYC_IDLETIME, 31 },
|
---|
178 | { RTTHREADTYPE_EMULATION, PRTYC_REGULAR, 0 },
|
---|
179 | { RTTHREADTYPE_DEFAULT, PRTYC_REGULAR, 5 },
|
---|
180 | { RTTHREADTYPE_GUI, PRTYC_REGULAR, 10 },
|
---|
181 | { RTTHREADTYPE_MAIN_WORKER, PRTYC_REGULAR, 12 },
|
---|
182 | { RTTHREADTYPE_VRDP_IO, PRTYC_REGULAR, 15 },
|
---|
183 | { RTTHREADTYPE_DEBUGGER, PRTYC_REGULAR, 20 },
|
---|
184 | { RTTHREADTYPE_MSG_PUMP, PRTYC_REGULAR, 25 },
|
---|
185 | { RTTHREADTYPE_IO, PRTYC_FOREGROUNDSERVER, 5 },
|
---|
186 | { RTTHREADTYPE_TIMER, PRTYC_TIMECRITICAL, 0 }
|
---|
187 | }
|
---|
188 | };
|
---|
189 |
|
---|
190 |
|
---|
191 | /** Pointer to the current priority configuration. */
|
---|
192 | static const PROCPRIORITY *g_pProcessPriority = &g_aDefaultPriority;
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Calculate the scheduling properties for all the threads in the default
|
---|
197 | * process priority, assuming the current thread have the type enmType.
|
---|
198 | *
|
---|
199 | * @returns iprt status code.
|
---|
200 | * @param enmType The thread type to be assumed for the current thread.
|
---|
201 | */
|
---|
202 | DECLHIDDEN(int) rtSchedNativeCalcDefaultPriority(RTTHREADTYPE enmType)
|
---|
203 | {
|
---|
204 | Assert(enmType > RTTHREADTYPE_INVALID && enmType < RTTHREADTYPE_END);
|
---|
205 | return VINF_SUCCESS;
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | DECLHIDDEN(int) rtProcNativeSetPriority(RTPROCPRIORITY enmPriority)
|
---|
210 | {
|
---|
211 | Assert(enmPriority > RTPROCPRIORITY_INVALID && enmPriority < RTPROCPRIORITY_LAST);
|
---|
212 |
|
---|
213 | if (enmPriority == RTPROCPRIORITY_DEFAULT)
|
---|
214 | {
|
---|
215 | g_pProcessPriority = &g_aDefaultPriority;
|
---|
216 | return VINF_SUCCESS;
|
---|
217 | }
|
---|
218 |
|
---|
219 | for (size_t i = 0; i < RT_ELEMENTS(g_aPriorities); i++)
|
---|
220 | if (g_aPriorities[i].enmPriority == enmPriority)
|
---|
221 | {
|
---|
222 | g_pProcessPriority = &g_aPriorities[i];
|
---|
223 | return VINF_SUCCESS;
|
---|
224 | }
|
---|
225 |
|
---|
226 | AssertFailedReturn(VERR_INTERNAL_ERROR);
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
|
---|
231 | {
|
---|
232 | Assert(enmType > RTTHREADTYPE_INVALID && enmType < RTTHREADTYPE_END);
|
---|
233 | AssertMsg(g_pProcessPriority && g_pProcessPriority->aTypes[enmType].enmType == enmType,
|
---|
234 | ("enmType=%d entry=%d\n", enmType, g_pProcessPriority->aTypes[enmType].enmType));
|
---|
235 |
|
---|
236 | #ifdef OS2_SCHED_ENABLED
|
---|
237 | APIRET rc = DosSetPriority(PRTYS_THREAD, g_pProcessPriority->aTypes[enmType].ulClass, g_pProcessPriority->aTypes[enmType].ulDelta, (ULONG)pThread->Core.Key & 0xffff /*tid*/);
|
---|
238 | AssertMsg(rc == NO_ERROR, ("%d\n", rc));
|
---|
239 | return RTErrConvertFromOS2(rc);
|
---|
240 | #else
|
---|
241 | return VINF_SUCCESS;
|
---|
242 | #endif
|
---|
243 | }
|
---|
244 |
|
---|