]> git.ipfire.org Git - thirdparty/squid.git/blame - src/EventLoop.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / EventLoop.h
CommitLineData
a553a5a3 1/*
4ac4a490 2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
a553a5a3 3 *
bbc27441
AJ
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.
a553a5a3 7 */
8
9#ifndef SQUID_EVENTLOOP_H
10#define SQUID_EVENTLOOP_H
11
c8ea3cc0 12#include <vector>
a553a5a3 13
f53969cc 14#define EVENT_LOOP_TIMEOUT 1000 /* 1s timeout */
6289f91f 15
314782d4
FC
16class AsyncEngine;
17class TimeEngine;
18
6289f91f 19/** An event loop. An event loop is the core inner loop of squid.
a553a5a3 20 * The event loop can be run until exit, or once. After it finishes control
21 * returns to the caller. If desired it can be run again.
6289f91f 22 \par
a553a5a3 23 * The event loop cannot be run once it is running until it has finished.
24 */
a553a5a3 25class EventLoop
26{
27
28public:
29 EventLoop();
6289f91f
AJ
30
31 /** register an async engine which will be given the opportunity to perform
8ff3fa2e 32 * in-main-thread tasks each event loop.
33 */
34 void registerEngine(AsyncEngine *engine);
6289f91f
AJ
35
36 /** start the event loop running. The loop will run until it is stopped by
26ac0430 37 * calling stop(), or when the loop is completely idle - nothing
8ff3fa2e 38 * dispatched in a loop, and all engines idle.
39 */
a553a5a3 40 void run();
6289f91f
AJ
41
42 /** run the loop once. This may not complete all events! It should therefor
a553a5a3 43 * be used with care.
44 * TODO: signal in runOnce whether or not the loop is over - IDLE vs OK vs
45 * TIMEOUT?
46 */
8ff3fa2e 47 bool runOnce();
6289f91f
AJ
48
49 /** set the primary async engine. The primary async engine recieves the
bef81ea5 50 * lowest requested timeout gathered from the other engines each loop.
51 * (There is a default of 10ms if all engines are idle or request higher
52 * delays).
26ac0430 53 * If no primary has been nominated, the last async engine added is
bef81ea5 54 * implicitly the default.
55 */
56 void setPrimaryEngine(AsyncEngine * engine);
6289f91f
AJ
57
58 /** set the time service. There can be only one time service set at any
26ac0430 59 * time. The time service is invoked on each loop
8ff3fa2e 60 */
61 void setTimeService(TimeEngine *engine);
6289f91f
AJ
62
63 /** stop the event loop - it will finish the current loop and then return to the
a553a5a3 64 * caller of run().
65 */
66 void stop();
67
8ff3fa2e 68 int errcount;
69
0a720258
AR
70 /// the [main program] loop running now; may be nil
71 /// for simplicity, we assume there are no concurrent loops
72 static EventLoop *Running;
73
a553a5a3 74private:
6289f91f 75 /** setup state variables prior to running */
a553a5a3 76 void prepareToRun();
6289f91f
AJ
77
78 /** check an individual engine */
bef81ea5 79 void checkEngine(AsyncEngine * engine, bool const primary);
6289f91f
AJ
80
81 /** dispatch calls and events scheduled during checkEngine() */
1fd133b5 82 bool dispatchCalls();
83
a553a5a3 84 bool last_loop;
c8ea3cc0 85 typedef std::vector<AsyncEngine *> engine_vector;
8ff3fa2e 86 engine_vector engines;
87 TimeEngine * timeService;
bef81ea5 88 AsyncEngine * primaryEngine;
6289f91f
AJ
89 int loop_delay; /**< the delay to be given to the primary engine */
90 bool error; /**< has an error occured in this loop */
91 bool runOnceResult; /**< the result from runOnce */
a553a5a3 92};
93
a553a5a3 94#endif /* SQUID_EVENTLOOP_H */
f53969cc 95