semaphore-r0drv-darwin.cpp revision f4db180328f833f9fc9cb07a1a4a0bc948a47afe
/* $Id$ */
/** @file
* InnoTek Portable Runtime - Semaphores, Ring-0 Driver, Darwin.
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License as published by the Free Software Foundation,
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
* distribution. VirtualBox OSE is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY of any kind.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "the-darwin-kernel.h"
#include <iprt/semaphore.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Darwin event semaphore.
*/
typedef struct RTSEMEVENTINTERNAL
{
/** Magic value (RTSEMEVENT_MAGIC). */
/** The number of waiting threads. */
/** Set if the event object is signaled. */
/** The number of threads in the process of waking up. */
/** The spinlock protecting us. */
/** Magic for the Darwin event semaphore structure. (Neil Gaiman) */
#define RTSEMEVENT_MAGIC 0x19601110
/**
* Darwin multiple release event semaphore.
*/
typedef struct RTSEMEVENTMULTIINTERNAL
{
/** Magic value (RTSEMEVENTMULTI_MAGIC). */
/** The number of waiting threads. */
/** Set if the event object is signaled. */
/** The number of threads in the process of waking up. */
/** The spinlock protecting us. */
/** Magic for the Darwin multiple release event semaphore structure. (Isaac Asimov) */
#define RTSEMEVENTMULTI_MAGIC 0x19200102
#if 0 /** @todo */
/**
* Darwin mutex semaphore.
*/
typedef struct RTSEMMUTEXINTERNAL
{
/** Magic value (RTSEMMUTEX_MAGIC). */
/** The mutex. */
/** Magic for the Darwin mutex semaphore structure. (Douglas Adams) */
#define RTSEMMUTEX_MAGIC 0x19520311
#endif
/**
* Wrapper for the darwin semaphore structure.
*/
typedef struct RTSEMFASTMUTEXINTERNAL
{
/** Magic value (RTSEMFASTMUTEX_MAGIC). */
/** The mutex. */
/** Magic value for RTSEMFASTMUTEXINTERNAL::u32Magic (John Ronald Reuel Tolkien). */
#define RTSEMFASTMUTEX_MAGIC 0x18920102
{
Assert(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
if (pEventInt)
{
{
return VINF_SUCCESS;
}
}
return VERR_NO_MEMORY;
}
{
return VERR_INVALID_HANDLE;
{
/* abort waiting thread, last man cleans up. */
}
/* the last waking thread is gonna do the cleanup */
else
{
}
return VINF_SUCCESS;
}
{
{
}
else
return VINF_SUCCESS;
}
{
int rc;
{
rc = VINF_SUCCESS;
}
else
{
if (cMillies == RT_INDEFINITE_WAIT)
rcWait = lck_spin_sleep(pEventInt->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)pEventInt, fInterruptible);
else
{
u64AbsTime += mach_absolute_time();
}
switch (rcWait)
{
case THREAD_AWAKENED:
{
/* the event was destroyed after we woke up, as the last thread do the cleanup. */
return VINF_SUCCESS;
}
rc = VINF_SUCCESS;
break;
case THREAD_TIMED_OUT:
rc = VERR_TIMEOUT;
break;
case THREAD_INTERRUPTED:
break;
case THREAD_RESTART:
/* Last one out does the cleanup. */
{
return VERR_SEM_DESTROYED;
}
break;
default:
break;
}
}
return rc;
}
{
}
{
}
{
Assert(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pEventMultiInt));
if (pEventMultiInt)
{
pEventMultiInt->cWaiters = 0;
pEventMultiInt->cWaking = 0;
pEventMultiInt->fSignaled = 0;
if (pEventMultiInt->pSpinlock)
{
return VINF_SUCCESS;
}
pEventMultiInt->u32Magic = 0;
}
return VERR_NO_MEMORY;
}
{
return VERR_INVALID_HANDLE;
if (pEventMultiInt->cWaiters > 0)
{
/* abort waiting thread, last man cleans up. */
}
else if (pEventMultiInt->cWaking)
/* the last waking thread is gonna do the cleanup */
else
{
}
return VINF_SUCCESS;
}
{
if (pEventMultiInt->cWaiters > 0)
{
}
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
static int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, wait_interrupt_t fInterruptible)
{
int rc;
if (pEventMultiInt->fSignaled)
{
rc = VINF_SUCCESS;
}
else
{
if (cMillies == RT_INDEFINITE_WAIT)
rcWait = lck_spin_sleep(pEventMultiInt->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)pEventMultiInt, fInterruptible);
else
{
u64AbsTime += mach_absolute_time();
}
switch (rcWait)
{
case THREAD_AWAKENED:
{
/* the event was destroyed after we woke up, as the last thread do the cleanup. */
return VINF_SUCCESS;
}
rc = VINF_SUCCESS;
break;
case THREAD_TIMED_OUT:
rc = VERR_TIMEOUT;
break;
case THREAD_INTERRUPTED:
break;
case THREAD_RESTART:
/* Last one out does the cleanup. */
{
return VERR_SEM_DESTROYED;
}
break;
default:
break;
}
}
return rc;
}
{
}
{
}
#if 0 /* need proper timeout lock function! */
{
AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
if (pMutexInt)
{
{
return VINF_SUCCESS;
}
}
return VERR_NO_MEMORY;
}
{
/*
* Validate input.
*/
if (!pMutexInt)
return VERR_INVALID_PARAMETER;
/*
* Invalidate it and signal the object just in case.
*/
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
if (!pMutexInt)
return VERR_INVALID_PARAMETER;
/*
* Get the mutex.
*/
#if 1
#else
if (cMillies == RT_INDEFINITE_WAIT)
else
{
}
switch (rcNt)
{
case STATUS_SUCCESS:
return VINF_SUCCESS;
return VERR_SEM_DESTROYED;
case STATUS_ALERTED:
return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
case STATUS_USER_APC:
return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
case STATUS_TIMEOUT:
return VERR_TIMEOUT;
default:
AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p: wait returned %lx!\n",
return VERR_INTERNAL_ERROR;
}
#endif
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
if (!pMutexInt)
return VERR_INVALID_PARAMETER;
if ( !pMutexInt
{
AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
return VERR_INVALID_PARAMETER;
}
/*
* Release the mutex.
*/
#ifdef RT_USE_FAST_MUTEX
#else
#endif
return VINF_SUCCESS;
}
#endif /* later */
{
AssertCompile(sizeof(RTSEMFASTMUTEXINTERNAL) > sizeof(void *));
if (pFastInt)
{
{
return VINF_SUCCESS;
}
}
return VERR_NO_MEMORY;
}
{
return VERR_INVALID_PARAMETER;
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}