]>
Commit | Line | Data |
---|---|---|
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 | #ifndef SQUID_EVENTLOOP_H | |
10 | #define SQUID_EVENTLOOP_H | |
11 | ||
12 | #include <vector> | |
13 | ||
14 | #define EVENT_LOOP_TIMEOUT 1000 /* 1s timeout */ | |
15 | ||
16 | class AsyncEngine; | |
17 | class TimeEngine; | |
18 | ||
19 | /** An event loop. An event loop is the core inner loop of squid. | |
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. | |
22 | \par | |
23 | * The event loop cannot be run once it is running until it has finished. | |
24 | */ | |
25 | class EventLoop | |
26 | { | |
27 | ||
28 | public: | |
29 | EventLoop(); | |
30 | ||
31 | /** register an async engine which will be given the opportunity to perform | |
32 | * in-main-thread tasks each event loop. | |
33 | */ | |
34 | void registerEngine(AsyncEngine *engine); | |
35 | ||
36 | /** start the event loop running. The loop will run until it is stopped by | |
37 | * calling stop(), or when the loop is completely idle - nothing | |
38 | * dispatched in a loop, and all engines idle. | |
39 | */ | |
40 | void run(); | |
41 | ||
42 | /** run the loop once. This may not complete all events! It should therefor | |
43 | * be used with care. | |
44 | * TODO: signal in runOnce whether or not the loop is over - IDLE vs OK vs | |
45 | * TIMEOUT? | |
46 | */ | |
47 | bool runOnce(); | |
48 | ||
49 | /** set the primary async engine. The primary async engine recieves the | |
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). | |
53 | * If no primary has been nominated, the last async engine added is | |
54 | * implicitly the default. | |
55 | */ | |
56 | void setPrimaryEngine(AsyncEngine * engine); | |
57 | ||
58 | /** set the time service. There can be only one time service set at any | |
59 | * time. The time service is invoked on each loop | |
60 | */ | |
61 | void setTimeService(TimeEngine *engine); | |
62 | ||
63 | /** stop the event loop - it will finish the current loop and then return to the | |
64 | * caller of run(). | |
65 | */ | |
66 | void stop(); | |
67 | ||
68 | int errcount; | |
69 | ||
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 | ||
74 | private: | |
75 | /** setup state variables prior to running */ | |
76 | void prepareToRun(); | |
77 | ||
78 | /** check an individual engine */ | |
79 | void checkEngine(AsyncEngine * engine, bool const primary); | |
80 | ||
81 | /** dispatch calls and events scheduled during checkEngine() */ | |
82 | bool dispatchCalls(); | |
83 | ||
84 | bool last_loop; | |
85 | typedef std::vector<AsyncEngine *> engine_vector; | |
86 | engine_vector engines; | |
87 | TimeEngine * timeService; | |
88 | AsyncEngine * primaryEngine; | |
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 */ | |
92 | }; | |
93 | ||
94 | #endif /* SQUID_EVENTLOOP_H */ | |
95 |