fileaio-solaris.cpp revision 10d739d22a5d5a13803f7e34de34de010099270c
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * IPRT - File async I/O, native implementation for the Solaris host platform.
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * Copyright (C) 2006-2012 Oracle Corporation
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * This file is part of VirtualBox Open Source Edition (OSE), as
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * available from http://www.virtualbox.org. This file is free software;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * you can redistribute it and/or modify it under the terms of the GNU
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * General Public License (GPL) as published by the Free Software
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * Foundation, in version 2 as it comes in the "COPYING" file of the
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * The contents of this file may alternatively be used under the terms
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * of the Common Development and Distribution License Version 1.0
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * VirtualBox OSE distribution, in which case the provisions of the
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * CDDL are applicable instead of those of the GPL.
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * You may elect to license modified versions of this file under the
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * terms and conditions of either the GPL or the CDDL or both.
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane/*******************************************************************************
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane* Header Files *
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane*******************************************************************************/
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane/*******************************************************************************
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane* Structures and Typedefs *
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane*******************************************************************************/
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * Async I/O completion context state.
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /** Handle to the port. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /** Current number of requests active on this context. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /** Flags given during creation. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /** Magic value (RTFILEAIOCTX_MAGIC). */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane/** Pointer to an internal context structure. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montanetypedef RTFILEAIOCTXINTERNAL *PRTFILEAIOCTXINTERNAL;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * Async I/O request state.
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /** The aio control block. Must be the FIRST
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /** Current state the request is in. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /** Flag whether this is a flush request. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /** Port notifier object to associate a request to a port. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /** Opaque user data. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /** Completion context we are assigned to. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /** Magic value (RTFILEAIOREQ_MAGIC). */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane/** Pointer to an internal request structure. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montanetypedef RTFILEAIOREQINTERNAL *PRTFILEAIOREQINTERNAL;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane/*******************************************************************************
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane* Defined Constants And Macros *
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane*******************************************************************************/
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane/** The max number of events to get in one call. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane#define AIO_MAXIMUM_REQUESTS_PER_CONTEXT 64
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane/** Id for the wakeup event. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-MontaneRTR3DECL(int) RTFileAioGetLimits(PRTFILEAIOLIMITS pAioLimits)
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane AssertPtrReturn(pAioLimits, VERR_INVALID_POINTER);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /* No limits known. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane pAioLimits->cReqsOutstandingMax = RTFILEAIO_UNLIMITED_REQS;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-MontaneRTR3DECL(int) RTFileAioReqCreate(PRTFILEAIOREQ phReq)
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane AssertPtrReturn(phReq, VERR_INVALID_POINTER);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOREQINTERNAL));
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /* Ininitialize static parts. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane pReqInt->AioCB.aio_sigevent.sigev_notify = SIGEV_PORT;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane pReqInt->AioCB.aio_sigevent.sigev_value.sival_ptr = &pReqInt->PortNotifier;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane pReqInt->PortNotifier.portnfy_user = pReqInt;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane pReqInt->u32Magic = RTFILEAIOREQ_MAGIC;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-MontaneRTDECL(int) RTFileAioReqDestroy(RTFILEAIOREQ hReq)
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * Validate the handle and ignore nil.
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane PRTFILEAIOREQINTERNAL pReqInt = hReq;
fb22a3d183d0fbde920275e3e14138a27151e734Jean-Noel Rouvignac RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * Trash the magic and free it.
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane ASMAtomicUoWriteU32(&pReqInt->u32Magic, ~RTFILEAIOREQ_MAGIC);
fb22a3d183d0fbde920275e3e14138a27151e734Jean-Noel Rouvignac * Worker setting up the request.
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-MontaneDECLINLINE(int) rtFileAioReqPrepareTransfer(RTFILEAIOREQ hReq, RTFILE hFile,
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane RTFOFF off, void *pvBuf, size_t cbTransfer,
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * Validate the input.
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane PRTFILEAIOREQINTERNAL pReqInt = hReq;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane pReqInt->AioCB.aio_lio_opcode = uTransferDirection;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane pReqInt->AioCB.aio_fildes = RTFileToNative(hFile);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane pReqInt->AioCB.aio_nbytes = cbTransfer;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-MontaneRTDECL(int) RTFileAioReqPrepareRead(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane void *pvBuf, size_t cbRead, void *pvUser)
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane return rtFileAioReqPrepareTransfer(hReq, hFile, LIO_READ,
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-MontaneRTDECL(int) RTFileAioReqPrepareWrite(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane void const *pvBuf, size_t cbWrite, void *pvUser)
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane return rtFileAioReqPrepareTransfer(hReq, hFile, LIO_WRITE,
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane off, (void *)pvBuf, cbWrite, pvUser);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-MontaneRTDECL(int) RTFileAioReqPrepareFlush(RTFILEAIOREQ hReq, RTFILE hFile, void *pvUser)
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)hReq;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane pReqInt->AioCB.aio_fildes = RTFileToNative(hFile);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-MontaneRTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq)
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane PRTFILEAIOREQINTERNAL pReqInt = hReq;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane RTFILEAIOREQ_VALID_RETURN_RC(pReqInt, NULL);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-MontaneRTDECL(int) RTFileAioReqCancel(RTFILEAIOREQ hReq)
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane PRTFILEAIOREQINTERNAL pReqInt = hReq;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane RTFILEAIOREQ_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_NOT_SUBMITTED);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane int rcSolaris = aio_cancel(pReqInt->AioCB.aio_fildes, &pReqInt->AioCB);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * Decrement request count because the request will never arrive at the
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane * completion port.
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane AssertMsg(VALID_PTR(pReqInt->pCtxInt),
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane ("Invalid state. Request was canceled but wasn't submitted\n"));
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane ASMAtomicDecS32(&pReqInt->pCtxInt->cRequests);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane else if (rcSolaris == AIO_NOTCANCELED)
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-MontaneRTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransfered)
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane PRTFILEAIOREQINTERNAL pReqInt = hReq;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, PREPARED, VERR_FILE_AIO_NOT_SUBMITTED);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane int rcSol = aio_error(&pReqInt->AioCB);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane Assert(rcSol != EINPROGRESS); /* Handled by our own state handling. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane *pcbTransfered = aio_return(&pReqInt->AioCB);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /* An error occurred. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-MontaneRTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax,
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane AssertPtrReturn(phAioCtx, VERR_INVALID_POINTER);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane AssertReturn(!(fFlags & ~RTFILEAIOCTX_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane pCtxInt = (PRTFILEAIOCTXINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOCTXINTERNAL));
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /* Init the event handle. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane pCtxInt->u32Magic = RTFILEAIOCTX_MAGIC;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-MontaneRTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx)
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /* Validate the handle and ignore nil. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane /* Cannot destroy a busy context. */
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-Montane ASMAtomicUoWriteU32(&pCtxInt->u32Magic, RTFILEAIOCTX_MAGIC_DEAD);
a3b0441c12b207c0fdfce0566dba2db5ecd3816cViolette Roche-MontaneRTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx)
return VINF_SUCCESS;
int rcSol = 0;
cReqsSubmit++;
if (cReqsSubmit)
for (i = 0; i < cReqs; i++)
if (cReqs)
cReqs--;
pahReqs++;
} while (cReqs);
return rc;
int cRequestsCompleted = 0;
return VERR_FILE_AIO_NO_REQUEST;
if (!cMinReqs)
while ( cMinReqs
int rcSol;
return rc;
return VINF_SUCCESS;