req.cpp revision 89ed8fe4cc94bfa1639d2101d5e076f2cae971dd
/* $Id: VMReq.cpp 17451 2007-01-15 14:08:28Z bird $ */
/** @file
* IPRT - Request packets
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* 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 (GPL) 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.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/semaphore.h>
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Create a request packet queueu
*
* @returns iprt status code.
* @param ppQueue Where to store the request queue pointer.
*/
{
if (!pQueue)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
return rc;
}
/**
* Destroy a request packet queueu
*
* @returns iprt status code.
* @param pQueue The request queue.
*/
{
/*
* Check input.
*/
if (!pQueue)
return VINF_SUCCESS;
return VINF_SUCCESS;
}
/**
* Process one or more request packets
*
* @returns iprt status code.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being added.
*
* @param pQueue The request queue.
* @param cMillies Number of milliseconds to wait for a pending request.
* Use RT_INDEFINITE_WAIT to only wait till one is added.
*/
{
/*
* Check input.
*/
if (!pQueue)
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
/*
* Process loop.
*
* We do not repeat the outer loop if we've got an informationtional status code
* since that code needs processing by our caller.
*/
int rc = VINF_SUCCESS;
while (rc <= VINF_SUCCESS)
{
/*
* Get pending requests.
*/
if (!pReqs)
{
ASMAtomicWriteBool(&pQueue->fBusy, false); /* this aint 100% perfect, but it's good enough for now... */
/** @todo We currently don't care if the entire time wasted here is larger than
* cMillies */
if (rc != VINF_SUCCESS)
break;
continue;
}
/*
* Reverse the list to process it in FIFO order.
*/
while (pReq)
{
}
/*
* Process the requests.
*/
while (pReqs)
{
/* Unchain the first request and advance the list. */
/* Process the request */
if (rc != VINF_SUCCESS)
break; /** @todo r=bird: we're dropping requests here! Add 2nd queue that can hold them. (will fix when writing a testcase) */
}
}
return rc;
}
/**
* Allocate and queue a call request.
*
* If it's desired to poll on the completion of the request set cMillies
* to 0 and use RTReqWait() to check for completation. In the other case
* use RT_INDEFINITE_WAIT.
* The returned request packet must be freed using RTReqFree().
*
* @returns iprt statuscode.
* Will not return VERR_INTERRUPTED.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pQueue The request queue.
* @param ppReq Where to store the pointer to the request.
* This will be NULL or a valid request pointer not matter what happens.
* @param cMillies Number of milliseconds to wait for the request to
* be completed. Use RT_INDEFINITE_WAIT to only
* wait till it's completed.
* @param pfnFunction Pointer to the function to call.
* @param cArgs Number of arguments following in the ellipsis.
* @param ... Function arguments.
*
* @remarks See remarks on RTReqCallV.
*/
RTDECL(int) RTReqCall(PRTREQQUEUE pQueue, PRTREQ *ppReq, unsigned cMillies, PFNRT pfnFunction, unsigned cArgs, ...)
{
return rc;
}
/**
* Allocate and queue a call request to a void function.
*
* If it's desired to poll on the completion of the request set cMillies
* to 0 and use RTReqWait() to check for completation. In the other case
* use RT_INDEFINITE_WAIT.
* The returned request packet must be freed using RTReqFree().
*
* @returns iprt status code.
* Will not return VERR_INTERRUPTED.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pQueue The request queue.
* @param ppReq Where to store the pointer to the request.
* This will be NULL or a valid request pointer not matter what happends.
* @param cMillies Number of milliseconds to wait for the request to
* be completed. Use RT_INDEFINITE_WAIT to only
* wait till it's completed.
* @param pfnFunction Pointer to the function to call.
* @param cArgs Number of arguments following in the ellipsis.
* @param ... Function arguments.
*
* @remarks See remarks on RTReqCallV.
*/
RTDECL(int) RTReqCallVoid(PRTREQQUEUE pQueue, PRTREQ *ppReq, unsigned cMillies, PFNRT pfnFunction, unsigned cArgs, ...)
{
return rc;
}
/**
* Allocate and queue a call request to a void function.
*
* If it's desired to poll on the completion of the request set cMillies
* to 0 and use RTReqWait() to check for completation. In the other case
* use RT_INDEFINITE_WAIT.
* The returned request packet must be freed using RTReqFree().
*
* @returns iprt status code.
* Will not return VERR_INTERRUPTED.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pQueue The request queue.
* @param ppReq Where to store the pointer to the request.
* This will be NULL or a valid request pointer not matter what happends, unless fFlags
* contains RTREQFLAGS_NO_WAIT when it will be optional and always NULL.
* @param cMillies Number of milliseconds to wait for the request to
* be completed. Use RT_INDEFINITE_WAIT to only
* wait till it's completed.
* @param fFlags A combination of the RTREQFLAGS values.
* @param pfnFunction Pointer to the function to call.
* @param cArgs Number of arguments following in the ellipsis.
* @param ... Function arguments.
*
* @remarks See remarks on RTReqCallV.
*/
RTDECL(int) RTReqCallEx(PRTREQQUEUE pQueue, PRTREQ *ppReq, unsigned cMillies, unsigned fFlags, PFNRT pfnFunction, unsigned cArgs, ...)
{
return rc;
}
/**
* Allocate and queue a call request.
*
* If it's desired to poll on the completion of the request set cMillies
* to 0 and use RTReqWait() to check for completation. In the other case
* use RT_INDEFINITE_WAIT.
* The returned request packet must be freed using RTReqFree().
*
* @returns iprt status code.
* Will not return VERR_INTERRUPTED.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pQueue The request queue.
* @param ppReq Where to store the pointer to the request.
* This will be NULL or a valid request pointer not matter what happends, unless fFlags
* contains RTREQFLAGS_NO_WAIT when it will be optional and always NULL.
* @param cMillies Number of milliseconds to wait for the request to
* be completed. Use RT_INDEFINITE_WAIT to only
* wait till it's completed.
* @param fFlags A combination of the RTREQFLAGS values.
* @param pfnFunction Pointer to the function to call.
* @param cArgs Number of arguments following in the ellipsis.
* @param Args Variable argument vector.
*
* @remarks Caveats:
* - Do not pass anything which is larger than an uintptr_t.
* - 64-bit integers are larger than uintptr_t on 32-bit hosts.
* Pass integers > 32-bit by reference (pointers).
* - Don't use NULL since it should be the integer 0 in C++ and may
* therefore end up with garbage in the bits 63:32 on 64-bit
* hosts because 'int' is 32-bit.
* Use (void *)NULL or (uintptr_t)0 instead of NULL.
*/
RTDECL(int) RTReqCallV(PRTREQQUEUE pQueue, PRTREQ *ppReq, unsigned cMillies, unsigned fFlags, PFNRT pfnFunction, unsigned cArgs, va_list Args)
{
LogFlow(("RTReqCallV: cMillies=%d fFlags=%#x pfnFunction=%p cArgs=%d\n", cMillies, fFlags, pfnFunction, cArgs));
/*
* Check input.
*/
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
{
}
{
return VERR_TOO_MUCH_DATA;
}
/*
* Allocate request
*/
if (rc != VINF_SUCCESS)
return rc;
/*
* Initialize the request data.
*/
/*
* Queue the request and return.
*/
if ( rc != VINF_SUCCESS
&& rc != VERR_TIMEOUT)
{
}
if (!(fFlags & RTREQFLAGS_NO_WAIT))
{
}
else
return rc;
}
/**
* Joins the list pList with whatever is linked up at *pHead.
*/
{
for (unsigned cIterations = 0;; cIterations++)
{
if (!pHead)
return;
return;
return;
}
}
/**
* Joins the list pList with whatever is linked up at *pHead.
*/
{
/*
* Split the list if it's too long.
*/
unsigned cReqs = 1;
{
if (cReqs++ > 25)
{
vmr3ReqJoinFreeSub(&pQueue->apReqFree[(i + 2 + (i == pQueue->iReqFree)) % RT_ELEMENTS(pQueue->apReqFree)], pTail->pNext);
return;
}
}
vmr3ReqJoinFreeSub(&pQueue->apReqFree[(pQueue->iReqFree + 2) % RT_ELEMENTS(pQueue->apReqFree)], pList);
}
/**
* Allocates a request packet.
*
* The caller allocates a request packet, fills in the request data
* union and queues the request.
*
* @returns iprt status code.
*
* @param pQueue The request queue.
* @param ppReq Where to store the pointer to the allocated packet.
* @param enmType Package type.
*/
{
/*
* Validate input.
*/
if ( enmType < RTREQTYPE_INVALID
|| enmType > RTREQTYPE_MAX)
{
AssertMsgFailed(("Invalid package type %d valid range %d-%d inclusivly.\n",
return VERR_RT_REQUEST_INVALID_TYPE;
}
/*
* Try get a recycled packet.
* While this could all be solved with a single list with a lock, it's a sport
* of mine to avoid locks.
*/
while (--cTries >= 0)
{
PRTREQ volatile *ppHead = &pQueue->apReqFree[ASMAtomicIncU32(&pQueue->iReqFree) % RT_ELEMENTS(pQueue->apReqFree)];
#if 0 /* sad, but this won't work safely because the reading of pReq->pNext. */
if ( pReq
if (pReq)
{
#else
if (pReq)
{
if ( pNext
{
}
#endif
/*
* Make sure the event sem is not signaled.
*/
if (!pReq->fEventSemClear)
{
{
/*
* This shall not happen, but if it does we'll just destroy
* the semaphore and create a new one.
*/
if (rc != VINF_SUCCESS)
return rc;
}
pReq->fEventSemClear = true;
}
else
/*
* Initialize the packet and return it.
*/
return VINF_SUCCESS;
}
}
/*
* Ok allocate one.
*/
if (!pReq)
return VERR_NO_MEMORY;
/*
* Create the semaphore.
*/
if (rc != VINF_SUCCESS)
{
return rc;
}
/*
* Initialize the packet and return it.
*/
pReq->fEventSemClear = true;
return VINF_SUCCESS;
}
/**
* Free a request packet.
*
* @returns iprt status code.
*
* @param pReq Package to free.
* @remark The request packet must be in allocated or completed state!
*/
{
/*
* Ignore NULL (all free functions should do this imho).
*/
if (!pReq)
return VINF_SUCCESS;
/*
* Check packet state.
*/
{
case RTREQSTATE_ALLOCATED:
case RTREQSTATE_COMPLETED:
break;
default:
return VERR_RT_REQUEST_STATE;
}
/*
* Make it a free packet and put it into one of the free packet lists.
*/
{
PRTREQ volatile *ppHead = &pQueue->apReqFree[ASMAtomicIncU32(&pQueue->iReqFree) % RT_ELEMENTS(pQueue->apReqFree)];
do
{
}
else
{
}
return VINF_SUCCESS;
}
/**
* Queue a request.
*
* The quest must be allocated using RTReqAlloc() and contain
* all the required data.
* If it's disired to poll on the completion of the request set cMillies
* to 0 and use RTReqWait() to check for completation. In the other case
* use RT_INDEFINITE_WAIT.
*
* @returns iprt status code.
* Will not return VERR_INTERRUPTED.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pReq The request to queue.
* @param cMillies Number of milliseconds to wait for the request to
* be completed. Use RT_INDEFINITE_WAIT to only
* wait till it's completed.
*/
{
/*
* Verify the supplied package.
*/
{
return VERR_RT_REQUEST_STATE;
}
{
AssertMsgFailed(("Invalid request package! Anyone cooking their own packages???\n"));
return VERR_RT_REQUEST_INVALID_PACKAGE;
}
{
AssertMsgFailed(("Invalid package type %d valid range %d-%d inclusivly. This was verified on alloc too...\n",
return VERR_RT_REQUEST_INVALID_TYPE;
}
int rc = VINF_SUCCESS;
/*
* Insert it.
*/
do
{
/*
* Notify queue thread.
*/
/*
* Wait and return.
*/
if (!(fFlags & RTREQFLAGS_NO_WAIT))
return rc;
}
/**
* Wait for a request to be completed.
*
* @returns iprt status code.
* Will not return VERR_INTERRUPTED.
* @returns VERR_TIMEOUT if cMillies was reached without the packet being completed.
*
* @param pReq The request to wait for.
* @param cMillies Number of milliseconds to wait.
* Use RT_INDEFINITE_WAIT to only wait till it's completed.
*/
{
/*
* Verify the supplied package.
*/
{
return VERR_RT_REQUEST_STATE;
}
{
AssertMsgFailed(("Invalid request package! Anyone cooking their own packages???\n"));
return VERR_RT_REQUEST_INVALID_PACKAGE;
}
{
AssertMsgFailed(("Invalid package type %d valid range %d-%d inclusivly. This was verified on alloc and queue too...\n",
return VERR_RT_REQUEST_INVALID_TYPE;
}
/*
* Wait on the package.
*/
int rc;
if (cMillies != RT_INDEFINITE_WAIT)
else
{
do
{
}
if (rc == VINF_SUCCESS)
rc = VINF_SUCCESS;
return rc;
}
/**
* Process one request.
*
* @returns IPRT status code.
*
* @param pReq Request packet to process.
*/
{
/*
* Process the request.
*/
{
/*
* A packed down call frame.
*/
case RTREQTYPE_INTERNAL:
{
union
{
DECLCALLBACKMEMBER(int, pfn00)(void);
DECLCALLBACKMEMBER(int, pfn07)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
DECLCALLBACKMEMBER(int, pfn08)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
DECLCALLBACKMEMBER(int, pfn09)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
DECLCALLBACKMEMBER(int, pfn10)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
DECLCALLBACKMEMBER(int, pfn11)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
DECLCALLBACKMEMBER(int, pfn12)(uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
} u;
#ifdef RT_ARCH_AMD64
{
case 6: rcRet = u.pfn06(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5]); break;
case 7: rcRet = u.pfn07(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6]); break;
case 8: rcRet = u.pfn08(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7]); break;
case 9: rcRet = u.pfn09(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8]); break;
case 10: rcRet = u.pfn10(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8], pauArgs[9]); break;
case 11: rcRet = u.pfn11(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8], pauArgs[9], pauArgs[10]); break;
case 12: rcRet = u.pfn12(pauArgs[0], pauArgs[1], pauArgs[2], pauArgs[3], pauArgs[4], pauArgs[5], pauArgs[6], pauArgs[7], pauArgs[8], pauArgs[9], pauArgs[10], pauArgs[11]); break;
default:
break;
}
#else /* x86: */
# ifdef __GNUC__
"subl %2, %%esp\n\t"
"andl $0xfffffff0, %%esp\n\t"
"shrl $2, %2\n\t"
"movl %%esp, %%edi\n\t"
"rep movsl\n\t"
"movl %%edx, %%edi\n\t"
"call *%%eax\n\t"
"mov %%edi, %%esp\n\t"
: "=a" (rcRet),
"=S" (pauArgs),
"=c" (cbArgs)
: "0" (u.pfn),
"1" (pauArgs),
"2" (cbArgs)
: "edi", "edx");
# else
{
}
# endif
#endif /* x86 */
break;
}
default:
break;
}
/*
* Complete the request.
*/
{
/* Free the packet, nobody is waiting. */
LogFlow(("rtReqProcessOne: Completed request %p: rcReq=%Rrc rcRet=%Rrc - freeing it\n",
}
else
{
/* Notify the waiter and him free up the packet. */
LogFlow(("rtReqProcessOne: Completed request %p: rcReq=%Rrc rcRet=%Rrc - notifying waiting thread\n",
if (rc2 != VINF_SUCCESS)
{
}
}
return rcRet;
}
/**
* Checks if the queue is busy or not.
*
* The caller is responsible for dealing with any concurrent submitts.
*
* @returns true if busy, false if idle.
* @param pQueue The queue.
*/
{
AssertPtrReturn(pQueue, false);
return true;
return true;
return true;
return false;
}