123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- // -*- C++ -*-
- //===--------------------------- mutex ------------------------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is dual licensed under the MIT and the University of Illinois Open
- // Source Licenses. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- #ifndef _LIBCPP_SGX_MUTEX
- #define _LIBCPP_SGX_MUTEX
- /*
- mutex synopsis
- namespace std
- {
- class mutex
- {
- public:
- constexpr mutex() noexcept;
- ~mutex();
- mutex(const mutex&) = delete;
- mutex& operator=(const mutex&) = delete;
- void lock();
- bool try_lock();
- void unlock();
- typedef sgx_thread_mutex_t* native_handle_type;
- native_handle_type native_handle();
- };
- class recursive_mutex
- {
- public:
- recursive_mutex();
- ~recursive_mutex();
- recursive_mutex(const recursive_mutex&) = delete;
- recursive_mutex& operator=(const recursive_mutex&) = delete;
- void lock();
- bool try_lock() noexcept;
- void unlock();
- typedef sgx_thread_mutex_t* native_handle_type;
- native_handle_type native_handle();
- };
- Not supported (time API) : class timed_mutex
- {
- public:
- timed_mutex();
- ~timed_mutex();
- timed_mutex(const timed_mutex&) = delete;
- timed_mutex& operator=(const timed_mutex&) = delete;
- void lock();
- bool try_lock();
- template <class Rep, class Period>
- bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
- template <class Clock, class Duration>
- bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
- void unlock();
- };
- Not supported (time API) : class recursive_timed_mutex
- {
- public:
- recursive_timed_mutex();
- ~recursive_timed_mutex();
- recursive_timed_mutex(const recursive_timed_mutex&) = delete;
- recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
- void lock();
- bool try_lock() noexcept;
- template <class Rep, class Period>
- bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
- template <class Clock, class Duration>
- bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
- void unlock();
- };
- struct defer_lock_t {};
- struct try_to_lock_t {};
- struct adopt_lock_t {};
- constexpr defer_lock_t defer_lock{};
- constexpr try_to_lock_t try_to_lock{};
- constexpr adopt_lock_t adopt_lock{};
- template <class Mutex>
- class lock_guard
- {
- public:
- typedef Mutex mutex_type;
- explicit lock_guard(mutex_type& m);
- lock_guard(mutex_type& m, adopt_lock_t);
- ~lock_guard();
- lock_guard(lock_guard const&) = delete;
- lock_guard& operator=(lock_guard const&) = delete;
- };
- template <class Mutex>
- class unique_lock
- {
- public:
- typedef Mutex mutex_type;
- unique_lock() noexcept;
- explicit unique_lock(mutex_type& m);
- unique_lock(mutex_type& m, defer_lock_t) noexcept;
- unique_lock(mutex_type& m, try_to_lock_t);
- unique_lock(mutex_type& m, adopt_lock_t);
- template <class Clock, class Duration>
- unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
- template <class Rep, class Period>
- unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
- ~unique_lock();
- unique_lock(unique_lock const&) = delete;
- unique_lock& operator=(unique_lock const&) = delete;
- unique_lock(unique_lock&& u) noexcept;
- unique_lock& operator=(unique_lock&& u) noexcept;
- void lock();
- bool try_lock();
- template <class Rep, class Period>
- bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
- template <class Clock, class Duration>
- bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
- void unlock();
- void swap(unique_lock& u) noexcept;
- mutex_type* release() noexcept;
- bool owns_lock() const noexcept;
- explicit operator bool () const noexcept;
- mutex_type* mutex() const noexcept;
- };
- template <class Mutex>
- void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
- template <class L1, class L2, class... L3>
- int try_lock(L1&, L2&, L3&...);
- template <class L1, class L2, class... L3>
- void lock(L1&, L2&, L3&...);
- struct once_flag
- {
- constexpr once_flag() noexcept;
- once_flag(const once_flag&) = delete;
- once_flag& operator=(const once_flag&) = delete;
- };
- template<class Callable, class ...Args>
- void call_once(once_flag& flag, Callable&& func, Args&&... args);
- } // std
- */
- #include <__config>
- #include <sgx_thread.h>
- #include <support/sgx/__sgx_mutex_base>
- #include <functional>
- #include <memory>
- #include <tuple>
- #include <__undef_min_max>
- #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
- #pragma GCC system_header
- #endif
- _LIBCPP_BEGIN_NAMESPACE_STD
- class _LIBCPP_TYPE_VIS recursive_mutex
- {
- sgx_thread_mutex_t __m_;
- public:
- recursive_mutex();
- ~recursive_mutex();
- recursive_mutex(const recursive_mutex&) = delete;
- recursive_mutex& operator=(const recursive_mutex&) = delete;
- public:
- void lock();
- bool try_lock() _NOEXCEPT;
- void unlock() _NOEXCEPT;
- typedef sgx_thread_mutex_t* native_handle_type;
- _LIBCPP_INLINE_VISIBILITY
- native_handle_type native_handle() { return &__m_; }
- };
- // We don't support timed_mutex and recursive_timed_mutex because they need time API
- template <class _L0, class _L1>
- int
- try_lock(_L0& __l0, _L1& __l1)
- {
- unique_lock<_L0> __u0(__l0, try_to_lock);
- if (__u0.owns_lock())
- {
- if (__l1.try_lock())
- {
- __u0.release();
- return -1;
- }
- else
- return 1;
- }
- return 0;
- }
- template <class _L0, class _L1, class _L2, class... _L3>
- int
- try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
- {
- int __r = 0;
- unique_lock<_L0> __u0(__l0, try_to_lock);
- if (__u0.owns_lock())
- {
- __r = try_lock(__l1, __l2, __l3...);
- if (__r == -1)
- __u0.release();
- else
- ++__r;
- }
- return __r;
- }
- template <class _L0, class _L1>
- void
- lock(_L0& __l0, _L1& __l1)
- {
- while (true)
- {
- {
- unique_lock<_L0> __u0(__l0);
- if (__l1.try_lock())
- {
- __u0.release();
- break;
- }
- }
- #if 0 // We don't have sched_yield
- sched_yield();
- #endif
- {
- unique_lock<_L1> __u1(__l1);
- if (__l0.try_lock())
- {
- __u1.release();
- break;
- }
- }
- #if 0 // We don't have sched_yield
- sched_yield();
- #endif
- }
- }
- template <class _L0, class _L1, class _L2, class ..._L3>
- void
- __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
- {
- while (true)
- {
- switch (__i)
- {
- case 0:
- {
- unique_lock<_L0> __u0(__l0);
- __i = try_lock(__l1, __l2, __l3...);
- if (__i == -1)
- {
- __u0.release();
- return;
- }
- }
- ++__i;
- #if 0 // We don't have sched_yield
- sched_yield();
- #endif
- break;
- case 1:
- {
- unique_lock<_L1> __u1(__l1);
- __i = try_lock(__l2, __l3..., __l0);
- if (__i == -1)
- {
- __u1.release();
- return;
- }
- }
- if (__i == sizeof...(_L3)+1)
- __i = 0;
- else
- __i += 2;
- #if 0 // We don't have sched_yield
- sched_yield();
- #endif
- break;
- default:
- __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
- return;
- }
- }
- }
- template <class _L0, class _L1, class _L2, class ..._L3>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
- {
- __lock_first(0, __l0, __l1, __l2, __l3...);
- }
- #if defined(_LIBCPP_SGX_HAS_CXX_ATOMIC)
- struct _LIBCPP_TYPE_VIS_ONLY once_flag;
- template<class _Callable, class... _Args>
- _LIBCPP_INLINE_VISIBILITY
- void call_once(once_flag&, _Callable&&, _Args&&...);
- struct _LIBCPP_TYPE_VIS_ONLY once_flag
- {
- _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_CONSTEXPR
- once_flag() _NOEXCEPT : __state_(0) {}
- private:
- once_flag(const once_flag&); // = delete;
- once_flag& operator=(const once_flag&); // = delete;
- unsigned long __state_;
- template<class _Callable, class... _Args>
- friend
- void call_once(once_flag&, _Callable&&, _Args&&...);
- };
- template <class _Fp>
- class __call_once_param
- {
- _Fp& __f_;
- public:
- _LIBCPP_INLINE_VISIBILITY
- explicit __call_once_param(_Fp& __f) : __f_(__f) {}
- _LIBCPP_INLINE_VISIBILITY
- void operator()()
- {
- typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
- __execute(_Index());
- }
- private:
- template <size_t ..._Indices>
- _LIBCPP_INLINE_VISIBILITY
- void __execute(__tuple_indices<_Indices...>)
- {
- __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
- }
- };
- template <class _Fp>
- void
- __call_once_proxy(void* __vp)
- {
- __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
- (*__p)();
- }
- void
- _LIBCPP_FUNC_VIS __call_once(volatile unsigned long&, void*, void(*)(void*));
- template<class _Callable, class... _Args>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
- {
- if(__libcpp_relaxed_load(& __flag.__state_) != ~0ul)
- {
- typedef tuple<_Callable&&, _Args&&...> _Gp;
- _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
- __call_once_param<_Gp> __p(__f);
- __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
- }
- }
- #endif // defined(_LIBCPP_SGX_HAS_CXX_ATOMIC)
- _LIBCPP_END_NAMESPACE_STD
- #endif // _LIBCPP_SGX_MUTEX
|