]>
Commit | Line | Data |
---|---|---|
a553a5a3 | 1 | |
2 | /* | |
3 | * $Id: EventLoop.cc,v 1.1 2006/08/07 02:28:22 robertc 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 "squid.h" | |
37 | #include "event.h" | |
38 | #include "EventLoop.h" | |
39 | #include "comm.h" | |
40 | ||
41 | EventLoop::EventLoop() : errcount(0), last_loop(false) | |
42 | {} | |
43 | ||
44 | void | |
45 | EventLoop::prepareToRun() | |
46 | { | |
47 | last_loop = false; | |
48 | errcount = 0; | |
49 | } | |
50 | ||
51 | void | |
52 | EventLoop::registerDispatcher(CompletionDispatcher *dispatcher) | |
53 | { | |
54 | dispatchers.push_back(dispatcher); | |
55 | } | |
56 | ||
57 | void | |
58 | EventLoop::run() | |
59 | { | |
60 | prepareToRun(); | |
61 | ||
62 | while (!last_loop) | |
63 | runOnce(); | |
64 | } | |
65 | ||
66 | void | |
67 | EventLoop::runOnce() | |
68 | { | |
69 | int loop_delay = EventScheduler::GetInstance()->checkEvents(); | |
70 | ||
71 | for (dispatcher_vector::iterator i = dispatchers.begin(); | |
72 | i != dispatchers.end(); ++i) | |
73 | (*i)->dispatch(); | |
74 | ||
75 | if (loop_delay < 0) | |
76 | loop_delay = 0; | |
77 | ||
78 | switch (comm_select(loop_delay)) { | |
79 | ||
80 | case COMM_OK: | |
81 | errcount = 0; /* reset if successful */ | |
82 | break; | |
83 | ||
84 | case COMM_IDLE: | |
85 | /* TODO: rather than busy loop, if everything has returned IDLE we should | |
86 | * wait for a reasonable timeout period, - if everything returned IDLE | |
87 | * then not only is there no work to do, there is no work coming in - | |
88 | * all the comm loops have no fds registered, and all the other | |
89 | * async engines have no work active or pending. | |
90 | * ... perhaps we can have a query method to say 'when could there be | |
91 | * work' - i.e. the event dispatcher can return the next event in its | |
92 | * queue, and everything else can return -1. | |
93 | */ | |
94 | errcount = 0; | |
95 | break; | |
96 | ||
97 | case COMM_ERROR: | |
98 | errcount++; | |
99 | debugs(1, 0, "Select loop Error. Retry " << errcount); | |
100 | ||
101 | if (errcount == 10) | |
102 | fatal_dump("Select Loop failed 10 times.!"); | |
103 | ||
104 | break; | |
105 | ||
106 | case COMM_TIMEOUT: | |
107 | break; | |
108 | ||
109 | case COMM_SHUTDOWN: | |
110 | stop(); | |
111 | ||
112 | break; | |
113 | ||
114 | default: | |
115 | fatal_dump("MAIN: Internal error -- this should never happen."); | |
116 | ||
117 | break; | |
118 | } | |
119 | } | |
120 | ||
121 | void | |
122 | EventLoop::stop() | |
123 | { | |
124 | last_loop = true; | |
125 | } |