1 | /* $Id: thread-r0drv-netbsd.c 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 1), Ring-0 Driver, NetBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "the-netbsd-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
33 | #include <iprt/thread.h>
|
---|
34 |
|
---|
35 | #include <iprt/asm.h>
|
---|
36 | #include <iprt/asm-amd64-x86.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/mp.h>
|
---|
40 | #include "internal/thread.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
44 | {
|
---|
45 | return (RTNATIVETHREAD)curlwp;
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | static int rtR0ThreadNbsdSleepCommon(RTMSINTERVAL cMillies)
|
---|
50 | {
|
---|
51 | int rc;
|
---|
52 | int cTicks;
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * 0 ms sleep -> yield.
|
---|
56 | */
|
---|
57 | if (!cMillies)
|
---|
58 | {
|
---|
59 | RTThreadYield();
|
---|
60 | return VINF_SUCCESS;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * Translate milliseconds into ticks and go to sleep.
|
---|
65 | */
|
---|
66 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
67 | {
|
---|
68 | if (hz == 1000)
|
---|
69 | cTicks = cMillies;
|
---|
70 | else if (hz == 100)
|
---|
71 | cTicks = cMillies / 10;
|
---|
72 | else
|
---|
73 | {
|
---|
74 | int64_t cTicks64 = ((uint64_t)cMillies * hz) / 1000;
|
---|
75 | cTicks = (int)cTicks64;
|
---|
76 | if (cTicks != cTicks64)
|
---|
77 | cTicks = INT_MAX;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | else
|
---|
81 | cTicks = 0; /* requires giant lock! */
|
---|
82 |
|
---|
83 | rc = tsleep((void *)RTThreadSleep,
|
---|
84 | PZERO | PCATCH,
|
---|
85 | "iprtsl", /* max 6 chars */
|
---|
86 | cTicks);
|
---|
87 | switch (rc)
|
---|
88 | {
|
---|
89 | case 0:
|
---|
90 | return VINF_SUCCESS;
|
---|
91 | case EWOULDBLOCK:
|
---|
92 | return VERR_TIMEOUT;
|
---|
93 | case EINTR:
|
---|
94 | case ERESTART:
|
---|
95 | return VERR_INTERRUPTED;
|
---|
96 | default:
|
---|
97 | AssertMsgFailed(("%d\n", rc));
|
---|
98 | return VERR_NO_TRANSLATION;
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
|
---|
104 | {
|
---|
105 | return rtR0ThreadNbsdSleepCommon(cMillies);
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | RTDECL(int) RTThreadSleepNoLog(RTMSINTERVAL cMillies)
|
---|
110 | {
|
---|
111 | return rtR0ThreadNbsdSleepCommon(cMillies);
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | RTDECL(bool) RTThreadYield(void)
|
---|
116 | {
|
---|
117 | yield();
|
---|
118 | return true;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
|
---|
123 | {
|
---|
124 | Assert(hThread == NIL_RTTHREAD);
|
---|
125 |
|
---|
126 | return curlwp->l_dopreempt == 0
|
---|
127 | && ASMIntAreEnabled(); /** @todo is there a native netbsd function/macro for this? */
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
|
---|
132 | {
|
---|
133 | Assert(hThread == NIL_RTTHREAD);
|
---|
134 |
|
---|
135 | return curlwp->l_dopreempt;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
|
---|
140 | {
|
---|
141 | /* yes, RTThreadPreemptIsPending is reliable. */
|
---|
142 | return true;
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | RTDECL(bool) RTThreadPreemptIsPossible(void)
|
---|
147 | {
|
---|
148 | /* yes, kernel preemption is possible. */
|
---|
149 | return true;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
|
---|
154 | {
|
---|
155 | AssertPtr(pState);
|
---|
156 |
|
---|
157 | curlwp->l_nopreempt++;
|
---|
158 | __insn_barrier();
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
|
---|
163 | {
|
---|
164 | AssertPtr(pState);
|
---|
165 | __insn_barrier();
|
---|
166 | if (--curlwp->l_nopreempt != 0)
|
---|
167 | return;
|
---|
168 | __insn_barrier();
|
---|
169 | if (__predict_false(curlwp->l_dopreempt))
|
---|
170 | kpreempt(0);
|
---|
171 | __insn_barrier();
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
|
---|
176 | {
|
---|
177 | Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
|
---|
178 | /** @todo NetBSD: Implement RTThreadIsInInterrupt. Required for guest
|
---|
179 | * additions! */
|
---|
180 | return !ASMIntAreEnabled();
|
---|
181 | }
|
---|