Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / asio / test / latency / coroutine.hpp
1 //
2 // coroutine.hpp
3 // ~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #ifndef COROUTINE_HPP
12 #define COROUTINE_HPP
13
14 class coroutine
15 {
16 public:
17   coroutine() : value_(0) {}
18   bool is_child() const { return value_ < 0; }
19   bool is_parent() const { return !is_child(); }
20   bool is_complete() const { return value_ == -1; }
21 private:
22   friend class coroutine_ref;
23   int value_;
24 };
25
26 class coroutine_ref
27 {
28 public:
29   coroutine_ref(coroutine& c) : value_(c.value_), modified_(false) {}
30   coroutine_ref(coroutine* c) : value_(c->value_), modified_(false) {}
31   ~coroutine_ref() { if (!modified_) value_ = -1; }
32   operator int() const { return value_; }
33   int& operator=(int v) { modified_ = true; return value_ = v; }
34 private:
35   void operator=(const coroutine_ref&);
36   int& value_;
37   bool modified_;
38 };
39
40 #define CORO_REENTER(c) \
41   switch (coroutine_ref _coro_value = c) \
42     case -1: if (_coro_value) \
43     { \
44       goto terminate_coroutine; \
45       terminate_coroutine: \
46       _coro_value = -1; \
47       goto bail_out_of_coroutine; \
48       bail_out_of_coroutine: \
49       break; \
50     } \
51     else case 0:
52
53 #define CORO_YIELD_IMPL(n) \
54   for (_coro_value = (n);;) \
55     if (_coro_value == 0) \
56     { \
57       case (n): ; \
58       break; \
59     } \
60     else \
61       switch (_coro_value ? 0 : 1) \
62         for (;;) \
63           case -1: if (_coro_value) \
64             goto terminate_coroutine; \
65           else for (;;) \
66             case 1: if (_coro_value) \
67               goto bail_out_of_coroutine; \
68             else case 0:
69
70 #define CORO_FORK_IMPL(n) \
71   for (_coro_value = -(n);; _coro_value = (n)) \
72     if (_coro_value == (n)) \
73     { \
74       case -(n): ; \
75       break; \
76     } \
77     else
78
79 #if defined(_MSC_VER)
80 # define CORO_YIELD CORO_YIELD_IMPL(__COUNTER__ + 1)
81 # define CORO_FORK CORO_FORK_IMPL(__COUNTER__ + 1)
82 #else // defined(_MSC_VER)
83 # define CORO_YIELD CORO_YIELD_IMPL(__LINE__)
84 # define CORO_FORK CORO_FORK_IMPL(__LINE__)
85 #endif // defined(_MSC_VER)
86
87 #endif // COROUTINE_HPP