From b26a5ecc646cdda84d0ceb498e921166d943da4f Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Wed, 4 Feb 2026 17:56:34 +1300 Subject: [PATCH 1/6] Initial libengines.la structure --- configure.ac | 1 + src/Makefile.am | 2 ++ src/engines/Makefile.am | 12 ++++++++++++ 3 files changed, 15 insertions(+) create mode 100644 src/engines/Makefile.am diff --git a/configure.ac b/configure.ac index 605cea996f9..4ee76fd089b 100644 --- a/configure.ac +++ b/configure.ac @@ -2578,6 +2578,7 @@ AC_CONFIG_FILES([ src/DiskIO/DiskThreads/Makefile src/DiskIO/IpcIo/Makefile src/DiskIO/Mmapped/Makefile + src/engines/Makefile src/error/Makefile src/eui/Makefile src/format/Makefile diff --git a/src/Makefile.am b/src/Makefile.am index e6bb8997182..a38fee26f65 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -22,6 +22,7 @@ SUBDIRS = \ time \ debug \ base \ + engines \ anyp \ helper \ dns \ @@ -536,6 +537,7 @@ squid_LDADD = \ $(ADAPTATION_LIBS) \ html/libhtml.la \ $(SNMP_LIBS) \ + engines/libengines.la \ mem/libmem.la \ store/libstore.la \ time/libtime.la \ diff --git a/src/engines/Makefile.am b/src/engines/Makefile.am new file mode 100644 index 00000000000..1daaece9384 --- /dev/null +++ b/src/engines/Makefile.am @@ -0,0 +1,12 @@ +## Copyright (C) 1996-2026 The Squid Software Foundation and contributors +## +## Squid software is distributed under GPLv2+ license and includes +## contributions from numerous individuals and organizations. +## Please see the COPYING and CONTRIBUTORS files for details. +## + +include $(top_srcdir)/src/Common.am + +noinst_LTLIBRARIES = libengines.la + +libengines_la_SOURCES = From f07c21a0c78539427d9f211284096ae33ad8d871 Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Wed, 4 Feb 2026 18:28:13 +1300 Subject: [PATCH 2/6] Move AsyncEngine to libengines --- src/AsyncEngine.cc | 11 ----------- src/EventLoop.cc | 1 - src/EventLoop.h | 3 +-- src/Makefile.am | 2 -- src/adaptation/ecap/ServiceRep.cc | 1 - src/{ => engines}/AsyncEngine.h | 0 src/engines/Makefile.am | 7 ++++++- src/event.h | 2 +- src/tests/testEventLoop.cc | 1 - 9 files changed, 8 insertions(+), 20 deletions(-) delete mode 100644 src/AsyncEngine.cc rename src/{ => engines}/AsyncEngine.h (100%) diff --git a/src/AsyncEngine.cc b/src/AsyncEngine.cc deleted file mode 100644 index e3bae3dea99..00000000000 --- a/src/AsyncEngine.cc +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright (C) 1996-2026 The Squid Software Foundation and contributors - * - * Squid software is distributed under GPLv2+ license and includes - * contributions from numerous individuals and organizations. - * Please see the COPYING and CONTRIBUTORS files for details. - */ - -#include "squid.h" -#include "AsyncEngine.h" - diff --git a/src/EventLoop.cc b/src/EventLoop.cc index cdc3e40eb93..82de81a2386 100644 --- a/src/EventLoop.cc +++ b/src/EventLoop.cc @@ -9,7 +9,6 @@ /* DEBUG: section 01 Main Loop */ #include "squid.h" -#include "AsyncEngine.h" #include "base/AsyncCallQueue.h" #include "debug/Stream.h" #include "EventLoop.h" diff --git a/src/EventLoop.h b/src/EventLoop.h index d80b2420e08..630d199a8a8 100644 --- a/src/EventLoop.h +++ b/src/EventLoop.h @@ -9,14 +9,13 @@ #ifndef SQUID_SRC_EVENTLOOP_H #define SQUID_SRC_EVENTLOOP_H +#include "engines/AsyncEngine.h" #include "time/forward.h" #include #define EVENT_LOOP_TIMEOUT 1000 /* 1s timeout */ -class AsyncEngine; - /** An event loop. An event loop is the core inner loop of squid. * The event loop can be run until exit, or once. After it finishes control * returns to the caller. If desired it can be run again. diff --git a/src/Makefile.am b/src/Makefile.am index a38fee26f65..6bab8680e3e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -215,8 +215,6 @@ squid_SOURCES = \ $(WINSVC_SOURCE) \ AccessLogEntry.cc \ AccessLogEntry.h \ - AsyncEngine.cc \ - AsyncEngine.h \ AuthReg.h \ BodyPipe.cc \ BodyPipe.h \ diff --git a/src/adaptation/ecap/ServiceRep.cc b/src/adaptation/ecap/ServiceRep.cc index 0cc8f65bcc1..0a697539c59 100644 --- a/src/adaptation/ecap/ServiceRep.cc +++ b/src/adaptation/ecap/ServiceRep.cc @@ -13,7 +13,6 @@ #include "adaptation/ecap/Host.h" #include "adaptation/ecap/ServiceRep.h" #include "adaptation/ecap/XactionRep.h" -#include "AsyncEngine.h" #include "base/TextException.h" #include "debug/Stream.h" #include "EventLoop.h" diff --git a/src/AsyncEngine.h b/src/engines/AsyncEngine.h similarity index 100% rename from src/AsyncEngine.h rename to src/engines/AsyncEngine.h diff --git a/src/engines/Makefile.am b/src/engines/Makefile.am index 1daaece9384..d4250e2bbfc 100644 --- a/src/engines/Makefile.am +++ b/src/engines/Makefile.am @@ -9,4 +9,9 @@ include $(top_srcdir)/src/Common.am noinst_LTLIBRARIES = libengines.la -libengines_la_SOURCES = +libengines_la_SOURCES = \ + AsyncEngine.h + +# XXX: autoconf only supplies CXXCOMPILE et al. properly when there is a .cc to build. +# XXX: temporary workaround for make check testHeaders auto-build. +EXCLUDE_FROM_HDR_TESTING += $(libengines_la_SOURCES) diff --git a/src/event.h b/src/event.h index e1fbeb72a7c..65e36abcc54 100644 --- a/src/event.h +++ b/src/event.h @@ -9,8 +9,8 @@ #ifndef SQUID_SRC_EVENT_H #define SQUID_SRC_EVENT_H -#include "AsyncEngine.h" #include "base/Packable.h" +#include "engines/AsyncEngine.h" #include "mem/forward.h" /* event scheduling facilities - run a callback after a given time period. */ diff --git a/src/tests/testEventLoop.cc b/src/tests/testEventLoop.cc index 8e3091442fa..3be99b1ed45 100644 --- a/src/tests/testEventLoop.cc +++ b/src/tests/testEventLoop.cc @@ -7,7 +7,6 @@ */ #include "squid.h" -#include "AsyncEngine.h" #include "compat/cppunit.h" #include "EventLoop.h" #include "time/Engine.h" From 9e573fbb1cb784358e8ec5ac99029d68b3e9ff74 Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Wed, 4 Feb 2026 18:28:44 +1300 Subject: [PATCH 3/6] Polish AsyncEngine documentation and source format --- src/engines/AsyncEngine.h | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/engines/AsyncEngine.h b/src/engines/AsyncEngine.h index b3fb05ee038..177beb4ccf4 100644 --- a/src/engines/AsyncEngine.h +++ b/src/engines/AsyncEngine.h @@ -6,35 +6,34 @@ * Please see the COPYING and CONTRIBUTORS files for details. */ -#ifndef SQUID_SRC_ASYNCENGINE_H -#define SQUID_SRC_ASYNCENGINE_H +#ifndef SQUID_SRC_ENGINES_ASYNCENGINE_H +#define SQUID_SRC_ENGINES_ASYNCENGINE_H -/* Abstract interface for async engines which an event loop can utilise. +/** + * Abstract interface for async engines which an event loop can utilise. * * Some implementations will be truly async, others like the event engine * will be pseudo async. */ - class AsyncEngine { - public: /* error codes returned from checkEvents. If the return value is not * negative, then it is the requested delay until the next call. If it is * negative, it is one of the following codes: */ enum CheckError { - /* this engine is completely idle: it has no pending events, and nothing - * registered with it that can create events - */ + /// This engine is completely idle: it has no pending events, and nothing + /// registered with it that can create events. EVENT_IDLE = -1, - /* some error has occurred in this engine */ + /// Some error has occurred in this engine. EVENT_ERROR = -2 }; virtual ~AsyncEngine() {} - /* Check the engine for events. If there are events that have completed, + /** + * Check the engine for events. If there are events that have completed, * the engine should at this point hand them off to their dispatcher. * Engines that operate asynchronously - i.e. the DiskThreads engine - * should hand events off to their dispatcher as they arrive rather than @@ -42,7 +41,7 @@ class AsyncEngine * use this call as the time to perform their checks with the OS for new * events. * - * The return value is the status code of the event checking. If its a + * The return value is the status code of the event checking. If it is a * non-negative value then it is used as hint for the minimum requested * time before checkEvents is called again. I.e. the event engine knows * how long it is until the next event will be scheduled - so it will @@ -50,10 +49,9 @@ class AsyncEngine * * The timeout value is a requested timeout for this engine - the engine * should not block for more than this period. (If it takes longer than the - * timeout to do actual checks that's fine though undesirable). + * timeout to do actual checks that is fine, though undesirable). */ virtual int checkEvents(int timeout) = 0; }; -#endif /* SQUID_SRC_ASYNCENGINE_H */ - +#endif /* SQUID_SRC_ENGINES_ASYNCENGINE_H */ From a789f2898437affa6f03c1e04e9b8f3d5c72f0c8 Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Fri, 6 Feb 2026 17:07:16 +1300 Subject: [PATCH 4/6] Reviewer requested forward.h --- src/engines/Makefile.am | 3 ++- src/engines/forward.h | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 src/engines/forward.h diff --git a/src/engines/Makefile.am b/src/engines/Makefile.am index d4250e2bbfc..1f47cfd68f1 100644 --- a/src/engines/Makefile.am +++ b/src/engines/Makefile.am @@ -10,7 +10,8 @@ include $(top_srcdir)/src/Common.am noinst_LTLIBRARIES = libengines.la libengines_la_SOURCES = \ - AsyncEngine.h + AsyncEngine.h \ + forward.h # XXX: autoconf only supplies CXXCOMPILE et al. properly when there is a .cc to build. # XXX: temporary workaround for make check testHeaders auto-build. diff --git a/src/engines/forward.h b/src/engines/forward.h new file mode 100644 index 00000000000..9f2273baa17 --- /dev/null +++ b/src/engines/forward.h @@ -0,0 +1,14 @@ +/* + * Copyright (C) 1996-2026 The Squid Software Foundation and contributors + * + * Squid software is distributed under GPLv2+ license and includes + * contributions from numerous individuals and organizations. + * Please see the COPYING and CONTRIBUTORS files for details. + */ + +#ifndef SQUID_SRC_ENGINES_FORWARD_H +#define SQUID_SRC_ENGINES_FORWARD_H + +class AsyncEngine; + +#endif /* SQUID_SRC_ENGINES_FORWARD_H */ From 2243c0ac469997871762963a902c983164fb2ed7 Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Sat, 7 Feb 2026 13:49:02 +1300 Subject: [PATCH 5/6] Move EventLoop to libengines.la ... and polish documentation --- src/Makefile.am | 46 +++++++-------- src/adaptation/ecap/ServiceRep.cc | 2 +- src/{ => engines}/EventLoop.cc | 3 +- src/{ => engines}/EventLoop.h | 56 +++++++++++-------- src/engines/Makefile.am | 6 +- src/engines/forward.h | 1 + src/main.cc | 2 +- src/tests/Stub.am | 2 +- .../{stub_EventLoop.cc => stub_libengines.cc} | 9 +-- src/tests/testEventLoop.cc | 2 +- src/tests/testStoreSupport.cc | 1 - src/tests/testStoreSupport.h | 2 +- 12 files changed, 70 insertions(+), 62 deletions(-) rename src/{ => engines}/EventLoop.cc (99%) rename src/{ => engines}/EventLoop.h (58%) rename src/tests/{stub_EventLoop.cc => stub_libengines.cc} (77%) diff --git a/src/Makefile.am b/src/Makefile.am index 6bab8680e3e..8446dbb7297 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -245,8 +245,6 @@ squid_SOURCES = \ Downloader.h \ ETag.cc \ ETag.h \ - EventLoop.cc \ - EventLoop.h \ ExternalACL.h \ ExternalACLEntry.cc \ ExternalACLEntry.h \ @@ -998,6 +996,24 @@ tests_testString_LDADD = \ $(XTRA_LIBS) tests_testString_LDFLAGS = $(LIBADD_DL) +# Tests of engines/* + +check_PROGRAMS += tests/testEventLoop +tests_testEventLoop_SOURCES = \ + tests/testEventLoop.cc +nodist_tests_testEventLoop_SOURCES = \ + tests/stub_SBuf.cc \ + tests/stub_debug.cc \ + tests/stub_fatal.cc \ + tests/stub_libtime.cc +tests_testEventLoop_LDADD = \ + engines/libengines.la \ + base/libbase.la \ + $(LIBCPPUNIT_LIBS) \ + $(COMPAT_LIB) \ + $(XTRA_LIBS) +tests_testEventLoop_LDFLAGS = $(LIBADD_DL) + ## Tests of fs/* if ENABLE_FS_ROCK @@ -1015,7 +1031,6 @@ tests_testRock_SOURCES = \ ConfigOption.cc \ ConfigParser.cc \ ETag.cc \ - EventLoop.cc \ FadingCounter.cc \ FileMap.h \ tests/stub_HelperChildConfig.cc \ @@ -1146,6 +1161,7 @@ tests_testRock_LDADD = \ $(SSL_LIBS) \ ipc/libipc.la \ base/libbase.la \ + engines/libengines.la \ mem/libmem.la \ store/libstore.la \ $(ADAPTATION_LIBS) \ @@ -1185,7 +1201,6 @@ tests_testUfs_SOURCES = \ ConfigOption.cc \ ConfigParser.cc \ ETag.cc \ - EventLoop.cc \ FadingCounter.cc \ FileMap.h \ tests/stub_HelperChildConfig.cc \ @@ -1323,6 +1338,7 @@ tests_testUfs_LDADD = \ comm/libcomm.la \ dns/libdns.la \ base/libbase.la \ + engines/libengines.la \ mem/libmem.la \ store/libstore.la \ $(ADAPTATION_LIBS) \ @@ -1359,7 +1375,6 @@ tests_testStore_SOURCES = \ ConfigOption.cc \ ConfigParser.cc \ ETag.cc \ - EventLoop.cc \ FileMap.h \ tests/stub_HelperChildConfig.cc \ HttpBody.cc \ @@ -1486,6 +1501,7 @@ tests_testStore_LDADD= \ fs/libfs.la \ mgr/libmgr.la \ anyp/libanyp.la \ + engines/libengines.la \ mem/libmem.la \ store/libstore.la \ sbuf/libsbuf.la \ @@ -1522,7 +1538,6 @@ tests_testDiskIO_SOURCES = \ ConfigParser.cc \ tests/testDiskIO.cc \ tests/stub_ETag.cc \ - EventLoop.cc \ FadingCounter.cc \ FileMap.h \ tests/stub_HelperChildConfig.cc \ @@ -1666,6 +1681,7 @@ tests_testDiskIO_LDADD = \ ipc/libipc.la \ dns/libdns.la \ base/libbase.la \ + engines/libengines.la \ mem/libmem.la \ sbuf/libsbuf.la \ $(top_builddir)/lib/libmisccontainers.la \ @@ -2080,7 +2096,6 @@ tests_testCacheManager_SOURCES = \ CpuAffinitySet.cc \ CpuAffinitySet.h \ tests/stub_ETag.cc \ - tests/stub_EventLoop.cc \ ExternalACLEntry.cc \ FadingCounter.cc \ FileMap.h \ @@ -2183,6 +2198,7 @@ tests_testCacheManager_SOURCES = \ tests/stub_libauth.cc \ tests/stub_libauth_acls.cc \ tests/stub_libdiskio.cc \ + tests/stub_libengines.cc \ tests/stub_liberror.cc \ tests/stub_libsecurity.cc \ tests/stub_main_cc.cc \ @@ -2375,22 +2391,6 @@ tests_testEvent_LDADD = \ $(XTRA_LIBS) tests_testEvent_LDFLAGS = $(LIBADD_DL) -check_PROGRAMS += tests/testEventLoop -tests_testEventLoop_SOURCES = \ - tests/testEventLoop.cc -nodist_tests_testEventLoop_SOURCES = \ - EventLoop.cc \ - tests/stub_SBuf.cc \ - tests/stub_debug.cc \ - tests/stub_fatal.cc \ - tests/stub_libtime.cc -tests_testEventLoop_LDADD = \ - base/libbase.la \ - $(LIBCPPUNIT_LIBS) \ - $(COMPAT_LIB) \ - $(XTRA_LIBS) -tests_testEventLoop_LDFLAGS = $(LIBADD_DL) - check_PROGRAMS += tests/testIoManip tests_testIoManip_SOURCES = \ tests/testIoManip.cc diff --git a/src/adaptation/ecap/ServiceRep.cc b/src/adaptation/ecap/ServiceRep.cc index 0a697539c59..ac8e0de54bc 100644 --- a/src/adaptation/ecap/ServiceRep.cc +++ b/src/adaptation/ecap/ServiceRep.cc @@ -15,7 +15,7 @@ #include "adaptation/ecap/XactionRep.h" #include "base/TextException.h" #include "debug/Stream.h" -#include "EventLoop.h" +#include "engines/EventLoop.h" #if HAVE_LIBECAP_ADAPTER_SERVICE_H #include diff --git a/src/EventLoop.cc b/src/engines/EventLoop.cc similarity index 99% rename from src/EventLoop.cc rename to src/engines/EventLoop.cc index 82de81a2386..3b13842a9c3 100644 --- a/src/EventLoop.cc +++ b/src/engines/EventLoop.cc @@ -11,7 +11,7 @@ #include "squid.h" #include "base/AsyncCallQueue.h" #include "debug/Stream.h" -#include "EventLoop.h" +#include "engines/EventLoop.h" #include "fatal.h" #include "time/Engine.h" @@ -168,4 +168,3 @@ EventLoop::stop() { last_loop = true; } - diff --git a/src/EventLoop.h b/src/engines/EventLoop.h similarity index 58% rename from src/EventLoop.h rename to src/engines/EventLoop.h index 630d199a8a8..59a43a931cd 100644 --- a/src/EventLoop.h +++ b/src/engines/EventLoop.h @@ -28,47 +28,52 @@ class EventLoop public: EventLoop(); - /** register an async engine which will be given the opportunity to perform + /** + * Register an engine which will be given the opportunity to perform * in-main-thread tasks each event loop. */ - void registerEngine(AsyncEngine *engine); + void registerEngine(AsyncEngine *); - /** start the event loop running. The loop will run until it is stopped by + /** + * Start this event loop running. The loop will run until it is stopped by * calling stop(), or when the loop is completely idle - nothing * dispatched in a loop, and all engines idle. */ void run(); - /** run the loop once. This may not complete all events! It should therefor - * be used with care. + /** + * This may not complete all events! use with care. * TODO: signal in runOnce whether or not the loop is over - IDLE vs OK vs * TIMEOUT? */ bool runOnce(); - /** set the primary async engine. The primary async engine receives the - * lowest requested timeout gathered from the other engines each loop. - * (There is a default of 10ms if all engines are idle or request higher - * delays). + /** + * The primary async engine receives the lowest requested timeout gathered + * from the other engines each loop. + * There is a default of 10ms if all engines are idle or request higher + * delays. + * * If no primary has been nominated, the last async engine added is * implicitly the default. */ - void setPrimaryEngine(AsyncEngine * engine); + void setPrimaryEngine(AsyncEngine *); - /** set the time service. There can be only one time service set at any - * time. The time service is invoked on each loop + /** + * Nominate a time service to invoke on each loop. + * There can be only one engine acting as time service. */ void setTimeService(Time::Engine *); - /** stop the event loop - it will finish the current loop and then return to the - * caller of run(). - */ + /// Finish the current loop and then return to the caller of run(). void stop(); int errcount; - /// the [main program] loop running now; may be nil - /// for simplicity, we assume there are no concurrent loops + /** + * The [main program] loop running now; may be nil. + * For simplicity, we assume there are no concurrent loops + */ static EventLoop *Running; private: @@ -76,7 +81,7 @@ class EventLoop void prepareToRun(); /** check an individual engine */ - void checkEngine(AsyncEngine * engine, bool const primary); + void checkEngine(AsyncEngine *, const bool primary); /** dispatch calls and events scheduled during checkEngine() */ bool dispatchCalls(); @@ -86,10 +91,15 @@ class EventLoop engine_vector engines; Time::Engine *timeService; AsyncEngine * primaryEngine; - int loop_delay; /**< the delay to be given to the primary engine */ - bool error; /**< has an error occurred in this loop */ - bool runOnceResult; /**< the result from runOnce */ -}; -#endif /* SQUID_SRC_EVENTLOOP_H */ + /// the delay to be given to the primary engine + int loop_delay; + + /// has an error occurred in this loop + bool error; + + /// the result from runOnce + bool runOnceResult; +}; +#endif /* SQUID_SRC_ENGINES_EVENTLOOP_H */ diff --git a/src/engines/Makefile.am b/src/engines/Makefile.am index 1f47cfd68f1..1e426b4e36e 100644 --- a/src/engines/Makefile.am +++ b/src/engines/Makefile.am @@ -11,8 +11,6 @@ noinst_LTLIBRARIES = libengines.la libengines_la_SOURCES = \ AsyncEngine.h \ + EventLoop.cc \ + EventLoop.h \ forward.h - -# XXX: autoconf only supplies CXXCOMPILE et al. properly when there is a .cc to build. -# XXX: temporary workaround for make check testHeaders auto-build. -EXCLUDE_FROM_HDR_TESTING += $(libengines_la_SOURCES) diff --git a/src/engines/forward.h b/src/engines/forward.h index 9f2273baa17..bc256391085 100644 --- a/src/engines/forward.h +++ b/src/engines/forward.h @@ -10,5 +10,6 @@ #define SQUID_SRC_ENGINES_FORWARD_H class AsyncEngine; +class EventLoop; #endif /* SQUID_SRC_ENGINES_FORWARD_H */ diff --git a/src/main.cc b/src/main.cc index c9ad17f905d..ea44bea7c78 100644 --- a/src/main.cc +++ b/src/main.cc @@ -33,7 +33,7 @@ #include "dns/forward.h" #include "errorpage.h" #include "event.h" -#include "EventLoop.h" +#include "engines/EventLoop.h" #include "ExternalACL.h" #include "fd.h" #include "format/Token.h" diff --git a/src/tests/Stub.am b/src/tests/Stub.am index 47026296055..8611d568f85 100644 --- a/src/tests/Stub.am +++ b/src/tests/Stub.am @@ -16,7 +16,6 @@ STUB_SOURCE = \ tests/stub_CommIO.cc \ tests/stub_DelayId.cc \ tests/stub_ETag.cc \ - tests/stub_EventLoop.cc \ tests/stub_HelperChildConfig.cc \ tests/stub_HttpControlMsg.cc \ tests/stub_HttpHeader.cc \ @@ -64,6 +63,7 @@ STUB_SOURCE = \ tests/stub_libcomm.cc \ tests/stub_libdiskio.cc \ tests/stub_libdns.cc \ + tests/stub_libengines.cc \ tests/stub_liberror.cc \ tests/stub_libeui.cc \ tests/stub_libformat.cc \ diff --git a/src/tests/stub_EventLoop.cc b/src/tests/stub_libengines.cc similarity index 77% rename from src/tests/stub_EventLoop.cc rename to src/tests/stub_libengines.cc index 8567acfbaed..c7b0d2220b6 100644 --- a/src/tests/stub_EventLoop.cc +++ b/src/tests/stub_libengines.cc @@ -7,16 +7,17 @@ */ #include "squid.h" -#include "EventLoop.h" -#define STUB_API "EventLoop.cc" +#define STUB_API "engines/libengines.la" #include "tests/STUB.h" -EventLoop *EventLoop::Running = nullptr; +#include "engines/AsyncEngine.h" +#include "engines/EventLoop.h" +EventLoop *EventLoop::Running = nullptr; EventLoop::EventLoop(): errcount(0), last_loop(false), timeService(nullptr), primaryEngine(nullptr), loop_delay(0), error(false), runOnceResult(false) STUB_NOP - void EventLoop::registerEngine(AsyncEngine *) STUB +void EventLoop::registerEngine(AsyncEngine *) STUB diff --git a/src/tests/testEventLoop.cc b/src/tests/testEventLoop.cc index 3be99b1ed45..1d296f138c6 100644 --- a/src/tests/testEventLoop.cc +++ b/src/tests/testEventLoop.cc @@ -8,7 +8,7 @@ #include "squid.h" #include "compat/cppunit.h" -#include "EventLoop.h" +#include "engines/EventLoop.h" #include "time/Engine.h" #include "unitTestMain.h" diff --git a/src/tests/testStoreSupport.cc b/src/tests/testStoreSupport.cc index 42b220a6edf..03b3f87b536 100644 --- a/src/tests/testStoreSupport.cc +++ b/src/tests/testStoreSupport.cc @@ -8,7 +8,6 @@ #include "squid.h" #include "event.h" -#include "EventLoop.h" #include "testStoreSupport.h" /* construct a stock loop with event dispatching, a time service that advances diff --git a/src/tests/testStoreSupport.h b/src/tests/testStoreSupport.h index 1808e9df8ad..2bf24e7da30 100644 --- a/src/tests/testStoreSupport.h +++ b/src/tests/testStoreSupport.h @@ -9,7 +9,7 @@ #ifndef SQUID_SRC_TESTS_TESTSTORESUPPORT_H #define SQUID_SRC_TESTS_TESTSTORESUPPORT_H -#include "EventLoop.h" +#include "engines/EventLoop.h" #include "time/Engine.h" /* construct a stock loop with event dispatching, a time service that advances From a634410f9a95b1ff9e9d00108e0e018079e0f6aa Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Sat, 7 Feb 2026 15:11:26 +1300 Subject: [PATCH 6/6] Polish code style of EventLoop * use C++11 initialization for class members * inline trivial class methods * update unit test stub file * update const correctness in API methods * use ranged loops (removes need for typedef) * wrap EVENT_LOOP_TIMEOUT to allow user customization Logic changes tested with curl HTTP fetch and logging. --- src/engines/EventLoop.cc | 51 +++++++----------------------------- src/engines/EventLoop.h | 48 +++++++++++++++++---------------- src/tests/stub_libengines.cc | 9 +++---- 3 files changed, 38 insertions(+), 70 deletions(-) diff --git a/src/engines/EventLoop.cc b/src/engines/EventLoop.cc index 3b13842a9c3..c663786cdbb 100644 --- a/src/engines/EventLoop.cc +++ b/src/engines/EventLoop.cc @@ -17,22 +17,10 @@ EventLoop *EventLoop::Running = nullptr; -EventLoop::EventLoop() : errcount(0), last_loop(false), timeService(nullptr), - primaryEngine(nullptr), - loop_delay(EVENT_LOOP_TIMEOUT), - error(false), - runOnceResult(false) -{} - void -EventLoop::checkEngine(AsyncEngine * engine, bool const primary) +EventLoop::checkEngine(AsyncEngine * const engine, const bool primary) { - int requested_delay; - - if (!primary) - requested_delay = engine->checkEvents(0); - else - requested_delay = engine->checkEvents(loop_delay); + auto requested_delay = engine->checkEvents(primary ? loop_delay : 0); if (requested_delay < 0) switch (requested_delay) { @@ -65,12 +53,6 @@ EventLoop::prepareToRun() errcount = 0; } -void -EventLoop::registerEngine(AsyncEngine *engine) -{ - engines.push_back(engine); -} - void EventLoop::run() { @@ -98,10 +80,9 @@ EventLoop::runOnce() do { // generate calls and events - typedef engine_vector::iterator EVI; - for (EVI i = engines.begin(); i != engines.end(); ++i) { - if (*i != waitingEngine) - checkEngine(*i, false); + for (auto engine : engines) { + if (engine != waitingEngine) + checkEngine(engine, false); } // dispatch calls accumulated so far @@ -138,33 +119,21 @@ EventLoop::runOnce() // dispatches calls accumulated during checkEngine() bool -EventLoop::dispatchCalls() +EventLoop::dispatchCalls() const { bool dispatchedSome = AsyncCallQueue::Instance().fire(); return dispatchedSome; } void -EventLoop::setPrimaryEngine(AsyncEngine * engine) +EventLoop::setPrimaryEngine(AsyncEngine * const e) { - for (engine_vector::iterator i = engines.begin(); - i != engines.end(); ++i) - if (*i == engine) { + for (auto engine : engines) { + if (engine == e) { primaryEngine = engine; return; } + } fatal("EventLoop::setPrimaryEngine: No such engine!."); } - -void -EventLoop::setTimeService(Time::Engine *engine) -{ - timeService = engine; -} - -void -EventLoop::stop() -{ - last_loop = true; -} diff --git a/src/engines/EventLoop.h b/src/engines/EventLoop.h index 59a43a931cd..665c89905ff 100644 --- a/src/engines/EventLoop.h +++ b/src/engines/EventLoop.h @@ -14,7 +14,10 @@ #include -#define EVENT_LOOP_TIMEOUT 1000 /* 1s timeout */ +#if !defined(EVENT_LOOP_TIMEOUT) +/// Maximum timeout for loop checks of the primary engine, in milliseconds. +#define EVENT_LOOP_TIMEOUT 1000 +#endif /** An event loop. An event loop is the core inner loop of squid. * The event loop can be run until exit, or once. After it finishes control @@ -24,15 +27,12 @@ */ class EventLoop { - public: - EventLoop(); - /** * Register an engine which will be given the opportunity to perform * in-main-thread tasks each event loop. */ - void registerEngine(AsyncEngine *); + void registerEngine(AsyncEngine *e) { engines.emplace_back(e); } /** * Start this event loop running. The loop will run until it is stopped by @@ -51,55 +51,57 @@ class EventLoop /** * The primary async engine receives the lowest requested timeout gathered * from the other engines each loop. - * There is a default of 10ms if all engines are idle or request higher - * delays. + * There is a default of EVENT_LOOP_TIMEOUT if all engines are idle, + * or request higher delays. * * If no primary has been nominated, the last async engine added is * implicitly the default. */ - void setPrimaryEngine(AsyncEngine *); + void setPrimaryEngine(AsyncEngine * const); /** * Nominate a time service to invoke on each loop. * There can be only one engine acting as time service. */ - void setTimeService(Time::Engine *); + void setTimeService(Time::Engine * const e) { timeService = e; } /// Finish the current loop and then return to the caller of run(). - void stop(); - - int errcount; + void stop() { last_loop = true; } +public: /** * The [main program] loop running now; may be nil. * For simplicity, we assume there are no concurrent loops */ static EventLoop *Running; + /// How many errors have occured so far in this iteration of the loop. + int errcount = 0; + private: /** setup state variables prior to running */ void prepareToRun(); /** check an individual engine */ - void checkEngine(AsyncEngine *, const bool primary); + void checkEngine(AsyncEngine * const, const bool primary); - /** dispatch calls and events scheduled during checkEngine() */ - bool dispatchCalls(); + /** dispatch AsyncCalls scheduled during checkEngine() */ + bool dispatchCalls() const; - bool last_loop; - typedef std::vector engine_vector; - engine_vector engines; - Time::Engine *timeService; - AsyncEngine * primaryEngine; +private: + bool last_loop = false; + std::vector engines; + Time::Engine *timeService = nullptr; + AsyncEngine *primaryEngine = nullptr; /// the delay to be given to the primary engine - int loop_delay; + int loop_delay = EVENT_LOOP_TIMEOUT; /// has an error occurred in this loop - bool error; + bool error = false; /// the result from runOnce - bool runOnceResult; + bool runOnceResult = false; }; #endif /* SQUID_SRC_ENGINES_EVENTLOOP_H */ diff --git a/src/tests/stub_libengines.cc b/src/tests/stub_libengines.cc index c7b0d2220b6..6261d44f058 100644 --- a/src/tests/stub_libengines.cc +++ b/src/tests/stub_libengines.cc @@ -15,9 +15,6 @@ #include "engines/EventLoop.h" EventLoop *EventLoop::Running = nullptr; -EventLoop::EventLoop(): errcount(0), last_loop(false), timeService(nullptr), - primaryEngine(nullptr), loop_delay(0), error(false), runOnceResult(false) - STUB_NOP - -void EventLoop::registerEngine(AsyncEngine *) STUB - +void EventLoop::run() STUB +bool EventLoop::runOnce() STUB +void EventLoop::setPrimaryEngine(AsyncEngine * const) STUB