recursive_mutex.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include <boost/fiber/waker.hpp>
  16. #ifdef BOOST_HAS_ABI_HEADERS
  17. # include BOOST_ABI_PREFIX
  18. #endif
  19. #ifdef _MSC_VER
  20. # pragma warning(push)
  21. # pragma warning(disable:4251)
  22. #endif
  23. namespace boost {
  24. namespace fibers {
  25. class condition_variable;
  26. class BOOST_FIBERS_DECL recursive_mutex {
  27. private:
  28. friend class condition_variable;
  29. detail::spinlock wait_queue_splk_{};
  30. wait_queue 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