EventLoop.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 01 Main Loop */
10 
11 #include "squid.h"
12 #include "AsyncEngine.h"
13 #include "base/AsyncCallQueue.h"
14 #include "Debug.h"
15 #include "EventLoop.h"
16 #include "fatal.h"
17 #include "SquidTime.h"
18 
19 EventLoop *EventLoop::Running = NULL;
20 
21 EventLoop::EventLoop() : errcount(0), last_loop(false), timeService(NULL),
22  primaryEngine(NULL),
23  loop_delay(EVENT_LOOP_TIMEOUT),
24  error(false),
25  runOnceResult(false)
26 {}
27 
28 void
29 EventLoop::checkEngine(AsyncEngine * engine, bool const primary)
30 {
31  int requested_delay;
32 
33  if (!primary)
34  requested_delay = engine->checkEvents(0);
35  else
36  requested_delay = engine->checkEvents(loop_delay);
37 
38  if (requested_delay < 0)
39  switch (requested_delay) {
40 
42  debugs(1, 9, "Engine " << engine << " is idle.");
43  break;
44 
46  runOnceResult = false;
47  error = true;
48  break;
49 
50  default:
51  fatal_dump("unknown AsyncEngine result");
52  }
53  else {
54  /* not idle or error */
55  runOnceResult = false;
56 
57  if (requested_delay < loop_delay)
58  loop_delay = requested_delay;
59  }
60 }
61 
62 void
64 {
65  last_loop = false;
66  errcount = 0;
67 }
68 
69 void
71 {
72  engines.push_back(engine);
73 }
74 
75 void
77 {
78  prepareToRun();
79 
80  assert(!Running);
81  Running = this;
82 
83  while (!runOnce());
84 
85  Running = NULL;
86 }
87 
88 bool
90 {
91  bool sawActivity = false;
92  runOnceResult = true;
93  error = false;
95 
96  AsyncEngine *waitingEngine = primaryEngine;
97  if (!waitingEngine && !engines.empty())
98  waitingEngine = engines.back();
99 
100  do {
101  // generate calls and events
102  typedef engine_vector::iterator EVI;
103  for (EVI i = engines.begin(); i != engines.end(); ++i) {
104  if (*i != waitingEngine)
105  checkEngine(*i, false);
106  }
107 
108  // dispatch calls accumulated so far
109  sawActivity = dispatchCalls();
110  if (sawActivity)
111  runOnceResult = false;
112  } while (sawActivity);
113 
114  if (waitingEngine != NULL)
115  checkEngine(waitingEngine, true);
116 
117  if (timeService != NULL)
118  timeService->tick();
119 
120  // dispatch calls scheduled by waitingEngine and timeService
121  sawActivity = dispatchCalls();
122  if (sawActivity)
123  runOnceResult = false;
124 
125  if (error) {
126  ++errcount;
127  debugs(1, DBG_CRITICAL, "Select loop Error. Retry " << errcount);
128  } else
129  errcount = 0;
130 
131  if (errcount == 10)
132  return true;
133 
134  if (last_loop)
135  return true;
136 
137  return runOnceResult;
138 }
139 
140 // dispatches calls accumulated during checkEngine()
141 bool
143 {
144  bool dispatchedSome = AsyncCallQueue::Instance().fire();
145  return dispatchedSome;
146 }
147 
148 void
150 {
151  for (engine_vector::iterator i = engines.begin();
152  i != engines.end(); ++i)
153  if (*i == engine) {
154  primaryEngine = engine;
155  return;
156  }
157 
158  fatal("EventLoop::setPrimaryEngine: No such engine!.");
159 }
160 
161 void
163 {
164  timeService = engine;
165 }
166 
167 void
169 {
170  last_loop = true;
171 }
172 
#define assert(EX)
Definition: assert.h:17
void checkEngine(AsyncEngine *engine, bool const primary)
Definition: EventLoop.cc:29
void run()
Definition: EventLoop.cc:76
void error(char *format,...)
int i
Definition: membanger.c:49
#define DBG_CRITICAL
Definition: Debug.h:44
virtual int checkEvents(int timeout)=0
void fatal_dump(const char *message)
Definition: fatal.cc:89
void stop()
Definition: EventLoop.cc:168
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
#define EVENT_LOOP_TIMEOUT
Definition: EventLoop.h:14
bool last_loop
Definition: EventLoop.h:84
bool error
Definition: EventLoop.h:90
int errcount
Definition: EventLoop.h:68
TimeEngine * timeService
Definition: EventLoop.h:87
void fatal(const char *message)
Definition: fatal.cc:39
bool dispatchCalls()
Definition: EventLoop.cc:142
AsyncEngine * primaryEngine
Definition: EventLoop.h:88
void prepareToRun()
Definition: EventLoop.cc:63
bool runOnceResult
Definition: EventLoop.h:91
static EventLoop * Running
Definition: EventLoop.h:72
engine_vector engines
Definition: EventLoop.h:86
void setPrimaryEngine(AsyncEngine *engine)
Definition: EventLoop.cc:149
bool runOnce()
Definition: EventLoop.cc:89
int loop_delay
Definition: EventLoop.h:89
virtual void tick()
Definition: stub_time.cc:27
void registerEngine(AsyncEngine *engine)
Definition: EventLoop.cc:70
void setTimeService(TimeEngine *engine)
Definition: EventLoop.cc:162
#define NULL
Definition: types.h:166
static AsyncCallQueue & Instance()
#define false
Definition: GnuRegex.c:233

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors