BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
context.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <fc/thread/thread.hpp>
4 #include <vector>
5 
6 #include <boost/version.hpp>
7 
8 #define BOOST_COROUTINES_NO_DEPRECATION_WARNING // Boost 1.61
9 #define BOOST_COROUTINE_NO_DEPRECATION_WARNING // Boost 1.62
10 
11 #if BOOST_VERSION >= 106800
12 #include <boost/context/continuation_fcontext.hpp>
13 #else
14 #include <boost/context/all.hpp>
15 #endif
16 
17 #if BOOST_VERSION >= 106100
18  #include <boost/coroutine/stack_allocator.hpp>
19  namespace bc = boost::context::detail;
20  namespace bco = boost::coroutines;
22 #else
23 # include <boost/coroutine/stack_context.hpp>
24  namespace bc = boost::context;
25  namespace bco = boost::coroutines;
26 # if !defined(NDEBUG)
27 # include <boost/assert.hpp>
28 # include <boost/coroutine/protected_stack_allocator.hpp>
29  typedef bco::protected_stack_allocator stack_allocator;
30 # else
31 # include <boost/coroutine/stack_allocator.hpp>
33 # endif
34 
35 #endif // BOOST_VERSION >= 106100
36 
37 namespace fc {
38  class thread;
39  class promise_base;
40  class task_base;
41 
47  struct context {
48  typedef fc::context* ptr;
49  bco::stack_context stack_ctx;
50 
51 #if BOOST_VERSION >= 106100
52  using context_fn = void (*)(bc::transfer_t);
53 #else
54  using context_fn = void(*)(intptr_t);
55 #endif
56 
58  : caller_context(0),
59  stack_alloc(&alloc),
60  next_blocked(0),
62  next(0),
63  ctx_thread(t),
64  canceled(false),
65 #ifndef NDEBUG
66  cancellation_reason(nullptr),
67 #endif
68  complete(false),
69  cur_task(0),
71  {
72  size_t stack_size = FC_CONTEXT_STACK_SIZE;
73  alloc.allocate(stack_ctx, stack_size);
74  my_context = bc::make_fcontext( stack_ctx.sp, stack_ctx.size, sf);
75  }
76 
78  my_context(nullptr),
79  caller_context(0),
80  stack_alloc(0),
81  next_blocked(0),
83  next(0),
84  ctx_thread(t),
85  canceled(false),
86 #ifndef NDEBUG
87  cancellation_reason(nullptr),
88 #endif
89  complete(false),
90  cur_task(0),
92  {}
93 
95  if(stack_alloc)
96  stack_alloc->deallocate( stack_ctx );
97  }
98 
99  void reinitialize()
100  {
101  canceled = false;
102 #ifndef NDEBUG
103  cancellation_reason = nullptr;
104 #endif
105  blocking_prom.clear();
106  caller_context = nullptr;
108  next_blocked = nullptr;
109  next_blocked_mutex = nullptr;
110  next = nullptr;
111  complete = false;
112  }
113 
115  blocked_promise( promise_base* p=0, bool r=true )
116  :prom(p),required(r){}
117 
119  bool required;
120  };
121 
128  void add_blocking_promise( promise_base* p, bool req = true ) {
129  for( auto i = blocking_prom.begin(); i != blocking_prom.end(); ++i ) {
130  if( i->prom == p ) {
131  i->required = req;
132  return;
133  }
134  }
135  blocking_prom.push_back( blocked_promise(p,req) );
136  }
143  if( blocking_prom.size() == 0 ) {
144  return true;
145  }
146  bool req = false;
147  for( uint32_t i = 0; i < blocking_prom.size(); ++i ) {
148  if( blocking_prom[i].prom == p ) {
149  blocking_prom[i].required = false;
150  return true;
151  }
152  req = req || blocking_prom[i].required;
153  }
154  return !req;
155  }
156 
158  for( auto i = blocking_prom.begin(); i != blocking_prom.end(); ++i ) {
159  if( i->prom == p ) {
160  blocking_prom.erase(i);
161  return;
162  }
163  }
164  }
165 
167  for( auto i = blocking_prom.begin(); i != blocking_prom.end(); ++i ) {
168  i->prom->set_exception( std::make_shared<timeout_exception>() );
169  }
170  }
172  for( auto i = blocking_prom.begin(); i != blocking_prom.end(); ++i ) {
173  i->prom->set_exception( e );
174  }
175  }
177  blocking_prom.clear();
178  }
179 
180  bool is_complete()const { return complete; }
181 
182  bc::fcontext_t my_context;
186  //promise_base* prom;
187  std::vector<blocked_promise> blocking_prom;
189  // time_point ready_time; // time that this context was put on ready queue
194  bool canceled;
195 #ifndef NDEBUG
196  const char* cancellation_reason;
197 #endif
198  bool complete;
200  uint64_t context_posted_num; // serial number set each tiem the context is added to the ready list
201  };
202 
203 } // naemspace fc
204 
blocked_promise(promise_base *p=0, bool r=true)
Definition: context.hpp:115
fc::context * next
Definition: context.hpp:192
fc::context * next_blocked
Definition: context.hpp:190
stack_allocator * stack_alloc
Definition: context.hpp:184
uint64_t context_posted_num
Definition: context.hpp:200
priority prio
Definition: context.hpp:185
void add_blocking_promise(promise_base *p, bool req=true)
Definition: context.hpp:128
#define FC_CONTEXT_STACK_SIZE
Definition: thread.hpp:3
void timeout_blocking_promises()
Definition: context.hpp:166
void(*)(intptr_t) context_fn
Definition: context.hpp:54
const char * cancellation_reason
Definition: context.hpp:196
bool complete
Definition: context.hpp:198
time_point resume_time
Definition: context.hpp:188
void set_exception_on_blocking_promises(const exception_ptr &e)
Definition: context.hpp:171
void reinitialize()
Definition: context.hpp:99
fc::context * ptr
Definition: context.hpp:48
bool try_unblock(promise_base *p)
Definition: context.hpp:142
std::shared_ptr< exception > exception_ptr
Definition: exception.hpp:131
task_base * cur_task
Definition: context.hpp:199
void clear_blocking_promises()
Definition: context.hpp:176
bool is_complete() const
Definition: context.hpp:180
bc::fcontext_t my_context
Definition: context.hpp:182
fc::context * next_blocked_mutex
Definition: context.hpp:191
bco::protected_stack_allocator stack_allocator
Definition: context.hpp:29
Defines exception&#39;s used by fc.
void remove_blocking_promise(promise_base *p)
Definition: context.hpp:157
std::vector< blocked_promise > blocking_prom
Definition: context.hpp:187
Definition: api.hpp:15
fc::context * caller_context
Definition: context.hpp:183
bco::stack_context stack_ctx
Definition: context.hpp:49
context(context_fn sf, stack_allocator &alloc, fc::thread *t)
Definition: context.hpp:57
fc::thread * ctx_thread
Definition: context.hpp:193
context(fc::thread *t)
Definition: context.hpp:77
bool canceled
Definition: context.hpp:194