]> git.ipfire.org Git - thirdparty/squid.git/blob - src/EventLoop.cc
Reversed bug #2011 fix because it may slow down ICAP, BodyPipe, and other code
[thirdparty/squid.git] / src / EventLoop.cc
1
2 /*
3 * $Id: EventLoop.cc,v 1.5 2007/07/23 19:55:21 rousskov Exp $
4 *
5 * DEBUG: section 1 Main Loop
6 * AUTHOR: Harvest Derived
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "EventLoop.h"
37
38 EventLoop::EventLoop() : errcount(0), last_loop(false), timeService(NULL),
39 primaryEngine(NULL)
40 {}
41
42 void
43 EventLoop::checkEngine(AsyncEngine * engine, bool const primary)
44 {
45 int requested_delay;
46
47 if (!primary)
48 requested_delay = engine->checkEvents(0);
49 else
50 requested_delay = engine->checkEvents(loop_delay);
51
52 if (requested_delay < 0)
53 switch (requested_delay) {
54
55 case AsyncEngine::EVENT_IDLE:
56 debugs(1, 9, "Engine " << engine << " is idle.");
57 break;
58
59 case AsyncEngine::EVENT_ERROR:
60 runOnceResult = false;
61 error = true;
62 break;
63
64 default:
65 fatal_dump("unknown AsyncEngine result");
66 }
67 else {
68 /* not idle or error */
69 runOnceResult = false;
70
71 if (requested_delay < loop_delay)
72 loop_delay = requested_delay;
73 }
74 }
75
76 void
77 EventLoop::prepareToRun()
78 {
79 last_loop = false;
80 errcount = 0;
81 }
82
83 void
84 EventLoop::registerDispatcher(CompletionDispatcher *dispatcher)
85 {
86 dispatchers.push_back(dispatcher);
87 }
88
89 void
90 EventLoop::registerEngine(AsyncEngine *engine)
91 {
92 engines.push_back(engine);
93 }
94
95 void
96 EventLoop::run()
97 {
98 prepareToRun();
99
100 while (!runOnce())
101
102 ;
103 }
104
105 bool
106 EventLoop::runOnce()
107 {
108 runOnceResult = true;
109 error = false;
110 loop_delay = 10; /* 10 ms default delay */
111
112 for (engine_vector::iterator i = engines.begin();
113 i != engines.end(); ++i) {
114 /* check the primary outside the loop */
115
116 if (*i == primaryEngine)
117 continue;
118
119 /* special case the last engine to be primary */
120 checkEngine(*i, primaryEngine == NULL && (i - engines.end() == -1));
121 }
122
123 if (primaryEngine != NULL)
124 checkEngine(primaryEngine, true);
125
126 if (timeService != NULL)
127 timeService->tick();
128
129 for (dispatcher_vector::iterator i = dispatchers.begin();
130 i != dispatchers.end(); ++i)
131 if ((*i)->dispatch())
132 runOnceResult = false;
133
134 if (error) {
135 ++errcount;
136 debugs(1, 0, "Select loop Error. Retry " << errcount);
137 } else
138 errcount = 0;
139
140 if (errcount == 10)
141 return true;
142
143 if (last_loop)
144 return true;
145
146 return runOnceResult;
147 }
148
149 void
150 EventLoop::setPrimaryEngine(AsyncEngine * engine)
151 {
152 for (engine_vector::iterator i = engines.begin();
153 i != engines.end(); ++i)
154 if (*i == engine) {
155 primaryEngine = engine;
156 return;
157 }
158
159 fatal("EventLoop::setPrimaryEngine: No such engine!.");
160 }
161
162 void
163 EventLoop::setTimeService(TimeEngine *engine)
164 {
165 timeService = engine;
166 }
167
168 void
169 EventLoop::stop()
170 {
171 last_loop = true;
172 }