]> git.ipfire.org Git - thirdparty/squid.git/blob - src/EventLoop.cc
merge from trunk
[thirdparty/squid.git] / src / EventLoop.cc
1 /*
2 * DEBUG: section 01 Main Loop
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.
21 *
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.
26 *
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
33 #include "squid.h"
34 #include "AsyncEngine.h"
35 #include "base/AsyncCallQueue.h"
36 #include "Debug.h"
37 #include "EventLoop.h"
38 #include "SquidTime.h"
39
40 EventLoop *EventLoop::Running = NULL;
41
42 EventLoop::EventLoop() : errcount(0), last_loop(false), timeService(NULL),
43 primaryEngine(NULL),
44 loop_delay(EVENT_LOOP_TIMEOUT),
45 error(false),
46 runOnceResult(false)
47 {}
48
49 void
50 EventLoop::checkEngine(AsyncEngine * engine, bool const primary)
51 {
52 int requested_delay;
53
54 if (!primary)
55 requested_delay = engine->checkEvents(0);
56 else
57 requested_delay = engine->checkEvents(loop_delay);
58
59 if (requested_delay < 0)
60 switch (requested_delay) {
61
62 case AsyncEngine::EVENT_IDLE:
63 debugs(1, 9, "Engine " << engine << " is idle.");
64 break;
65
66 case AsyncEngine::EVENT_ERROR:
67 runOnceResult = false;
68 error = true;
69 break;
70
71 default:
72 fatal_dump("unknown AsyncEngine result");
73 }
74 else {
75 /* not idle or error */
76 runOnceResult = false;
77
78 if (requested_delay < loop_delay)
79 loop_delay = requested_delay;
80 }
81 }
82
83 void
84 EventLoop::prepareToRun()
85 {
86 last_loop = false;
87 errcount = 0;
88 }
89
90 void
91 EventLoop::registerEngine(AsyncEngine *engine)
92 {
93 engines.push_back(engine);
94 }
95
96 void
97 EventLoop::run()
98 {
99 prepareToRun();
100
101 assert(!Running);
102 Running = this;
103
104 while (!runOnce());
105
106 Running = NULL;
107 }
108
109 bool
110 EventLoop::runOnce()
111 {
112 bool sawActivity = false;
113 runOnceResult = true;
114 error = false;
115 loop_delay = EVENT_LOOP_TIMEOUT;
116
117 AsyncEngine *waitingEngine = primaryEngine;
118 if (!waitingEngine && !engines.empty())
119 waitingEngine = engines.back();
120
121 do {
122 // generate calls and events
123 typedef engine_vector::iterator EVI;
124 for (EVI i = engines.begin(); i != engines.end(); ++i) {
125 if (*i != waitingEngine)
126 checkEngine(*i, false);
127 }
128
129 // dispatch calls accumulated so far
130 sawActivity = dispatchCalls();
131 if (sawActivity)
132 runOnceResult = false;
133 } while (sawActivity);
134
135 if (waitingEngine != NULL)
136 checkEngine(waitingEngine, true);
137
138 if (timeService != NULL)
139 timeService->tick();
140
141 // dispatch calls scheduled by waitingEngine and timeService
142 sawActivity = dispatchCalls();
143 if (sawActivity)
144 runOnceResult = false;
145
146 if (error) {
147 ++errcount;
148 debugs(1, DBG_CRITICAL, "Select loop Error. Retry " << errcount);
149 } else
150 errcount = 0;
151
152 if (errcount == 10)
153 return true;
154
155 if (last_loop)
156 return true;
157
158 return runOnceResult;
159 }
160
161 // dispatches calls accumulated during checkEngine()
162 bool
163 EventLoop::dispatchCalls()
164 {
165 bool dispatchedSome = AsyncCallQueue::Instance().fire();
166 return dispatchedSome;
167 }
168
169 void
170 EventLoop::setPrimaryEngine(AsyncEngine * engine)
171 {
172 for (engine_vector::iterator i = engines.begin();
173 i != engines.end(); ++i)
174 if (*i == engine) {
175 primaryEngine = engine;
176 return;
177 }
178
179 fatal("EventLoop::setPrimaryEngine: No such engine!.");
180 }
181
182 void
183 EventLoop::setTimeService(TimeEngine *engine)
184 {
185 timeService = engine;
186 }
187
188 void
189 EventLoop::stop()
190 {
191 last_loop = true;
192 }