I'm doing some research in C++ green threads, mostly boost::coroutine2
and similar POSIX functions like makecontext()/swapcontext()
, and planning to implement a C++ green thread library on top of boost::coroutine2
. Both require the user code to allocate a stack for every new function/coroutine.
My target platform is x64/Linux. I want my green thread library to be suitable for general use, so the stacks should expand as required (a reasonable upper limit is fine, e.g. 10MB), it would be great if the stacks could shrink when too much memory is unused (not required). I haven't figured out an appropriate algorithm to allocate stacks.
After some googling, I figured out a few options myself:
- use split stack implemented by the compiler (gcc -fsplit-stack), but split stack has performance overhead. Go has already moved away from split stack due to performance reasons.
- allocate a large chunk of memory with
mmap()
hope the kernel is smart enough to leave the physical memory unallocated and allocate only when the stacks are accessed. In this case, we are at the mercy of the kernel. - reserve a large memory space with
mmap(PROT_NONE)
and setup aSIGSEGV
signal handler. In the signal handler, when theSIGSEGV
is caused by stack access (the accessed memory is inside the large memory space reserved), allocate needed memory withmmap(PROT_READ | PROT_WRITE)
. Here is the problem for this approach:mmap()
isn't asynchronous safe, cannot be called inside a signal handler. It still can be implemented, very tricky though: create another thread during program startup for memory allocation, and usepipe() + read()/write()
to send memory allocation information from the signal handler to the thread.
A few more questions about option 3:
- I'm not sure the performance overhead of this approach, how well/bad the kernel/CPU performs when the memory space is extremely fragmented due to thousands of
mmap()
call ? - Is this approach correct if the unallocated memory is accessed in kernel space ? e.g. when
read()
is called ?
Are there any other (better) options for stack allocation for green threads ? How are green thread stacks allocated in other implementations, e.g. Go/Java ?
mmap
is not async safe according to POSIX, it is actually async safe in Linux and pretty much every reasonable, usable UNIX variant out there. – Brutusmmap
can be good for green threads? I'm not an expert but I wanted to know. – Pachyderm