Boost.Coroutine not using segmented stacks

620 views Asked by At

Can anyone give me an example of how I can use segmented stacks with boost coroutines? Do I have to annotate every function that is called from the coroutine with a special split-stack attribute?

When I try and write a program that should use segmented stacks, it just segfaults.


Here is what I have done so far https://wandbox.org/permlink/TltQwGpy4hRoHgDY The code seems to segfault very quickly, if segmented stacks were used I would expect it to be able to handle more iterations. The program errors out after 35 iterations.

#include <boost/coroutine2/all.hpp>

#include <iostream>
#include <array>

using std::cout;
using std::endl;

class Int {
    int a{2};
};

void foo(int num) {
    cout << "In iteration " << num << endl;
    std::array<Int, 1000> arr;
    static_cast<void>(arr);
    foo(num + 1);
}

int main() {
    using Coroutine_t = boost::coroutines2::coroutine<int>::push_type;
    auto coro = Coroutine_t{[&](auto& yield) {
        foo(yield.get());
    }};

    coro(0);
}
2

There are 2 answers

0
Curious On BEST ANSWER

Compiling that code with -fsplit-stack solves the problem. Annotations are not required. All functions are by default treated as split stacks. Example - https://wandbox.org/permlink/Pzzj5gMoUAyU0h7Q

Easy as that.

0
xlrg On

compile boost (boost.context and boost.coroutine) with b2 property segmented-stacks=on (enables special code inside boost.coroutine and boost.context).

your app has to be compiled with -DBOOST_USE_SEGMENTED_STACKS and -fsplit-stack (required by boost.coroutines headers).

see documentation: http://www.boost.org/doc/libs/1_65_1/libs/coroutine/doc/html/coroutine/stack/segmented_stack_allocator.html

boost.coroutine contains an example that demonstrates segmented stacks (in directory coroutine/example/asymmetric/ call b2 toolset=gcc segmented-stacks=on).

please note: while llvm supports segmented stacks, clang seams not to provide the __splitstack_<xyz> functions.