Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

179 lines
4.7KB

  1. // std::mutex implementation -*- C++ -*-
  2. // Copyright (C) 2003-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/std_mutex.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{mutex}
  23. */
  24. #ifndef _GLIBCXX_MUTEX_H
  25. #define _GLIBCXX_MUTEX_H 1
  26. #pragma GCC system_header
  27. #if __cplusplus < 201103L
  28. # include <bits/c++0x_warning.h>
  29. #else
  30. #include <system_error>
  31. #include <bits/functexcept.h>
  32. #include <bits/gthr.h>
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  36. /**
  37. * @defgroup mutexes Mutexes
  38. * @ingroup concurrency
  39. *
  40. * Classes for mutex support.
  41. * @{
  42. */
  43. #ifdef _GLIBCXX_HAS_GTHREADS
  44. // Common base class for std::mutex and std::timed_mutex
  45. class __mutex_base
  46. {
  47. protected:
  48. typedef __gthread_mutex_t __native_type;
  49. #ifdef __GTHREAD_MUTEX_INIT
  50. __native_type _M_mutex = __GTHREAD_MUTEX_INIT;
  51. constexpr __mutex_base() noexcept = default;
  52. #else
  53. __native_type _M_mutex;
  54. __mutex_base() noexcept
  55. {
  56. // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
  57. __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
  58. }
  59. ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
  60. #endif
  61. __mutex_base(const __mutex_base&) = delete;
  62. __mutex_base& operator=(const __mutex_base&) = delete;
  63. };
  64. /// The standard mutex type.
  65. class mutex : private __mutex_base
  66. {
  67. public:
  68. typedef __native_type* native_handle_type;
  69. #ifdef __GTHREAD_MUTEX_INIT
  70. constexpr
  71. #endif
  72. mutex() noexcept = default;
  73. ~mutex() = default;
  74. mutex(const mutex&) = delete;
  75. mutex& operator=(const mutex&) = delete;
  76. void
  77. lock()
  78. {
  79. int __e = __gthread_mutex_lock(&_M_mutex);
  80. // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
  81. if (__e)
  82. __throw_system_error(__e);
  83. }
  84. bool
  85. try_lock() noexcept
  86. {
  87. // XXX EINVAL, EAGAIN, EBUSY
  88. return !__gthread_mutex_trylock(&_M_mutex);
  89. }
  90. void
  91. unlock()
  92. {
  93. // XXX EINVAL, EAGAIN, EPERM
  94. __gthread_mutex_unlock(&_M_mutex);
  95. }
  96. native_handle_type
  97. native_handle() noexcept
  98. { return &_M_mutex; }
  99. };
  100. #endif // _GLIBCXX_HAS_GTHREADS
  101. /// Do not acquire ownership of the mutex.
  102. struct defer_lock_t { explicit defer_lock_t() = default; };
  103. /// Try to acquire ownership of the mutex without blocking.
  104. struct try_to_lock_t { explicit try_to_lock_t() = default; };
  105. /// Assume the calling thread has already obtained mutex ownership
  106. /// and manage it.
  107. struct adopt_lock_t { explicit adopt_lock_t() = default; };
  108. /// Tag used to prevent a scoped lock from acquiring ownership of a mutex.
  109. _GLIBCXX17_INLINE constexpr defer_lock_t defer_lock { };
  110. /// Tag used to prevent a scoped lock from blocking if a mutex is locked.
  111. _GLIBCXX17_INLINE constexpr try_to_lock_t try_to_lock { };
  112. /// Tag used to make a scoped lock take ownership of a locked mutex.
  113. _GLIBCXX17_INLINE constexpr adopt_lock_t adopt_lock { };
  114. /** @brief A simple scoped lock type.
  115. *
  116. * A lock_guard controls mutex ownership within a scope, releasing
  117. * ownership in the destructor.
  118. */
  119. template<typename _Mutex>
  120. class lock_guard
  121. {
  122. public:
  123. typedef _Mutex mutex_type;
  124. explicit lock_guard(mutex_type& __m) : _M_device(__m)
  125. { _M_device.lock(); }
  126. lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m)
  127. { } // calling thread owns mutex
  128. ~lock_guard()
  129. { _M_device.unlock(); }
  130. lock_guard(const lock_guard&) = delete;
  131. lock_guard& operator=(const lock_guard&) = delete;
  132. private:
  133. mutex_type& _M_device;
  134. };
  135. // @} group mutexes
  136. _GLIBCXX_END_NAMESPACE_VERSION
  137. } // namespace
  138. #endif // C++11
  139. #endif // _GLIBCXX_MUTEX_H