Fibers: stacks a thread switches to and back

src/fiber.c, not used yet: a fiber runs a function on a stack of its
own, yielding back to the thread which resumed it, for a thread to
serve many clients with code written as if each had a thread.

The switch is our own on x86-64 (System V), aarch64, i386, arm (ARM or
Thumb code, with or without a floating point unit) and mips o32 (with
soft, 32 or 64 bit floating point registers, position independent or
not), Windows' fibers on Windows. It saves the registers a function
has to keep and swaps the stack. Elsewhere there are no fibers
(WITH_FIBERS is not defined), nor where x86-64 Linux checks returns
against a shadow stack, which fiberusable() tells: each client has a
thread there.

Stacks are mapped with a guard page below them. A fiber whose function
returned is started again with another, its stack kept. Switches are
annotated for AddressSanitizer.

tests/fibertest.c tests them: yielding and resuming, the integer and
floating point registers a function keeps across switches both ways, a
thousand fibers resumed at random each keeping its stack, recursion
deep in a stack, fibers sleeping on the event loop's timers. The units
case, which was the loop case, runs it with looptest.

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:51:40 +03:00
1 parent cc98c498fe
commit 4e1455b419
8 files changed
+910 -42

No files matched your search

+11 -6
View File
@@ -391,6 +391,7 @@ endif()
set(3PROXY_CORE_SOURCES
src/3proxy.c
src/loop.c
src/fiber.c
src/auth.c
src/acl.c
src/limiter.c
@@ -628,13 +629,17 @@ 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
# The event loop and fibers on their own, for tests/cases/units.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()
add_executable(fibertest tests/fibertest.c src/fiber.c src/loop.c)
foreach(unit looptest fibertest)
target_include_directories(${unit} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(${unit} PRIVATE Threads::Threads)
if(WIN32)
target_link_libraries(${unit} PRIVATE ${WINDOWS_LIBS})
endif()
endforeach()
# Build 3proxy_crypt utility
add_executable(3proxy_crypt