recursive_mutex.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. //
  6. // based on boost::interprocess::sync::interprocess_spinlock
  7. #ifndef BOOST_FIBERS_RECURSIVE_MUTEX_H
  8. #define BOOST_FIBERS_RECURSIVE_MUTEX_H
  9. #include <cstddef>
  10. #include <boost/config.hpp>
  11. #include <boost/assert.hpp>
  12. #include <boost/fiber/context.hpp>
  13. #include <boost/fiber/detail/config.hpp>
  14. #include <boost/fiber/detail/spinlock.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 recursive_mutex {
  26. private:
  27. friend class condition_variable;
  28. typedef context::wait_queue_t wait_queue_type;
  29. detail::spinlock wait_queue_splk_{};
  30. wait_queue_type wait_queue_{};
  31. context * owner_{ nullptr };
  32. std::size_t count_{ 0 };
  33. public:
  34. recursive_mutex() = default;
  35. ~recursive_mutex() {
  36. BOOST_ASSERT( nullptr == owner_);
  37. BOOST_ASSERT( 0 == count_);
  38. BOOST_ASSERT( wait_queue_.empty() );
  39. }
  40. recursive_mutex( recursive_mutex const&) = delete;
  41. recursive_mutex & operator=( recursive_mutex const&) = delete;
  42. void lock();
  43. bool try_lock() noexcept;
  44. void unlock();
  45. };
  46. }}
  47. #ifdef _MSC_VER
  48. # pragma warning(pop)
  49. #endif
  50. #ifdef BOOST_HAS_ABI_HEADERS
  51. # include BOOST_ABI_SUFFIX
  52. #endif
  53. #endif // BOOST_FIBERS_RECURSIVE_MUTEX_H