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