1 | /* $Id: thread2-r0drv-freebsd.c 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 2), Ring-0 Driver, FreeBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Contributed by knut st. osmundsen.
|
---|
8 | *
|
---|
9 | * Copyright (C) 2007-2023 Oracle and/or its affiliates.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox base platform packages, as
|
---|
12 | * available from https://www.virtualbox.org.
|
---|
13 | *
|
---|
14 | * This program is free software; you can redistribute it and/or
|
---|
15 | * modify it under the terms of the GNU General Public License
|
---|
16 | * as published by the Free Software Foundation, in version 3 of the
|
---|
17 | * License.
|
---|
18 | *
|
---|
19 | * This program is distributed in the hope that it will be useful, but
|
---|
20 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
22 | * General Public License for more details.
|
---|
23 | *
|
---|
24 | * You should have received a copy of the GNU General Public License
|
---|
25 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
26 | *
|
---|
27 | * The contents of this file may alternatively be used under the terms
|
---|
28 | * of the Common Development and Distribution License Version 1.0
|
---|
29 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
30 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
31 | * CDDL are applicable instead of those of the GPL.
|
---|
32 | *
|
---|
33 | * You may elect to license modified versions of this file under the
|
---|
34 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
35 | *
|
---|
36 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
37 | * --------------------------------------------------------------------
|
---|
38 | *
|
---|
39 | * This code is based on:
|
---|
40 | *
|
---|
41 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
42 | *
|
---|
43 | * Permission is hereby granted, free of charge, to any person
|
---|
44 | * obtaining a copy of this software and associated documentation
|
---|
45 | * files (the "Software"), to deal in the Software without
|
---|
46 | * restriction, including without limitation the rights to use,
|
---|
47 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
48 | * copies of the Software, and to permit persons to whom the
|
---|
49 | * Software is furnished to do so, subject to the following
|
---|
50 | * conditions:
|
---|
51 | *
|
---|
52 | * The above copyright notice and this permission notice shall be
|
---|
53 | * included in all copies or substantial portions of the Software.
|
---|
54 | *
|
---|
55 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
56 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
57 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
58 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
59 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
60 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
61 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
62 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
63 | */
|
---|
64 |
|
---|
65 |
|
---|
66 | /*********************************************************************************************************************************
|
---|
67 | * Header Files *
|
---|
68 | *********************************************************************************************************************************/
|
---|
69 | #include "the-freebsd-kernel.h"
|
---|
70 |
|
---|
71 | #include <iprt/thread.h>
|
---|
72 | #include <iprt/errcore.h>
|
---|
73 | #include <iprt/assert.h>
|
---|
74 |
|
---|
75 | #include "internal/thread.h"
|
---|
76 |
|
---|
77 |
|
---|
78 | DECLHIDDEN(int) rtThreadNativeInit(void)
|
---|
79 | {
|
---|
80 | return VINF_SUCCESS;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
85 | {
|
---|
86 | return rtThreadGetByNative(RTThreadNativeSelf());
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
|
---|
91 | {
|
---|
92 | int iPriority;
|
---|
93 |
|
---|
94 | switch (enmType)
|
---|
95 | {
|
---|
96 | case RTTHREADTYPE_INFREQUENT_POLLER: iPriority = PZERO + 8; break;
|
---|
97 | case RTTHREADTYPE_EMULATION: iPriority = PZERO + 4; break;
|
---|
98 | case RTTHREADTYPE_DEFAULT: iPriority = PZERO; break;
|
---|
99 | case RTTHREADTYPE_MSG_PUMP: iPriority = PZERO - 4; break;
|
---|
100 | case RTTHREADTYPE_IO: iPriority = PRIBIO; break;
|
---|
101 | case RTTHREADTYPE_TIMER: iPriority = PRI_MIN_KERN; break;
|
---|
102 | default:
|
---|
103 | AssertMsgFailed(("enmType=%d\n", enmType));
|
---|
104 | return VERR_INVALID_PARAMETER;
|
---|
105 | }
|
---|
106 |
|
---|
107 | #if __FreeBSD_version < 700000
|
---|
108 | /* Do like they're doing in subr_ntoskrnl.c... */
|
---|
109 | mtx_lock_spin(&sched_lock);
|
---|
110 | #else
|
---|
111 | thread_lock(curthread);
|
---|
112 | #endif
|
---|
113 | sched_prio(curthread, iPriority);
|
---|
114 | #if __FreeBSD_version < 600000
|
---|
115 | curthread->td_base_pri = iPriority;
|
---|
116 | #endif
|
---|
117 | #if __FreeBSD_version < 700000
|
---|
118 | mtx_unlock_spin(&sched_lock);
|
---|
119 | #else
|
---|
120 | thread_unlock(curthread);
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | return VINF_SUCCESS;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread)
|
---|
128 | {
|
---|
129 | NOREF(pThread);
|
---|
130 | /* There is nothing special that needs doing here, but the
|
---|
131 | user really better know what he's cooking. */
|
---|
132 | return VINF_SUCCESS;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | DECLHIDDEN(void) rtThreadNativeWaitKludge(PRTTHREADINT pThread)
|
---|
137 | {
|
---|
138 | /** @todo fix RTThreadWait/RTR0Term race on freebsd. */
|
---|
139 | RTThreadSleep(1);
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread)
|
---|
144 | {
|
---|
145 | NOREF(pThread);
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Native thread main function.
|
---|
151 | *
|
---|
152 | * @param pvThreadInt The thread structure.
|
---|
153 | */
|
---|
154 | static void rtThreadNativeMain(void *pvThreadInt)
|
---|
155 | {
|
---|
156 | const struct thread *Self = curthread;
|
---|
157 | PRTTHREADINT pThreadInt = (PRTTHREADINT)pvThreadInt;
|
---|
158 | int rc;
|
---|
159 |
|
---|
160 | rc = rtThreadMain(pThreadInt, (RTNATIVETHREAD)Self, &pThreadInt->szName[0]);
|
---|
161 |
|
---|
162 | #if __FreeBSD_version >= 800002
|
---|
163 | kproc_exit(rc);
|
---|
164 | #else
|
---|
165 | kthread_exit(rc);
|
---|
166 | #endif
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread)
|
---|
171 | {
|
---|
172 | int rc;
|
---|
173 | struct proc *pProc;
|
---|
174 |
|
---|
175 | #if __FreeBSD_version >= 800002
|
---|
176 | rc = kproc_create(rtThreadNativeMain, pThreadInt, &pProc, RFHIGHPID, 0, "%s", pThreadInt->szName);
|
---|
177 | #else
|
---|
178 | rc = kthread_create(rtThreadNativeMain, pThreadInt, &pProc, RFHIGHPID, 0, "%s", pThreadInt->szName);
|
---|
179 | #endif
|
---|
180 | if (!rc)
|
---|
181 | {
|
---|
182 | *pNativeThread = (RTNATIVETHREAD)FIRST_THREAD_IN_PROC(pProc);
|
---|
183 | rc = VINF_SUCCESS;
|
---|
184 | }
|
---|
185 | else
|
---|
186 | rc = RTErrConvertFromErrno(rc);
|
---|
187 | return rc;
|
---|
188 | }
|
---|
189 |
|
---|