VirtualBox

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

Last change on this file since 1507 was 403, checked in by vboxsync, 18 years ago

Need RTThreadWait in ring-0 too when using the generic timers, so thread.cpp was ported to ring-0. Fixed a bug in RTTimerStart() (the generic code). (hope this doesn't break the other platforms...)

  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1/* $Id: thread2-r0drv-darwin.cpp 403 2007-01-28 08:45:05Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - Threads (Part 2), 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 "internal/thread.h"
30
31
32int rtThreadNativeInit(void)
33{
34 /* No TLS in Ring-0. :-/ */
35 return VINF_SUCCESS;
36}
37
38
39RTDECL(RTTHREAD) RTThreadSelf(void)
40{
41 return rtThreadGetByNative((RTNATIVETHREAD)current_thread());
42}
43
44
45int rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
46{
47 /*
48 * Convert the priority type to scheduling policies.
49 * (This is really just guess work.)
50 */
51 bool fSetExtended = false;
52 thread_extended_policy Extended = { true };
53 bool fSetTimeContstraint = false;
54 thread_time_constraint_policy TimeConstraint = { 0, 0, 0, true };
55 thread_precedence_policy Precedence = { 0 };
56 switch (enmType)
57 {
58 case RTTHREADTYPE_INFREQUENT_POLLER:
59 Precedence.importance = 1;
60 break;
61
62 case RTTHREADTYPE_EMULATION:
63 Precedence.importance = 30;
64 break;
65
66 case RTTHREADTYPE_DEFAULT:
67 Precedence.importance = 31;
68 break;
69
70 case RTTHREADTYPE_MSG_PUMP:
71 Precedence.importance = 34;
72 break;
73
74 case RTTHREADTYPE_IO:
75 Precedence.importance = 98;
76 break;
77
78 case RTTHREADTYPE_TIMER:
79 Precedence.importance = 0x7fffffff;
80
81 fSetExtended = true;
82 Extended.timeshare = FALSE;
83
84 fSetTimeContstraint = true;
85 TimeConstraint.period = 0; /* not really true for a real timer thread, but we've really no idea. */
86 TimeConstraint.computation = rtDarwinAbsTimeFromNano(100000); /* 100 us*/
87 TimeConstraint.constraint = rtDarwinAbsTimeFromNano(500000); /* 500 us */
88 TimeConstraint.preemptible = FALSE;
89 break;
90
91 default:
92 AssertMsgFailed(("enmType=%d\n", enmType));
93 return VERR_INVALID_PARAMETER;
94 }
95
96 /*
97 * Do the actual modification.
98 */
99 kern_return_t kr = thread_policy_set((thread_t)pThread->Core.Key, THREAD_PRECEDENCE_POLICY,
100 (thread_policy_t)&Precedence, THREAD_PRECEDENCE_POLICY_COUNT);
101 AssertMsg(kr == KERN_SUCCESS, ("%rc\n", kr)); NOREF(kr);
102
103 if (fSetExtended)
104 {
105 kr = thread_policy_set((thread_t)pThread->Core.Key, THREAD_EXTENDED_POLICY,
106 (thread_policy_t)&Extended, THREAD_EXTENDED_POLICY_COUNT);
107 AssertMsg(kr == KERN_SUCCESS, ("%rc\n", kr));
108 }
109
110 if (fSetTimeContstraint)
111 {
112 kr = thread_policy_set((thread_t)pThread->Core.Key, THREAD_TIME_CONSTRAINT_POLICY,
113 (thread_policy_t)&TimeConstraint, THREAD_TIME_CONSTRAINT_POLICY_COUNT);
114 AssertMsg(kr == KERN_SUCCESS, ("%rc\n", kr));
115 }
116
117 return VINF_SUCCESS; /* ignore any errors for now */
118}
119
120
121int rtThreadNativeAdopt(PRTTHREADINT pThread)
122{
123 return VERR_NOT_IMPLEMENTED;
124}
125
126
127/**
128 * Native kernel thread wrapper function.
129 *
130 * This will forward to rtThreadMain and do termination upon return.
131 *
132 * @param pvArg Pointer to the argument package.
133 * @param Ignored Wait result, which we ignore.
134 */
135static void rtThreadNativeMain(void *pvArg, wait_result_t Ignored)
136{
137 const thread_t Self = current_thread();
138
139 rtThreadMain((PRTTHREADINT)pvArg, (RTNATIVETHREAD)Self);
140
141 kern_return_t kr = thread_terminate(Self);
142 AssertFatalMsgFailed(("kr=%d\n", kr));
143}
144
145
146int rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread)
147{
148 thread_t NativeThread;
149 kern_return_t kr = kernel_thread_start(rtThreadNativeMain, pThreadInt, &NativeThread);
150 if (kr == KERN_SUCCESS)
151 {
152 *pNativeThread = (RTNATIVETHREAD)NativeThread;
153 thread_deallocate(NativeThread);
154 return VINF_SUCCESS;
155 }
156 return RTErrConvertFromMachKernReturn(kr);
157}
158
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