aiomgr.cpp revision 377d3eefbc092f9d4220535d896c510d560bbd74
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * IPRT - Async I/O manager.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * Copyright (C) 2013 Oracle Corporation
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * available from http://www.virtualbox.org. This file is free software;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * you can redistribute it and/or modify it under the terms of the GNU
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * General Public License (GPL) as published by the Free Software
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * The contents of this file may alternatively be used under the terms
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * of the Common Development and Distribution License Version 1.0
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * VirtualBox OSE distribution, in which case the provisions of the
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * CDDL are applicable instead of those of the GPL.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * You may elect to license modified versions of this file under the
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * terms and conditions of either the GPL or the CDDL or both.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/*******************************************************************************
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync* Header Files *
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync*******************************************************************************/
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/*******************************************************************************
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync* Structures and Typedefs *
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync*******************************************************************************/
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/** Pointer to an internal async I/O file instance. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * Blocking event types.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Invalid tye */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** An endpoint is added to the manager. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** An endpoint is about to be closed. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** 32bit hack */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * Async I/O manager instance data.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsynctypedef struct RTAIOMGRINT
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Magic value. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Reference count. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Running flag. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync volatile bool fRunning;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Async I/O context handle. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** async I/O thread. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** List of files assigned to this manager. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Number of requests active currently. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Number of maximum requests active. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Pointer to an array of free async I/O request handles. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Index of the next free entry in the cache. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Size of the array. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Critical section protecting the blocking event handling. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Event semaphore for blocking external events.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * The caller waits on it until the async I/O manager
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * finished processing the event. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Flag whether a blocking event is pending and needs
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * processing by the I/O manager. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Blocking event type */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Event type data */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** The file to be added */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** The file to be closed */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/** Pointer to an internal async I/O manager instance. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * Async I/O manager file instance data.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Magic value. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Reference count. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** File handle. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** async I/O manager this file belongs to. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Work queue for new requests. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Data for exclusive use by the assigned async I/O manager. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Pointer to the next file assigned to the manager. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** List of pending requests (not submitted due to usage restrictions
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * or a pending flush request) */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Tail of pending requests. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Tree of currently locked ranges.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * If a write task is enqueued the range gets locked and any other
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * task writing to that range has to wait until the task completes.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Number of requests with a range lock active. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Number of requests currently being processed for this endpoint
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * (excluded flush requests). */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/*******************************************************************************
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync* Defined Constants And Macros *
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync*******************************************************************************/
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn((a_hAioMgr)->u32Magic == RTAIOMGR_MAGIC, (a_rc)); \
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync } while (0)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync#define RTAIOMGR_VALID_RETURN(a_hAioMgr) RTAIOMGR_VALID_RETURN_RC((hAioMgr), VERR_INVALID_HANDLE)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/** Validates a handle and returns (void) if not valid. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturnVoid((a_hAioMgr)->u32Magic == RTAIOMGR_MAGIC); \
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync } while (0)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/*******************************************************************************
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync* Internal Functions *
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync*******************************************************************************/
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * async I/O manager worker loop.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * @returns IPRT status code.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * @param hThreadSelf The thread handle this worker belongs to.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * @param pvUser Opaque user data (Pointer to async I/O manager instance).
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncstatic DECLCALLBACK(int) rtAioMgrWorker(RTTHREAD hThreadSelf, void *pvUser)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync bool fRunning = true;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync int rc = RTFileAioCtxWait(pThis->hAioCtx, 1, RT_INDEFINITE_WAIT, &ahReqsCompleted[0],
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * Destroys an async I/O manager.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * @returns nothing.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * @param pThis The async I/O manager instance to destroy.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rc = RTThreadWait(pThis->hThread, RT_INDEFINITE_WAIT, NULL);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * Destroys an async I/O manager file.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * @returns nothing.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * @param pThis The async I/O manager file.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncstatic void rtAioMgrFileDestroy(PRTAIOMGRFILEINT pThis)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(int) RTAioMgrCreate(PRTAIOMGR phAioMgr, uint32_t cReqsMax)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(cReqsMax > 0, VERR_INVALID_PARAMETER);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis = (PRTAIOMGRINT)RTMemAllocZ(sizeof(RTAIOMGRINT));
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rc = RTFileAioCtxCreate(&pThis->hAioCtx, cReqsMax == UINT32_MAX
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rc = RTThreadCreateF(&pThis->hThread, rtAioMgrWorker, pThis, 0, RTTHREADTYPE_IO,
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(pThis->u32Magic == RTAIOMGR_MAGIC, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertMsg(cRefs > 1 && cRefs < _1G, ("%#x %p\n", cRefs, pThis));
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(pThis->u32Magic == RTAIOMGR_MAGIC, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertMsg(cRefs < _1G, ("%#x %p\n", cRefs, pThis));
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(int) RTAioMgrFileCreate(RTAIOMGR hAioMgr, RTFILE hFile, PFNRTAIOMGRREQCOMPLETE pfnReqComplete,
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(hAioMgr != NIL_RTAIOMGR, VERR_INVALID_HANDLE);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(hFile != NIL_RTFILE, VERR_INVALID_HANDLE);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertPtrReturn(pfnReqComplete, VERR_INVALID_POINTER);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertPtrReturn(phAioMgrFile, VERR_INVALID_POINTER);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis = (PRTAIOMGRFILEINT)RTMemAllocZ(sizeof(RTAIOMGRFILEINT));
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rc = RTFileAioCtxAssociateWithFile(pThis->pAioMgr->hAioCtx, hFile);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(uint32_t) RTAioMgrFileRetain(RTAIOMGRFILE hAioMgrFile)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(hAioMgrFile != NIL_RTAIOMGRFILE, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(pThis->u32Magic == RTAIOMGRFILE_MAGIC, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertMsg(cRefs > 1 && cRefs < _1G, ("%#x %p\n", cRefs, pThis));
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(uint32_t) RTAioMgrFileRelease(RTAIOMGRFILE hAioMgrFile)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(pThis->u32Magic == RTAIOMGRFILE_MAGIC, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertMsg(cRefs < _1G, ("%#x %p\n", cRefs, pThis));
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(int) RTAioMgrFileRead(RTAIOMGRFILE hAioMgrFile, RTFOFF off,
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(int) RTAioMgrFileWrite(RTAIOMGRFILE hAioMgrFile, RTFOFF off,