VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/init.cpp@ 11822

Last change on this file since 11822 was 11822, checked in by vboxsync, 16 years ago

IPRT: RTR3Init cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.2 KB
Line 
1/* $Id: init.cpp 11822 2008-08-29 14:21:03Z vboxsync $ */
2/** @file
3 * IPRT - Init Ring-3.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#define LOG_GROUP RTLOGGROUP_DEFAULT
37#ifdef RT_OS_WINDOWS
38# include <process.h>
39#else
40# include <unistd.h>
41#endif
42
43#include <iprt/runtime.h>
44#include <iprt/path.h>
45#include <iprt/assert.h>
46#include <iprt/log.h>
47#include <iprt/time.h>
48#include <iprt/err.h>
49#include <iprt/string.h>
50#include <iprt/param.h>
51#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
52# include <iprt/file.h>
53# include <VBox/sup.h>
54# include <stdlib.h>
55#endif
56#include "internal/path.h"
57#include "internal/process.h"
58#include "internal/thread.h"
59#include "internal/thread.h"
60#include "internal/time.h"
61
62
63/*******************************************************************************
64* Global Variables *
65*******************************************************************************/
66/** The number of calls to RTR3Init. */
67static int32_t volatile g_cUsers = 0;
68/** Whether we're currently initializing the IPRT. */
69static bool volatile g_fInitializing = false;
70
71/** Program path.
72 * The size is hardcoded, so we'll have to check for overflow when setting it
73 * since some hosts might support longer paths.
74 * @internal
75 */
76char g_szrtProgramPath[RTPATH_MAX];
77
78/**
79 * Program start nanosecond TS.
80 */
81uint64_t g_u64ProgramStartNanoTS;
82
83/**
84 * Program start microsecond TS.
85 */
86uint64_t g_u64ProgramStartMicroTS;
87
88/**
89 * Program start millisecond TS.
90 */
91uint64_t g_u64ProgramStartMilliTS;
92
93/**
94 * The process identifier of the running process.
95 */
96RTPROCESS g_ProcessSelf = NIL_RTPROCESS;
97
98/**
99 * The current process priority.
100 */
101RTPROCPRIORITY g_enmProcessPriority = RTPROCPRIORITY_DEFAULT;
102
103
104/**
105 * Internal initialization worker.
106 *
107 * @returns IPRT status code.
108 * @param fInitSUPLib Whether to call SUPR3Init.
109 * @param pszProgramPath The program path, NULL if not specified.
110 */
111static int rtR3Init(bool fInitSUPLib, const char *pszProgramPath)
112{
113 /* no entry log flow, because prefixes and thread may freak out. */
114
115 /*
116 * Do reference counting, only initialize the first time around.
117 *
118 * We are ASSUMING that nobody will be able to race RTR3Init calls when the
119 * first one, the real init, is running (second assertion).
120 */
121 int32_t cUsers = ASMAtomicIncS32(&g_cUsers);
122 if (cUsers != 1)
123 {
124 AssertMsg(cUsers > 1, ("%d\n", cUsers));
125 Assert(!g_fInitializing);
126#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
127 if (fInitSUPLib)
128 SUPR3Init(NULL);
129#endif
130 }
131 ASMAtomicWriteBool(&g_fInitializing, true);
132
133#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
134# ifdef VBOX
135 /*
136 * This MUST be done as the very first thing, before any file is opened.
137 * The log is opened on demand, but the first log entries may be caused
138 * by rtThreadInit() below.
139 */
140 const char *pszDisableHostCache = getenv("VBOX_DISABLE_HOST_DISK_CACHE");
141 if ( pszDisableHostCache != NULL
142 && strlen(pszDisableHostCache) > 0
143 && strcmp(pszDisableHostCache, "0") != 0)
144 {
145 RTFileSetForceFlags(RTFILE_O_WRITE, RTFILE_O_WRITE_THROUGH, 0);
146 RTFileSetForceFlags(RTFILE_O_READWRITE, RTFILE_O_WRITE_THROUGH, 0);
147 }
148# endif /* VBOX */
149#endif /* !IN_GUEST && !RT_NO_GIP */
150
151 /*
152 * Thread Thread database and adopt the caller thread as 'main'.
153 * This must be done before everything else or else we'll call into threading
154 * without having initialized TLS entries and suchlike.
155 */
156 int rc = rtThreadInit();
157 if (RT_FAILURE(rc))
158 {
159 AssertMsgFailed(("Failed to get executable directory path, rc=%d!\n", rc));
160 ASMAtomicWriteBool(&g_fInitializing, false);
161 ASMAtomicDecS32(&g_cUsers);
162 return rc;
163 }
164
165#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
166 if (fInitSUPLib)
167 {
168 /*
169 * Init GIP first.
170 * (The more time for updates before real use, the better.)
171 */
172 SUPR3Init(NULL);
173 }
174#endif
175
176 /*
177 * Init the program start TSes.
178 */
179 g_u64ProgramStartNanoTS = RTTimeNanoTS();
180 g_u64ProgramStartMicroTS = g_u64ProgramStartNanoTS / 1000;
181 g_u64ProgramStartMilliTS = g_u64ProgramStartNanoTS / 1000000;
182
183#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
184 /*
185 * The threading is initialized we can safely sleep a bit if GIP
186 * needs some time to update itself updating.
187 */
188 if (fInitSUPLib && g_pSUPGlobalInfoPage)
189 {
190 RTThreadSleep(20);
191 RTTimeNanoTS();
192 }
193#endif
194
195 /*
196 * Get the executable path.
197 *
198 * We're also checking the depth here since we'll be
199 * appending filenames to the executable path. Currently
200 * we assume 16 bytes are what we need.
201 */
202 char szPath[RTPATH_MAX - 16];
203 rc = RTPathProgram(szPath, sizeof(szPath));
204 if (RT_FAILURE(rc))
205 {
206 AssertMsgFailed(("Failed to get executable directory path, rc=%d!\n", rc));
207 ASMAtomicWriteBool(&g_fInitializing, false);
208 ASMAtomicDecS32(&g_cUsers);
209 return rc;
210 }
211
212 /*
213 * The Process ID.
214 */
215#ifdef _MSC_VER
216 g_ProcessSelf = _getpid(); /* crappy ansi compiler */
217#else
218 g_ProcessSelf = getpid();
219#endif
220
221 /*
222 * More stuff to come.
223 */
224
225 LogFlow(("RTR3Init: returns VINF_SUCCESS\n"));
226 ASMAtomicWriteBool(&g_fInitializing, false);
227 return VINF_SUCCESS;
228}
229
230
231RTR3DECL(int) RTR3Init(void)
232{
233 return rtR3Init(false /* fInitSUPLib */, NULL);
234}
235
236
237RTR3DECL(int) RTR3InitWithProgramPath(const char *pszProgramPath)
238{
239 return rtR3Init(false /* fInitSUPLib */, pszProgramPath);
240}
241
242
243RTR3DECL(int) RTR3InitAndSUPLib(void)
244{
245 return rtR3Init(true /* fInitSUPLib */, NULL /* pszProgramPath */);
246}
247
248
249RTR3DECL(int) RTR3InitAndSUPLibWithProgramPath(const char *pszProgramPath)
250{
251 return rtR3Init(true /* fInitSUPLib */, pszProgramPath);
252}
253
254
255#if 0 /** @todo implement RTR3Term. */
256RTR3DECL(void) RTR3Term(void)
257{
258}
259#endif
260
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