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