timed_mutex.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright Oliver Kowalke 2013.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_FIBERS_TIMED_MUTEX_H
  6. #define BOOST_FIBERS_TIMED_MUTEX_H
  7. #include <chrono>
  8. #include <boost/assert.hpp>
  9. #include <boost/config.hpp>
  10. #include <boost/fiber/context.hpp>
  11. #include <boost/fiber/detail/config.hpp>
  12. #include <boost/fiber/detail/convert.hpp>
  13. #include <boost/fiber/detail/spinlock.hpp>
  14. #include <boost/fiber/waker.hpp>
  15. #ifdef BOOST_HAS_ABI_HEADERS
  16. # include BOOST_ABI_PREFIX
  17. #endif
  18. #ifdef _MSC_VER
  19. # pragma warning(push)
  20. # pragma warning(disable:4251)
  21. #endif
  22. namespace boost {
  23. namespace fibers {
  24. class condition_variable;
  25. class BOOST_FIBERS_DECL timed_mutex {
  26. private:
  27. friend class condition_variable;
  28. detail::spinlock wait_queue_splk_{};
  29. wait_queue wait_queue_{};
  30. context * owner_{ nullptr };
  31. bool try_lock_until_( std::chrono::steady_clock::time_point const& timeout_time) noexcept;
  32. public:
  33. timed_mutex() = default;
  34. ~timed_mutex() {
  35. BOOST_ASSERT( nullptr == owner_);
  36. BOOST_ASSERT( wait_queue_.empty() );
  37. }
  38. timed_mutex( timed_mutex const&) = delete;
  39. timed_mutex & operator=( timed_mutex const&) = delete;
  40. void lock();
  41. bool try_lock();
  42. template< typename Clock, typename Duration >
  43. bool try_lock_until( std::chrono::time_point< Clock, Duration > const& timeout_time_) {
  44. std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_);
  45. return try_lock_until_( timeout_time);
  46. }
  47. template< typename Rep, typename Period >
  48. bool try_lock_for( std::chrono::duration< Rep, Period > const& timeout_duration) {
  49. return try_lock_until_( std::chrono::steady_clock::now() + timeout_duration);
  50. }
  51. void unlock();
  52. };
  53. }}
  54. #ifdef _MSC_VER
  55. # pragma warning(pop)
  56. #endif
  57. #ifdef BOOST_HAS_ABI_HEADERS
  58. # include BOOST_ABI_SUFFIX
  59. #endif
  60. #endif // BOOST_FIBERS_TIMED_MUTEX_H