peerplays-fc/src/thread/context.hpp

205 lines
5.7 KiB
C++
Raw Normal View History

#pragma once
#include <fc/thread/thread.hpp>
#include <fc/exception/exception.hpp>
2012-09-08 21:37:25 +00:00
#include <vector>
2012-09-08 02:50:37 +00:00
#include <boost/version.hpp>
#define BOOST_COROUTINES_NO_DEPRECATION_WARNING // Boost 1.61
#define BOOST_COROUTINE_NO_DEPRECATION_WARNING // Boost 1.62
2017-11-09 12:18:58 +00:00
#if BOOST_VERSION >= 106800
#include <boost/context/continuation_fcontext.hpp>
#else
#include <boost/context/all.hpp>
#endif
2017-11-09 12:18:58 +00:00
#if BOOST_VERSION >= 106100
#include <boost/coroutine/stack_allocator.hpp>
namespace bc = boost::context::detail;
namespace bco = boost::coroutines;
typedef bco::stack_allocator stack_allocator;
#else
2014-09-07 22:02:39 +00:00
# include <boost/coroutine/stack_context.hpp>
2013-08-11 14:18:08 +00:00
namespace bc = boost::context;
namespace bco = boost::coroutines;
# if !defined(NDEBUG)
2014-09-07 22:02:39 +00:00
# include <boost/assert.hpp>
# include <boost/coroutine/protected_stack_allocator.hpp>
typedef bco::protected_stack_allocator stack_allocator;
# else
# include <boost/coroutine/stack_allocator.hpp>
typedef bco::stack_allocator stack_allocator;
# endif
#endif // BOOST_VERSION >= 106100
2012-09-08 02:50:37 +00:00
namespace fc {
class thread;
class promise_base;
class task_base;
/**
* maintains information associated with each context such as
* where it is blocked, what time it should resume, priority,
* etc.
*/
struct context {
typedef fc::context* ptr;
bco::stack_context stack_ctx;
2017-11-09 12:18:58 +00:00
#if BOOST_VERSION >= 106100
using context_fn = void (*)(bc::transfer_t);
#else
using context_fn = void(*)(intptr_t);
#endif
2012-09-08 02:50:37 +00:00
2017-11-09 12:18:58 +00:00
context( context_fn sf, stack_allocator& alloc, fc::thread* t )
2012-09-08 02:50:37 +00:00
: caller_context(0),
stack_alloc(&alloc),
next_blocked(0),
2012-11-09 04:12:45 +00:00
next_blocked_mutex(0),
2012-09-08 02:50:37 +00:00
next(0),
ctx_thread(t),
canceled(false),
#ifndef NDEBUG
cancellation_reason(nullptr),
#endif
2012-09-08 02:50:37 +00:00
complete(false),
cur_task(0),
context_posted_num(0)
2012-09-08 02:50:37 +00:00
{
2014-11-19 16:24:56 +00:00
size_t stack_size = FC_CONTEXT_STACK_SIZE;
2014-09-07 22:02:39 +00:00
alloc.allocate(stack_ctx, stack_size);
my_context = bc::make_fcontext( stack_ctx.sp, stack_ctx.size, sf);
2012-09-08 02:50:37 +00:00
}
context( fc::thread* t) :
2014-09-07 22:02:39 +00:00
my_context(nullptr),
caller_context(0),
2012-09-08 02:50:37 +00:00
stack_alloc(0),
next_blocked(0),
2012-11-09 04:12:45 +00:00
next_blocked_mutex(0),
2012-09-08 02:50:37 +00:00
next(0),
ctx_thread(t),
canceled(false),
#ifndef NDEBUG
cancellation_reason(nullptr),
#endif
2012-09-08 02:50:37 +00:00
complete(false),
cur_task(0),
context_posted_num(0)
{}
2012-09-08 02:50:37 +00:00
~context() {
if(stack_alloc)
2014-09-07 22:02:39 +00:00
stack_alloc->deallocate( stack_ctx );
2012-09-08 02:50:37 +00:00
}
void reinitialize()
{
canceled = false;
#ifndef NDEBUG
cancellation_reason = nullptr;
#endif
blocking_prom.clear();
caller_context = nullptr;
resume_time = fc::time_point();
next_blocked = nullptr;
next_blocked_mutex = nullptr;
next = nullptr;
complete = false;
}
2012-09-08 02:50:37 +00:00
struct blocked_promise {
blocked_promise( promise_base* p=0, bool r=true )
:prom(p),required(r){}
promise_base* prom;
bool required;
};
/**
* @todo Have a list of promises so that we can wait for
* P1 or P2 and either will unblock instead of requiring both
* @param req - require this promise to 'unblock', otherwise try_unblock
* will allow it to be one of many that could 'unblock'
*/
void add_blocking_promise( promise_base* p, bool req = true ) {
for( auto i = blocking_prom.begin(); i != blocking_prom.end(); ++i ) {
if( i->prom == p ) {
i->required = req;
return;
}
}
blocking_prom.push_back( blocked_promise(p,req) );
}
/**
* If all of the required promises and any optional promises then
* return true, else false.
* @todo check list
*/
bool try_unblock( promise_base* p ) {
if( blocking_prom.size() == 0 ) {
return true;
}
bool req = false;
for( uint32_t i = 0; i < blocking_prom.size(); ++i ) {
if( blocking_prom[i].prom == p ) {
blocking_prom[i].required = false;
return true;
}
req = req || blocking_prom[i].required;
}
return !req;
}
void remove_blocking_promise( promise_base* p ) {
for( auto i = blocking_prom.begin(); i != blocking_prom.end(); ++i ) {
if( i->prom == p ) {
blocking_prom.erase(i);
return;
}
}
}
void timeout_blocking_promises() {
for( auto i = blocking_prom.begin(); i != blocking_prom.end(); ++i ) {
i->prom->set_exception( std::make_shared<timeout_exception>() );
2012-09-08 02:50:37 +00:00
}
}
void set_exception_on_blocking_promises( const exception_ptr& e ) {
2012-09-08 02:50:37 +00:00
for( auto i = blocking_prom.begin(); i != blocking_prom.end(); ++i ) {
i->prom->set_exception( e );
2012-09-08 02:50:37 +00:00
}
}
void clear_blocking_promises() {
blocking_prom.clear();
}
bool is_complete()const { return complete; }
bc::fcontext_t my_context;
fc::context* caller_context;
2014-09-07 22:02:39 +00:00
stack_allocator* stack_alloc;
2012-09-08 02:50:37 +00:00
priority prio;
//promise_base* prom;
std::vector<blocked_promise> blocking_prom;
time_point resume_time;
2012-09-23 06:01:27 +00:00
// time_point ready_time; // time that this context was put on ready queue
2012-09-08 02:50:37 +00:00
fc::context* next_blocked;
2012-11-09 04:12:45 +00:00
fc::context* next_blocked_mutex;
2012-09-08 02:50:37 +00:00
fc::context* next;
fc::thread* ctx_thread;
bool canceled;
#ifndef NDEBUG
const char* cancellation_reason;
#endif
2012-09-08 02:50:37 +00:00
bool complete;
task_base* cur_task;
uint64_t context_posted_num; // serial number set each tiem the context is added to the ready list
2012-09-08 02:50:37 +00:00
};
} // naemspace fc