timed_mutex.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #ifdef BOOST_HAS_ABI_HEADERS
  15. # include BOOST_ABI_PREFIX
  16. #endif
  17. #ifdef _MSC_VER
  18. # pragma warning(push)
  19. # pragma warning(disable:4251)
  20. #endif
  21. namespace boost {
  22. namespace fibers {
  23. class condition_variable;
  24. class BOOST_FIBERS_DECL timed_mutex {
  25. private:
  26. friend class condition_variable;
  27. typedef context::wait_queue_t wait_queue_type;
  28. detail::spinlock wait_queue_splk_{};
  29. wait_queue_type 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