__sgx_mutex_base 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP___SGX_MUTEX_BASE
  11. #define _LIBCPP___SGX_MUTEX_BASE
  12. #include <__config>
  13. #include <system_error>
  14. #include <sgx_thread.h>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. #pragma GCC system_header
  17. #endif
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. class _LIBCPP_TYPE_VIS mutex
  20. {
  21. sgx_thread_mutex_t __m_;
  22. public:
  23. _LIBCPP_INLINE_VISIBILITY
  24. #ifndef _LIBCPP_HAS_NO_CONSTEXPR
  25. constexpr mutex() _NOEXCEPT : __m_ SGX_THREAD_NONRECURSIVE_MUTEX_INITIALIZER {}
  26. #else
  27. mutex() _NOEXCEPT {__m_ = (sgx_thread_mutex_t)SGX_THREAD_NONRECURSIVE_MUTEX_INITIALIZER;}
  28. #endif
  29. ~mutex();
  30. mutex(const mutex&) = delete;
  31. mutex& operator=(const mutex&) = delete;
  32. public:
  33. void lock();
  34. bool try_lock() _NOEXCEPT;
  35. void unlock() _NOEXCEPT;
  36. typedef sgx_thread_mutex_t* native_handle_type;
  37. _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;}
  38. };
  39. struct _LIBCPP_TYPE_VIS defer_lock_t {};
  40. struct _LIBCPP_TYPE_VIS try_to_lock_t {};
  41. struct _LIBCPP_TYPE_VIS adopt_lock_t {};
  42. #if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MUTEX)
  43. extern const defer_lock_t defer_lock;
  44. extern const try_to_lock_t try_to_lock;
  45. extern const adopt_lock_t adopt_lock;
  46. #else
  47. constexpr defer_lock_t defer_lock = defer_lock_t();
  48. constexpr try_to_lock_t try_to_lock = try_to_lock_t();
  49. constexpr adopt_lock_t adopt_lock = adopt_lock_t();
  50. #endif
  51. template <class _Mutex>
  52. class _LIBCPP_TYPE_VIS_ONLY lock_guard
  53. {
  54. public:
  55. typedef _Mutex mutex_type;
  56. private:
  57. mutex_type& __m_;
  58. public:
  59. _LIBCPP_INLINE_VISIBILITY
  60. explicit lock_guard(mutex_type& __m)
  61. : __m_(__m) {__m_.lock();}
  62. _LIBCPP_INLINE_VISIBILITY
  63. lock_guard(mutex_type& __m, adopt_lock_t)
  64. : __m_(__m) {}
  65. _LIBCPP_INLINE_VISIBILITY
  66. ~lock_guard() {__m_.unlock();}
  67. lock_guard(lock_guard const&) = delete;
  68. lock_guard& operator=(lock_guard const&) = delete;
  69. };
  70. template <class _Mutex>
  71. class _LIBCPP_TYPE_VIS_ONLY unique_lock
  72. {
  73. public:
  74. typedef _Mutex mutex_type;
  75. private:
  76. mutex_type* __m_;
  77. bool __owns_;
  78. public:
  79. _LIBCPP_INLINE_VISIBILITY
  80. unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
  81. _LIBCPP_INLINE_VISIBILITY
  82. explicit unique_lock(mutex_type& __m)
  83. : __m_(&__m), __owns_(true) {__m_->lock();}
  84. _LIBCPP_INLINE_VISIBILITY
  85. unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
  86. : __m_(&__m), __owns_(false) {}
  87. _LIBCPP_INLINE_VISIBILITY
  88. unique_lock(mutex_type& __m, try_to_lock_t)
  89. : __m_(&__m), __owns_(__m.try_lock()) {}
  90. _LIBCPP_INLINE_VISIBILITY
  91. unique_lock(mutex_type& __m, adopt_lock_t)
  92. : __m_(&__m), __owns_(true) {}
  93. _LIBCPP_INLINE_VISIBILITY
  94. ~unique_lock()
  95. {
  96. if (__owns_)
  97. __m_->unlock();
  98. }
  99. unique_lock(unique_lock const&) = delete;
  100. unique_lock& operator=(unique_lock const&) = delete;
  101. public:
  102. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  103. _LIBCPP_INLINE_VISIBILITY
  104. unique_lock(unique_lock&& __u) _NOEXCEPT
  105. : __m_(__u.__m_), __owns_(__u.__owns_)
  106. {__u.__m_ = nullptr; __u.__owns_ = false;}
  107. _LIBCPP_INLINE_VISIBILITY
  108. unique_lock& operator=(unique_lock&& __u) _NOEXCEPT
  109. {
  110. if (__owns_)
  111. __m_->unlock();
  112. __m_ = __u.__m_;
  113. __owns_ = __u.__owns_;
  114. __u.__m_ = nullptr;
  115. __u.__owns_ = false;
  116. return *this;
  117. }
  118. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  119. void lock();
  120. bool try_lock();
  121. void unlock();
  122. _LIBCPP_INLINE_VISIBILITY
  123. void swap(unique_lock& __u) _NOEXCEPT
  124. {
  125. _VSTD::swap(__m_, __u.__m_);
  126. _VSTD::swap(__owns_, __u.__owns_);
  127. }
  128. _LIBCPP_INLINE_VISIBILITY
  129. mutex_type* release() _NOEXCEPT
  130. {
  131. mutex_type* __m = __m_;
  132. __m_ = nullptr;
  133. __owns_ = false;
  134. return __m;
  135. }
  136. _LIBCPP_INLINE_VISIBILITY
  137. bool owns_lock() const _NOEXCEPT {return __owns_;}
  138. _LIBCPP_INLINE_VISIBILITY
  139. _LIBCPP_EXPLICIT
  140. operator bool () const _NOEXCEPT {return __owns_;}
  141. _LIBCPP_INLINE_VISIBILITY
  142. mutex_type* mutex() const _NOEXCEPT {return __m_;}
  143. };
  144. template <class _Mutex>
  145. void
  146. unique_lock<_Mutex>::lock()
  147. {
  148. if (__m_ == nullptr)
  149. __throw_system_error(EPERM, "unique_lock::lock: references null mutex");
  150. if (__owns_)
  151. __throw_system_error(EDEADLK, "unique_lock::lock: already locked");
  152. __m_->lock();
  153. __owns_ = true;
  154. }
  155. template <class _Mutex>
  156. bool
  157. unique_lock<_Mutex>::try_lock()
  158. {
  159. if (__m_ == nullptr)
  160. __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex");
  161. if (__owns_)
  162. __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked");
  163. __owns_ = __m_->try_lock();
  164. return __owns_;
  165. }
  166. template <class _Mutex>
  167. void
  168. unique_lock<_Mutex>::unlock()
  169. {
  170. if (!__owns_)
  171. __throw_system_error(EPERM, "unique_lock::unlock: not locked");
  172. __m_->unlock();
  173. __owns_ = false;
  174. }
  175. template <class _Mutex>
  176. inline _LIBCPP_INLINE_VISIBILITY
  177. void
  178. swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT
  179. {__x.swap(__y);}
  180. //enum class cv_status
  181. _LIBCPP_DECLARE_STRONG_ENUM(cv_status)
  182. {
  183. no_timeout,
  184. timeout
  185. };
  186. _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(cv_status)
  187. class _LIBCPP_TYPE_VIS condition_variable
  188. {
  189. sgx_thread_cond_t __cv_;
  190. public:
  191. _LIBCPP_INLINE_VISIBILITY
  192. #ifndef _LIBCPP_HAS_NO_CONSTEXPR
  193. constexpr condition_variable() : __cv_ SGX_THREAD_COND_INITIALIZER {}
  194. #else
  195. condition_variable() {__cv_ = (pthread_cond_t)SGX_THREAD_COND_INITIALIZER;}
  196. #endif
  197. ~condition_variable();
  198. condition_variable(const condition_variable&) = delete;
  199. condition_variable& operator=(const condition_variable&) = delete;
  200. void notify_one() _NOEXCEPT;
  201. void notify_all() _NOEXCEPT;
  202. void wait(unique_lock<mutex>& __lk) _NOEXCEPT;
  203. template <class _Predicate>
  204. void wait(unique_lock<mutex>& __lk, _Predicate __pred);
  205. typedef sgx_thread_cond_t* native_handle_type;
  206. _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;}
  207. };
  208. template <class _Predicate>
  209. void
  210. condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred)
  211. {
  212. while (!__pred())
  213. wait(__lk);
  214. }
  215. _LIBCPP_END_NAMESPACE_STD
  216. #endif // _LIBCPP___SGX_MUTEX_BASE