Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:58

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_ASYNC_FUTURE_HPP
0012 #define BOOST_COMPUTE_ASYNC_FUTURE_HPP
0013 
0014 #include <boost/compute/event.hpp>
0015 
0016 namespace boost {
0017 namespace compute {
0018 
0019 /// \class future
0020 /// \brief Holds the result of an asynchronous computation.
0021 ///
0022 /// \see event, wait_list
0023 template<class T>
0024 class future
0025 {
0026 public:
0027     future()
0028         : m_event(0)
0029     {
0030     }
0031 
0032     future(const T &result, const event &event)
0033         : m_result(result),
0034           m_event(event)
0035     {
0036     }
0037 
0038     future(const future<T> &other)
0039         : m_result(other.m_result),
0040           m_event(other.m_event)
0041     {
0042     }
0043 
0044     future& operator=(const future<T> &other)
0045     {
0046         if(this != &other){
0047             m_result = other.m_result;
0048             m_event = other.m_event;
0049         }
0050 
0051         return *this;
0052     }
0053 
0054     ~future()
0055     {
0056     }
0057 
0058     /// Returns the result of the computation. This will block until
0059     /// the result is ready.
0060     T get()
0061     {
0062         wait();
0063 
0064         return m_result;
0065     }
0066 
0067     /// Returns \c true if the future is valid.
0068     bool valid() const
0069     {
0070         return m_event != 0;
0071     }
0072 
0073     /// Blocks until the computation is complete.
0074     void wait() const
0075     {
0076         m_event.wait();
0077     }
0078 
0079     /// Returns the underlying event object.
0080     event get_event() const
0081     {
0082         return m_event;
0083     }
0084 
0085     #if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
0086     /// Invokes a generic callback function once the future is ready.
0087     ///
0088     /// The function specified by callback must be invokable with zero arguments.
0089     ///
0090     /// \see_opencl_ref{clSetEventCallback}
0091     /// \opencl_version_warning{1,1}
0092     template<class Function>
0093     future& then(Function callback)
0094     {
0095         m_event.set_callback(callback);
0096         return *this;
0097     }
0098     #endif // BOOST_COMPUTE_CL_VERSION_1_1
0099 
0100 private:
0101     T m_result;
0102     event m_event;
0103 };
0104 
0105 /// \internal_
0106 template<>
0107 class future<void>
0108 {
0109 public:
0110     future()
0111         : m_event(0)
0112     {
0113     }
0114 
0115     template<class T>
0116     future(const future<T> &other)
0117         : m_event(other.get_event())
0118     {
0119     }
0120 
0121     explicit future(const event &event)
0122         : m_event(event)
0123     {
0124     }
0125 
0126     template<class T>
0127     future<void> &operator=(const future<T> &other)
0128     {
0129         m_event = other.get_event();
0130 
0131         return *this;
0132     }
0133 
0134     future<void> &operator=(const future<void> &other)
0135     {
0136         if(this != &other){
0137             m_event = other.m_event;
0138         }
0139 
0140         return *this;
0141     }
0142 
0143     ~future()
0144     {
0145     }
0146 
0147     void get()
0148     {
0149         wait();
0150     }
0151 
0152     bool valid() const
0153     {
0154         return m_event != 0;
0155     }
0156 
0157     void wait() const
0158     {
0159         m_event.wait();
0160     }
0161 
0162     event get_event() const
0163     {
0164         return m_event;
0165     }
0166 
0167     #if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
0168     /// Invokes a generic callback function once the future is ready.
0169     ///
0170     /// The function specified by callback must be invokable with zero arguments.
0171     ///
0172     /// \see_opencl_ref{clSetEventCallback}
0173     /// \opencl_version_warning{1,1}
0174     template<class Function>
0175     future<void> &then(Function callback)
0176     {
0177         m_event.set_callback(callback);
0178         return *this;
0179     }
0180     #endif // BOOST_COMPUTE_CL_VERSION_1_1
0181 
0182 private:
0183     event m_event;
0184 };
0185 
0186 /// \internal_
0187 template<class Result>
0188 inline future<Result> make_future(const Result &result, const event &event)
0189 {
0190     return future<Result>(result, event);
0191 }
0192 
0193 } // end compute namespace
0194 } // end boost namespace
0195 
0196 #endif // BOOST_COMPUTE_ASYNC_FUTURE_HPP