aiomgr.cpp revision 377d3eefbc092f9d4220535d896c510d560bbd74
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/* $Id$ */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/** @file
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * IPRT - Async I/O manager.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/*
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * Copyright (C) 2013 Oracle Corporation
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync *
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 *
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 *
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
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/*******************************************************************************
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync* Header Files *
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync*******************************************************************************/
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync#include <iprt/aiomgr.h>
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync#include <iprt/err.h>
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync#include <iprt/asm.h>
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync#include <iprt/mem.h>
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync#include <iprt/file.h>
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync#include <iprt/thread.h>
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync#include <iprt/assert.h>
377d3eefbc092f9d4220535d896c510d560bbd74vboxsync#include <iprt/critsect.h>
377d3eefbc092f9d4220535d896c510d560bbd74vboxsync#include <iprt/semaphore.h>
377d3eefbc092f9d4220535d896c510d560bbd74vboxsync#include <iprt/queueatomic.h>
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync#include "internal/magics.h"
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/*******************************************************************************
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync* Structures and Typedefs *
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync*******************************************************************************/
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/** Pointer to an internal async I/O file instance. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsynctypedef struct RTAIOMGRFILEINT *PRTAIOMGRFILEINT;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/**
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * Blocking event types.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsynctypedef enum RTAIOMGREVENT
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Invalid tye */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTAIOMGREVENT_INVALID = 0,
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** An endpoint is added to the manager. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTAIOMGREVENT_ADD_FILE,
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** An endpoint is about to be closed. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTAIOMGREVENT_CLOSE_FILE,
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** 32bit hack */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTAIOMGREVENT_32BIT_HACK = 0x7fffffff
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync} RTAIOMGREVENT;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/**
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * Async I/O manager instance data.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsynctypedef struct RTAIOMGRINT
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Magic value. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync uint32_t u32Magic;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Reference count. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync volatile uint32_t cRefs;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Running flag. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync volatile bool fRunning;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Async I/O context handle. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTFILEAIOCTX hAioCtx;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** async I/O thread. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTTHREAD hThread;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** List of files assigned to this manager. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PRTAIOMGRFILEINT pFilesHead;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Number of requests active currently. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync unsigned cReqsActive;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Number of maximum requests active. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync uint32_t cReqsActiveMax;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Pointer to an array of free async I/O request handles. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTFILEAIOREQ *pahReqsFree;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Index of the next free entry in the cache. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync uint32_t iFreeEntry;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Size of the array. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync unsigned cReqEntries;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Critical section protecting the blocking event handling. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTCRITSECT CritSectBlockingEvent;
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 RTSEMEVENT EventSemBlock;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Flag whether a blocking event is pending and needs
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * processing by the I/O manager. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync volatile bool fBlockingEventPending;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Blocking event type */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync volatile RTAIOMGREVENT enmBlockingEvent;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Event type data */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync union
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync {
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** The file to be added */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync volatile PRTAIOMGRFILEINT pFileAdd;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** The file to be closed */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync volatile PRTAIOMGRFILEINT pFileClose;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync } BlockingEventData;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync} RTAIOMGRINT;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/** Pointer to an internal async I/O manager instance. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsynctypedef RTAIOMGRINT *PRTAIOMGRINT;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/**
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * Async I/O manager file instance data.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsynctypedef struct RTAIOMGRFILEINT
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Magic value. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync uint32_t u32Magic;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Reference count. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync volatile uint32_t cRefs;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** File handle. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTFILE hFile;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** async I/O manager this file belongs to. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PRTAIOMGRINT pAioMgr;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Work queue for new requests. */
377d3eefbc092f9d4220535d896c510d560bbd74vboxsync RTQUEUEATOMIC QueueReqs;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Data for exclusive use by the assigned async I/O manager. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync struct
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync {
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Pointer to the next file assigned to the manager. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PRTAIOMGRFILEINT pNext;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync#if 0
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** List of pending requests (not submitted due to usage restrictions
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * or a pending flush request) */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync R3PTRTYPE(PPDMACTASKFILE) pReqsPendingHead;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Tail of pending requests. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync R3PTRTYPE(PPDMACTASKFILE) pReqsPendingTail;
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 */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PAVLRFOFFTREE pTreeRangesLocked;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Number of requests with a range lock active. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync unsigned cLockedReqsActive;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync /** Number of requests currently being processed for this endpoint
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * (excluded flush requests). */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync unsigned cRequestsActive;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync#endif
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync } AioMgr;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync} RTAIOMGRFILEINT;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/*******************************************************************************
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync* Defined Constants And Macros *
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync*******************************************************************************/
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync#define RTAIOMGR_VALID_RETURN_RC(a_hAioMgr, a_rc) \
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync do { \
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertPtrReturn((a_hAioMgr), (a_rc)); \
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn((a_hAioMgr)->u32Magic == RTAIOMGR_MAGIC, (a_rc)); \
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync } while (0)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
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
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/** Validates a handle and returns (void) if not valid. */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync#define RTAIOMGR_VALID_RETURN_VOID(a_hAioMgr) \
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync do { \
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertPtrReturnVoid(a_hAioMgr); \
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturnVoid((a_hAioMgr)->u32Magic == RTAIOMGR_MAGIC); \
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync } while (0)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/*******************************************************************************
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync* Internal Functions *
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync*******************************************************************************/
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/**
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * async I/O manager worker loop.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync *
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).
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncstatic DECLCALLBACK(int) rtAioMgrWorker(RTTHREAD hThreadSelf, void *pvUser)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PRTAIOMGRINT pThis = (PRTAIOMGRINT)pvUser;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync bool fRunning = true;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync do
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync {
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync uint32_t cReqsCompleted = 0;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTFILEAIOREQ ahReqsCompleted[32];
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync int rc = RTFileAioCtxWait(pThis->hAioCtx, 1, RT_INDEFINITE_WAIT, &ahReqsCompleted[0],
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RT_ELEMENTS(ahReqsCompleted), &cReqsCompleted);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync {
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync }
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync fRunning = ASMAtomicUoReadBool(&pThis->fRunning);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync } while (fRunning);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync return VINF_SUCCESS;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync}
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/**
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * Destroys an async I/O manager.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync *
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * @returns nothing.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * @param pThis The async I/O manager instance to destroy.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncstatic void rtAioMgrDestroy(PRTAIOMGRINT pThis)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync int rc;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync ASMAtomicXchgBool(&pThis->fRunning, false);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rc = RTFileAioCtxWakeup(pThis->hAioCtx);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertRC(rc);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rc = RTThreadWait(pThis->hThread, RT_INDEFINITE_WAIT, NULL);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertRC(rc);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rc = RTFileAioCtxDestroy(pThis->hAioCtx);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertRC(rc);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis->hThread = NIL_RTTHREAD;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis->hAioCtx = NIL_RTFILEAIOCTX;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis->u32Magic = ~RTAIOMGR_MAGIC;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTMemFree(pThis);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync}
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync/**
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * Destroys an async I/O manager file.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync *
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * @returns nothing.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync * @param pThis The async I/O manager file.
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync */
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncstatic void rtAioMgrFileDestroy(PRTAIOMGRFILEINT pThis)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis->u32Magic = ~RTAIOMGRFILE_MAGIC;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTMemFree(pThis);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync}
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(int) RTAioMgrCreate(PRTAIOMGR phAioMgr, uint32_t cReqsMax)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync int rc = VINF_SUCCESS;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PRTAIOMGRINT pThis;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertPtrReturn(phAioMgr, VERR_INVALID_POINTER);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(cReqsMax > 0, VERR_INVALID_PARAMETER);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis = (PRTAIOMGRINT)RTMemAllocZ(sizeof(RTAIOMGRINT));
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync if (pThis)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync {
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis->u32Magic = RTAIOMGR_MAGIC;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis->cRefs = 1;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis->fRunning = true;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rc = RTFileAioCtxCreate(&pThis->hAioCtx, cReqsMax == UINT32_MAX
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync ? RTFILEAIO_UNLIMITED_REQS
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync : cReqsMax,
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTFILEAIOCTX_FLAGS_WAIT_WITHOUT_PENDING_REQUESTS);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync if (RT_SUCCESS(rc))
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync {
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rc = RTThreadCreateF(&pThis->hThread, rtAioMgrWorker, pThis, 0, RTTHREADTYPE_IO,
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTTHREADFLAGS_WAITABLE, "AioMgr-%p", pThis);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync if (RT_FAILURE(rc))
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync {
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rc = RTFileAioCtxDestroy(pThis->hAioCtx);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertRC(rc);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync }
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync }
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync if (RT_FAILURE(rc))
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync RTMemFree(pThis);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync }
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync else
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rc = VERR_NO_MEMORY;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync if (RT_SUCCESS(rc))
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync *phAioMgr = pThis;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync return rc;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync}
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(uint32_t) RTAioMgrRetain(RTAIOMGR hAioMgr)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PRTAIOMGRINT pThis = hAioMgr;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(hAioMgr != NIL_RTAIOMGR, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertPtrReturn(pThis, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(pThis->u32Magic == RTAIOMGR_MAGIC, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertMsg(cRefs > 1 && cRefs < _1G, ("%#x %p\n", cRefs, pThis));
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync return cRefs;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync}
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(uint32_t) RTAioMgrRelease(RTAIOMGR hAioMgr)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PRTAIOMGRINT pThis = hAioMgr;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync if (pThis == NIL_RTAIOMGR)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync return 0;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertPtrReturn(pThis, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(pThis->u32Magic == RTAIOMGR_MAGIC, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertMsg(cRefs < _1G, ("%#x %p\n", cRefs, pThis));
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync if (cRefs == 0)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rtAioMgrDestroy(pThis);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync return cRefs;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync}
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(int) RTAioMgrFileCreate(RTAIOMGR hAioMgr, RTFILE hFile, PFNRTAIOMGRREQCOMPLETE pfnReqComplete,
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PRTAIOMGRFILE phAioMgrFile)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync int rc = VINF_SUCCESS;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PRTAIOMGRFILEINT pThis;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
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
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis = (PRTAIOMGRFILEINT)RTMemAllocZ(sizeof(RTAIOMGRFILEINT));
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync if (pThis)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync {
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis->u32Magic = RTAIOMGRFILE_MAGIC;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis->cRefs = 1;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis->hFile = hFile;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync pThis->pAioMgr = hAioMgr;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rc = RTFileAioCtxAssociateWithFile(pThis->pAioMgr->hAioCtx, hFile);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync if (RT_FAILURE(rc))
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rtAioMgrFileDestroy(pThis);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync }
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync else
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rc = VERR_NO_MEMORY;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync if (RT_SUCCESS(rc))
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync *phAioMgrFile = pThis;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync return rc;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync}
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(uint32_t) RTAioMgrFileRetain(RTAIOMGRFILE hAioMgrFile)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PRTAIOMGRFILEINT pThis = hAioMgrFile;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(hAioMgrFile != NIL_RTAIOMGRFILE, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertPtrReturn(pThis, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(pThis->u32Magic == RTAIOMGRFILE_MAGIC, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertMsg(cRefs > 1 && cRefs < _1G, ("%#x %p\n", cRefs, pThis));
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync return cRefs;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync}
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(uint32_t) RTAioMgrFileRelease(RTAIOMGRFILE hAioMgrFile)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PRTAIOMGRFILEINT pThis = hAioMgrFile;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync if (pThis == NIL_RTAIOMGRFILE)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync return 0;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertPtrReturn(pThis, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertReturn(pThis->u32Magic == RTAIOMGRFILE_MAGIC, UINT32_MAX);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync AssertMsg(cRefs < _1G, ("%#x %p\n", cRefs, pThis));
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync if (cRefs == 0)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync rtAioMgrFileDestroy(pThis);
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync return cRefs;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync}
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(int) RTAioMgrFileRead(RTAIOMGRFILE hAioMgrFile, RTFOFF off,
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PRTSGBUF pSgBuf, size_t cbRead, void *pvUser)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync return VERR_NOT_IMPLEMENTED;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync}
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(int) RTAioMgrFileWrite(RTAIOMGRFILE hAioMgrFile, RTFOFF off,
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync PRTSGBUF pSgBuf, size_t cbWrite, void *pvUser)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync return VERR_NOT_IMPLEMENTED;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync}
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsyncRTDECL(int) RTAioMgrFileFlush(RTAIOMGRFILE hAioMgrFile, void *pvUser)
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync{
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync return VERR_NOT_IMPLEMENTED;
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync}
527eff46676ca64c098dc7d1bf0857408c3a6f82vboxsync