123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- //===------------------------- mutex.cpp ----------------------------------===//
- //
- // 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.
- //
- //===----------------------------------------------------------------------===//
- #include "__config"
- #if !defined(_LIBCPP_SGX_CONFIG)
- #define _LIBCPP_BUILDING_MUTEX
- #include "mutex"
- #include "limits"
- #include "system_error"
- #include "cassert"
- #include "include/atomic_support.h"
- _LIBCPP_BEGIN_NAMESPACE_STD
- #ifndef _LIBCPP_HAS_NO_THREADS
- const defer_lock_t defer_lock = {};
- const try_to_lock_t try_to_lock = {};
- const adopt_lock_t adopt_lock = {};
- mutex::~mutex()
- {
- __libcpp_mutex_destroy(&__m_);
- }
- void
- mutex::lock()
- {
- int ec = __libcpp_mutex_lock(&__m_);
- if (ec)
- __throw_system_error(ec, "mutex lock failed");
- }
- bool
- mutex::try_lock() _NOEXCEPT
- {
- return __libcpp_mutex_trylock(&__m_) == 0;
- }
- void
- mutex::unlock() _NOEXCEPT
- {
- int ec = __libcpp_mutex_unlock(&__m_);
- (void)ec;
- assert(ec == 0);
- }
- // recursive_mutex
- recursive_mutex::recursive_mutex()
- {
- int ec = __libcpp_recursive_mutex_init(&__m_);
- if (ec)
- __throw_system_error(ec, "recursive_mutex constructor failed");
- }
- recursive_mutex::~recursive_mutex()
- {
- int e = __libcpp_mutex_destroy(&__m_);
- (void)e;
- assert(e == 0);
- }
- void
- recursive_mutex::lock()
- {
- int ec = __libcpp_mutex_lock(&__m_);
- if (ec)
- __throw_system_error(ec, "recursive_mutex lock failed");
- }
- void
- recursive_mutex::unlock() _NOEXCEPT
- {
- int e = __libcpp_mutex_unlock(&__m_);
- (void)e;
- assert(e == 0);
- }
- bool
- recursive_mutex::try_lock() _NOEXCEPT
- {
- return __libcpp_mutex_trylock(&__m_) == 0;
- }
- // timed_mutex
- timed_mutex::timed_mutex()
- : __locked_(false)
- {
- }
- timed_mutex::~timed_mutex()
- {
- lock_guard<mutex> _(__m_);
- }
- void
- timed_mutex::lock()
- {
- unique_lock<mutex> lk(__m_);
- while (__locked_)
- __cv_.wait(lk);
- __locked_ = true;
- }
- bool
- timed_mutex::try_lock() _NOEXCEPT
- {
- unique_lock<mutex> lk(__m_, try_to_lock);
- if (lk.owns_lock() && !__locked_)
- {
- __locked_ = true;
- return true;
- }
- return false;
- }
- void
- timed_mutex::unlock() _NOEXCEPT
- {
- lock_guard<mutex> _(__m_);
- __locked_ = false;
- __cv_.notify_one();
- }
- // recursive_timed_mutex
- recursive_timed_mutex::recursive_timed_mutex()
- : __count_(0),
- __id_(0)
- {
- }
- recursive_timed_mutex::~recursive_timed_mutex()
- {
- lock_guard<mutex> _(__m_);
- }
- void
- recursive_timed_mutex::lock()
- {
- __libcpp_thread_id id = __libcpp_thread_get_current_id();
- unique_lock<mutex> lk(__m_);
- if (__libcpp_thread_id_equal(id, __id_))
- {
- if (__count_ == numeric_limits<size_t>::max())
- __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
- ++__count_;
- return;
- }
- while (__count_ != 0)
- __cv_.wait(lk);
- __count_ = 1;
- __id_ = id;
- }
- bool
- recursive_timed_mutex::try_lock() _NOEXCEPT
- {
- __libcpp_thread_id id = __libcpp_thread_get_current_id();
- unique_lock<mutex> lk(__m_, try_to_lock);
- if (lk.owns_lock() && (__count_ == 0 || __libcpp_thread_id_equal(id, __id_)))
- {
- if (__count_ == numeric_limits<size_t>::max())
- return false;
- ++__count_;
- __id_ = id;
- return true;
- }
- return false;
- }
- void
- recursive_timed_mutex::unlock() _NOEXCEPT
- {
- unique_lock<mutex> lk(__m_);
- if (--__count_ == 0)
- {
- __id_ = 0;
- lk.unlock();
- __cv_.notify_one();
- }
- }
- #endif // !_LIBCPP_HAS_NO_THREADS
- // If dispatch_once_f ever handles C++ exceptions, and if one can get to it
- // without illegal macros (unexpected macros not beginning with _UpperCase or
- // __lowercase), and if it stops spinning waiting threads, then call_once should
- // call into dispatch_once_f instead of here. Relevant radar this code needs to
- // keep in sync with: 7741191.
- #ifndef _LIBCPP_HAS_NO_THREADS
- static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
- static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
- #endif
- /// NOTE: Changes to flag are done via relaxed atomic stores
- /// even though the accesses are protected by a mutex because threads
- /// just entering 'call_once` concurrently read from flag.
- void
- __call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
- {
- #if defined(_LIBCPP_HAS_NO_THREADS)
- if (flag == 0)
- {
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- flag = 1;
- func(arg);
- flag = ~0ul;
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- flag = 0ul;
- throw;
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- #else // !_LIBCPP_HAS_NO_THREADS
- __libcpp_mutex_lock(&mut);
- while (flag == 1)
- __libcpp_condvar_wait(&cv, &mut);
- if (flag == 0)
- {
- #ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
- #endif // _LIBCPP_NO_EXCEPTIONS
- __libcpp_relaxed_store(&flag, 1ul);
- __libcpp_mutex_unlock(&mut);
- func(arg);
- __libcpp_mutex_lock(&mut);
- __libcpp_relaxed_store(&flag, ~0ul);
- __libcpp_mutex_unlock(&mut);
- __libcpp_condvar_broadcast(&cv);
- #ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __libcpp_mutex_lock(&mut);
- __libcpp_relaxed_store(&flag, 0ul);
- __libcpp_mutex_unlock(&mut);
- __libcpp_condvar_broadcast(&cv);
- throw;
- }
- #endif // _LIBCPP_NO_EXCEPTIONS
- }
- else
- __libcpp_mutex_unlock(&mut);
- #endif // !_LIBCPP_HAS_NO_THREADS
- }
- _LIBCPP_END_NAMESPACE_STD
- #endif // !defined(_LIBCPP_SGX_CONFIG)
|