initterm-r0drv.cpp revision 3400a1195003605b6899c61cd77ea05053a0db48
0N/A/* $Id$ */
0N/A/** @file
0N/A * IPRT - Initialization & Termination, R0 Driver, Common.
0N/A */
0N/A
0N/A/*
0N/A * Copyright (C) 2006-2007 Sun Microsystems, Inc.
0N/A *
0N/A * This file is part of VirtualBox Open Source Edition (OSE), as
0N/A * available from http://www.virtualbox.org. This file is free software;
0N/A * you can redistribute it and/or modify it under the terms of the GNU
0N/A * General Public License (GPL) as published by the Free Software
0N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
0N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
0N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
0N/A *
0N/A * The contents of this file may alternatively be used under the terms
0N/A * of the Common Development and Distribution License Version 1.0
0N/A * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
0N/A * VirtualBox OSE distribution, in which case the provisions of the
0N/A * CDDL are applicable instead of those of the GPL.
0N/A *
0N/A * You may elect to license modified versions of this file under the
0N/A * terms and conditions of either the GPL or the CDDL or both.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0N/A * Clara, CA 95054 USA or visit http://www.sun.com if you need
0N/A * additional information or have any questions.
0N/A */
0N/A
0N/A
0N/A/*******************************************************************************
0N/A* Header Files *
0N/A*******************************************************************************/
0N/A#include <iprt/initterm.h>
0N/A#include <iprt/asm.h>
0N/A#include <iprt/assert.h>
0N/A#include <iprt/err.h>
0N/A#ifndef IN_GUEST /* play safe for now */
0N/A# include "r0drv/mp-r0drv.h"
0N/A# include "r0drv/power-r0drv.h"
0N/A#endif
0N/A
0N/A#include "internal/initterm.h"
0N/A#include "internal/thread.h"
0N/A
0N/A
0N/A/*******************************************************************************
0N/A* Global Variables *
0N/A*******************************************************************************/
0N/A/** Count of current IPRT users.
0N/A * In ring-0 several drivers / kmods / kexts / wossnames may share the
0N/A * same runtime code. So, we need to keep count in order not to terminate
0N/A * it prematurely. */
0N/Astatic int32_t volatile g_crtR0Users = 0;
0N/A
0N/A
0N/A/**
0N/A * Initalizes the ring-0 driver runtime library.
0N/A *
0N/A * @returns iprt status code.
0N/A * @param fReserved Flags reserved for the future.
0N/A */
0N/ARTR0DECL(int) RTR0Init(unsigned fReserved)
0N/A{
0N/A int rc;
0N/A Assert(fReserved == 0);
0N/A
0N/A /*
0N/A * The first user initializes it.
0N/A * We rely on the module loader to ensure that there are no
0N/A * initialization races should two modules share the IPRT.
0N/A */
0N/A if (ASMAtomicIncS32(&g_crtR0Users) != 1)
0N/A return VINF_SUCCESS;
0N/A
0N/A rc = rtR0InitNative();
0N/A if (RT_SUCCESS(rc))
0N/A {
0N/A#if !defined(RT_OS_LINUX)
0N/A rc = rtThreadInit();
0N/A#endif
0N/A if (RT_SUCCESS(rc))
0N/A {
0N/A#ifndef IN_GUEST /* play safe for now */
0N/A rc = rtR0MpNotificationInit();
0N/A if (RT_SUCCESS(rc))
0N/A rc = rtR0PowerNotificationInit();
0N/A#endif
0N/A if (RT_SUCCESS(rc))
0N/A return rc;
0N/A }
0N/A
0N/A rtR0TermNative();
0N/A }
0N/A return rc;
0N/A}
0N/A
0N/A
0N/A/**
0N/A * Terminates the ring-0 driver runtime library.
0N/A */
0N/ARTR0DECL(void) RTR0Term(void)
0N/A{
0N/A /*
0N/A * Last user does the cleanup.
0N/A */
0N/A int32_t cNewUsers = ASMAtomicDecS32(&g_crtR0Users);
0N/A Assert(cNewUsers >= 0);
0N/A if (cNewUsers != 0)
0N/A return;
0N/A
0N/A#if !defined(RT_OS_LINUX) && !defined(RT_OS_WINDOWS)
0N/A rtThreadTerm();
0N/A#endif
0N/A#ifndef IN_GUEST /* play safe for now */
0N/A rtR0PowerNotificationTerm();
0N/A rtR0MpNotificationTerm();
0N/A#endif
0N/A rtR0TermNative();
0N/A}
0N/A
0N/A