1 | /* $Id: thread-r0drv-os2.cpp 39443 2011-11-28 15:01:21Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 1), Ring-0 Driver, OS/2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
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 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include "the-os2-kernel.h"
|
---|
35 | #include "internal/iprt.h"
|
---|
36 | #include <iprt/thread.h>
|
---|
37 |
|
---|
38 | #include <iprt/asm.h>
|
---|
39 | #include <iprt/asm-amd64-x86.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/err.h>
|
---|
42 | #include <iprt/mp.h>
|
---|
43 | #include "internal/thread.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Global Variables *
|
---|
48 | *******************************************************************************/
|
---|
49 | /** Per-cpu preemption counters. */
|
---|
50 | static int32_t volatile g_acPreemptDisabled[256];
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
55 | {
|
---|
56 | PLINFOSEG pLIS = (PLINFOSEG)RTR0Os2Virt2Flat(g_fpLIS);
|
---|
57 | AssertReturn(pLIS, NIL_RTNATIVETHREAD);
|
---|
58 | return pLIS->tidCurrent | (pLIS->pidCurrent << 16);
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | static int rtR0ThreadOs2SleepCommon(RTMSINTERVAL cMillies)
|
---|
63 | {
|
---|
64 | int rc = KernBlock((ULONG)RTThreadSleep,
|
---|
65 | cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies,
|
---|
66 | 0, NULL, NULL);
|
---|
67 | switch (rc)
|
---|
68 | {
|
---|
69 | case NO_ERROR:
|
---|
70 | return VINF_SUCCESS;
|
---|
71 | case ERROR_TIMEOUT:
|
---|
72 | return VERR_TIMEOUT;
|
---|
73 | case ERROR_INTERRUPT:
|
---|
74 | return VERR_INTERRUPTED;
|
---|
75 | default:
|
---|
76 | AssertMsgFailed(("%d\n", rc));
|
---|
77 | return VERR_NO_TRANSLATION;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
|
---|
83 | {
|
---|
84 | return rtR0ThreadOs2SleepCommon(cMillies);
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | RTDECL(int) RTThreadSleepNoBlock(RTMSINTERVAL cMillies)
|
---|
89 | {
|
---|
90 | return rtR0ThreadOs2SleepCommon(cMillies);
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | RTDECL(bool) RTThreadYield(void)
|
---|
95 | {
|
---|
96 | /** @todo implement me (requires a devhelp) */
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
|
---|
102 | {
|
---|
103 | Assert(hThread == NIL_RTTHREAD);
|
---|
104 | int32_t c = g_acPreemptDisabled[ASMGetApicId()];
|
---|
105 | AssertMsg(c >= 0 && c < 32, ("%d\n", c));
|
---|
106 | return c == 0
|
---|
107 | && ASMIntAreEnabled();
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
|
---|
112 | {
|
---|
113 | Assert(hThread == NIL_RTTHREAD);
|
---|
114 |
|
---|
115 | union
|
---|
116 | {
|
---|
117 | RTFAR16 fp;
|
---|
118 | uint8_t fResched;
|
---|
119 | } u;
|
---|
120 | int rc = RTR0Os2DHQueryDOSVar(DHGETDOSV_YIELDFLAG, 0, &u.fp);
|
---|
121 | AssertReturn(rc == 0, false);
|
---|
122 | if (u.fResched)
|
---|
123 | return true;
|
---|
124 |
|
---|
125 | /** @todo Check if DHGETDOSV_YIELDFLAG includes TCYIELDFLAG. */
|
---|
126 | rc = RTR0Os2DHQueryDOSVar(DHGETDOSV_TCYIELDFLAG, 0, &u.fp);
|
---|
127 | AssertReturn(rc == 0, false);
|
---|
128 | if (u.fResched)
|
---|
129 | return true;
|
---|
130 | return false;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
|
---|
135 | {
|
---|
136 | /* yes, RTThreadPreemptIsPending is reliable. */
|
---|
137 | return true;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | RTDECL(bool) RTThreadPreemptIsPossible(void)
|
---|
142 | {
|
---|
143 | /* no kernel preemption on OS/2. */
|
---|
144 | return false;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
|
---|
149 | {
|
---|
150 | AssertPtr(pState);
|
---|
151 | Assert(pState->u32Reserved == 0);
|
---|
152 |
|
---|
153 | /* No preemption on OS/2, so do our own accounting. */
|
---|
154 | int32_t c = ASMAtomicIncS32(&g_acPreemptDisabled[ASMGetApicId()]);
|
---|
155 | AssertMsg(c > 0 && c < 32, ("%d\n", c));
|
---|
156 | pState->u32Reserved = c;
|
---|
157 | RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
|
---|
162 | {
|
---|
163 | AssertPtr(pState);
|
---|
164 | AssertMsg(pState->u32Reserved > 0 && pState->u32Reserved < 32, ("%d\n", pState->u32Reserved));
|
---|
165 | RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
|
---|
166 |
|
---|
167 | /* No preemption on OS/2, so do our own accounting. */
|
---|
168 | int32_t volatile *pc = &g_acPreemptDisabled[ASMGetApicId()];
|
---|
169 | AssertMsg(pState->u32Reserved == (uint32_t)*pc, ("uchDummy=%d *pc=%d \n", pState->u32Reserved, *pc));
|
---|
170 | ASMAtomicUoWriteS32(pc, pState->u32Reserved - 1);
|
---|
171 | pState->u32Reserved = 0;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
|
---|
176 | {
|
---|
177 | Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
|
---|
178 |
|
---|
179 | union
|
---|
180 | {
|
---|
181 | RTFAR16 fp;
|
---|
182 | uint8_t cInterruptLevel;
|
---|
183 | } u;
|
---|
184 | /** @todo OS/2: verify the usage of DHGETDOSV_INTERRUPTLEV. */
|
---|
185 | int rc = RTR0Os2DHQueryDOSVar(DHGETDOSV_INTERRUPTLEV, 0, &u.fp);
|
---|
186 | AssertReturn(rc == 0, true);
|
---|
187 |
|
---|
188 | return u.cInterruptLevel > 0;
|
---|
189 | }
|
---|
190 |
|
---|