context_switcher.cpp 1.17 KB
Newer Older
1
#include "context_switcher/context_switcher.h"
2

3 4 5 6
#ifdef THREAD_SANITIZER
#include "context_switcher/tsan_fiber_api.h"
#endif

7 8
namespace context_switcher {

9
#ifdef CS_USE_CSCONTEXT
10
continuation switch_context(continuation &&cont) {
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  continuation_t cont_pointer = cont.consume();
  continuation_t result = cscontext::__cs_switch_context(cont_pointer);
  return continuation{result};
}
#else
#ifdef THREAD_SANITIZER
continuation switch_context(continuation &&cont) {
  continuation_t cont_pointer = cont.consume();
  void *next_fiber = cont.get_tsan_fiber();
  void *old_fiber = __tsan_get_current_fiber();
  __tsan_switch_to_fiber(next_fiber, 0);
  fcontext::transfer_t transfer = fcontext::jump_fcontext(cont_pointer, old_fiber);
  if (transfer.data) {
    return continuation{transfer.continuation, transfer.data};
  } else {
    return continuation{nullptr, 0};
  }
}
#else
continuation switch_context(continuation &&cont) {
  continuation_t cont_pointer = cont.consume();
  fcontext::transfer_t transfer = fcontext::jump_fcontext(cont_pointer, (void *) 1);
  if (transfer.data) {
    return continuation{transfer.continuation};
  } else {
    return continuation{nullptr};
  }
38
}
39 40
#endif
#endif
41 42

}