mutex.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef BOOST_SYSTEM_DETAIL_MUTEX_HPP_INCLUDED
  2. #define BOOST_SYSTEM_DETAIL_MUTEX_HPP_INCLUDED
  3. // Copyright 2023 Peter Dimov
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/config.hpp>
  7. #if defined(BOOST_SYSTEM_DISABLE_THREADS)
  8. namespace boost
  9. {
  10. namespace system
  11. {
  12. namespace detail
  13. {
  14. struct mutex
  15. {
  16. void lock()
  17. {
  18. }
  19. void unlock()
  20. {
  21. }
  22. };
  23. } // namespace detail
  24. } // namespace system
  25. } // namespace boost
  26. #else // defined(BOOST_SYSTEM_DISABLE_THREADS)
  27. #if defined(BOOST_MSSTL_VERSION) && BOOST_MSSTL_VERSION >= 140
  28. // Under the MS STL, std::mutex::mutex() is not constexpr, as is
  29. // required by the standard, which leads to initialization order
  30. // issues. However, shared_mutex is based on SRWLock and its
  31. // default constructor is constexpr, so we use that instead.
  32. #include <boost/winapi/config.hpp>
  33. // SRWLOCK is not available when targeting Windows XP
  34. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  35. #include <shared_mutex>
  36. #if BOOST_MSSTL_VERSION >= 142 || _HAS_SHARED_MUTEX
  37. # define BOOST_SYSTEM_HAS_MSSTL_SHARED_MUTEX
  38. #endif
  39. #endif // BOOST_MSSTL_VERSION >= 142 || _HAS_SHARED_MUTEX
  40. #endif // BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  41. #if defined(BOOST_SYSTEM_HAS_MSSTL_SHARED_MUTEX)
  42. namespace boost
  43. {
  44. namespace system
  45. {
  46. namespace detail
  47. {
  48. typedef std::shared_mutex mutex;
  49. } // namespace detail
  50. } // namespace system
  51. } // namespace boost
  52. #else // defined(BOOST_SYSTEM_HAS_MSSTL_SHARED_MUTEX)
  53. #include <mutex>
  54. namespace boost
  55. {
  56. namespace system
  57. {
  58. namespace detail
  59. {
  60. using std::mutex;
  61. } // namespace detail
  62. } // namespace system
  63. } // namespace boost
  64. #endif // defined(BOOST_SYSTEM_HAS_MSSTL_SHARED_MUTEX)
  65. #endif // defined(BOOST_SYSTEM_DISABLE_THREADS)
  66. namespace boost
  67. {
  68. namespace system
  69. {
  70. namespace detail
  71. {
  72. template<class Mtx> class lock_guard
  73. {
  74. private:
  75. Mtx& mtx_;
  76. private:
  77. lock_guard( lock_guard const& );
  78. lock_guard& operator=( lock_guard const& );
  79. public:
  80. explicit lock_guard( Mtx& mtx ): mtx_( mtx )
  81. {
  82. mtx_.lock();
  83. }
  84. ~lock_guard()
  85. {
  86. mtx_.unlock();
  87. }
  88. };
  89. } // namespace detail
  90. } // namespace system
  91. } // namespace boost
  92. #endif // #ifndef BOOST_SYSTEM_DETAIL_MUTEX_HPP_INCLUDED