1 | /* $Id: thread-r0drv-os2.cpp 75553 2018-11-18 04:56:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 1), Ring-0 Driver, OS/2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*********************************************************************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *********************************************************************************************************************************/
|
---|
35 | #include "the-os2-kernel.h"
|
---|
36 | #include "internal/iprt.h"
|
---|
37 | #include <iprt/thread.h>
|
---|
38 |
|
---|
39 | #include <iprt/asm.h>
|
---|
40 | #include <iprt/asm-amd64-x86.h>
|
---|
41 | #include <iprt/assert.h>
|
---|
42 | #include <iprt/err.h>
|
---|
43 | #include <iprt/mp.h>
|
---|
44 | #include "internal/thread.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Global Variables *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | /** Per-cpu preemption counters. */
|
---|
51 | static int32_t volatile g_acPreemptDisabled[256];
|
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
56 | {
|
---|
57 | PLINFOSEG pLIS = (PLINFOSEG)RTR0Os2Virt2Flat(g_fpLIS);
|
---|
58 | AssertMsgReturn(pLIS, ("g_fpLIS=%04x:%04x - logging too early again?\n", g_fpLIS.sel, g_fpLIS.off), NIL_RTNATIVETHREAD);
|
---|
59 | return pLIS->tidCurrent | (pLIS->pidCurrent << 16);
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | static int rtR0ThreadOs2SleepCommon(RTMSINTERVAL cMillies)
|
---|
64 | {
|
---|
65 | int rc = KernBlock((ULONG)RTThreadSleep,
|
---|
66 | cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies,
|
---|
67 | 0, NULL, NULL);
|
---|
68 | switch (rc)
|
---|
69 | {
|
---|
70 | case NO_ERROR:
|
---|
71 | return VINF_SUCCESS;
|
---|
72 | case ERROR_TIMEOUT:
|
---|
73 | return VERR_TIMEOUT;
|
---|
74 | case ERROR_INTERRUPT:
|
---|
75 | return VERR_INTERRUPTED;
|
---|
76 | default:
|
---|
77 | AssertMsgFailed(("%d\n", rc));
|
---|
78 | return VERR_NO_TRANSLATION;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
|
---|
84 | {
|
---|
85 | return rtR0ThreadOs2SleepCommon(cMillies);
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | RTDECL(int) RTThreadSleepNoBlock(RTMSINTERVAL cMillies)
|
---|
90 | {
|
---|
91 | return rtR0ThreadOs2SleepCommon(cMillies);
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | RTDECL(bool) RTThreadYield(void)
|
---|
96 | {
|
---|
97 | /** @todo implement me (requires a devhelp) */
|
---|
98 | return false;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
|
---|
103 | {
|
---|
104 | Assert(hThread == NIL_RTTHREAD);
|
---|
105 | int32_t c = g_acPreemptDisabled[ASMGetApicId()];
|
---|
106 | AssertMsg(c >= 0 && c < 32, ("%d\n", c));
|
---|
107 | return c == 0
|
---|
108 | && ASMIntAreEnabled();
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
|
---|
113 | {
|
---|
114 | Assert(hThread == NIL_RTTHREAD);
|
---|
115 |
|
---|
116 | union
|
---|
117 | {
|
---|
118 | RTFAR16 fp;
|
---|
119 | uint8_t fResched;
|
---|
120 | } u;
|
---|
121 | int rc = RTR0Os2DHQueryDOSVar(DHGETDOSV_YIELDFLAG, 0, &u.fp);
|
---|
122 | AssertReturn(rc == 0, false);
|
---|
123 | if (u.fResched)
|
---|
124 | return true;
|
---|
125 |
|
---|
126 | /** @todo Check if DHGETDOSV_YIELDFLAG includes TCYIELDFLAG. */
|
---|
127 | rc = RTR0Os2DHQueryDOSVar(DHGETDOSV_TCYIELDFLAG, 0, &u.fp);
|
---|
128 | AssertReturn(rc == 0, false);
|
---|
129 | if (u.fResched)
|
---|
130 | return true;
|
---|
131 | return false;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
|
---|
136 | {
|
---|
137 | /* yes, RTThreadPreemptIsPending is reliable. */
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | RTDECL(bool) RTThreadPreemptIsPossible(void)
|
---|
143 | {
|
---|
144 | /* no kernel preemption on OS/2. */
|
---|
145 | return false;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
|
---|
150 | {
|
---|
151 | AssertPtr(pState);
|
---|
152 | Assert(pState->u32Reserved == 0);
|
---|
153 |
|
---|
154 | /* No preemption on OS/2, so do our own accounting. */
|
---|
155 | int32_t c = ASMAtomicIncS32(&g_acPreemptDisabled[ASMGetApicId()]);
|
---|
156 | AssertMsg(c > 0 && c < 32, ("%d\n", c));
|
---|
157 | pState->u32Reserved = c;
|
---|
158 | RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
|
---|
163 | {
|
---|
164 | AssertPtr(pState);
|
---|
165 | AssertMsg(pState->u32Reserved > 0 && pState->u32Reserved < 32, ("%d\n", pState->u32Reserved));
|
---|
166 | RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
|
---|
167 |
|
---|
168 | /* No preemption on OS/2, so do our own accounting. */
|
---|
169 | int32_t volatile *pc = &g_acPreemptDisabled[ASMGetApicId()];
|
---|
170 | AssertMsg(pState->u32Reserved == (uint32_t)*pc, ("uchDummy=%d *pc=%d \n", pState->u32Reserved, *pc));
|
---|
171 | ASMAtomicUoWriteS32(pc, pState->u32Reserved - 1);
|
---|
172 | pState->u32Reserved = 0;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
|
---|
177 | {
|
---|
178 | Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
|
---|
179 |
|
---|
180 | union
|
---|
181 | {
|
---|
182 | RTFAR16 fp;
|
---|
183 | uint8_t cInterruptLevel;
|
---|
184 | } u;
|
---|
185 | /** @todo OS/2: verify the usage of DHGETDOSV_INTERRUPTLEV. */
|
---|
186 | int rc = RTR0Os2DHQueryDOSVar(DHGETDOSV_INTERRUPTLEV, 0, &u.fp);
|
---|
187 | AssertReturn(rc == 0, true);
|
---|
188 |
|
---|
189 | return u.cInterruptLevel > 0;
|
---|
190 | }
|
---|
191 |
|
---|