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/11 Vicente J. Botet Escriba Chris@102: // first implementation of a simple user scheduler. Chris@102: // 2013/11 Vicente J. Botet Escriba Chris@102: // rename loop_executor. Chris@102: Chris@102: #ifndef BOOST_THREAD_EXECUTORS_LOOP_EXECUTOR_HPP Chris@102: #define BOOST_THREAD_EXECUTORS_LOOP_EXECUTOR_HPP Chris@102: 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: Chris@102: class loop_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: /// the thread safe work queue Chris@102: concurrent::sync_queue work_queue; Chris@102: Chris@102: public: 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: task(); 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: Chris@102: Chris@102: public: Chris@102: /// loop_executor is not copyable. Chris@102: BOOST_THREAD_NO_COPYABLE(loop_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: loop_executor() 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 loop_executor destructor. Chris@102: */ Chris@102: ~loop_executor() Chris@102: { Chris@102: // signal to all the worker thread that there will be no more submissions. Chris@102: close(); Chris@102: } Chris@102: Chris@102: /** Chris@102: * The main loop of the worker thread Chris@102: */ Chris@102: void loop() 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: /** Chris@102: * \b Effects: close the \c loop_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 loop_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: * run queued closures Chris@102: */ Chris@102: void run_queued_closures() Chris@102: { Chris@102: sync_queue::underlying_queue_type q = work_queue.underlying_queue(); Chris@102: while (! q.empty()) Chris@102: { Chris@102: work& task = q.front(); Chris@102: task(); Chris@102: q.pop_front(); Chris@102: } Chris@102: } Chris@102: Chris@102: }; Chris@102: } Chris@102: using executors::loop_executor; Chris@102: Chris@102: } Chris@102: Chris@102: #include Chris@102: Chris@102: #endif