mirror of
https://github.com/3proxy/3proxy.git
synced 2026-11-27 23:59:41 +08:00
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:
1 parent
cc98c498fe
commit
4e1455b419
8 files changed
+910
-42
No files matched your search
+60
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
3APA3A simplest proxy server
|
||||
(c) 2002-2026 by Vladimir Dubrovin <vlad@3proxy.org>
|
||||
|
||||
please read License Agreement
|
||||
*/
|
||||
|
||||
/* Fibers: stacks of their own a thread switches to and back, for a thread
|
||||
to serve many clients each with code written as if it had a thread of
|
||||
its own. The switch is our own where the processor is one of ours,
|
||||
Windows' fibers on Windows; elsewhere there are none (WITH_FIBERS is not
|
||||
defined), and each client has a thread. A fiber is resumed by the thread
|
||||
it belongs to and yields back to it: fibers do not switch to each other,
|
||||
nor move between threads. */
|
||||
|
||||
#ifndef _FIBER_H
|
||||
#define _FIBER_H
|
||||
|
||||
#if defined _WIN32
|
||||
#if !defined __WATCOMC__ && !defined _WINCE
|
||||
#define FIBER_WINDOWS
|
||||
#endif
|
||||
#elif defined __x86_64__ && defined __LP64__
|
||||
#define FIBER_X86_64
|
||||
#elif defined __aarch64__ && defined __LP64__
|
||||
#define FIBER_AARCH64
|
||||
#elif defined __i386__
|
||||
#define FIBER_I386
|
||||
#elif defined __arm__
|
||||
#define FIBER_ARM
|
||||
#elif defined __mips__ && defined _MIPS_SIM && defined _ABIO32 && _MIPS_SIM == _ABIO32
|
||||
#define FIBER_MIPS
|
||||
#endif
|
||||
|
||||
#if defined FIBER_WINDOWS || defined FIBER_X86_64 || defined FIBER_AARCH64 || defined FIBER_I386 || defined FIBER_ARM || defined FIBER_MIPS
|
||||
#define WITH_FIBERS
|
||||
|
||||
struct fiber {
|
||||
void *sp; /* where its registers are while it does not run */
|
||||
unsigned char *map; /* its stack, the guard page at the bottom */
|
||||
size_t mapped;
|
||||
void (*func)(void *arg); /* what it runs, NULL once returned */
|
||||
void *arg;
|
||||
int started;
|
||||
#ifdef FIBER_WINDOWS
|
||||
void *handle;
|
||||
#endif
|
||||
void *fake; /* AddressSanitizer's, while it does not run */
|
||||
};
|
||||
|
||||
int fiberusable(void);
|
||||
int fiberinit(struct fiber *f, size_t stacksize);
|
||||
void fiberfree(struct fiber *f);
|
||||
void fiberstart(struct fiber *f, void (*func)(void *arg), void *arg);
|
||||
int fiberresume(struct fiber *f);
|
||||
void fiberyield(void);
|
||||
struct fiber *fibercurrent(void);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
Reference in new issue
Block a user