VirtualBox

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