VirtualBox

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

Last change on this file since 28800 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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