1 | /* $Id: thread-r0drv-os2.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 1), Ring-0 Driver, OS/2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Contributed by knut st. osmundsen.
|
---|
8 | *
|
---|
9 | * Copyright (C) 2007-2022 Oracle and/or its affiliates.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox base platform packages, as
|
---|
12 | * available from https://www.virtualbox.org.
|
---|
13 | *
|
---|
14 | * This program is free software; you can redistribute it and/or
|
---|
15 | * modify it under the terms of the GNU General Public License
|
---|
16 | * as published by the Free Software Foundation, in version 3 of the
|
---|
17 | * License.
|
---|
18 | *
|
---|
19 | * This program is distributed in the hope that it will be useful, but
|
---|
20 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
22 | * General Public License for more details.
|
---|
23 | *
|
---|
24 | * You should have received a copy of the GNU General Public License
|
---|
25 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
26 | *
|
---|
27 | * The contents of this file may alternatively be used under the terms
|
---|
28 | * of the Common Development and Distribution License Version 1.0
|
---|
29 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
30 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
31 | * CDDL are applicable instead of those of the GPL.
|
---|
32 | *
|
---|
33 | * You may elect to license modified versions of this file under the
|
---|
34 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
35 | *
|
---|
36 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
37 | * --------------------------------------------------------------------
|
---|
38 | *
|
---|
39 | * This code is based on:
|
---|
40 | *
|
---|
41 | * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
42 | *
|
---|
43 | * Permission is hereby granted, free of charge, to any person
|
---|
44 | * obtaining a copy of this software and associated documentation
|
---|
45 | * files (the "Software"), to deal in the Software without
|
---|
46 | * restriction, including without limitation the rights to use,
|
---|
47 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
48 | * copies of the Software, and to permit persons to whom the
|
---|
49 | * Software is furnished to do so, subject to the following
|
---|
50 | * conditions:
|
---|
51 | *
|
---|
52 | * The above copyright notice and this permission notice shall be
|
---|
53 | * included in all copies or substantial portions of the Software.
|
---|
54 | *
|
---|
55 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
56 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
57 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
58 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
59 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
60 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
61 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
62 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
63 | */
|
---|
64 |
|
---|
65 |
|
---|
66 | /*********************************************************************************************************************************
|
---|
67 | * Header Files *
|
---|
68 | *********************************************************************************************************************************/
|
---|
69 | #include "the-os2-kernel.h"
|
---|
70 | #include "internal/iprt.h"
|
---|
71 | #include <iprt/thread.h>
|
---|
72 |
|
---|
73 | #include <iprt/asm.h>
|
---|
74 | #include <iprt/asm-amd64-x86.h>
|
---|
75 | #include <iprt/assert.h>
|
---|
76 | #include <iprt/err.h>
|
---|
77 | #include <iprt/mp.h>
|
---|
78 | #include "internal/thread.h"
|
---|
79 |
|
---|
80 |
|
---|
81 | /*********************************************************************************************************************************
|
---|
82 | * Global Variables *
|
---|
83 | *********************************************************************************************************************************/
|
---|
84 | /** Per-cpu preemption counters. */
|
---|
85 | static int32_t volatile g_acPreemptDisabled[256];
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
90 | {
|
---|
91 | PLINFOSEG pLIS = (PLINFOSEG)RTR0Os2Virt2Flat(g_fpLIS);
|
---|
92 | AssertMsgReturn(pLIS, ("g_fpLIS=%04x:%04x - logging too early again?\n", g_fpLIS.sel, g_fpLIS.off), NIL_RTNATIVETHREAD);
|
---|
93 | return pLIS->tidCurrent | (pLIS->pidCurrent << 16);
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | static int rtR0ThreadOs2SleepCommon(RTMSINTERVAL cMillies)
|
---|
98 | {
|
---|
99 | int rc = KernBlock((ULONG)RTThreadSleep,
|
---|
100 | cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies,
|
---|
101 | 0, NULL, NULL);
|
---|
102 | switch (rc)
|
---|
103 | {
|
---|
104 | case NO_ERROR:
|
---|
105 | return VINF_SUCCESS;
|
---|
106 | case ERROR_TIMEOUT:
|
---|
107 | return VERR_TIMEOUT;
|
---|
108 | case ERROR_INTERRUPT:
|
---|
109 | return VERR_INTERRUPTED;
|
---|
110 | default:
|
---|
111 | AssertMsgFailed(("%d\n", rc));
|
---|
112 | return VERR_NO_TRANSLATION;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
|
---|
118 | {
|
---|
119 | return rtR0ThreadOs2SleepCommon(cMillies);
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | RTDECL(int) RTThreadSleepNoBlock(RTMSINTERVAL cMillies)
|
---|
124 | {
|
---|
125 | return rtR0ThreadOs2SleepCommon(cMillies);
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | RTDECL(bool) RTThreadYield(void)
|
---|
130 | {
|
---|
131 | /** @todo implement me (requires a devhelp) */
|
---|
132 | return false;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
|
---|
137 | {
|
---|
138 | Assert(hThread == NIL_RTTHREAD);
|
---|
139 | int32_t c = g_acPreemptDisabled[ASMGetApicId()];
|
---|
140 | AssertMsg(c >= 0 && c < 32, ("%d\n", c));
|
---|
141 | return c == 0
|
---|
142 | && ASMIntAreEnabled();
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
|
---|
147 | {
|
---|
148 | Assert(hThread == NIL_RTTHREAD);
|
---|
149 |
|
---|
150 | union
|
---|
151 | {
|
---|
152 | RTFAR16 fp;
|
---|
153 | uint8_t fResched;
|
---|
154 | } u;
|
---|
155 | int rc = RTR0Os2DHQueryDOSVar(DHGETDOSV_YIELDFLAG, 0, &u.fp);
|
---|
156 | AssertReturn(rc == 0, false);
|
---|
157 | if (u.fResched)
|
---|
158 | return true;
|
---|
159 |
|
---|
160 | /** @todo Check if DHGETDOSV_YIELDFLAG includes TCYIELDFLAG. */
|
---|
161 | rc = RTR0Os2DHQueryDOSVar(DHGETDOSV_TCYIELDFLAG, 0, &u.fp);
|
---|
162 | AssertReturn(rc == 0, false);
|
---|
163 | if (u.fResched)
|
---|
164 | return true;
|
---|
165 | return false;
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
|
---|
170 | {
|
---|
171 | /* yes, RTThreadPreemptIsPending is reliable. */
|
---|
172 | return true;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | RTDECL(bool) RTThreadPreemptIsPossible(void)
|
---|
177 | {
|
---|
178 | /* no kernel preemption on OS/2. */
|
---|
179 | return false;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
|
---|
184 | {
|
---|
185 | AssertPtr(pState);
|
---|
186 | Assert(pState->u32Reserved == 0);
|
---|
187 |
|
---|
188 | /* No preemption on OS/2, so do our own accounting. */
|
---|
189 | int32_t c = ASMAtomicIncS32(&g_acPreemptDisabled[ASMGetApicId()]);
|
---|
190 | AssertMsg(c > 0 && c < 32, ("%d\n", c));
|
---|
191 | pState->u32Reserved = c;
|
---|
192 | RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
|
---|
197 | {
|
---|
198 | AssertPtr(pState);
|
---|
199 | AssertMsg(pState->u32Reserved > 0 && pState->u32Reserved < 32, ("%d\n", pState->u32Reserved));
|
---|
200 | RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
|
---|
201 |
|
---|
202 | /* No preemption on OS/2, so do our own accounting. */
|
---|
203 | int32_t volatile *pc = &g_acPreemptDisabled[ASMGetApicId()];
|
---|
204 | AssertMsg(pState->u32Reserved == (uint32_t)*pc, ("uchDummy=%d *pc=%d \n", pState->u32Reserved, *pc));
|
---|
205 | ASMAtomicUoWriteS32(pc, pState->u32Reserved - 1);
|
---|
206 | pState->u32Reserved = 0;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
|
---|
211 | {
|
---|
212 | Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
|
---|
213 |
|
---|
214 | union
|
---|
215 | {
|
---|
216 | RTFAR16 fp;
|
---|
217 | uint8_t cInterruptLevel;
|
---|
218 | } u;
|
---|
219 | /** @todo OS/2: verify the usage of DHGETDOSV_INTERRUPTLEV. */
|
---|
220 | int rc = RTR0Os2DHQueryDOSVar(DHGETDOSV_INTERRUPTLEV, 0, &u.fp);
|
---|
221 | AssertReturn(rc == 0, true);
|
---|
222 |
|
---|
223 | return u.cInterruptLevel > 0;
|
---|
224 | }
|
---|
225 |
|
---|