1 | /* $Id: thread-r0drv-freebsd.c 22150 2009-08-11 09:41:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 1), Ring-0 Driver, FreeBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2009 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include "the-freebsd-kernel.h"
|
---|
35 |
|
---|
36 | #include <iprt/thread.h>
|
---|
37 | #include <iprt/asm.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 |
|
---|
41 | #include "internal/thread.h"
|
---|
42 |
|
---|
43 |
|
---|
44 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
45 | {
|
---|
46 | return (RTNATIVETHREAD)curthread;
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | RTDECL(int) RTThreadSleep(unsigned cMillies)
|
---|
51 | {
|
---|
52 | int rc;
|
---|
53 | int cTicks;
|
---|
54 |
|
---|
55 | /*
|
---|
56 | * 0 ms sleep -> yield.
|
---|
57 | */
|
---|
58 | if (!cMillies)
|
---|
59 | {
|
---|
60 | RTThreadYield();
|
---|
61 | return VINF_SUCCESS;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * Translate milliseconds into ticks and go to sleep.
|
---|
66 | */
|
---|
67 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
68 | {
|
---|
69 | if (hz == 1000)
|
---|
70 | cTicks = cMillies;
|
---|
71 | else if (hz == 100)
|
---|
72 | cTicks = cMillies / 10;
|
---|
73 | else
|
---|
74 | {
|
---|
75 | int64_t cTicks64 = ((uint64_t)cMillies * hz) / 1000;
|
---|
76 | cTicks = (int)cTicks64;
|
---|
77 | if (cTicks != cTicks64)
|
---|
78 | cTicks = INT_MAX;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | else
|
---|
82 | cTicks = 0; /* requires giant lock! */
|
---|
83 |
|
---|
84 | rc = tsleep((void *)RTThreadSleep,
|
---|
85 | PZERO | PCATCH,
|
---|
86 | "iprtsl", /* max 6 chars */
|
---|
87 | cTicks);
|
---|
88 | switch (rc)
|
---|
89 | {
|
---|
90 | case 0:
|
---|
91 | return VINF_SUCCESS;
|
---|
92 | case EWOULDBLOCK:
|
---|
93 | return VERR_TIMEOUT;
|
---|
94 | case EINTR:
|
---|
95 | case ERESTART:
|
---|
96 | return VERR_INTERRUPTED;
|
---|
97 | default:
|
---|
98 | AssertMsgFailed(("%d\n", rc));
|
---|
99 | return VERR_NO_TRANSLATION;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | RTDECL(bool) RTThreadYield(void)
|
---|
105 | {
|
---|
106 | uio_yield();
|
---|
107 | return false; /** @todo figure this one ... */
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
|
---|
112 | {
|
---|
113 | Assert(hThread == NIL_RTTHREAD);
|
---|
114 |
|
---|
115 | return curthread->td_critnest == 0
|
---|
116 | && ASMIntAreEnabled(); /** @todo is there a native freebsd function/macro for this? */
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
|
---|
121 | {
|
---|
122 | Assert(hThread == NIL_RTTHREAD);
|
---|
123 |
|
---|
124 | return curthread->td_owepreempt == 1;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
|
---|
129 | {
|
---|
130 | /* yes, RTThreadPreemptIsPending is reliable. */
|
---|
131 | return true;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | RTDECL(bool) RTThreadPreemptIsPossible(void)
|
---|
136 | {
|
---|
137 | /* yes, kernel preemption is possible. */
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
|
---|
143 | {
|
---|
144 | AssertPtr(pState);
|
---|
145 | Assert(pState->u32Reserved == 0);
|
---|
146 | pState->u32Reserved = 42;
|
---|
147 |
|
---|
148 | critical_enter();
|
---|
149 | RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
|
---|
154 | {
|
---|
155 | AssertPtr(pState);
|
---|
156 | Assert(pState->u32Reserved == 42);
|
---|
157 | pState->u32Reserved = 0;
|
---|
158 |
|
---|
159 | RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
|
---|
160 | critical_exit();
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
|
---|
165 | {
|
---|
166 | Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
|
---|
167 | /** @todo FreeBSD: Implement RTThreadIsInInterrupt. Required for guest
|
---|
168 | * additions! */
|
---|
169 | return !ASMIntAreEnabled();
|
---|
170 | }
|
---|
171 |
|
---|