Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:01:11

0001 // Copyright (C) 2014 Vicente J. Botet Escriba
0002 //
0003 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
0004 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 //
0006 // 2014/01 Vicente J. Botet Escriba
0007 //    first implementation of a thread_executor.
0008 
0009 #ifndef BOOST_THREAD_THREAD_EXECUTOR_HPP
0010 #define BOOST_THREAD_THREAD_EXECUTOR_HPP
0011 
0012 #include <boost/thread/detail/config.hpp>
0013 #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION && defined BOOST_THREAD_PROVIDES_EXECUTORS && defined BOOST_THREAD_USES_MOVE
0014 
0015 #include <boost/throw_exception.hpp>
0016 #include <boost/thread/detail/delete.hpp>
0017 #include <boost/thread/detail/move.hpp>
0018 #include <boost/thread/executors/work.hpp>
0019 #include <boost/thread/executors/executor.hpp>
0020 #include <boost/thread/mutex.hpp>
0021 #include <boost/thread/lock_guard.hpp>
0022 #include <boost/thread/thread_only.hpp>
0023 #include <boost/thread/scoped_thread.hpp>
0024 #include <boost/thread/csbl/vector.hpp>
0025 #include <boost/thread/concurrent_queues/queue_op_status.hpp>
0026 
0027 #include <boost/config/abi_prefix.hpp>
0028 
0029 namespace boost
0030 {
0031 namespace executors
0032 {
0033   class thread_executor
0034   {
0035   public:
0036     /// type-erasure to store the works to do
0037     typedef  executors::work work;
0038     bool closed_;
0039     typedef scoped_thread<> thread_t;
0040     typedef csbl::vector<thread_t> threads_type;
0041     threads_type threads_;
0042     mutable mutex mtx_;
0043 
0044     /**
0045      * Effects: try to execute one task.
0046      * Returns: whether a task has been executed.
0047      * Throws: whatever the current task constructor throws or the task() throws.
0048      */
0049     bool try_executing_one()
0050     {
0051       return false;
0052     }
0053 
0054   public:
0055     /// thread_executor is not copyable.
0056     BOOST_THREAD_NO_COPYABLE(thread_executor)
0057 
0058     /**
0059      * \b Effects: creates a inline executor that runs closures immediately.
0060      *
0061      * \b Throws: Nothing.
0062      */
0063     thread_executor()
0064     : closed_(false)
0065     {
0066     }
0067     /**
0068      * \b Effects: Waits for closures (if any) to complete, then joins and destroys the threads.
0069      *
0070      * \b Synchronization: The completion of all the closures happen before the completion of the \c thread_executor destructor.
0071      */
0072     ~thread_executor()
0073     {
0074       // signal to all the worker thread that there will be no more submissions.
0075       close();
0076       // all the scoped threads will join before destroying
0077     }
0078 
0079     /**
0080      * \b Effects: close the \c thread_executor for submissions.
0081      * The loop will work until there is no more closures to run.
0082      */
0083     void close()
0084     {
0085       lock_guard<mutex> lk(mtx_);
0086       closed_ = true;
0087     }
0088 
0089     /**
0090      * \b Returns: whether the pool is closed for submissions.
0091      */
0092     bool closed(lock_guard<mutex>& )
0093     {
0094       return closed_;
0095     }
0096     bool closed()
0097     {
0098       lock_guard<mutex> lk(mtx_);
0099       return closed(lk);
0100     }
0101 
0102     /**
0103      * \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible.
0104      *
0105      * \b Effects: The specified \c closure will be scheduled for execution at some point in the future.
0106      * If invoked closure throws an exception the \c thread_executor will call \c std::terminate, as is the case with threads.
0107      *
0108      * \b Synchronization: completion of \c closure on a particular thread happens before destruction of thread's thread local variables.
0109      *
0110      * \b Throws: \c sync_queue_is_closed if the thread pool is closed.
0111      * Whatever exception that can be throw while storing the closure.
0112      */
0113 
0114 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0115     template <typename Closure>
0116     void submit(Closure & closure)
0117     {
0118       lock_guard<mutex> lk(mtx_);
0119       if (closed(lk))  BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
0120       threads_.reserve(threads_.size() + 1);
0121       thread th(closure);
0122       threads_.push_back(thread_t(boost::move(th)));
0123     }
0124 #endif
0125     void submit(void (*closure)())
0126     {
0127       lock_guard<mutex> lk(mtx_);
0128       if (closed(lk))  BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
0129       threads_.reserve(threads_.size() + 1);
0130       thread th(closure);
0131       threads_.push_back(thread_t(boost::move(th)));
0132     }
0133 
0134     template <typename Closure>
0135     void submit(BOOST_THREAD_FWD_REF(Closure) closure)
0136     {
0137       lock_guard<mutex> lk(mtx_);
0138       if (closed(lk))  BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
0139       threads_.reserve(threads_.size() + 1);
0140       thread th(boost::forward<Closure>(closure));
0141       threads_.push_back(thread_t(boost::move(th)));
0142     }
0143 
0144     /**
0145      * \b Requires: This must be called from an scheduled task.
0146      *
0147      * \b Effects: reschedule functions until pred()
0148      */
0149     template <typename Pred>
0150     bool reschedule_until(Pred const&)
0151     {
0152       return false;
0153     }
0154 
0155   };
0156 }
0157 using executors::thread_executor;
0158 }
0159 
0160 #include <boost/config/abi_suffix.hpp>
0161 
0162 #endif
0163 #endif