BitShares-Core
7.0.2
BitShares blockchain node software and command-line wallet software
|
Go to the documentation of this file.
8 #if defined(_MSC_VER) && !defined(NDEBUG)
10 const DWORD MS_VC_EXCEPTION=0x406D1388;
13 typedef struct tagTHREADNAME_INFO
22 static void set_thread_name(
const char* threadName)
26 info.szName = threadName;
32 RaiseException(MS_VC_EXCEPTION, 0,
sizeof(info)/
sizeof(ULONG_PTR), (ULONG_PTR*)&info);
34 __except(EXCEPTION_EXECUTE_HANDLER)
38 #elif defined(__linux__)
40 static void set_thread_name(
const char* threadName)
42 pthread_setname_np(pthread_self(), threadName);
44 #elif defined(__APPLE__) && !defined(NDEBUG)
46 static void set_thread_name(
const char* threadName)
48 pthread_setname_np(threadName);
51 static void set_thread_name(
const char* threadName)
69 static __thread
thread* t = NULL;
76 boost::thread* t =
new boost::thread( [
this,p,
name,notifier]() {
78 set_thread_name(
name.c_str());
79 this->my =
new thread_d( *
this, notifier );
87 wlog(
"unhandled exception" );
92 std::cerr <<
"unhandled exception in thread '" <<
name <<
"'\n";
98 wlog(
"unhandled exception" );
99 p->
set_exception( std::make_shared<unhandled_exception>(
FC_LOG_MESSAGE( warn,
"unhandled exception: ${diagnostic}", (
"diagnostic",boost::current_exception_diagnostic_information()) ) ) );
103 std::cerr <<
"unhandled exception in thread '" <<
name <<
"'\n";
104 std::cerr << boost::current_exception_diagnostic_information() <<
"\n";
151 set_thread_name(my->
name.c_str());
163 #if defined(__linux__) || defined(__APPLE__)
169 #if defined(__linux__) || defined(__APPLE__)
181 async( [
this](){
quit();},
"thread::quit" );
205 BOOST_ASSERT( my->
blocked == 0 );
208 unstarted_task->
set_exception(std::make_shared<canceled_exception>(
FC_LOG_MESSAGE(error,
"cancellation reason: thread quitting")));
212 scheduled_task->
set_exception(std::make_shared<canceled_exception>(
FC_LOG_MESSAGE(error,
"cancellation reason: thread quitting")));
256 catch( canceled_exception& e )
258 dlog(
"thread canceled: ${e}", (
"e", e.to_detail_string()) );
292 for(
size_t i = 0; i < p.size(); ++i )
299 for(
auto i = p.begin(); i != p.end(); ++i )
300 ss << (*i)->get_desc() <<
", ";
308 for( uint32_t i = 0; i < p.size(); ++i )
318 sleep_priority_less() );
324 for(
auto i = p.begin(); i != p.end(); ++i )
329 for( uint32_t i = 0; i < p.size(); ++i )
336 void thread::async_task( task_base* t,
const priority& p ) {
353 do { t->
_next = stale_head;
354 }
while( !my->
task_in_queue.compare_exchange_weak( stale_head, t, boost::memory_order_release ) );
359 if(
this != &
current() && !stale_head ) {
410 sleep_priority_less() );
424 BOOST_ASSERT(p->ready());
457 auto cur = cur_blocked;
473 prev_blocked = cur_blocked;
484 void thread::notify_task_has_been_canceled()
504 work->
_next = stale_head;
505 }
while( !t->
task_in_queue.compare_exchange_weak( stale_head, work, boost::memory_order_release ) );
511 if( notifier ) notifier->
busy();
519 unhandled_exception_filter_type unhandled_structured_exception_filter =
nullptr;
521 void set_unhandled_structured_exception_filter(unhandled_exception_filter_type new_filter)
523 detail::unhandled_structured_exception_filter = new_filter;
525 unhandled_exception_filter_type get_unhandled_structured_exception_filter()
527 return detail::unhandled_structured_exception_filter;
static thread & current()
void remove_blocking_promise(promise_base *p)
thread(const std::string &name="", thread_idle_notifier *notifier=0)
std::shared_ptr< promise< T > > ptr
void set_name(const string &n)
associates a name with this thread.
Used to generate a useful error report when an exception is thrown.
int wait_any_until(std::vector< promise_base::ptr > &&v, const time_point &tp)
bool start_next_fiber(bool reschedule=false)
const T & wait(const microseconds &timeout=microseconds::maximum())
void add_context_to_ready_list(context *context_to_add, bool at_end=false)
boost::thread * boost_thread
std::vector< task_base * > task_sch_queue
void debug(const std::string &d)
print debug info about the state of every context / promise.
void set_exception_on_blocking_promises(const exception_ptr &e)
void usleep(const microseconds &u)
std::vector< task_base * > task_pqueue
static ptr create(const char *desc FC_TASK_NAME_DEFAULT_ARG)
virtual task_base * idle()=0
void check_fiber_exceptions()
T wait(boost::signals2::signal< void(T)> &sig, const microseconds &timeout_us=microseconds::maximum())
const char * current_task_desc() const
fc::context * next_blocked
friend void sleep_until(const time_point &)
boost::atomic< task_base * > task_in_queue
void cleanup_thread_specific_data()
priority current_priority() const
void add_to_blocked(fc::context *c)
std::vector< fc::context * > sleep_pqueue
time_point check_for_timeouts()
const char * get_desc() const
void add_blocking_promise(promise_base *p, bool req=true)
std::string to_detail_string(log_level ll=log_level::all) const
bool try_unblock(promise_base *p)
virtual std::shared_ptr< exception > dynamic_copy_exception() const
const char * thread_name()
void set_value(const T &v)
void yield_until(const time_point &tp, bool reschedule)
boost::condition_variable task_ready
void notify_task_has_been_canceled()
static time_point maximum()
std::shared_ptr< promise_base > ptr
void set_exception(const fc::exception_ptr &e)
void unblock(fc::context *c)
std::vector< fc::context * > ready_heap
auto async(Functor &&f, const char *desc FC_TASK_NAME_DEFAULT_ARG, priority prio=priority()) -> fc::future< decltype(f())>
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
int wait_any(const fc::future< T1 > &f1, const fc::future< T2 > &f2, const microseconds timeout_us=microseconds::maximum())
void sleep_until(const time_point &tp)
thread *& current_thread()
boost::mutex task_ready_mutex
const string & name() const
returns the name given by set_name() for this thread
friend int wait_any_until(std::vector< promise_base::ptr > &&v, const time_point &tp)
#define FC_LOG_MESSAGE(LOG_LEVEL, FORMAT,...)
A helper method for generating log messages.