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

#include "barrier.h"
6 7

#include "context_switcher/context_switcher.h"
8

9 10 11
using namespace context_switcher;
using namespace std;

12
// Memory for custom stack and continuation semantics
13
const size_t STACK_SIZE = 512 * 32;
14
const size_t NUM_STACKS = 4;
15
char custom_stacks[NUM_STACKS][STACK_SIZE];
16

17
int main() {
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  context_switcher::continuation cont_t1, cont_main;
  barrier bar{2};
  int error = 0;

  auto t1 = std::thread([&]() {
    while (true) {
      bar.wait();
      auto cont = enter_context(custom_stacks[0], STACK_SIZE, [&](continuation &&cont) {
        error++;
        cont_t1 = std::move(cont);
        bar.wait();
        error++;
        return std::move(cont_main);
      });
    }
  });

  int count = 0;

37
  while (true) {
38 39 40 41 42 43 44 45 46 47 48
    count++;
    if (count % 100 == 0) {
      printf("%d\n", count);
    }
    bar.wait();
    auto cont = enter_context(custom_stacks[1], STACK_SIZE, [&](continuation &&cont) {
      error++;
      cont_main = std::move(cont);
      bar.wait();
      error++;
      return std::move(cont_t1);
49
    });
50
  }
51

52
  return 0;
53
}