VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/thread2-r0drv-darwin.cpp

Last change on this file was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.3 KB
Line 
1/* $Id: thread2-r0drv-darwin.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - Threads (Part 2), Ring-0 Driver, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "the-darwin-kernel.h"
42#include "internal/iprt.h"
43#include <iprt/thread.h>
44
45#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
46# include <iprt/asm-amd64-x86.h>
47#endif
48#include <iprt/assert.h>
49#include <iprt/errcore.h>
50#include "internal/thread.h"
51
52
53DECLHIDDEN(int) rtThreadNativeInit(void)
54{
55 /* No TLS in Ring-0. :-/ */
56 return VINF_SUCCESS;
57}
58
59
60RTDECL(RTTHREAD) RTThreadSelf(void)
61{
62 return rtThreadGetByNative((RTNATIVETHREAD)current_thread());
63}
64
65
66DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
67{
68 /*
69 * Convert the priority type to scheduling policies.
70 * (This is really just guess work.)
71 */
72 bool fSetExtended = false;
73 thread_extended_policy Extended = { true };
74 bool fSetTimeContstraint = false;
75 thread_time_constraint_policy TimeConstraint = { 0, 0, 0, true };
76 thread_precedence_policy Precedence = { 0 };
77 switch (enmType)
78 {
79 case RTTHREADTYPE_INFREQUENT_POLLER:
80 Precedence.importance = 1;
81 break;
82
83 case RTTHREADTYPE_EMULATION:
84 Precedence.importance = 30;
85 break;
86
87 case RTTHREADTYPE_DEFAULT:
88 Precedence.importance = 31;
89 break;
90
91 case RTTHREADTYPE_MSG_PUMP:
92 Precedence.importance = 34;
93 break;
94
95 case RTTHREADTYPE_IO:
96 Precedence.importance = 98;
97 break;
98
99 case RTTHREADTYPE_TIMER:
100 Precedence.importance = 0x7fffffff;
101
102 fSetExtended = true;
103 Extended.timeshare = FALSE;
104
105 fSetTimeContstraint = true;
106 TimeConstraint.period = 0; /* not really true for a real timer thread, but we've really no idea. */
107 TimeConstraint.computation = rtDarwinAbsTimeFromNano(100000); /* 100 us*/
108 TimeConstraint.constraint = rtDarwinAbsTimeFromNano(500000); /* 500 us */
109 TimeConstraint.preemptible = FALSE;
110 break;
111
112 default:
113 AssertMsgFailed(("enmType=%d\n", enmType));
114 return VERR_INVALID_PARAMETER;
115 }
116 RT_ASSERT_INTS_ON();
117
118 /*
119 * Do the actual modification.
120 */
121 kern_return_t kr = thread_policy_set((thread_t)pThread->Core.Key, THREAD_PRECEDENCE_POLICY,
122 (thread_policy_t)&Precedence, THREAD_PRECEDENCE_POLICY_COUNT);
123 AssertMsg(kr == KERN_SUCCESS, ("%rc\n", kr)); NOREF(kr);
124
125 if (fSetExtended)
126 {
127 kr = thread_policy_set((thread_t)pThread->Core.Key, THREAD_EXTENDED_POLICY,
128 (thread_policy_t)&Extended, THREAD_EXTENDED_POLICY_COUNT);
129 AssertMsg(kr == KERN_SUCCESS, ("%rc\n", kr));
130 }
131
132 if (fSetTimeContstraint)
133 {
134 kr = thread_policy_set((thread_t)pThread->Core.Key, THREAD_TIME_CONSTRAINT_POLICY,
135 (thread_policy_t)&TimeConstraint, THREAD_TIME_CONSTRAINT_POLICY_COUNT);
136 AssertMsg(kr == KERN_SUCCESS, ("%rc\n", kr));
137 }
138
139 return VINF_SUCCESS; /* ignore any errors for now */
140}
141
142
143DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread)
144{
145 RT_NOREF(pThread);
146 return VERR_NOT_IMPLEMENTED;
147}
148
149
150DECLHIDDEN(void) rtThreadNativeWaitKludge(PRTTHREADINT pThread)
151{
152 RT_NOREF(pThread);
153 /** @todo fix RTThreadWait/RTR0Term race on darwin. */
154 RTThreadSleep(1);
155}
156
157
158DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread)
159{
160 RT_NOREF(pThread);
161}
162
163
164/**
165 * Native kernel thread wrapper function.
166 *
167 * This will forward to rtThreadMain and do termination upon return.
168 *
169 * @param pvArg Pointer to the argument package.
170 * @param Ignored Wait result, which we ignore.
171 */
172static void rtThreadNativeMain(void *pvArg, wait_result_t Ignored)
173{
174 RT_NOREF(Ignored);
175 const thread_t Self = current_thread();
176 PRTTHREADINT pThread = (PRTTHREADINT)pvArg;
177
178 rtThreadMain(pThread, (RTNATIVETHREAD)Self, &pThread->szName[0]);
179
180 kern_return_t kr = thread_terminate(Self);
181 AssertFatalMsgFailed(("kr=%d\n", kr));
182}
183
184
185DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread)
186{
187 RT_ASSERT_PREEMPTIBLE();
188 IPRT_DARWIN_SAVE_EFL_AC();
189
190 thread_t NativeThread;
191 kern_return_t kr = kernel_thread_start(rtThreadNativeMain, pThreadInt, &NativeThread);
192 if (kr == KERN_SUCCESS)
193 {
194 *pNativeThread = (RTNATIVETHREAD)NativeThread;
195 thread_deallocate(NativeThread);
196 IPRT_DARWIN_RESTORE_EFL_AC();
197 return VINF_SUCCESS;
198 }
199 IPRT_DARWIN_RESTORE_EFL_AC();
200 return RTErrConvertFromMachKernReturn(kr);
201}
202
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