VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/netbsd/thread-r0drv-netbsd.c@ 77120

Last change on this file since 77120 was 77120, checked in by vboxsync, 6 years ago

IPRT: Some license header cleanups.

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

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