Chris@102: // Copyright (C) 2013,2014 Vicente J. Botet Escriba Chris@102: // Chris@102: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@102: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@102: // Chris@102: // 2013/09 Vicente J. Botet Escriba Chris@102: // Adapt to boost from CCIA C++11 implementation Chris@102: Chris@102: #ifndef BOOST_THREAD_EXECUTORS_EXECUTOR_HPP Chris@102: #define BOOST_THREAD_EXECUTORS_EXECUTOR_HPP Chris@102: Chris@102: #include Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: Chris@102: #include Chris@102: Chris@102: namespace boost Chris@102: { Chris@102: namespace executors Chris@102: { Chris@102: class executor Chris@102: { Chris@102: public: Chris@102: /// type-erasure to store the works to do Chris@102: typedef executors::work work; Chris@102: Chris@102: /// executor is not copyable. Chris@102: BOOST_THREAD_NO_COPYABLE(executor) Chris@102: executor() {} Chris@102: Chris@102: /** Chris@102: * \par Effects Chris@102: * Destroys the executor. Chris@102: * Chris@102: * \par Synchronization Chris@102: * The completion of all the closures happen before the completion of the executor destructor. Chris@102: */ Chris@102: virtual ~executor() {}; Chris@102: Chris@102: /** Chris@102: * \par Effects Chris@102: * Close the \c executor for submissions. Chris@102: * The worker threads will work until there is no more closures to run. Chris@102: */ Chris@102: virtual void close() = 0; Chris@102: Chris@102: /** Chris@102: * \par Returns Chris@102: * Whether the pool is closed for submissions. Chris@102: */ Chris@102: virtual bool closed() = 0; Chris@102: Chris@102: /** Chris@102: * \par Effects Chris@102: * The specified closure will be scheduled for execution at some point in the future. Chris@102: * If invoked closure throws an exception the executor will call std::terminate, as is the case with threads. Chris@102: * Chris@102: * \par Synchronization Chris@102: * Ccompletion of closure on a particular thread happens before destruction of thread's thread local variables. Chris@102: * Chris@102: * \par Throws Chris@102: * \c sync_queue_is_closed if the thread pool is closed. Chris@102: * Whatever exception that can be throw while storing the closure. Chris@102: */ Chris@102: virtual void submit(BOOST_THREAD_RV_REF(work) closure) = 0; Chris@102: // virtual void submit(work& closure) = 0; Chris@102: Chris@102: /** Chris@102: * \par Requires Chris@102: * \c Closure is a model of Callable(void()) and a model of CopyConstructible/MoveConstructible. Chris@102: * Chris@102: * \par Effects Chris@102: * The specified closure will be scheduled for execution at some point in the future. Chris@102: * If invoked closure throws an exception the thread pool will call std::terminate, as is the case with threads. Chris@102: * Chris@102: * \par Synchronization Chris@102: * Completion of closure on a particular thread happens before destruction of thread's thread local variables. Chris@102: * Chris@102: * \par Throws Chris@102: * \c sync_queue_is_closed if the thread pool is closed. Chris@102: * Whatever exception that can be throw while storing the closure. Chris@102: */ Chris@102: Chris@102: #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) Chris@102: template Chris@102: void submit(Closure & closure) Chris@102: { Chris@102: work w ((closure)); Chris@102: submit(boost::move(w)); Chris@102: } Chris@102: #endif Chris@102: void submit(void (*closure)()) Chris@102: { Chris@102: work w ((closure)); Chris@102: submit(boost::move(w)); Chris@102: } Chris@102: Chris@102: template Chris@102: void submit(BOOST_THREAD_RV_REF(Closure) closure) Chris@102: { Chris@102: work w = boost::move(closure); Chris@102: submit(boost::move(w)); Chris@102: } Chris@102: Chris@102: /** Chris@102: * \par Effects Chris@102: * Try to execute one task. Chris@102: * Chris@102: * \par Returns Chris@102: * Whether a task has been executed. Chris@102: * Chris@102: * \par Throws Chris@102: * Whatever the current task constructor throws or the task() throws. Chris@102: */ Chris@102: virtual bool try_executing_one() = 0; Chris@102: Chris@102: /** Chris@102: * \par Requires Chris@102: * This must be called from an scheduled task. Chris@102: * Chris@102: * \par Effects Chris@102: * Reschedule functions until pred() Chris@102: */ Chris@102: template Chris@102: bool reschedule_until(Pred const& pred) Chris@102: { Chris@102: do { Chris@102: //schedule_one_or_yield(); Chris@102: if ( ! try_executing_one()) Chris@102: { Chris@102: return false; Chris@102: } Chris@102: } while (! pred()); Chris@102: return true; Chris@102: } Chris@102: }; Chris@102: Chris@102: } Chris@102: using executors::executor; Chris@102: } Chris@102: Chris@102: #include Chris@102: Chris@102: #endif