]> git.ipfire.org Git - thirdparty/squid.git/blob - src/EventLoop.cc
247ada8e23001d145222f7681c672535088881d1
[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 loop_delay(EVENT_LOOP_TIMEOUT),
43 error(false),
44 runOnceResult(false)
45 {}
46
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
81 void
82 EventLoop::prepareToRun()
83 {
84 last_loop = false;
85 errcount = 0;
86 }
87
88 void
89 EventLoop::registerEngine(AsyncEngine *engine)
90 {
91 engines.push_back(engine);
92 }
93
94 void
95 EventLoop::run()
96 {
97 prepareToRun();
98
99 while (!runOnce());
100 }
101
102 bool
103 EventLoop::runOnce()
104 {
105 bool sawActivity = false;
106 runOnceResult = true;
107 error = false;
108 loop_delay = EVENT_LOOP_TIMEOUT;
109
110 AsyncEngine *waitingEngine = primaryEngine;
111 if (!waitingEngine && !engines.empty())
112 waitingEngine = engines.back();
113
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 }
121
122 // dispatch calls accumulated so far
123 sawActivity = dispatchCalls();
124 if (sawActivity)
125 runOnceResult = false;
126 } while (sawActivity);
127
128 if (waitingEngine != NULL)
129 checkEngine(waitingEngine, true);
130
131 if (timeService != NULL)
132 timeService->tick();
133
134 // dispatch calls scheduled by waitingEngine and timeService
135 sawActivity = dispatchCalls();
136 if (sawActivity)
137 runOnceResult = false;
138
139 if (error) {
140 ++errcount;
141 debugs(1, DBG_CRITICAL, "Select loop Error. Retry " << errcount);
142 } else
143 errcount = 0;
144
145 if (errcount == 10)
146 return true;
147
148 if (last_loop)
149 return true;
150
151 return runOnceResult;
152 }
153
154 // dispatches calls accumulated during checkEngine()
155 bool
156 EventLoop::dispatchCalls()
157 {
158 bool dispatchedSome = AsyncCallQueue::Instance().fire();
159 return dispatchedSome;
160 }
161
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!.");
173 }
174
175 void
176 EventLoop::setTimeService(TimeEngine *engine)
177 {
178 timeService = engine;
179 }
180
181 void
182 EventLoop::stop()
183 {
184 last_loop = true;
185 }