main.cpp 1.07 KB
Newer Older
1
#include <utility>
2
#include <cstdio>
3 4

#include "context_switcher/context_switcher.h"
5 6 7
using namespace context_switcher;
using namespace std;

8
// Memory for custom stack and continuation semantics
9 10
const size_t STACK_SIZE = 512 * 16;
const size_t NUM_STACKS = 4;
11
char custom_stacks[NUM_STACKS][STACK_SIZE];
12

13
volatile int result;
14

15 16 17 18 19 20 21 22
int main() {
  context_switcher::continuation outer_cont = enter_context(custom_stacks[0], STACK_SIZE, [](continuation &&main_cont) {
    enter_context(custom_stacks[1], STACK_SIZE, [&main_cont](continuation &&middle_cont) {
      enter_context(custom_stacks[2], STACK_SIZE, [&main_cont](continuation &&inner_cont) {
        for (int i = 0; i < 10; i++) {
          printf("Inner %d\n", i);
          main_cont = context_switcher::switch_context(std::move(main_cont));
        }
23

24 25
        return std::move(inner_cont);
      });
26

27 28
      return std::move(middle_cont);
    });
29

30 31 32 33 34 35 36
    return std::move(main_cont);
  });

  for (int i = 0; i < 10; i++) {
    printf("Outer %d\n", i);
    outer_cont = context_switcher::switch_context(std::move(outer_cont));
  }
37

38
  return 0;
39
}