1 | /* $Id: thread2-r0drv-darwin.cpp 4781 2007-09-13 19:07:42Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Threads (Part 2), Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #include "the-darwin-kernel.h"
|
---|
22 | #include <iprt/thread.h>
|
---|
23 | #include <iprt/err.h>
|
---|
24 | #include <iprt/assert.h>
|
---|
25 | #include "internal/thread.h"
|
---|
26 |
|
---|
27 |
|
---|
28 | int rtThreadNativeInit(void)
|
---|
29 | {
|
---|
30 | /* No TLS in Ring-0. :-/ */
|
---|
31 | return VINF_SUCCESS;
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
36 | {
|
---|
37 | return rtThreadGetByNative((RTNATIVETHREAD)current_thread());
|
---|
38 | }
|
---|
39 |
|
---|
40 |
|
---|
41 | int rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
|
---|
42 | {
|
---|
43 | /*
|
---|
44 | * Convert the priority type to scheduling policies.
|
---|
45 | * (This is really just guess work.)
|
---|
46 | */
|
---|
47 | bool fSetExtended = false;
|
---|
48 | thread_extended_policy Extended = { true };
|
---|
49 | bool fSetTimeContstraint = false;
|
---|
50 | thread_time_constraint_policy TimeConstraint = { 0, 0, 0, true };
|
---|
51 | thread_precedence_policy Precedence = { 0 };
|
---|
52 | switch (enmType)
|
---|
53 | {
|
---|
54 | case RTTHREADTYPE_INFREQUENT_POLLER:
|
---|
55 | Precedence.importance = 1;
|
---|
56 | break;
|
---|
57 |
|
---|
58 | case RTTHREADTYPE_EMULATION:
|
---|
59 | Precedence.importance = 30;
|
---|
60 | break;
|
---|
61 |
|
---|
62 | case RTTHREADTYPE_DEFAULT:
|
---|
63 | Precedence.importance = 31;
|
---|
64 | break;
|
---|
65 |
|
---|
66 | case RTTHREADTYPE_MSG_PUMP:
|
---|
67 | Precedence.importance = 34;
|
---|
68 | break;
|
---|
69 |
|
---|
70 | case RTTHREADTYPE_IO:
|
---|
71 | Precedence.importance = 98;
|
---|
72 | break;
|
---|
73 |
|
---|
74 | case RTTHREADTYPE_TIMER:
|
---|
75 | Precedence.importance = 0x7fffffff;
|
---|
76 |
|
---|
77 | fSetExtended = true;
|
---|
78 | Extended.timeshare = FALSE;
|
---|
79 |
|
---|
80 | fSetTimeContstraint = true;
|
---|
81 | TimeConstraint.period = 0; /* not really true for a real timer thread, but we've really no idea. */
|
---|
82 | TimeConstraint.computation = rtDarwinAbsTimeFromNano(100000); /* 100 us*/
|
---|
83 | TimeConstraint.constraint = rtDarwinAbsTimeFromNano(500000); /* 500 us */
|
---|
84 | TimeConstraint.preemptible = FALSE;
|
---|
85 | break;
|
---|
86 |
|
---|
87 | default:
|
---|
88 | AssertMsgFailed(("enmType=%d\n", enmType));
|
---|
89 | return VERR_INVALID_PARAMETER;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Do the actual modification.
|
---|
94 | */
|
---|
95 | kern_return_t kr = thread_policy_set((thread_t)pThread->Core.Key, THREAD_PRECEDENCE_POLICY,
|
---|
96 | (thread_policy_t)&Precedence, THREAD_PRECEDENCE_POLICY_COUNT);
|
---|
97 | AssertMsg(kr == KERN_SUCCESS, ("%rc\n", kr)); NOREF(kr);
|
---|
98 |
|
---|
99 | if (fSetExtended)
|
---|
100 | {
|
---|
101 | kr = thread_policy_set((thread_t)pThread->Core.Key, THREAD_EXTENDED_POLICY,
|
---|
102 | (thread_policy_t)&Extended, THREAD_EXTENDED_POLICY_COUNT);
|
---|
103 | AssertMsg(kr == KERN_SUCCESS, ("%rc\n", kr));
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (fSetTimeContstraint)
|
---|
107 | {
|
---|
108 | kr = thread_policy_set((thread_t)pThread->Core.Key, THREAD_TIME_CONSTRAINT_POLICY,
|
---|
109 | (thread_policy_t)&TimeConstraint, THREAD_TIME_CONSTRAINT_POLICY_COUNT);
|
---|
110 | AssertMsg(kr == KERN_SUCCESS, ("%rc\n", kr));
|
---|
111 | }
|
---|
112 |
|
---|
113 | return VINF_SUCCESS; /* ignore any errors for now */
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | int rtThreadNativeAdopt(PRTTHREADINT pThread)
|
---|
118 | {
|
---|
119 | return VERR_NOT_IMPLEMENTED;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Native kernel thread wrapper function.
|
---|
125 | *
|
---|
126 | * This will forward to rtThreadMain and do termination upon return.
|
---|
127 | *
|
---|
128 | * @param pvArg Pointer to the argument package.
|
---|
129 | * @param Ignored Wait result, which we ignore.
|
---|
130 | */
|
---|
131 | static void rtThreadNativeMain(void *pvArg, wait_result_t Ignored)
|
---|
132 | {
|
---|
133 | const thread_t Self = current_thread();
|
---|
134 | PRTTHREADINT pThread = (PRTTHREADINT)pvArg;
|
---|
135 |
|
---|
136 | rtThreadMain(pThread, (RTNATIVETHREAD)Self, &pThread->szName[0]);
|
---|
137 |
|
---|
138 | kern_return_t kr = thread_terminate(Self);
|
---|
139 | AssertFatalMsgFailed(("kr=%d\n", kr));
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | int rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread)
|
---|
144 | {
|
---|
145 | thread_t NativeThread;
|
---|
146 | kern_return_t kr = kernel_thread_start(rtThreadNativeMain, pThreadInt, &NativeThread);
|
---|
147 | if (kr == KERN_SUCCESS)
|
---|
148 | {
|
---|
149 | *pNativeThread = (RTNATIVETHREAD)NativeThread;
|
---|
150 | thread_deallocate(NativeThread);
|
---|
151 | return VINF_SUCCESS;
|
---|
152 | }
|
---|
153 | return RTErrConvertFromMachKernReturn(kr);
|
---|
154 | }
|
---|
155 |
|
---|