fileaio-posix.cpp revision ad27e1d5e48ca41245120c331cc88b50464813ce
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * IPRT - File async I/O, native implementation for POSIX compliant host platforms.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2006-2007 Oracle Corporation
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * available from http://www.virtualbox.org. This file is free software;
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * you can redistribute it and/or modify it under the terms of the GNU
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * General Public License (GPL) as published by the Free Software
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * The contents of this file may alternatively be used under the terms
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * of the Common Development and Distribution License Version 1.0
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * VirtualBox OSE distribution, in which case the provisions of the
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * CDDL are applicable instead of those of the GPL.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * You may elect to license modified versions of this file under the
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * terms and conditions of either the GPL or the CDDL or both.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync/*******************************************************************************
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync* Header Files *
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync*******************************************************************************/
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync * Linux does not define this value.
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync * Just define it with really big
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync#if 0 /* Only used for debugging */
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync/** Invalid entry in the waiting array. */
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync/** No-op replacement for rtFileAioCtxDump for non debug builds */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync/*******************************************************************************
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync* Structures and Typedefs *
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync*******************************************************************************/
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * Async I/O request state.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** The aio control block. FIRST ELEMENT! */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Next element in the chain. */
be196d173cf52fa33016912e4745dbe1170ac53avboxsync /** Previous element in the chain. */
be196d173cf52fa33016912e4745dbe1170ac53avboxsync /** Current state the request is in. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Flag whether this is a flush request. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Flag indicating if the request was canceled. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync volatile bool fCanceled;
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Opaque user data. */
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync /** Number of bytes actually transferred. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Status code. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Completion context we are assigned to. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Entry in the waiting list the request is in. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Magic value (RTFILEAIOREQ_MAGIC). */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * Async I/O completion context state.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Current number of requests active on this context. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Maximum number of requests this context can handle. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** The ID of the thread which is currently waiting for requests. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Flag whether the thread was woken up. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync volatile bool fWokenUp;
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Flag whether the thread is currently waiting in the syscall. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync volatile bool fWaiting;
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Magic value (RTFILEAIOCTX_MAGIC). */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Flag whether the thread was woken up due to a internal event. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync volatile bool fWokenUpInternal;
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** List of new requests which needs to be inserted into apReqs by the
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * waiting thread. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Special entry for requests which are canceled. Because only one
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * request can be canceled at a time and the thread canceling the request
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * has to wait we need only one entry. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** Event semaphore the canceling thread is waiting for completion of
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * the operation. */
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync /** Head of submitted elements waiting to get into the array. */
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync /** Tail of submitted elements waiting to get into the array. */
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync /** Maximum number of elements in the waiting array. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** First free slot in the waiting list. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** List of requests we are currently waiting on.
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync * Size depends on cMaxRequests and AIO_LISTIO_MAX. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * Internal worker for waking up the waiting thread.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncstatic void rtFileAioCtxWakeup(PRTFILEAIOCTXINTERNAL pCtxInt)
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * Read the thread handle before the status flag.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * If we read the handle after the flag we might
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * end up with an invalid handle because the thread
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * waiting in RTFileAioCtxWakeup() might get scheduled
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * before we read the flag and returns.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * We can ensure that the handle is valid if fWaiting is true
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * when reading the handle before the status flag.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync ASMAtomicReadHandle(&pCtxInt->hThreadWait, &hThread);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync bool fWaiting = ASMAtomicReadBool(&pCtxInt->fWaiting);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * If a thread waits the handle must be valid.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * It is possible that the thread returns from
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * aio_suspend() before the signal is send.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * This is no problem because we already set fWokenUp
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * to true which will let the thread return VERR_INTERRUPTED
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * and the next call to RTFileAioCtxWait() will not
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * return VERR_INTERRUPTED because signals are not saved
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * and will simply vanish if the destination thread can't
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * receive it.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * Internal worker processing events and inserting new requests into the waiting list.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncstatic int rtFileAioCtxProcessEvents(PRTFILEAIOCTXINTERNAL pCtxInt)
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Process new requests first. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUpInternal, false);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync for (unsigned iSlot = 0; iSlot < RT_ELEMENTS(pCtxInt->apReqsNewHead); iSlot++)
c7ff622115966b69b482bd2896662e40d823b22fvboxsync PRTFILEAIOREQINTERNAL pReqHead = ASMAtomicXchgPtrT(&pCtxInt->apReqsNewHead[iSlot], NULL, PRTFILEAIOREQINTERNAL);
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync while ( (pCtxInt->iFirstFree < pCtxInt->cReqsWaitMax)
be196d173cf52fa33016912e4745dbe1170ac53avboxsync /* Clear pointer to next and previous element just for safety. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync pCtxInt->apReqs[pCtxInt->iFirstFree]->pNext = NULL;
be196d173cf52fa33016912e4745dbe1170ac53avboxsync pCtxInt->apReqs[pCtxInt->iFirstFree]->pPrev = NULL;
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync Assert( (pCtxInt->iFirstFree <= pCtxInt->cMaxRequests)
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync && (pCtxInt->iFirstFree <= pCtxInt->cReqsWaitMax));
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync /* Append the rest to the wait list. */
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync /* Update tail. */
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync RTFIELAIOREQ_ASSERT_STATE(pReqHead->pNext, SUBMITTED);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Check if a request needs to be canceled. */
c7ff622115966b69b482bd2896662e40d823b22fvboxsync PRTFILEAIOREQINTERNAL pReqToCancel = ASMAtomicReadPtrT(&pCtxInt->pReqToCancel, PRTFILEAIOREQINTERNAL);
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync /* The request can be in the array waiting for completion or still in the list because it is full. */
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync if (pReqToCancel->iWaitingList != RTFILEAIOCTX_WAIT_ENTRY_INVALID)
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync /* Put it out of the waiting list. */
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync pCtxInt->apReqs[pReqToCancel->iWaitingList] = pCtxInt->apReqs[--pCtxInt->iFirstFree];
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync pCtxInt->apReqs[pReqToCancel->iWaitingList]->iWaitingList = pReqToCancel->iWaitingList;
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync /* Unlink from the waiting list. */
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync /* We canceled the tail. */
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync /* We canceled the head. */
e7e430ae596798f719b2bc3841d9607dbf92aba0vboxsync AssertMsg(pCtxInt->cRequests >= 0, ("Canceled request not which is not in this context\n"));
be196d173cf52fa33016912e4745dbe1170ac53avboxsyncRTR3DECL(int) RTFileAioGetLimits(PRTFILEAIOLIMITS pAioLimits)
be196d173cf52fa33016912e4745dbe1170ac53avboxsync &cReqsOutstandingMax, /* Where to store the old value. */
be196d173cf52fa33016912e4745dbe1170ac53avboxsync NULL); /* Where the size of the new value is stored. */
353e4c614c12b55dbab6685ea690ceb1e6b0e8ddvboxsync pAioLimits->cReqsOutstandingMax = cReqsOutstandingMax;
353e4c614c12b55dbab6685ea690ceb1e6b0e8ddvboxsync * The AIO API is implemented in a kernel module which is not
353e4c614c12b55dbab6685ea690ceb1e6b0e8ddvboxsync * loaded by default.
353e4c614c12b55dbab6685ea690ceb1e6b0e8ddvboxsync * If it is loaded there are additional sysctl parameters.
353e4c614c12b55dbab6685ea690ceb1e6b0e8ddvboxsync rcBSD = sysctlbyname("vfs.aio.max_aio_per_proc", /* name */
353e4c614c12b55dbab6685ea690ceb1e6b0e8ddvboxsync &cReqsOutstandingMax, /* Where to store the old value. */
353e4c614c12b55dbab6685ea690ceb1e6b0e8ddvboxsync NULL); /* Where the size of the new value is stored. */
353e4c614c12b55dbab6685ea690ceb1e6b0e8ddvboxsync /* ENOENT means the value is unknown thus the module is not loaded. */
be196d173cf52fa33016912e4745dbe1170ac53avboxsync pAioLimits->cReqsOutstandingMax = cReqsOutstandingMax;
be196d173cf52fa33016912e4745dbe1170ac53avboxsync pAioLimits->cReqsOutstandingMax = RTFILEAIO_UNLIMITED_REQS;
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncRTR3DECL(int) RTFileAioReqCreate(PRTFILEAIOREQ phReq)
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOREQINTERNAL));
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync pReqInt->iWaitingList = RTFILEAIOCTX_WAIT_ENTRY_INVALID;
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * Validate the handle and ignore nil.
be196d173cf52fa33016912e4745dbe1170ac53avboxsync RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * Trash the magic and free it.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync ASMAtomicUoWriteU32(&pReqInt->u32Magic, ~RTFILEAIOREQ_MAGIC);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * Worker setting up the request.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncDECLINLINE(int) rtFileAioReqPrepareTransfer(RTFILEAIOREQ hReq, RTFILE hFile,
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * Validate the input.
be196d173cf52fa33016912e4745dbe1170ac53avboxsync RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync pReqInt->AioCB.aio_lio_opcode = uTransferDirection;
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncRTDECL(int) RTFileAioReqPrepareRead(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync return rtFileAioReqPrepareTransfer(hReq, hFile, LIO_READ,
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncRTDECL(int) RTFileAioReqPrepareWrite(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync return rtFileAioReqPrepareTransfer(hReq, hFile, LIO_WRITE,
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncRTDECL(int) RTFileAioReqPrepareFlush(RTFILEAIOREQ hReq, RTFILE hFile, void *pvUser)
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)hReq;
be196d173cf52fa33016912e4745dbe1170ac53avboxsync RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncRTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq)
be196d173cf52fa33016912e4745dbe1170ac53avboxsync RTFILEAIOREQ_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_NOT_SUBMITTED);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync int rcPosix = aio_cancel(pReqInt->AioCB.aio_fildes, &pReqInt->AioCB);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * Notify the waiting thread that the request was canceled.
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync ("Invalid state. Request was canceled but wasn't submitted\n"));
c7ff622115966b69b482bd2896662e40d823b22fvboxsync ASMAtomicWritePtr(&pCtxInt->pReqToCancel, pReqInt);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Wait for acknowledge. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync int rc = RTSemEventWait(pCtxInt->SemEventCancel, RT_INDEFINITE_WAIT);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncRTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransfered)
be196d173cf52fa33016912e4745dbe1170ac53avboxsync RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
be196d173cf52fa33016912e4745dbe1170ac53avboxsync RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, PREPARED, VERR_FILE_AIO_NOT_SUBMITTED);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncRTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax)
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync cReqsWaitMax = RT_MIN(cAioReqsMax, AIO_LISTIO_MAX);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync pCtxInt = (PRTFILEAIOCTXINTERNAL)RTMemAllocZ( sizeof(RTFILEAIOCTXINTERNAL)
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Create event semaphore. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync int rc = RTSemEventCreate(&pCtxInt->SemEventCancel);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncRTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx)
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncRTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx)
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncRTDECL(int) RTFileAioCtxAssociateWithFile(RTFILEAIOCTX hAioCtx, RTFILE hFile)
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync * Dumps the state of a async I/O context.
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsyncstatic void rtFileAioCtxDump(PRTFILEAIOCTXINTERNAL pCtxInt)
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlow(("cMaxRequests=%u\n", pCtxInt->cMaxRequests));
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlow(("hThreadWait=%#p\n", pCtxInt->hThreadWait));
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlow(("fWokenUp=%RTbool\n", pCtxInt->fWokenUp));
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlow(("fWaiting=%RTbool\n", pCtxInt->fWaiting));
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlow(("fWokenUpInternal=%RTbool\n", pCtxInt->fWokenUpInternal));
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pCtxInt->apReqsNewHead); i++)
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlow(("apReqsNewHead[%u]=%#p\n", i, pCtxInt->apReqsNewHead[i]));
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlow(("pReqToCancel=%#p\n", pCtxInt->pReqToCancel));
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlow(("pReqsWaitHead=%#p\n", pCtxInt->pReqsWaitHead));
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlow(("pReqsWaitTail=%#p\n", pCtxInt->pReqsWaitTail));
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlow(("cReqsWaitMax=%u\n", pCtxInt->cReqsWaitMax));
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync for (unsigned i = 0; i < pCtxInt->cReqsWaitMax; i++)
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlow(("apReqs[%u]=%#p\n", i, pCtxInt->apReqs[i]));
be196d173cf52fa33016912e4745dbe1170ac53avboxsyncRTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs)
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Parameter checks */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Check that we don't exceed the limit */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync if (ASMAtomicUoReadS32(&pCtxInt->cRequests) + cReqs > pCtxInt->cMaxRequests)
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync while ( (i < cReqs)
be196d173cf52fa33016912e4745dbe1170ac53avboxsync /* Undo everything and stop submitting. */
be196d173cf52fa33016912e4745dbe1170ac53avboxsync /* Unlink from the list again. */
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync /* Link them together. */
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync rcPosix = lio_listio(LIO_NOWAIT, (struct aiocb **)pahReqs, cReqsSubmit, NULL);
be196d173cf52fa33016912e4745dbe1170ac53avboxsync /* Check which ones were not submitted. */
e7e430ae596798f719b2bc3841d9607dbf92aba0vboxsync for (i = 0; i < cReqsSubmit; i++)
be196d173cf52fa33016912e4745dbe1170ac53avboxsync /* Was not submitted. */
571c90a734400801da973f986190fac9fc5efd0dvboxsync /* An error occurred. */
4db77474b77c0e66ae34b2400666a6020870868dvboxsync * Looks like Apple and glibc interpret the standard in different ways.
4db77474b77c0e66ae34b2400666a6020870868dvboxsync * glibc returns the error code which would be in errno but Apple returns
4db77474b77c0e66ae34b2400666a6020870868dvboxsync * -1 and sets errno to the appropriate value
be196d173cf52fa33016912e4745dbe1170ac53avboxsync /* Unlink from the list. */
e7e430ae596798f719b2bc3841d9607dbf92aba0vboxsync ASMAtomicAddS32(&pCtxInt->cRequests, cReqsSubmitted);
9f264ec6758444825b2c0ee118d6e8ada1a18837vboxsync AssertMsg(pCtxInt->cRequests >= 0, ("Adding requests resulted in overflow\n"));
9f264ec6758444825b2c0ee118d6e8ada1a18837vboxsync AssertMsg(pCtxInt->cRequests >= 0, ("Adding requests resulted in overflow\n"));
d33dba3295a06726b0511a32b409715d40f12500vboxsync * Check if we have a flush request now.
d33dba3295a06726b0511a32b409715d40f12500vboxsync * If not we hit the AIO_LISTIO_MAX limit
d33dba3295a06726b0511a32b409715d40f12500vboxsync * and will continue submitting requests
d33dba3295a06726b0511a32b409715d40f12500vboxsync * lio_listio does not work with flush requests so
d33dba3295a06726b0511a32b409715d40f12500vboxsync * we have to use aio_fsync directly.
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync /* Link them together. */
9f264ec6758444825b2c0ee118d6e8ada1a18837vboxsync AssertMsg(pCtxInt->cRequests >= 0, ("Adding requests resulted in overflow\n"));
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync * Forward successfully submitted requests to the thread waiting for requests.
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync * We search for a free slot first and if we don't find one
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync * we will grab the first one and append our list to the existing entries.
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync unsigned iSlot = 0;
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync while ( (iSlot < RT_ELEMENTS(pCtxInt->apReqsNewHead))
c7ff622115966b69b482bd2896662e40d823b22fvboxsync && !ASMAtomicCmpXchgPtr(&pCtxInt->apReqsNewHead[iSlot], pHead, NULL))
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync /* Nothing found. */
c7ff622115966b69b482bd2896662e40d823b22fvboxsync PRTFILEAIOREQINTERNAL pOldHead = ASMAtomicXchgPtrT(&pCtxInt->apReqsNewHead[0], NULL, PRTFILEAIOREQINTERNAL);
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync /* Find the end of the current head and link the old list to the current. */
c7ff622115966b69b482bd2896662e40d823b22fvboxsync ASMAtomicWritePtr(&pCtxInt->apReqsNewHead[0], pHead);
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync /* Set the internal wakeup flag and wakeup the thread if possible. */
3633fa76957c4740b8d4372e0dfc938d3c3ea569vboxsync bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUpInternal, true);
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsyncRTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, RTMSINTERVAL cMillies,
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs)
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync PRTFILEAIOCTXINTERNAL pCtxInt = (PRTFILEAIOCTXINTERNAL)hAioCtx;
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlowFunc(("hAioCtx=%#p cMinReqs=%zu cMillies=%u pahReqs=%#p cReqs=%zu pcbReqs=%#p\n",
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync hAioCtx, cMinReqs, cMillies, pahReqs, cReqs, pcReqs));
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Check parameters. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync AssertReturn(cReqs >= cMinReqs, VERR_OUT_OF_RANGE);
e7e430ae596798f719b2bc3841d9607dbf92aba0vboxsync int32_t cRequestsWaiting = ASMAtomicReadS32(&pCtxInt->cRequests);
6477a269f84e2038ebe9897def6d9ed72f7dad6evboxsync if (RT_UNLIKELY(cMinReqs > (uint32_t)cRequestsWaiting))
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Wait for at least one. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* For the wakeup call. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync ASMAtomicWriteHandle(&pCtxInt->hThreadWait, RTThreadSelf());
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Update the waiting list once before we enter the loop. */
34eda39547d54f9e806d52ab45cbc50cc24b3613vboxsync for (unsigned i = 0; i < pCtxInt->cReqsWaitMax; i++)
a7aa94e0115a73841f34ebbfa00f63fa1904e51fvboxsync RTAssertMsg2Weak("wait[%d] = %#p\n", i, pCtxInt->apReqs[i]);
34eda39547d54f9e806d52ab45cbc50cc24b3613vboxsync AssertMsgFailed(("No request to wait for. pReqsWaitHead=%#p pReqsWaitTail=%#p\n",
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlow(("Waiting for %d requests to complete\n", pCtxInt->iFirstFree));
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync int rcPosix = aio_suspend((const struct aiocb * const *)pCtxInt->apReqs,
b85a35a21b7b0b5ffabec62099b9a426228c3a08vboxsync LogFlow(("aio_suspend failed %d nent=%u\n", errno, pCtxInt->iFirstFree));
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Check that this is an external wakeup event. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Requests finished. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync unsigned iReqCurr = 0;
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync unsigned cDone = 0;
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Remove completed requests from the waiting list. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync PRTFILEAIOREQINTERNAL pReq = pCtxInt->apReqs[iReqCurr];
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Completed store the return code. */
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync /* Call aio_return() to free resources. */
be196d173cf52fa33016912e4745dbe1170ac53avboxsync /* Mark the request as finished. */
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync /* If there are other entries waiting put the head into the now free entry. */
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync PRTFILEAIOREQINTERNAL pReqInsert = pCtxInt->pReqsWaitHead;
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync /* List is empty now. Clear tail too. */
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync pCtxInt->apReqs[pReqInsert->iWaitingList] = pReqInsert;
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync * Move the last entry into the current position to avoid holes
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync * but only if it is not the last element already.
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync pCtxInt->apReqs[pReq->iWaitingList] = pCtxInt->apReqs[--pCtxInt->iFirstFree];
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync pCtxInt->apReqs[pReq->iWaitingList]->iWaitingList = pReq->iWaitingList;
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Put the request into the completed list. */
5fa66c0f504a87c7a177cb850c98baaa27ca340fvboxsync pReq->iWaitingList = RTFILEAIOCTX_WAIT_ENTRY_INVALID;
e1e9bbad321dedb62faa51b503ea9faf898a56f7vboxsync AssertMsg((cDone <= cReqs), ("Overflow cReqs=%u cMinReqs=%u cDone=%u\n",
e7e430ae596798f719b2bc3841d9607dbf92aba0vboxsync AssertMsg(pCtxInt->cRequests >= 0, ("Finished more requests than currently active\n"));
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Recalculate the timeout. */
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsync Timeout.tv_sec = Timeout.tv_sec - (TimeDiff / 1000000);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync Timeout.tv_nsec = Timeout.tv_nsec - (TimeDiff % 1000000);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /* Check for new elements. */
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync ASMAtomicWriteHandle(&pCtxInt->hThreadWait, NIL_RTTHREAD);
cba55d7782acd89472e03b4376591ff57fe7b80evboxsyncRTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx)
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync /** @todo r=bird: Define the protocol for how to resume work after calling
cba55d7782acd89472e03b4376591ff57fe7b80evboxsync * this function. */