An event loop: sockets and timers for one thread

src/loop.c, not used yet: one thread waiting for many sockets and
timers, calling back what waits for them, for clients to be served by
a thread each serves many of.

Sockets are waited for with epoll on Linux, kqueue on the BSDs and
macOS, poll() (WSAPoll() on Windows) elsewhere or with LOOP_POLL,
level-triggered; one with nothing to wait for is taken from the system,
or an error or a hang up would be reported all the same. One deleted
while the sockets ready are being called is not called.

Timers wait in a wheel of 1024 one-second slots. One due within a turn
waits at the head of the slot of its second, all there due in the same
second; one due later behind them, in the order of their seconds, or,
if due before the last there, at the head of the last slot of the
turn, looked at again then. Moving a timer later costs nothing while
it waits in the wheel: it is looked at when its old deadline comes and
moved then. When its slot comes a timer due within the second waits in
a heap, so it fires to the millisecond, as does one set within the
second. The clock is monotonic.

Another thread posts to the loop, waking it with an eventfd, a pipe, or
on Windows a UDP socket sending to itself.

tests/looptest.c tests it on its own, timers on a clock of its own
through several turns of the wheel: built by CMake, run by the loop
case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Vladimir DubrovinandClaude Opus 5.5 committed 2026-10-24 21:30:13 +03:00
1 parent fdebdb16dc
commit cc98c498fe
7 files changed
+1240 -2

No files matched your search

+9
View File
@@ -390,6 +390,7 @@ endif()
# Source files for 3proxy core
set(3PROXY_CORE_SOURCES
src/3proxy.c
src/loop.c
src/auth.c
src/acl.c
src/limiter.c
@@ -627,6 +628,14 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(3proxy PRIVATE dl)
endif()
# The event loop on its own, for tests/cases/loop.py: not installed
add_executable(looptest tests/looptest.c src/loop.c)
target_include_directories(looptest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(looptest PRIVATE Threads::Threads)
if(WIN32)
target_link_libraries(looptest PRIVATE ${WINDOWS_LIBS})
endif()
# Build 3proxy_crypt utility
add_executable(3proxy_crypt
src/3proxy_crypt.c