30 #include <boost/atomic/atomic.hpp>
31 #include <boost/lockfree/queue.hpp>
67 : idle_threads( 2 * num_threads ), waiting_tasks( 200 )
69 notifiers.resize( num_threads );
70 threads.reserve( num_threads );
71 for( uint32_t i = 0; i < num_threads; i++ )
74 notifiers[i].my_pool =
this;
82 waiting_tasks.consume_all( [] (
task_base* t ) {
83 t->
cancel(
"thread pool quitting" );
90 while( idle_threads.pop( ini ) )
91 if( ini->
is_idle.exchange(
false ) )
93 return threads[ini->
id];
95 boost::unique_lock<fc::spin_yield_lock> lock(pool_lock);
96 while( idle_threads.pop( ini ) )
97 if( ini->
is_idle.exchange(
false ) )
99 return threads[ini->
id];
101 while( !waiting_tasks.push(
task ) )
102 elog(
"Worker pool internal error" );
109 if( waiting_tasks.pop(
task ) )
112 if( waiting_tasks.pop(
task ) )
114 while( !idle_threads.push( ini ) )
115 elog(
"Worker pool internal error" );
119 std::vector<idle_notifier_impl> notifiers;
120 std::vector<thread*> threads;
121 boost::lockfree::queue<idle_notifier_impl*> idle_threads;
122 boost::lockfree::queue<task_base*> waiting_tasks;
130 if( result )
is_idle.store(
false );
159 serial_valve::ticket_guard::ticket_guard( boost::atomic<future<void>*>& latch )
162 future<void>* my_future =
new future<void>( my_promise );
167 ticket = latch.load();
168 FC_ASSERT( ticket,
"Valve is shutting down!" );
170 while( !latch.compare_exchange_weak( ticket, my_future ) );
179 serial_valve::ticket_guard::~ticket_guard()
181 my_promise->set_value();
186 void serial_valve::ticket_guard::wait_for_my_turn()