1 | /* $Id: thread2-r0drv-darwin.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 2), Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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-darwin-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
33 | #include <iprt/thread.h>
|
---|
34 |
|
---|
35 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
36 | # include <iprt/asm-amd64-x86.h>
|
---|
37 | #endif
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include "internal/thread.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | DECLHIDDEN(int) rtThreadNativeInit(void)
|
---|
44 | {
|
---|
45 | /* No TLS in Ring-0. :-/ */
|
---|
46 | return VINF_SUCCESS;
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
51 | {
|
---|
52 | return rtThreadGetByNative((RTNATIVETHREAD)current_thread());
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
|
---|
57 | {
|
---|
58 | /*
|
---|
59 | * Convert the priority type to scheduling policies.
|
---|
60 | * (This is really just guess work.)
|
---|
61 | */
|
---|
62 | bool fSetExtended = false;
|
---|
63 | thread_extended_policy Extended = { true };
|
---|
64 | bool fSetTimeContstraint = false;
|
---|
65 | thread_time_constraint_policy TimeConstraint = { 0, 0, 0, true };
|
---|
66 | thread_precedence_policy Precedence = { 0 };
|
---|
67 | switch (enmType)
|
---|
68 | {
|
---|
69 | case RTTHREADTYPE_INFREQUENT_POLLER:
|
---|
70 | Precedence.importance = 1;
|
---|
71 | break;
|
---|
72 |
|
---|
73 | case RTTHREADTYPE_EMULATION:
|
---|
74 | Precedence.importance = 30;
|
---|
75 | break;
|
---|
76 |
|
---|
77 | case RTTHREADTYPE_DEFAULT:
|
---|
78 | Precedence.importance = 31;
|
---|
79 | break;
|
---|
80 |
|
---|
81 | case RTTHREADTYPE_MSG_PUMP:
|
---|
82 | Precedence.importance = 34;
|
---|
83 | break;
|
---|
84 |
|
---|
85 | case RTTHREADTYPE_IO:
|
---|
86 | Precedence.importance = 98;
|
---|
87 | break;
|
---|
88 |
|
---|
89 | case RTTHREADTYPE_TIMER:
|
---|
90 | Precedence.importance = 0x7fffffff;
|
---|
91 |
|
---|
92 | fSetExtended = true;
|
---|
93 | Extended.timeshare = FALSE;
|
---|
94 |
|
---|
95 | fSetTimeContstraint = true;
|
---|
96 | TimeConstraint.period = 0; /* not really true for a real timer thread, but we've really no idea. */
|
---|
97 | TimeConstraint.computation = rtDarwinAbsTimeFromNano(100000); /* 100 us*/
|
---|
98 | TimeConstraint.constraint = rtDarwinAbsTimeFromNano(500000); /* 500 us */
|
---|
99 | TimeConstraint.preemptible = FALSE;
|
---|
100 | break;
|
---|
101 |
|
---|
102 | default:
|
---|
103 | AssertMsgFailed(("enmType=%d\n", enmType));
|
---|
104 | return VERR_INVALID_PARAMETER;
|
---|
105 | }
|
---|
106 | RT_ASSERT_INTS_ON();
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * Do the actual modification.
|
---|
110 | */
|
---|
111 | kern_return_t kr = thread_policy_set((thread_t)pThread->Core.Key, THREAD_PRECEDENCE_POLICY,
|
---|
112 | (thread_policy_t)&Precedence, THREAD_PRECEDENCE_POLICY_COUNT);
|
---|
113 | AssertMsg(kr == KERN_SUCCESS, ("%rc\n", kr)); NOREF(kr);
|
---|
114 |
|
---|
115 | if (fSetExtended)
|
---|
116 | {
|
---|
117 | kr = thread_policy_set((thread_t)pThread->Core.Key, THREAD_EXTENDED_POLICY,
|
---|
118 | (thread_policy_t)&Extended, THREAD_EXTENDED_POLICY_COUNT);
|
---|
119 | AssertMsg(kr == KERN_SUCCESS, ("%rc\n", kr));
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (fSetTimeContstraint)
|
---|
123 | {
|
---|
124 | kr = thread_policy_set((thread_t)pThread->Core.Key, THREAD_TIME_CONSTRAINT_POLICY,
|
---|
125 | (thread_policy_t)&TimeConstraint, THREAD_TIME_CONSTRAINT_POLICY_COUNT);
|
---|
126 | AssertMsg(kr == KERN_SUCCESS, ("%rc\n", kr));
|
---|
127 | }
|
---|
128 |
|
---|
129 | return VINF_SUCCESS; /* ignore any errors for now */
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread)
|
---|
134 | {
|
---|
135 | return VERR_NOT_IMPLEMENTED;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | DECLHIDDEN(void) rtThreadNativeWaitKludge(PRTTHREADINT pThread)
|
---|
140 | {
|
---|
141 | /** @todo fix RTThreadWait/RTR0Term race on darwin. */
|
---|
142 | RTThreadSleep(1);
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread)
|
---|
147 | {
|
---|
148 | NOREF(pThread);
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Native kernel thread wrapper function.
|
---|
154 | *
|
---|
155 | * This will forward to rtThreadMain and do termination upon return.
|
---|
156 | *
|
---|
157 | * @param pvArg Pointer to the argument package.
|
---|
158 | * @param Ignored Wait result, which we ignore.
|
---|
159 | */
|
---|
160 | static void rtThreadNativeMain(void *pvArg, wait_result_t Ignored)
|
---|
161 | {
|
---|
162 | const thread_t Self = current_thread();
|
---|
163 | PRTTHREADINT pThread = (PRTTHREADINT)pvArg;
|
---|
164 |
|
---|
165 | rtThreadMain(pThread, (RTNATIVETHREAD)Self, &pThread->szName[0]);
|
---|
166 |
|
---|
167 | kern_return_t kr = thread_terminate(Self);
|
---|
168 | AssertFatalMsgFailed(("kr=%d\n", kr));
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread)
|
---|
173 | {
|
---|
174 | RT_ASSERT_PREEMPTIBLE();
|
---|
175 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
176 |
|
---|
177 | thread_t NativeThread;
|
---|
178 | kern_return_t kr = kernel_thread_start(rtThreadNativeMain, pThreadInt, &NativeThread);
|
---|
179 | if (kr == KERN_SUCCESS)
|
---|
180 | {
|
---|
181 | *pNativeThread = (RTNATIVETHREAD)NativeThread;
|
---|
182 | thread_deallocate(NativeThread);
|
---|
183 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
184 | return VINF_SUCCESS;
|
---|
185 | }
|
---|
186 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
187 | return RTErrConvertFromMachKernReturn(kr);
|
---|
188 | }
|
---|
189 |
|
---|