1 | /* $Id: thread-r0drv-netbsd.c 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 1), Ring-0 Driver, NetBSD.
|
---|
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 |
|
---|
30 | /*********************************************************************************************************************************
|
---|
31 | * Header Files *
|
---|
32 | *********************************************************************************************************************************/
|
---|
33 | #include "the-netbsd-kernel.h"
|
---|
34 | #include "internal/iprt.h"
|
---|
35 | #include <iprt/thread.h>
|
---|
36 |
|
---|
37 | #include <iprt/asm.h>
|
---|
38 | #include <iprt/asm-amd64-x86.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/mp.h>
|
---|
42 | #include "internal/thread.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
46 | {
|
---|
47 | return (RTNATIVETHREAD)curlwp;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | static int rtR0ThreadNbsdSleepCommon(RTMSINTERVAL cMillies)
|
---|
52 | {
|
---|
53 | int rc;
|
---|
54 | int cTicks;
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * 0 ms sleep -> yield.
|
---|
58 | */
|
---|
59 | if (!cMillies)
|
---|
60 | {
|
---|
61 | RTThreadYield();
|
---|
62 | return VINF_SUCCESS;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Translate milliseconds into ticks and go to sleep.
|
---|
67 | */
|
---|
68 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
69 | {
|
---|
70 | if (hz == 1000)
|
---|
71 | cTicks = cMillies;
|
---|
72 | else if (hz == 100)
|
---|
73 | cTicks = cMillies / 10;
|
---|
74 | else
|
---|
75 | {
|
---|
76 | int64_t cTicks64 = ((uint64_t)cMillies * hz) / 1000;
|
---|
77 | cTicks = (int)cTicks64;
|
---|
78 | if (cTicks != cTicks64)
|
---|
79 | cTicks = INT_MAX;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | else
|
---|
83 | cTicks = 0; /* requires giant lock! */
|
---|
84 |
|
---|
85 | rc = tsleep((void *)RTThreadSleep,
|
---|
86 | PZERO | PCATCH,
|
---|
87 | "iprtsl", /* max 6 chars */
|
---|
88 | cTicks);
|
---|
89 | switch (rc)
|
---|
90 | {
|
---|
91 | case 0:
|
---|
92 | return VINF_SUCCESS;
|
---|
93 | case EWOULDBLOCK:
|
---|
94 | return VERR_TIMEOUT;
|
---|
95 | case EINTR:
|
---|
96 | case ERESTART:
|
---|
97 | return VERR_INTERRUPTED;
|
---|
98 | default:
|
---|
99 | AssertMsgFailed(("%d\n", rc));
|
---|
100 | return VERR_NO_TRANSLATION;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
|
---|
106 | {
|
---|
107 | return rtR0ThreadNbsdSleepCommon(cMillies);
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | RTDECL(int) RTThreadSleepNoLog(RTMSINTERVAL cMillies)
|
---|
112 | {
|
---|
113 | return rtR0ThreadNbsdSleepCommon(cMillies);
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | RTDECL(bool) RTThreadYield(void)
|
---|
118 | {
|
---|
119 | yield();
|
---|
120 | return true;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
|
---|
125 | {
|
---|
126 | Assert(hThread == NIL_RTTHREAD);
|
---|
127 |
|
---|
128 | return curlwp->l_dopreempt == 0
|
---|
129 | && ASMIntAreEnabled(); /** @todo is there a native netbsd function/macro for this? */
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
|
---|
134 | {
|
---|
135 | Assert(hThread == NIL_RTTHREAD);
|
---|
136 |
|
---|
137 | return curlwp->l_dopreempt;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
|
---|
142 | {
|
---|
143 | /* yes, RTThreadPreemptIsPending is reliable. */
|
---|
144 | return true;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | RTDECL(bool) RTThreadPreemptIsPossible(void)
|
---|
149 | {
|
---|
150 | /* yes, kernel preemption is possible. */
|
---|
151 | return true;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
|
---|
156 | {
|
---|
157 | AssertPtr(pState);
|
---|
158 |
|
---|
159 | curlwp->l_nopreempt++;
|
---|
160 | __insn_barrier();
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
|
---|
165 | {
|
---|
166 | AssertPtr(pState);
|
---|
167 | __insn_barrier();
|
---|
168 | if (--curlwp->l_nopreempt != 0)
|
---|
169 | return;
|
---|
170 | __insn_barrier();
|
---|
171 | if (__predict_false(curlwp->l_dopreempt))
|
---|
172 | kpreempt(0);
|
---|
173 | __insn_barrier();
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
|
---|
178 | {
|
---|
179 | Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
|
---|
180 | /** @todo NetBSD: Implement RTThreadIsInInterrupt. Required for guest
|
---|
181 | * additions! */
|
---|
182 | return !ASMIntAreEnabled();
|
---|
183 | }
|
---|