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