1 // Copyright (C) 2015 Vicente J. Botet Escriba 2 // 3 // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 // 6 // 2013/11 Vicente J. Botet Escriba 7 // first implementation of a simple serial scheduler. 8 9 #ifndef BOOST_THREAD_SERIAL_EXECUTOR_CONT_HPP 10 #define BOOST_THREAD_SERIAL_EXECUTOR_CONT_HPP 11 12 #include <boost/thread/detail/config.hpp> 13 #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION && defined BOOST_THREAD_PROVIDES_EXECUTORS && defined BOOST_THREAD_USES_MOVE 14 15 #include <exception> // std::terminate 16 #include <boost/throw_exception.hpp> 17 #include <boost/thread/detail/delete.hpp> 18 #include <boost/thread/detail/move.hpp> 19 #include <boost/thread/concurrent_queues/sync_queue.hpp> 20 #include <boost/thread/executors/work.hpp> 21 #include <boost/thread/executors/generic_executor_ref.hpp> 22 #include <boost/thread/mutex.hpp> 23 #include <boost/thread/future.hpp> 24 #include <boost/thread/scoped_thread.hpp> 25 #include <boost/thread/lock_guard.hpp> 26 27 #include <boost/config/abi_prefix.hpp> 28 29 namespace boost 30 { 31 namespace executors 32 { 33 class serial_executor_cont 34 { 35 public: 36 /// type-erasure to store the works to do 37 typedef executors::work work; 38 private: 39 40 generic_executor_ref ex_; 41 BOOST_THREAD_FUTURE<void> fut_; // protected by mtx_ 42 bool closed_; // protected by mtx_ 43 mutex mtx_; 44 45 struct continuation { 46 work task; 47 template <class X> 48 struct result { 49 typedef void type; 50 }; continuationboost::executors::serial_executor_cont::continuation51 continuation(BOOST_THREAD_RV_REF(work) tsk) 52 : task(boost::move(tsk)) {} operator ()boost::executors::serial_executor_cont::continuation53 void operator()(BOOST_THREAD_FUTURE<void> f) 54 { 55 try { 56 task(); 57 } catch (...) { 58 std::terminate(); 59 } 60 } 61 }; 62 closed(lock_guard<mutex> &) const63 bool closed(lock_guard<mutex>&) const 64 { 65 return closed_; 66 } 67 public: 68 /** 69 * \par Returns 70 * The underlying executor wrapped on a generic executor reference. 71 */ underlying_executor()72 generic_executor_ref& underlying_executor() BOOST_NOEXCEPT { return ex_; } 73 74 /// serial_executor_cont is not copyable. 75 BOOST_THREAD_NO_COPYABLE(serial_executor_cont) 76 77 /** 78 * \b Effects: creates a serial executor that runs closures in fifo order using one the associated executor. 79 * 80 * \b Throws: Whatever exception is thrown while initializing the needed resources. 81 * 82 * \b Notes: 83 * * The lifetime of the associated executor must outlive the serial executor. 84 * * The current implementation doesn't support submission from synchronous continuation, that is, 85 * - the executor must execute the continuation asynchronously or 86 * - the continuation can not submit to this serial executor. 87 */ 88 template <class Executor> serial_executor_cont(Executor & ex)89 serial_executor_cont(Executor& ex) 90 : ex_(ex), fut_(make_ready_future()), closed_(false) 91 { 92 } 93 /** 94 * \b Effects: Destroys the thread pool. 95 * 96 * \b Synchronization: The completion of all the closures happen before the completion of the \c serial_executor_cont destructor. 97 */ ~serial_executor_cont()98 ~serial_executor_cont() 99 { 100 // signal to the worker thread that there will be no more submissions. 101 close(); 102 } 103 104 /** 105 * \b Effects: close the \c serial_executor_cont for submissions. 106 * The loop will work until there is no more closures to run. 107 */ close()108 void close() 109 { 110 lock_guard<mutex> lk(mtx_); 111 closed_ = true;; 112 } 113 114 /** 115 * \b Returns: whether the pool is closed for submissions. 116 */ closed()117 bool closed() 118 { 119 lock_guard<mutex> lk(mtx_); 120 return closed(lk); 121 } 122 123 /** 124 * Effects: none. 125 * Returns: always false. 126 * Throws: No. 127 * Remark: A serial executor can not execute one of its pending tasks as the tasks depends on the other tasks. 128 */ try_executing_one()129 bool try_executing_one() 130 { 131 return false; 132 } 133 134 /** 135 * \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible. 136 * 137 * \b Effects: The specified \c closure will be scheduled for execution after the last submitted closure finish. 138 * If the invoked closure throws an exception the \c serial_executor_cont will call \c std::terminate, as is the case with threads. 139 * 140 * \b Throws: \c sync_queue_is_closed if the executor is closed. 141 * Whatever exception that can be throw while storing the closure. 142 * 143 */ 144 145 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) 146 template <typename Closure> submit(Closure & closure)147 void submit(Closure & closure) 148 { 149 lock_guard<mutex> lk(mtx_); 150 if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() ); 151 fut_ = fut_.then(ex_, continuation(work(closure))); 152 } 153 #endif submit(void (* closure)())154 void submit(void (*closure)()) 155 { 156 lock_guard<mutex> lk(mtx_); 157 if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() ); 158 fut_ = fut_.then(ex_, continuation(work(closure))); 159 } 160 161 template <typename Closure> submit(BOOST_THREAD_FWD_REF (Closure)closure)162 void submit(BOOST_THREAD_FWD_REF(Closure) closure) 163 { 164 lock_guard<mutex> lk(mtx_); 165 if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() ); 166 fut_ = fut_.then(ex_, continuation(work(boost::forward<Closure>(closure)))); 167 } 168 169 }; 170 } 171 using executors::serial_executor_cont; 172 } 173 174 #include <boost/config/abi_suffix.hpp> 175 176 #endif 177 #endif 178