1 // Copyright (C) 2013,2014 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/09 Vicente J. Botet Escriba 7 // Adapt to boost from CCIA C++11 implementation 8 9 #ifndef BOOST_THREAD_EXECUTORS_EXECUTOR_HPP 10 #define BOOST_THREAD_EXECUTORS_EXECUTOR_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 <boost/thread/detail/delete.hpp> 16 #include <boost/thread/detail/move.hpp> 17 #include <boost/thread/executors/work.hpp> 18 19 #include <boost/config/abi_prefix.hpp> 20 21 namespace boost 22 { 23 namespace executors 24 { 25 class executor 26 { 27 public: 28 /// type-erasure to store the works to do 29 typedef executors::work work; 30 31 /// executor is not copyable. 32 BOOST_THREAD_NO_COPYABLE(executor) executor()33 executor() {} 34 35 /** 36 * \par Effects 37 * Destroys the executor. 38 * 39 * \par Synchronization 40 * The completion of all the closures happen before the completion of the executor destructor. 41 */ ~executor()42 virtual ~executor() {} 43 44 /** 45 * \par Effects 46 * Close the \c executor for submissions. 47 * The worker threads will work until there is no more closures to run. 48 */ 49 virtual void close() = 0; 50 51 /** 52 * \par Returns 53 * Whether the pool is closed for submissions. 54 */ 55 virtual bool closed() = 0; 56 57 /** 58 * \par Effects 59 * The specified closure will be scheduled for execution at some point in the future. 60 * If invoked closure throws an exception the executor will call std::terminate, as is the case with threads. 61 * 62 * \par Synchronization 63 * Ccompletion of closure on a particular thread happens before destruction of thread's thread local variables. 64 * 65 * \par Throws 66 * \c sync_queue_is_closed if the thread pool is closed. 67 * Whatever exception that can be throw while storing the closure. 68 */ 69 virtual void submit(BOOST_THREAD_RV_REF(work) closure) = 0; 70 // virtual void submit(work& closure) = 0; 71 72 /** 73 * \par Requires 74 * \c Closure is a model of Callable(void()) and a model of CopyConstructible/MoveConstructible. 75 * 76 * \par Effects 77 * The specified closure will be scheduled for execution at some point in the future. 78 * If invoked closure throws an exception the thread pool will call std::terminate, as is the case with threads. 79 * 80 * \par Synchronization 81 * Completion of closure on a particular thread happens before destruction of thread's thread local variables. 82 * 83 * \par Throws 84 * \c sync_queue_is_closed if the thread pool is closed. 85 * Whatever exception that can be throw while storing the closure. 86 */ 87 88 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) 89 template <typename Closure> submit(Closure & closure)90 void submit(Closure & closure) 91 { 92 work w ((closure)); 93 submit(boost::move(w)); 94 } 95 #endif submit(void (* closure)())96 void submit(void (*closure)()) 97 { 98 work w ((closure)); 99 submit(boost::move(w)); 100 } 101 102 template <typename Closure> submit(BOOST_THREAD_FWD_REF (Closure)closure)103 void submit(BOOST_THREAD_FWD_REF(Closure) closure) 104 { 105 //submit(work(boost::forward<Closure>(closure))); 106 work w((boost::forward<Closure>(closure))); 107 submit(boost::move(w)); 108 } 109 110 /** 111 * \par Effects 112 * Try to execute one task. 113 * 114 * \par Returns 115 * Whether a task has been executed. 116 * 117 * \par Throws 118 * Whatever the current task constructor throws or the task() throws. 119 */ 120 virtual bool try_executing_one() = 0; 121 122 /** 123 * \par Requires 124 * This must be called from an scheduled task. 125 * 126 * \par Effects 127 * Reschedule functions until pred() 128 */ 129 template <typename Pred> reschedule_until(Pred const & pred)130 bool reschedule_until(Pred const& pred) 131 { 132 do { 133 //schedule_one_or_yield(); 134 if ( ! try_executing_one()) 135 { 136 return false; 137 } 138 } while (! pred()); 139 return true; 140 } 141 }; 142 143 } 144 using executors::executor; 145 } 146 147 #include <boost/config/abi_suffix.hpp> 148 149 #endif 150 #endif 151