VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/os2/thread-r0drv-os2.cpp@ 21536

Last change on this file since 21536 was 21536, checked in by vboxsync, 16 years ago

iprt/thread.h: Redefined RTThreadPreemptIsEnabled for systems without preemption (we keep count ourselves). Added RTThreadPreemptIsPossible and RTThreadIsInInterrupt. Fixed RTThreadPreemptIsEnabled on FreeBSD and Solaris/vbi.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1/* $Id: thread-r0drv-os2.cpp 21536 2009-07-13 14:49:39Z 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
36#include <iprt/thread.h>
37#include <iprt/asm.h>
38#include <iprt/assert.h>
39#include <iprt/err.h>
40#include "internal/thread.h"
41
42
43/*******************************************************************************
44* Global Variables *
45*******************************************************************************/
46/** Per-cpu preemption counters. */
47static int32_t volatile g_acPreemptDisabled[256];
48
49
50
51RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
52{
53 PLINFOSEG pLIS = (PLINFOSEG)RTR0Os2Virt2Flat(g_fpLIS);
54 AssertReturn(pLIS, NIL_RTNATIVETHREAD);
55 return pLIS->tidCurrent | (pLIS->pidCurrent << 16);
56}
57
58
59RTDECL(int) RTThreadSleep(unsigned cMillies)
60{
61 int rc = KernBlock((ULONG)RTThreadSleep,
62 cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies,
63 0, NULL, NULL);
64 switch (rc)
65 {
66 case NO_ERROR:
67 return VINF_SUCCESS;
68 case ERROR_TIMEOUT:
69 return VERR_TIMEOUT;
70 case ERROR_INTERRUPT:
71 return VERR_INTERRUPTED;
72 default:
73 AssertMsgFailed(("%d\n", rc));
74 return VERR_NO_TRANSLATION;
75 }
76}
77
78
79RTDECL(bool) RTThreadYield(void)
80{
81 /** @todo implement me (requires a devhelp) */
82 return false;
83}
84
85
86RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
87{
88 Assert(hThread == NIL_RTTHREAD);
89 int32_t c = g_acPreemptDisabled[ASMGetApicId()];
90 AssertMsg(c >= 0 && c < 32, ("%d\n", c));
91 return c == 0
92 && ASMIntAreEnabled();
93}
94
95
96RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
97{
98 Assert(hThread == NIL_RTTHREAD);
99
100 union
101 {
102 RTFAR16 fp;
103 uint8_t fResched;
104 } u;
105 int rc = RTR0Os2DHQueryDOSVar(DHGETDOSV_YIELDFLAG, 0, &u.fp);
106 AssertReturn(rc == 0, false);
107 if (u.fResched)
108 return true;
109
110 /** @todo Check if DHGETDOSV_YIELDFLAG includes TCYIELDFLAG. */
111 rc = RTR0Os2DHQueryDOSVar(DHGETDOSV_TCYIELDFLAG, 0, &u.fp);
112 AssertReturn(rc == 0, false);
113 if (u.fResched)
114 return true;
115 return false;
116}
117
118
119RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
120{
121 /* yes, RTThreadPreemptIsPending is reliable. */
122 return true;
123}
124
125
126RTDECL(bool) RTThreadPreemptIsPossible(void)
127{
128 /* no kernel preemption on OS/2. */
129 return false;
130}
131
132
133RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
134{
135 AssertPtr(pState);
136
137 /* No preemption on OS/2, so do our own accounting. */
138 int32_t c = ASMAtomicIncS32(&g_acPreemptDisabled[ASMGetApicId()]);
139 AssertMsg(c > 0 && c < 32, ("%d\n", c));
140 pState->uchDummy = (unsigned char)c;
141}
142
143
144RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
145{
146 AssertPtr(pState);
147 AssertMsg(pState->uchDummy > 0 && pState->uchDummy < 32, ("%d\n", pState->uchDummy));
148
149 /* No preemption on OS/2, so do our own accounting. */
150 int32_t volatile *pc = &g_acPreemptDisabled[ASMGetApicId()];
151 AssertMsg(pState->uchDummy == (uint32_t)*pc, ("uchDummy=%d *pc=%d \n", pState->uchDummy, *pc));
152 ASMAtomicUoWriteS32(pc, pState->uchDummy - 1);
153 pState->uchDummy = 0;
154}
155
156
157RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
158{
159 Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
160
161 union
162 {
163 RTFAR16 fp;
164 uint8_t cInterruptLevel;
165 } u;
166 /** @todo OS/2: verify the usage of DHGETDOSV_INTERRUPTLEV. */
167 int rc = RTR0Os2DHQueryDOSVar(DHGETDOSV_INTERRUPTLEV, 0, &u.fp);
168 AssertReturn(rc == 0, true);
169
170 return cInterruptLevel > 0;
171}
172
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette