mutex.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #elif defined(BOOST_MSSTL_VERSION) && BOOST_MSSTL_VERSION >= 140
  27. // Under the MS STL, std::mutex::mutex() is not constexpr, as is
  28. // required by the standard, which leads to initialization order
  29. // issues. However, shared_mutex is based on SRWLock and its
  30. // default constructor is constexpr, so we use that instead.
  31. #include <shared_mutex>
  32. namespace boost
  33. {
  34. namespace system
  35. {
  36. namespace detail
  37. {
  38. typedef std::shared_mutex mutex;
  39. } // namespace detail
  40. } // namespace system
  41. } // namespace boost
  42. #else
  43. #include <mutex>
  44. namespace boost
  45. {
  46. namespace system
  47. {
  48. namespace detail
  49. {
  50. using std::mutex;
  51. } // namespace detail
  52. } // namespace system
  53. } // namespace boost
  54. #endif
  55. namespace boost
  56. {
  57. namespace system
  58. {
  59. namespace detail
  60. {
  61. template<class Mtx> class lock_guard
  62. {
  63. private:
  64. Mtx& mtx_;
  65. private:
  66. lock_guard( lock_guard const& );
  67. lock_guard& operator=( lock_guard const& );
  68. public:
  69. explicit lock_guard( Mtx& mtx ): mtx_( mtx )
  70. {
  71. mtx_.lock();
  72. }
  73. ~lock_guard()
  74. {
  75. mtx_.unlock();
  76. }
  77. };
  78. } // namespace detail
  79. } // namespace system
  80. } // namespace boost
  81. #endif // #ifndef BOOST_SYSTEM_DETAIL_MUTEX_HPP_INCLUDED