1 | /* $Id: thread-r0drv-darwin.cpp 1 1970-01-01 00:00:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * InnoTek Portable Runtime - Threads, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung 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 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #include "the-darwin-kernel.h"
|
---|
26 | #include <iprt/thread.h>
|
---|
27 | #include <iprt/err.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include "r0drv/thread-r0drv.h"
|
---|
30 |
|
---|
31 |
|
---|
32 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
33 | {
|
---|
34 | return (RTTHREAD)current_thread();
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | RTDECL(int) RTThreadSleep(unsigned cMillies)
|
---|
39 | {
|
---|
40 | uint64_t u64Deadline;
|
---|
41 | clock_interval_to_deadline(cMillies, kMillisecondScale, &u64Deadline);
|
---|
42 | clock_delay_until(u64Deadline);
|
---|
43 | return VINF_SUCCESS;
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | RTDECL(bool) RTThreadYield(void)
|
---|
48 | {
|
---|
49 | thread_block(THREAD_CONTINUE_NULL);
|
---|
50 | return true; /* this is fishy */
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | int rtThreadNativeSetPriority(RTTHREAD Thread, RTTHREADTYPE enmType)
|
---|
55 | {
|
---|
56 | /*
|
---|
57 | * Convert the priority type to scheduling policies.
|
---|
58 | */
|
---|
59 | bool fSetExtended = false;
|
---|
60 | thread_extended_policy Extended = { true };
|
---|
61 | bool fSetTimeContstraint = false;
|
---|
62 | thread_time_constraint_policy TimeConstraint = { 0, 0, 0, true };
|
---|
63 | thread_precedence_policy Precedence = { 0 };
|
---|
64 | switch (enmType)
|
---|
65 | {
|
---|
66 | case RTTHREADTYPE_INFREQUENT_POLLER:
|
---|
67 | Precedence.importance = 1;
|
---|
68 | break;
|
---|
69 |
|
---|
70 | case RTTHREADTYPE_EMULATION:
|
---|
71 | Precedence.importance = 30;
|
---|
72 | break;
|
---|
73 |
|
---|
74 | case RTTHREADTYPE_DEFAULT:
|
---|
75 | Precedence.importance = 31;
|
---|
76 | break;
|
---|
77 |
|
---|
78 | case RTTHREADTYPE_MSG_PUMP:
|
---|
79 | Precedence.importance = 34;
|
---|
80 | break;
|
---|
81 |
|
---|
82 | case RTTHREADTYPE_IO:
|
---|
83 | Precedence.importance = 98;
|
---|
84 | break;
|
---|
85 |
|
---|
86 | case RTTHREADTYPE_TIMER:
|
---|
87 | Precedence.importance = 0x7fffffff;
|
---|
88 |
|
---|
89 | fSetExtended = true;
|
---|
90 | Extended.timeshare = FALSE;
|
---|
91 |
|
---|
92 | fSetTimeContstraint = true;
|
---|
93 | TimeConstraint.period = 0; /* not really true for a real timer thread, but we've really no idea. */
|
---|
94 | TimeConstraint.computation = rtDarwinAbsTimeFromNano(100000); /* 100 us*/
|
---|
95 | TimeConstraint.constraint = rtDarwinAbsTimeFromNano(500000); /* 500 us */
|
---|
96 | TimeConstraint.preemptible = FALSE;
|
---|
97 | break;
|
---|
98 |
|
---|
99 | default:
|
---|
100 | AssertMsgFailed(("enmType=%d\n", enmType));
|
---|
101 | return VERR_INVALID_PARAMETER;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Do the actual modification.
|
---|
106 | */
|
---|
107 | kern_return_t rc = thread_policy_set((thread_t)Thread, THREAD_PRECEDENCE_POLICY,
|
---|
108 | (thread_policy_t)&Precedence, THREAD_PRECEDENCE_POLICY_COUNT);
|
---|
109 | AssertMsg(rc == KERN_SUCCESS, ("%rc\n", rc)); NOREF(rc);
|
---|
110 |
|
---|
111 | if (fSetExtended)
|
---|
112 | {
|
---|
113 | rc = thread_policy_set((thread_t)Thread, THREAD_EXTENDED_POLICY,
|
---|
114 | (thread_policy_t)&Extended, THREAD_EXTENDED_POLICY_COUNT);
|
---|
115 | AssertMsg(rc == KERN_SUCCESS, ("%rc\n", rc));
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (fSetTimeContstraint)
|
---|
119 | {
|
---|
120 | rc = thread_policy_set((thread_t)Thread, THREAD_TIME_CONSTRAINT_POLICY,
|
---|
121 | (thread_policy_t)&TimeConstraint, THREAD_TIME_CONSTRAINT_POLICY_COUNT);
|
---|
122 | AssertMsg(rc == KERN_SUCCESS, ("%rc\n", rc));
|
---|
123 | }
|
---|
124 |
|
---|
125 | return VINF_SUCCESS; /* ignore any errors for now */
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Native kernel thread wrapper function.
|
---|
131 | *
|
---|
132 | * This will forward to rtThreadMain and do termination upon return.
|
---|
133 | *
|
---|
134 | * @param pvArg Pointer to the argument package.
|
---|
135 | * @param Ignored Wait result, which we ignore.
|
---|
136 | */
|
---|
137 | static void rtThreadNativeMain(void *pvArg, wait_result_t Ignored)
|
---|
138 | {
|
---|
139 | const thread_t Self = current_thread();
|
---|
140 |
|
---|
141 | rtThreadMain((RTNATIVETHREAD)Self, (PRTR0THREADARGS)pvArg);
|
---|
142 |
|
---|
143 | kern_return_t rc = thread_terminate(Self);
|
---|
144 | AssertFatalMsgFailed(("rc=%d\n", rc));
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | int rtThreadNativeCreate(PRTR0THREADARGS pArgs, PRTNATIVETHREAD pNativeThread)
|
---|
149 | {
|
---|
150 | thread_t NativeThread;
|
---|
151 | kern_return_t rc = kernel_thread_start(rtThreadNativeMain, pArgs, &NativeThread);
|
---|
152 | if (rc == KERN_SUCCESS)
|
---|
153 | {
|
---|
154 | *pNativeThread = (RTNATIVETHREAD)NativeThread;
|
---|
155 | thread_deallocate(NativeThread);
|
---|
156 | return VINF_SUCCESS;
|
---|
157 | }
|
---|
158 | return RTErrConvertFromMachKernReturn(rc);
|
---|
159 | }
|
---|
160 |
|
---|