sgx_mutex 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // -*- C++ -*-
  2. //===--------------------------- mutex ------------------------------------===//
  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
  11. #define _LIBCPP_SGX_MUTEX
  12. /*
  13. mutex synopsis
  14. namespace std
  15. {
  16. class mutex
  17. {
  18. public:
  19. constexpr mutex() noexcept;
  20. ~mutex();
  21. mutex(const mutex&) = delete;
  22. mutex& operator=(const mutex&) = delete;
  23. void lock();
  24. bool try_lock();
  25. void unlock();
  26. typedef sgx_thread_mutex_t* native_handle_type;
  27. native_handle_type native_handle();
  28. };
  29. class recursive_mutex
  30. {
  31. public:
  32. recursive_mutex();
  33. ~recursive_mutex();
  34. recursive_mutex(const recursive_mutex&) = delete;
  35. recursive_mutex& operator=(const recursive_mutex&) = delete;
  36. void lock();
  37. bool try_lock() noexcept;
  38. void unlock();
  39. typedef sgx_thread_mutex_t* native_handle_type;
  40. native_handle_type native_handle();
  41. };
  42. Not supported (time API) : class timed_mutex
  43. {
  44. public:
  45. timed_mutex();
  46. ~timed_mutex();
  47. timed_mutex(const timed_mutex&) = delete;
  48. timed_mutex& operator=(const timed_mutex&) = delete;
  49. void lock();
  50. bool try_lock();
  51. template <class Rep, class Period>
  52. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
  53. template <class Clock, class Duration>
  54. bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
  55. void unlock();
  56. };
  57. Not supported (time API) : class recursive_timed_mutex
  58. {
  59. public:
  60. recursive_timed_mutex();
  61. ~recursive_timed_mutex();
  62. recursive_timed_mutex(const recursive_timed_mutex&) = delete;
  63. recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
  64. void lock();
  65. bool try_lock() noexcept;
  66. template <class Rep, class Period>
  67. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
  68. template <class Clock, class Duration>
  69. bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
  70. void unlock();
  71. };
  72. struct defer_lock_t {};
  73. struct try_to_lock_t {};
  74. struct adopt_lock_t {};
  75. constexpr defer_lock_t defer_lock{};
  76. constexpr try_to_lock_t try_to_lock{};
  77. constexpr adopt_lock_t adopt_lock{};
  78. template <class Mutex>
  79. class lock_guard
  80. {
  81. public:
  82. typedef Mutex mutex_type;
  83. explicit lock_guard(mutex_type& m);
  84. lock_guard(mutex_type& m, adopt_lock_t);
  85. ~lock_guard();
  86. lock_guard(lock_guard const&) = delete;
  87. lock_guard& operator=(lock_guard const&) = delete;
  88. };
  89. template <class Mutex>
  90. class unique_lock
  91. {
  92. public:
  93. typedef Mutex mutex_type;
  94. unique_lock() noexcept;
  95. explicit unique_lock(mutex_type& m);
  96. unique_lock(mutex_type& m, defer_lock_t) noexcept;
  97. unique_lock(mutex_type& m, try_to_lock_t);
  98. unique_lock(mutex_type& m, adopt_lock_t);
  99. template <class Clock, class Duration>
  100. unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
  101. template <class Rep, class Period>
  102. unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
  103. ~unique_lock();
  104. unique_lock(unique_lock const&) = delete;
  105. unique_lock& operator=(unique_lock const&) = delete;
  106. unique_lock(unique_lock&& u) noexcept;
  107. unique_lock& operator=(unique_lock&& u) noexcept;
  108. void lock();
  109. bool try_lock();
  110. template <class Rep, class Period>
  111. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
  112. template <class Clock, class Duration>
  113. bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
  114. void unlock();
  115. void swap(unique_lock& u) noexcept;
  116. mutex_type* release() noexcept;
  117. bool owns_lock() const noexcept;
  118. explicit operator bool () const noexcept;
  119. mutex_type* mutex() const noexcept;
  120. };
  121. template <class Mutex>
  122. void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
  123. template <class L1, class L2, class... L3>
  124. int try_lock(L1&, L2&, L3&...);
  125. template <class L1, class L2, class... L3>
  126. void lock(L1&, L2&, L3&...);
  127. struct once_flag
  128. {
  129. constexpr once_flag() noexcept;
  130. once_flag(const once_flag&) = delete;
  131. once_flag& operator=(const once_flag&) = delete;
  132. };
  133. template<class Callable, class ...Args>
  134. void call_once(once_flag& flag, Callable&& func, Args&&... args);
  135. } // std
  136. */
  137. #include <__config>
  138. #include <sgx_thread.h>
  139. #include <support/sgx/__sgx_mutex_base>
  140. #include <functional>
  141. #include <memory>
  142. #include <tuple>
  143. #include <__undef_min_max>
  144. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  145. #pragma GCC system_header
  146. #endif
  147. _LIBCPP_BEGIN_NAMESPACE_STD
  148. class _LIBCPP_TYPE_VIS recursive_mutex
  149. {
  150. sgx_thread_mutex_t __m_;
  151. public:
  152. recursive_mutex();
  153. ~recursive_mutex();
  154. recursive_mutex(const recursive_mutex&) = delete;
  155. recursive_mutex& operator=(const recursive_mutex&) = delete;
  156. public:
  157. void lock();
  158. bool try_lock() _NOEXCEPT;
  159. void unlock() _NOEXCEPT;
  160. typedef sgx_thread_mutex_t* native_handle_type;
  161. _LIBCPP_INLINE_VISIBILITY
  162. native_handle_type native_handle() { return &__m_; }
  163. };
  164. // We don't support timed_mutex and recursive_timed_mutex because they need time API
  165. template <class _L0, class _L1>
  166. int
  167. try_lock(_L0& __l0, _L1& __l1)
  168. {
  169. unique_lock<_L0> __u0(__l0, try_to_lock);
  170. if (__u0.owns_lock())
  171. {
  172. if (__l1.try_lock())
  173. {
  174. __u0.release();
  175. return -1;
  176. }
  177. else
  178. return 1;
  179. }
  180. return 0;
  181. }
  182. template <class _L0, class _L1, class _L2, class... _L3>
  183. int
  184. try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
  185. {
  186. int __r = 0;
  187. unique_lock<_L0> __u0(__l0, try_to_lock);
  188. if (__u0.owns_lock())
  189. {
  190. __r = try_lock(__l1, __l2, __l3...);
  191. if (__r == -1)
  192. __u0.release();
  193. else
  194. ++__r;
  195. }
  196. return __r;
  197. }
  198. template <class _L0, class _L1>
  199. void
  200. lock(_L0& __l0, _L1& __l1)
  201. {
  202. while (true)
  203. {
  204. {
  205. unique_lock<_L0> __u0(__l0);
  206. if (__l1.try_lock())
  207. {
  208. __u0.release();
  209. break;
  210. }
  211. }
  212. #if 0 // We don't have sched_yield
  213. sched_yield();
  214. #endif
  215. {
  216. unique_lock<_L1> __u1(__l1);
  217. if (__l0.try_lock())
  218. {
  219. __u1.release();
  220. break;
  221. }
  222. }
  223. #if 0 // We don't have sched_yield
  224. sched_yield();
  225. #endif
  226. }
  227. }
  228. template <class _L0, class _L1, class _L2, class ..._L3>
  229. void
  230. __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
  231. {
  232. while (true)
  233. {
  234. switch (__i)
  235. {
  236. case 0:
  237. {
  238. unique_lock<_L0> __u0(__l0);
  239. __i = try_lock(__l1, __l2, __l3...);
  240. if (__i == -1)
  241. {
  242. __u0.release();
  243. return;
  244. }
  245. }
  246. ++__i;
  247. #if 0 // We don't have sched_yield
  248. sched_yield();
  249. #endif
  250. break;
  251. case 1:
  252. {
  253. unique_lock<_L1> __u1(__l1);
  254. __i = try_lock(__l2, __l3..., __l0);
  255. if (__i == -1)
  256. {
  257. __u1.release();
  258. return;
  259. }
  260. }
  261. if (__i == sizeof...(_L3)+1)
  262. __i = 0;
  263. else
  264. __i += 2;
  265. #if 0 // We don't have sched_yield
  266. sched_yield();
  267. #endif
  268. break;
  269. default:
  270. __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
  271. return;
  272. }
  273. }
  274. }
  275. template <class _L0, class _L1, class _L2, class ..._L3>
  276. inline _LIBCPP_INLINE_VISIBILITY
  277. void
  278. lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
  279. {
  280. __lock_first(0, __l0, __l1, __l2, __l3...);
  281. }
  282. #if defined(_LIBCPP_SGX_HAS_CXX_ATOMIC)
  283. struct _LIBCPP_TYPE_VIS_ONLY once_flag;
  284. template<class _Callable, class... _Args>
  285. _LIBCPP_INLINE_VISIBILITY
  286. void call_once(once_flag&, _Callable&&, _Args&&...);
  287. struct _LIBCPP_TYPE_VIS_ONLY once_flag
  288. {
  289. _LIBCPP_INLINE_VISIBILITY
  290. _LIBCPP_CONSTEXPR
  291. once_flag() _NOEXCEPT : __state_(0) {}
  292. private:
  293. once_flag(const once_flag&); // = delete;
  294. once_flag& operator=(const once_flag&); // = delete;
  295. unsigned long __state_;
  296. template<class _Callable, class... _Args>
  297. friend
  298. void call_once(once_flag&, _Callable&&, _Args&&...);
  299. };
  300. template <class _Fp>
  301. class __call_once_param
  302. {
  303. _Fp& __f_;
  304. public:
  305. _LIBCPP_INLINE_VISIBILITY
  306. explicit __call_once_param(_Fp& __f) : __f_(__f) {}
  307. _LIBCPP_INLINE_VISIBILITY
  308. void operator()()
  309. {
  310. typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
  311. __execute(_Index());
  312. }
  313. private:
  314. template <size_t ..._Indices>
  315. _LIBCPP_INLINE_VISIBILITY
  316. void __execute(__tuple_indices<_Indices...>)
  317. {
  318. __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
  319. }
  320. };
  321. template <class _Fp>
  322. void
  323. __call_once_proxy(void* __vp)
  324. {
  325. __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
  326. (*__p)();
  327. }
  328. void
  329. _LIBCPP_FUNC_VIS __call_once(volatile unsigned long&, void*, void(*)(void*));
  330. template<class _Callable, class... _Args>
  331. inline _LIBCPP_INLINE_VISIBILITY
  332. void
  333. call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
  334. {
  335. if(__libcpp_relaxed_load(& __flag.__state_) != ~0ul)
  336. {
  337. typedef tuple<_Callable&&, _Args&&...> _Gp;
  338. _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
  339. __call_once_param<_Gp> __p(__f);
  340. __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
  341. }
  342. }
  343. #endif // defined(_LIBCPP_SGX_HAS_CXX_ATOMIC)
  344. _LIBCPP_END_NAMESPACE_STD
  345. #endif // _LIBCPP_SGX_MUTEX