]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/EventLoop.cc
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 01 Main Loop */
12 #include "AsyncEngine.h"
13 #include "base/AsyncCallQueue.h"
15 #include "EventLoop.h"
17 #include "SquidTime.h"
19 EventLoop
*EventLoop::Running
= NULL
;
21 EventLoop::EventLoop() : errcount(0), last_loop(false), timeService(NULL
),
23 loop_delay(EVENT_LOOP_TIMEOUT
),
29 EventLoop::checkEngine(AsyncEngine
* engine
, bool const primary
)
34 requested_delay
= engine
->checkEvents(0);
36 requested_delay
= engine
->checkEvents(loop_delay
);
38 if (requested_delay
< 0)
39 switch (requested_delay
) {
41 case AsyncEngine::EVENT_IDLE
:
42 debugs(1, 9, "Engine " << engine
<< " is idle.");
45 case AsyncEngine::EVENT_ERROR
:
46 runOnceResult
= false;
51 fatal_dump("unknown AsyncEngine result");
54 /* not idle or error */
55 runOnceResult
= false;
57 if (requested_delay
< loop_delay
)
58 loop_delay
= requested_delay
;
63 EventLoop::prepareToRun()
70 EventLoop::registerEngine(AsyncEngine
*engine
)
72 engines
.push_back(engine
);
91 bool sawActivity
= false;
94 loop_delay
= EVENT_LOOP_TIMEOUT
;
96 AsyncEngine
*waitingEngine
= primaryEngine
;
97 if (!waitingEngine
&& !engines
.empty())
98 waitingEngine
= engines
.back();
101 // generate calls and events
102 typedef engine_vector::iterator EVI
;
103 for (EVI i
= engines
.begin(); i
!= engines
.end(); ++i
) {
104 if (*i
!= waitingEngine
)
105 checkEngine(*i
, false);
108 // dispatch calls accumulated so far
109 sawActivity
= dispatchCalls();
111 runOnceResult
= false;
112 } while (sawActivity
);
114 if (waitingEngine
!= NULL
)
115 checkEngine(waitingEngine
, true);
117 if (timeService
!= NULL
)
120 // dispatch calls scheduled by waitingEngine and timeService
121 sawActivity
= dispatchCalls();
123 runOnceResult
= false;
127 debugs(1, DBG_CRITICAL
, "Select loop Error. Retry " << errcount
);
137 return runOnceResult
;
140 // dispatches calls accumulated during checkEngine()
142 EventLoop::dispatchCalls()
144 bool dispatchedSome
= AsyncCallQueue::Instance().fire();
145 return dispatchedSome
;
149 EventLoop::setPrimaryEngine(AsyncEngine
* engine
)
151 for (engine_vector::iterator i
= engines
.begin();
152 i
!= engines
.end(); ++i
)
154 primaryEngine
= engine
;
158 fatal("EventLoop::setPrimaryEngine: No such engine!.");
162 EventLoop::setTimeService(TimeEngine
*engine
)
164 timeService
= engine
;