reqpool.cpp revision 8867771015571c5542d39e393d7fe6304421a928
/* $Id$ */
/** @file
* IPRT - Request Pool.
*/
/*
* Copyright (C) 2006-2011 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/critsect.h>
#include <iprt/semaphore.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef struct RTREQPOOLTHREAD
{
/** Node in the RTREQPOOLINT::IdleThreads list. */
/** Node in the RTREQPOOLINT::WorkerThreads list. */
/** The submit timestamp of the pending request. */
/** The submit timestamp of the request processing. */
/** When this CPU went idle the last time. */
/** The number of requests processed by this thread. */
/** Total time the requests processed by this thread took to process. */
/** Total time the requests processed by this thread had to wait in
* the queue before being scheduled. */
/** The CPU this was scheduled last time we checked. */
/** The submitter will put an incoming request here when scheduling an idle
* thread. */
/** The request the thread is currently processing. */
PRTREQINT volatile pPendingReq;
/** The thread handle. */
/** Nano seconds timestamp representing the birth time of the thread. */
/** Pointer to the request thread pool instance the thread is associated
* with. */
struct RTREQPOOLINT *pPool;
/** Pointer to a worker thread. */
typedef RTREQPOOLTHREAD *PRTREQPOOLTHREAD;
/**
* Request thread pool instance data.
*/
typedef struct RTREQPOOLINT
{
/** Magic value (RTREQPOOL_MAGIC). */
/** The worker thread type. */
/** The current number of worker threads. */
/** The maximum number of worker threads. */
/** The number of threads which should be spawned before throttling kicks
* in. */
/** The minimum number of worker threads. */
/** The number of milliseconds a thread needs to be idle before it is
* considered for retirement. */
/** The max number of milliseconds to push back a submitter before creating
* a new worker thread once the threshold has been reached. */
/** The minimum number of milliseconds to push back a submitter before
* creating a new worker thread once the threshold has been reached. */
/** The current submitter push back in milliseconds.
* This is recalculated when worker threads come and go. */
/** Statistics: The total number of threads created. */
/** Statistics: The timestamp when the last thread was created. */
/** Linked list of worker threads. */
/** Event semaphore that submitters block on when pushing back . */
/** Critical section serializing access to members of this structure. */
/** Destruction indicator. The worker threads checks in their loop. */
bool volatile fDestructing;
/** Reference counter. */
/** Number of threads pushing back. */
uint32_t volatile cPushingBack;
/** The number of idle thread or threads in the process of becoming
* idle. This is increased before the to-be-idle thread tries to enter
* the critical section and add itself to the list. */
uint32_t volatile cIdleThreads;
/** Linked list of idle threads. */
/** Head of the request FIFO. */
/** Where to insert the next request. */
} RTREQPOOLINT;
/** Pointer to a request thread pool instance. */
typedef RTREQPOOLINT *PRTREQPOOLINT;
{
else
}
{
/*
* Update thread state.
*/
/*
* Do the actual processing.
*/
/** @todo */
/*
* Update thread statistics and state.
*/
pThread->cReqProcessed++;
}
{
/*
* The work loop.
*/
while (pPool->fDestructing)
{
/*
* Pending work?
*/
if (pReq)
else
{
/* Recheck the todo request pointer after entering the critsect. */
if (!pReq)
{
/* Any pending requests in the queue? */
if (pReq)
{
}
}
if (pReq)
{
/*
* Un-idle ourselves and process the request.
*/
{
}
}
else
{
/*
* Nothing to do, go idle.
*/
{
}
}
}
}
/*
* Clean up on the way out.
*/
/** @todo .... */
return VINF_SUCCESS;
}
{
/*
* Prepare the request.
*/
/*
* Try schedule the request to any currently idle thread.
*/
if (pThread)
{
/** @todo CPU affinity... */
return VINF_SUCCESS;
}
/*
* Put the request in the pending queue.
*/
/*
* If there is an incoming worker thread already or we've reached the
* maximum number of worker threads, we're done.
*/
if ( pPool->cIdleThreads > 0
{
return VINF_SUCCESS;
}
/*
* Push back before creating a new worker thread.
*/
{
pPool->cPushingBack++;
/** @todo this is everything but perfect... it makes wake up order
* assumptions. A better solution would be having a lazily
* allocated push back event on each request. */
pPool->cPushingBack--;
}
/*
* Create a new thread for processing the request, or should we wait?
*/
if (pThread)
{
pPool->cCurThreads++;
pPool->cThreadsCreated++;
static uint32_t s_idThread = 0;
if (RT_SUCCESS(rc))
else
{
pPool->cCurThreads--;
}
}
return VINF_SUCCESS;
}