wait_guard.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2015 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_WAIT_GUARD_HPP
  11. #define BOOST_COMPUTE_ASYNC_WAIT_GUARD_HPP
  12. #include <boost/noncopyable.hpp>
  13. namespace boost {
  14. namespace compute {
  15. /// \class wait_guard
  16. /// \brief A guard object for synchronizing an operation on the device
  17. ///
  18. /// The wait_guard class stores a waitable object representing an operation
  19. /// on a compute device (e.g. \ref event, \ref future "future<T>") and calls
  20. /// its \c wait() method when the guard object goes out of scope.
  21. ///
  22. /// This is useful for ensuring that an OpenCL operation completes before
  23. /// leaving the current scope and cleaning up any resources.
  24. ///
  25. /// For example:
  26. /// \code
  27. /// // enqueue a compute kernel for execution
  28. /// event e = queue.enqueue_nd_range_kernel(...);
  29. ///
  30. /// // call e.wait() upon exiting the current scope
  31. /// wait_guard<event> guard(e);
  32. /// \endcode
  33. ///
  34. /// \ref wait_list, wait_for_all()
  35. template<class Waitable>
  36. class wait_guard : boost::noncopyable
  37. {
  38. public:
  39. /// Creates a new wait_guard object for \p waitable.
  40. wait_guard(const Waitable &waitable)
  41. : m_waitable(waitable)
  42. {
  43. }
  44. /// Destroys the wait_guard object. The default implementation will call
  45. /// \c wait() on the stored waitable object.
  46. ~wait_guard()
  47. {
  48. m_waitable.wait();
  49. }
  50. private:
  51. Waitable m_waitable;
  52. };
  53. } // end compute namespace
  54. } // end boost namespace
  55. #endif // BOOST_COMPUTE_ASYNC_WAIT_GUARD_HPP