]>
Commit | Line | Data |
---|---|---|
a553a5a3 | 1 | /* |
b510f3a1 | 2 | * DEBUG: section 01 Main Loop |
a553a5a3 | 3 | * AUTHOR: Harvest Derived |
4 | * | |
5 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
6 | * ---------------------------------------------------------- | |
7 | * | |
8 | * Squid is the result of efforts by numerous individuals from | |
9 | * the Internet community; see the CONTRIBUTORS file for full | |
10 | * details. Many organizations have provided support for Squid's | |
11 | * development; see the SPONSORS file for full details. Squid is | |
12 | * Copyrighted (C) 2001 by the Regents of the University of | |
13 | * California; see the COPYRIGHT file for full details. Squid | |
14 | * incorporates software developed and/or copyrighted by other | |
15 | * sources; see the CREDITS file for full details. | |
16 | * | |
17 | * This program is free software; you can redistribute it and/or modify | |
18 | * it under the terms of the GNU General Public License as published by | |
19 | * the Free Software Foundation; either version 2 of the License, or | |
20 | * (at your option) any later version. | |
26ac0430 | 21 | * |
a553a5a3 | 22 | * This program is distributed in the hope that it will be useful, |
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | * GNU General Public License for more details. | |
26ac0430 | 26 | * |
a553a5a3 | 27 | * You should have received a copy of the GNU General Public License |
28 | * along with this program; if not, write to the Free Software | |
29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
30 | * | |
31 | */ | |
32 | ||
f7f3304a | 33 | #include "squid.h" |
314782d4 | 34 | #include "AsyncEngine.h" |
602d9612 | 35 | #include "base/AsyncCallQueue.h" |
582c2af2 | 36 | #include "Debug.h" |
a553a5a3 | 37 | #include "EventLoop.h" |
314782d4 FC |
38 | #include "SquidTime.h" |
39 | ||
bef81ea5 | 40 | EventLoop::EventLoop() : errcount(0), last_loop(false), timeService(NULL), |
2e2f336f AJ |
41 | primaryEngine(NULL), |
42 | loop_delay(EVENT_LOOP_TIMEOUT), | |
43 | error(false), | |
44 | runOnceResult(false) | |
a553a5a3 | 45 | {} |
46 | ||
bef81ea5 | 47 | void |
48 | EventLoop::checkEngine(AsyncEngine * engine, bool const primary) | |
49 | { | |
50 | int requested_delay; | |
51 | ||
52 | if (!primary) | |
53 | requested_delay = engine->checkEvents(0); | |
54 | else | |
55 | requested_delay = engine->checkEvents(loop_delay); | |
56 | ||
57 | if (requested_delay < 0) | |
58 | switch (requested_delay) { | |
59 | ||
60 | case AsyncEngine::EVENT_IDLE: | |
61 | debugs(1, 9, "Engine " << engine << " is idle."); | |
62 | break; | |
63 | ||
64 | case AsyncEngine::EVENT_ERROR: | |
65 | runOnceResult = false; | |
66 | error = true; | |
67 | break; | |
68 | ||
69 | default: | |
70 | fatal_dump("unknown AsyncEngine result"); | |
71 | } | |
72 | else { | |
73 | /* not idle or error */ | |
74 | runOnceResult = false; | |
75 | ||
76 | if (requested_delay < loop_delay) | |
77 | loop_delay = requested_delay; | |
78 | } | |
79 | } | |
80 | ||
a553a5a3 | 81 | void |
82 | EventLoop::prepareToRun() | |
83 | { | |
84 | last_loop = false; | |
85 | errcount = 0; | |
86 | } | |
87 | ||
8ff3fa2e | 88 | void |
89 | EventLoop::registerEngine(AsyncEngine *engine) | |
90 | { | |
91 | engines.push_back(engine); | |
92 | } | |
93 | ||
a553a5a3 | 94 | void |
95 | EventLoop::run() | |
96 | { | |
97 | prepareToRun(); | |
98 | ||
3d0ac046 | 99 | while (!runOnce()); |
a553a5a3 | 100 | } |
101 | ||
8ff3fa2e | 102 | bool |
a553a5a3 | 103 | EventLoop::runOnce() |
104 | { | |
1fd133b5 | 105 | bool sawActivity = false; |
bef81ea5 | 106 | runOnceResult = true; |
107 | error = false; | |
6289f91f | 108 | loop_delay = EVENT_LOOP_TIMEOUT; |
8ff3fa2e | 109 | |
1fd133b5 | 110 | AsyncEngine *waitingEngine = primaryEngine; |
111 | if (!waitingEngine && !engines.empty()) | |
112 | waitingEngine = engines.back(); | |
bef81ea5 | 113 | |
1fd133b5 | 114 | do { |
115 | // generate calls and events | |
116 | typedef engine_vector::iterator EVI; | |
117 | for (EVI i = engines.begin(); i != engines.end(); ++i) { | |
118 | if (*i != waitingEngine) | |
119 | checkEngine(*i, false); | |
120 | } | |
bef81ea5 | 121 | |
1fd133b5 | 122 | // dispatch calls accumulated so far |
123 | sawActivity = dispatchCalls(); | |
124 | if (sawActivity) | |
125 | runOnceResult = false; | |
26ac0430 | 126 | } while (sawActivity); |
8ff3fa2e | 127 | |
1fd133b5 | 128 | if (waitingEngine != NULL) |
129 | checkEngine(waitingEngine, true); | |
bef81ea5 | 130 | |
8ff3fa2e | 131 | if (timeService != NULL) |
132 | timeService->tick(); | |
a553a5a3 | 133 | |
1fd133b5 | 134 | // dispatch calls scheduled by waitingEngine and timeService |
135 | sawActivity = dispatchCalls(); | |
136 | if (sawActivity) | |
137 | runOnceResult = false; | |
a553a5a3 | 138 | |
8ff3fa2e | 139 | if (error) { |
140 | ++errcount; | |
fa84c01d | 141 | debugs(1, DBG_CRITICAL, "Select loop Error. Retry " << errcount); |
8ff3fa2e | 142 | } else |
143 | errcount = 0; | |
a553a5a3 | 144 | |
8ff3fa2e | 145 | if (errcount == 10) |
146 | return true; | |
a553a5a3 | 147 | |
8ff3fa2e | 148 | if (last_loop) |
149 | return true; | |
a553a5a3 | 150 | |
bef81ea5 | 151 | return runOnceResult; |
152 | } | |
153 | ||
1fd133b5 | 154 | // dispatches calls accumulated during checkEngine() |
155 | bool | |
156 | EventLoop::dispatchCalls() | |
157 | { | |
158 | bool dispatchedSome = AsyncCallQueue::Instance().fire(); | |
159 | return dispatchedSome; | |
160 | } | |
161 | ||
bef81ea5 | 162 | void |
163 | EventLoop::setPrimaryEngine(AsyncEngine * engine) | |
164 | { | |
165 | for (engine_vector::iterator i = engines.begin(); | |
166 | i != engines.end(); ++i) | |
167 | if (*i == engine) { | |
168 | primaryEngine = engine; | |
169 | return; | |
170 | } | |
171 | ||
172 | fatal("EventLoop::setPrimaryEngine: No such engine!."); | |
8ff3fa2e | 173 | } |
a553a5a3 | 174 | |
8ff3fa2e | 175 | void |
176 | EventLoop::setTimeService(TimeEngine *engine) | |
177 | { | |
178 | timeService = engine; | |
a553a5a3 | 179 | } |
180 | ||
181 | void | |
182 | EventLoop::stop() | |
183 | { | |
184 | last_loop = true; | |
185 | } |