Chris@102: // Copyright (C) 2013 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/11 Vicente J. Botet Escriba Chris@102: // first implementation of a simple serial scheduler. Chris@102: Chris@102: #ifndef BOOST_THREAD_SERIAL_EXECUTOR_HPP Chris@102: #define BOOST_THREAD_SERIAL_EXECUTOR_HPP Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include 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 serial_executor Chris@102: { Chris@102: public: Chris@102: /// type-erasure to store the works to do Chris@102: typedef executors::work work; Chris@102: private: Chris@102: typedef scoped_thread<> thread_t; Chris@102: Chris@102: /// the thread safe work queue Chris@102: concurrent::sync_queue work_queue; Chris@102: generic_executor_ref ex; Chris@102: thread_t thr; Chris@102: Chris@102: struct try_executing_one_task { Chris@102: work& task; Chris@102: boost::promise &p; Chris@102: try_executing_one_task(work& task, boost::promise &p) Chris@102: : task(task), p(p) {} Chris@102: void operator()() { Chris@102: try { Chris@102: task(); Chris@102: p.set_value(); Chris@102: } catch (...) Chris@102: { Chris@102: p.set_exception(current_exception()); Chris@102: } Chris@102: } Chris@102: }; Chris@102: public: Chris@102: /** Chris@102: * \par Returns Chris@102: * The underlying executor wrapped on a generic executor reference. Chris@102: */ Chris@102: generic_executor_ref& underlying_executor() BOOST_NOEXCEPT { return ex; } Chris@102: Chris@102: /** Chris@102: * Effects: try to execute one task. Chris@102: * Returns: whether a task has been executed. Chris@102: * Throws: whatever the current task constructor throws or the task() throws. Chris@102: */ Chris@102: bool try_executing_one() Chris@102: { Chris@102: work task; Chris@102: try Chris@102: { Chris@102: if (work_queue.try_pull(task) == queue_op_status::success) Chris@102: { Chris@102: boost::promise p; Chris@102: try_executing_one_task tmp(task,p); Chris@102: ex.submit(tmp); Chris@102: p.get_future().wait(); Chris@102: return true; Chris@102: } Chris@102: return false; Chris@102: } Chris@102: catch (...) Chris@102: { Chris@102: std::terminate(); Chris@102: return false; Chris@102: } Chris@102: } Chris@102: private: Chris@102: /** Chris@102: * Effects: schedule one task or yields Chris@102: * Throws: whatever the current task constructor throws or the task() throws. Chris@102: */ Chris@102: void schedule_one_or_yield() Chris@102: { Chris@102: if ( ! try_executing_one()) Chris@102: { Chris@102: this_thread::yield(); Chris@102: } Chris@102: } Chris@102: Chris@102: /** Chris@102: * The main loop of the worker thread Chris@102: */ Chris@102: void worker_thread() Chris@102: { Chris@102: while (!closed()) Chris@102: { Chris@102: schedule_one_or_yield(); Chris@102: } Chris@102: while (try_executing_one()) Chris@102: { Chris@102: } Chris@102: } Chris@102: Chris@102: public: Chris@102: /// serial_executor is not copyable. Chris@102: BOOST_THREAD_NO_COPYABLE(serial_executor) Chris@102: Chris@102: /** Chris@102: * \b Effects: creates a thread pool that runs closures using one of its closure-executing methods. Chris@102: * Chris@102: * \b Throws: Whatever exception is thrown while initializing the needed resources. Chris@102: */ Chris@102: template Chris@102: serial_executor(Executor& ex) Chris@102: : ex(ex), thr(&serial_executor::worker_thread, this) Chris@102: { Chris@102: } Chris@102: /** Chris@102: * \b Effects: Destroys the thread pool. Chris@102: * Chris@102: * \b Synchronization: The completion of all the closures happen before the completion of the \c serial_executor destructor. Chris@102: */ Chris@102: ~serial_executor() Chris@102: { Chris@102: // signal to the worker thread that there will be no more submissions. Chris@102: close(); Chris@102: } Chris@102: Chris@102: /** Chris@102: * \b Effects: close the \c serial_executor for submissions. Chris@102: * The loop will work until there is no more closures to run. Chris@102: */ Chris@102: void close() Chris@102: { Chris@102: work_queue.close(); Chris@102: } Chris@102: Chris@102: /** Chris@102: * \b Returns: whether the pool is closed for submissions. Chris@102: */ Chris@102: bool closed() Chris@102: { Chris@102: return work_queue.closed(); Chris@102: } Chris@102: Chris@102: /** Chris@102: * \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible. Chris@102: * Chris@102: * \b Effects: The specified \c closure will be scheduled for execution at some point in the future. Chris@102: * If invoked closure throws an exception the \c serial_executor will call \c std::terminate, as is the case with threads. Chris@102: * Chris@102: * \b Synchronization: completion of \c closure on a particular thread happens before destruction of thread's thread local variables. Chris@102: * Chris@102: * \b Throws: \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_queue.push(work(closure)); Chris@102: } Chris@102: #endif Chris@102: void submit(void (*closure)()) Chris@102: { Chris@102: work_queue.push(work(closure)); Chris@102: } Chris@102: Chris@102: template Chris@102: void submit(BOOST_THREAD_RV_REF(Closure) closure) Chris@102: { Chris@102: work_queue.push(work(boost::forward(closure))); Chris@102: } Chris@102: Chris@102: /** Chris@102: * \b Requires: This must be called from an scheduled task. Chris@102: * Chris@102: * \b Effects: reschedule functions until pred() Chris@102: */ Chris@102: template Chris@102: bool reschedule_until(Pred const& pred) Chris@102: { Chris@102: do { 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::serial_executor; Chris@102: } Chris@102: Chris@102: #include Chris@102: Chris@102: #endif