future.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <[email protected]>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_ASYNC_FUTURE_HPP
  11. #define BOOST_COMPUTE_ASYNC_FUTURE_HPP
  12. #include <boost/compute/event.hpp>
  13. namespace boost {
  14. namespace compute {
  15. /// \class future
  16. /// \brief Holds the result of an asynchronous computation.
  17. ///
  18. /// \see event, wait_list
  19. template<class T>
  20. class future
  21. {
  22. public:
  23. future()
  24. : m_event(0)
  25. {
  26. }
  27. future(const T &result, const event &event)
  28. : m_result(result),
  29. m_event(event)
  30. {
  31. }
  32. future(const future<T> &other)
  33. : m_result(other.m_result),
  34. m_event(other.m_event)
  35. {
  36. }
  37. future& operator=(const future<T> &other)
  38. {
  39. if(this != &other){
  40. m_result = other.m_result;
  41. m_event = other.m_event;
  42. }
  43. return *this;
  44. }
  45. ~future()
  46. {
  47. }
  48. /// Returns the result of the computation. This will block until
  49. /// the result is ready.
  50. T get()
  51. {
  52. wait();
  53. return m_result;
  54. }
  55. /// Returns \c true if the future is valid.
  56. bool valid() const
  57. {
  58. return m_event != 0;
  59. }
  60. /// Blocks until the computation is complete.
  61. void wait() const
  62. {
  63. m_event.wait();
  64. }
  65. /// Returns the underlying event object.
  66. event get_event() const
  67. {
  68. return m_event;
  69. }
  70. #if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  71. /// Invokes a generic callback function once the future is ready.
  72. ///
  73. /// The function specified by callback must be invokable with zero arguments.
  74. ///
  75. /// \see_opencl_ref{clSetEventCallback}
  76. /// \opencl_version_warning{1,1}
  77. template<class Function>
  78. future& then(Function callback)
  79. {
  80. m_event.set_callback(callback);
  81. return *this;
  82. }
  83. #endif // BOOST_COMPUTE_CL_VERSION_1_1
  84. private:
  85. T m_result;
  86. event m_event;
  87. };
  88. /// \internal_
  89. template<>
  90. class future<void>
  91. {
  92. public:
  93. future()
  94. : m_event(0)
  95. {
  96. }
  97. template<class T>
  98. future(const future<T> &other)
  99. : m_event(other.get_event())
  100. {
  101. }
  102. explicit future(const event &event)
  103. : m_event(event)
  104. {
  105. }
  106. template<class T>
  107. future<void> &operator=(const future<T> &other)
  108. {
  109. m_event = other.get_event();
  110. return *this;
  111. }
  112. future<void> &operator=(const future<void> &other)
  113. {
  114. if(this != &other){
  115. m_event = other.m_event;
  116. }
  117. return *this;
  118. }
  119. ~future()
  120. {
  121. }
  122. void get()
  123. {
  124. wait();
  125. }
  126. bool valid() const
  127. {
  128. return m_event != 0;
  129. }
  130. void wait() const
  131. {
  132. m_event.wait();
  133. }
  134. event get_event() const
  135. {
  136. return m_event;
  137. }
  138. #if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  139. /// Invokes a generic callback function once the future is ready.
  140. ///
  141. /// The function specified by callback must be invokable with zero arguments.
  142. ///
  143. /// \see_opencl_ref{clSetEventCallback}
  144. /// \opencl_version_warning{1,1}
  145. template<class Function>
  146. future<void> &then(Function callback)
  147. {
  148. m_event.set_callback(callback);
  149. return *this;
  150. }
  151. #endif // BOOST_COMPUTE_CL_VERSION_1_1
  152. private:
  153. event m_event;
  154. };
  155. /// \internal_
  156. template<class Result>
  157. inline future<Result> make_future(const Result &result, const event &event)
  158. {
  159. return future<Result>(result, event);
  160. }
  161. } // end compute namespace
  162. } // end boost namespace
  163. #endif // BOOST_COMPUTE_ASYNC_FUTURE_HPP