/*
- * $Id: EventLoop.cc,v 1.5 2007/07/23 19:55:21 rousskov Exp $
+ * $Id: EventLoop.cc,v 1.6 2008/02/12 23:49:44 rousskov Exp $
*
* DEBUG: section 1 Main Loop
* AUTHOR: Harvest Derived
*/
#include "EventLoop.h"
+#include "AsyncCallQueue.h"
EventLoop::EventLoop() : errcount(0), last_loop(false), timeService(NULL),
primaryEngine(NULL)
errcount = 0;
}
-void
-EventLoop::registerDispatcher(CompletionDispatcher *dispatcher)
-{
- dispatchers.push_back(dispatcher);
-}
-
void
EventLoop::registerEngine(AsyncEngine *engine)
{
bool
EventLoop::runOnce()
{
+ bool sawActivity = false;
runOnceResult = true;
error = false;
loop_delay = 10; /* 10 ms default delay */
- for (engine_vector::iterator i = engines.begin();
- i != engines.end(); ++i) {
- /* check the primary outside the loop */
+ AsyncEngine *waitingEngine = primaryEngine;
+ if (!waitingEngine && !engines.empty())
+ waitingEngine = engines.back();
- if (*i == primaryEngine)
- continue;
+ do {
+ // generate calls and events
+ typedef engine_vector::iterator EVI;
+ for (EVI i = engines.begin(); i != engines.end(); ++i) {
+ if (*i != waitingEngine)
+ checkEngine(*i, false);
+ }
- /* special case the last engine to be primary */
- checkEngine(*i, primaryEngine == NULL && (i - engines.end() == -1));
- }
+ // dispatch calls accumulated so far
+ sawActivity = dispatchCalls();
+ if (sawActivity)
+ runOnceResult = false;
+ } while (sawActivity);
- if (primaryEngine != NULL)
- checkEngine(primaryEngine, true);
+ if (waitingEngine != NULL)
+ checkEngine(waitingEngine, true);
if (timeService != NULL)
timeService->tick();
- for (dispatcher_vector::iterator i = dispatchers.begin();
- i != dispatchers.end(); ++i)
- if ((*i)->dispatch())
- runOnceResult = false;
+ // dispatch calls scheduled by waitingEngine and timeService
+ sawActivity = dispatchCalls();
+ if (sawActivity)
+ runOnceResult = false;
if (error) {
++errcount;
return runOnceResult;
}
+// dispatches calls accumulated during checkEngine()
+bool
+EventLoop::dispatchCalls()
+{
+ bool dispatchedSome = AsyncCallQueue::Instance().fire();
+ return dispatchedSome;
+}
+
void
EventLoop::setPrimaryEngine(AsyncEngine * engine)
{
/*
- * $Id: EventLoop.h,v 1.3 2006/08/19 12:31:21 robertc Exp $
+ * $Id: EventLoop.h,v 1.4 2008/02/12 23:49:44 rousskov Exp $
*
*
* SQUID Web Proxy Cache http://www.squid-cache.org/
#include "squid.h"
#include "Array.h"
#include "AsyncEngine.h"
-#include "CompletionDispatcher.h"
#include "SquidTime.h"
/* An event loop. An event loop is the core inner loop of squid.
public:
EventLoop();
- /* register an event dispatcher to be invoked on each event loop. */
- void registerDispatcher(CompletionDispatcher *dispatcher);
/* register an async engine which will be given the opportunity to perform
* in-main-thread tasks each event loop.
*/
void prepareToRun();
/* check an individual engine */
void checkEngine(AsyncEngine * engine, bool const primary);
+ /* dispatch calls and events scheduled during checkEngine() */
+ bool dispatchCalls();
+
bool last_loop;
- typedef Vector<CompletionDispatcher *> dispatcher_vector;
- dispatcher_vector dispatchers;
typedef Vector<AsyncEngine *> engine_vector;
engine_vector engines;
TimeEngine * timeService;