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 
0007 #ifndef BOOST_THREAD_EXECUTORS_GENERIC_EXECUTOR_REF_HPP
0008 #define BOOST_THREAD_EXECUTORS_GENERIC_EXECUTOR_REF_HPP
0009 
0010 #include <boost/thread/detail/config.hpp>
0011 #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION && defined BOOST_THREAD_PROVIDES_EXECUTORS && defined BOOST_THREAD_USES_MOVE
0012 
0013 #include <boost/thread/detail/delete.hpp>
0014 #include <boost/thread/detail/move.hpp>
0015 #include <boost/thread/executors/executor.hpp>
0016 
0017 #include <boost/shared_ptr.hpp>
0018 
0019 #include <boost/config/abi_prefix.hpp>
0020 
0021 namespace boost
0022 {
0023   namespace executors
0024   {
0025 
0026   template <class Executor>
0027   class executor_ref : public executor
0028   {
0029     Executor& ex;
0030   public:
0031     /// type-erasure to store the works to do
0032     typedef  executors::work work;
0033 
0034     /// executor is not copyable.
0035     BOOST_THREAD_NO_COPYABLE(executor_ref)
0036     executor_ref(Executor& ex_) : ex(ex_) {}
0037 
0038     /**
0039      * \par Effects
0040      * Destroys the executor.
0041      *
0042      * \par Synchronization
0043      * The completion of all the closures happen before the completion of the executor destructor.
0044      */
0045     ~executor_ref() {}
0046 
0047     /**
0048      * \par Effects
0049      * Close the \c executor for submissions.
0050      * The worker threads will work until there is no more closures to run.
0051      */
0052     void close() { ex.close(); }
0053 
0054     /**
0055      * \par Returns
0056      * Whether the pool is closed for submissions.
0057      */
0058     bool closed() { return ex.closed(); }
0059 
0060     /**
0061      * \par Effects
0062      * The specified closure will be scheduled for execution at some point in the future.
0063      * If invoked closure throws an exception the executor will call std::terminate, as is the case with threads.
0064      *
0065      * \par Synchronization
0066      * Ccompletion of closure on a particular thread happens before destruction of thread's thread local variables.
0067      *
0068      * \par Throws
0069      * \c sync_queue_is_closed if the thread pool is closed.
0070      * Whatever exception that can be throw while storing the closure.
0071      */
0072     void submit(BOOST_THREAD_RV_REF(work) closure) {
0073       ex.submit(boost::move(closure));
0074     }
0075 //    void submit(work& closure) {
0076 //      ex.submit(closure);
0077 //    }
0078 
0079 
0080     /**
0081      * \par Effects
0082      * Try to execute one task.
0083      *
0084      * \par Returns
0085      * Whether a task has been executed.
0086      *
0087      * \par Throws
0088      * Whatever the current task constructor throws or the task() throws.
0089      */
0090     bool try_executing_one() { return ex.try_executing_one(); }
0091 
0092   };
0093 
0094   class generic_executor_ref
0095   {
0096     shared_ptr<executor> ex;
0097   public:
0098     /// type-erasure to store the works to do
0099     typedef executors::work work;
0100 
0101     template<typename Executor>
0102     generic_executor_ref(Executor& ex_)
0103     //: ex(make_shared<executor_ref<Executor> >(ex_)) // todo check why this doesn't works with C++03
0104     : ex( new executor_ref<Executor>(ex_) )
0105     {
0106     }
0107 
0108     //generic_executor_ref(generic_executor_ref const& other) noexcept    {}
0109     //generic_executor_ref& operator=(generic_executor_ref const& other) noexcept    {}
0110 
0111 
0112     /**
0113      * \par Effects
0114      * Close the \c executor for submissions.
0115      * The worker threads will work until there is no more closures to run.
0116      */
0117     void close() { ex->close(); }
0118 
0119     /**
0120      * \par Returns
0121      * Whether the pool is closed for submissions.
0122      */
0123     bool closed() { return ex->closed(); }
0124 
0125     /**
0126      * \par Requires
0127      * \c Closure is a model of Callable(void()) and a model of CopyConstructible/MoveConstructible.
0128      *
0129      * \par Effects
0130      * The specified closure will be scheduled for execution at some point in the future.
0131      * If invoked closure throws an exception the thread pool will call std::terminate, as is the case with threads.
0132      *
0133      * \par Synchronization
0134      * Completion of closure on a particular thread happens before destruction of thread's thread local variables.
0135      *
0136      * \par Throws
0137      * \c sync_queue_is_closed if the thread pool is closed.
0138      * Whatever exception that can be throw while storing the closure.
0139      */
0140 
0141     void submit(BOOST_THREAD_RV_REF(work) closure)
0142     {
0143       ex->submit(boost::move(closure));
0144     }
0145 
0146 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0147     template <typename Closure>
0148     void submit(Closure & closure)
0149     {
0150       //work w ((closure));
0151       //submit(boost::move(w));
0152       submit(work(closure));
0153     }
0154 #endif
0155     void submit(void (*closure)())
0156     {
0157       work w ((closure));
0158       submit(boost::move(w));
0159       //submit(work(closure));
0160     }
0161 
0162     template <typename Closure>
0163     void submit(BOOST_THREAD_FWD_REF(Closure) closure)
0164     {
0165       work w((boost::forward<Closure>(closure)));
0166       submit(boost::move(w));
0167     }
0168 
0169 //    size_t num_pending_closures() const
0170 //    {
0171 //      return ex->num_pending_closures();
0172 //    }
0173 
0174     /**
0175      * \par Effects
0176      * Try to execute one task.
0177      *
0178      * \par Returns
0179      * Whether a task has been executed.
0180      *
0181      * \par Throws
0182      * Whatever the current task constructor throws or the task() throws.
0183      */
0184     bool try_executing_one() { return ex->try_executing_one(); }
0185 
0186     /**
0187      * \par Requires
0188      * This must be called from an scheduled task.
0189      *
0190      * \par Effects
0191      * reschedule functions until pred()
0192      */
0193     template <typename Pred>
0194     bool reschedule_until(Pred const& pred)
0195     {
0196       do {
0197         //schedule_one_or_yield();
0198         if ( ! try_executing_one())
0199         {
0200           return false;
0201         }
0202       } while (! pred());
0203       return true;
0204     }
0205 
0206   };
0207   }
0208   using executors::executor_ref;
0209   using executors::generic_executor_ref;
0210 }
0211 
0212 #include <boost/config/abi_suffix.hpp>
0213 
0214 #endif
0215 #endif