2010-05-12 03:42:16 +08:00
|
|
|
// -*- C++ -*-
|
2021-11-18 05:25:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-05-12 03:42:16 +08:00
|
|
|
//
|
2019-01-19 18:56:40 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-05-12 03:42:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_MUTEX
|
|
|
|
#define _LIBCPP_MUTEX
|
|
|
|
|
|
|
|
/*
|
|
|
|
mutex synopsis
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
|
|
|
|
class mutex
|
|
|
|
{
|
|
|
|
public:
|
2012-07-22 00:13:09 +08:00
|
|
|
constexpr mutex() noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
~mutex();
|
|
|
|
|
|
|
|
mutex(const mutex&) = delete;
|
|
|
|
mutex& operator=(const mutex&) = delete;
|
|
|
|
|
|
|
|
void lock();
|
|
|
|
bool try_lock();
|
|
|
|
void unlock();
|
|
|
|
|
|
|
|
typedef pthread_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();
|
2012-07-22 00:13:09 +08:00
|
|
|
bool try_lock() noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
void unlock();
|
|
|
|
|
|
|
|
typedef pthread_mutex_t* native_handle_type;
|
|
|
|
native_handle_type native_handle();
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
2012-07-22 00:13:09 +08:00
|
|
|
bool try_lock() noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2019-09-26 22:51:10 +08:00
|
|
|
struct defer_lock_t { explicit defer_lock_t() = default; };
|
|
|
|
struct try_to_lock_t { explicit try_to_lock_t() = default; };
|
|
|
|
struct adopt_lock_t { explicit adopt_lock_t() = default; };
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2018-01-03 01:17:01 +08:00
|
|
|
inline constexpr defer_lock_t defer_lock{};
|
|
|
|
inline constexpr try_to_lock_t try_to_lock{};
|
|
|
|
inline constexpr adopt_lock_t adopt_lock{};
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2017-03-24 11:40:36 +08:00
|
|
|
template <class... MutexTypes>
|
|
|
|
class scoped_lock // C++17
|
2016-06-14 11:48:09 +08:00
|
|
|
{
|
|
|
|
public:
|
2021-10-27 01:45:52 +08:00
|
|
|
using mutex_type = Mutex; // Only if sizeof...(MutexTypes) == 1
|
2017-03-24 11:40:36 +08:00
|
|
|
|
|
|
|
explicit scoped_lock(MutexTypes&... m);
|
2017-07-28 01:44:03 +08:00
|
|
|
scoped_lock(adopt_lock_t, MutexTypes&... m);
|
2017-03-24 11:40:36 +08:00
|
|
|
~scoped_lock();
|
|
|
|
scoped_lock(scoped_lock const&) = delete;
|
|
|
|
scoped_lock& operator=(scoped_lock const&) = delete;
|
2016-06-14 11:48:09 +08:00
|
|
|
private:
|
|
|
|
tuple<MutexTypes&...> pm; // exposition only
|
|
|
|
};
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
template <class Mutex>
|
|
|
|
class unique_lock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef Mutex mutex_type;
|
2012-07-22 00:13:09 +08:00
|
|
|
unique_lock() noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
explicit unique_lock(mutex_type& m);
|
2012-07-22 00:13:09 +08:00
|
|
|
unique_lock(mutex_type& m, defer_lock_t) noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
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;
|
|
|
|
|
2012-07-22 00:13:09 +08:00
|
|
|
unique_lock(unique_lock&& u) noexcept;
|
|
|
|
unique_lock& operator=(unique_lock&& u) noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2012-07-22 00:13:09 +08:00
|
|
|
void swap(unique_lock& u) noexcept;
|
|
|
|
mutex_type* release() noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2012-07-22 00:13:09 +08:00
|
|
|
bool owns_lock() const noexcept;
|
|
|
|
explicit operator bool () const noexcept;
|
|
|
|
mutex_type* mutex() const noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class Mutex>
|
2012-07-22 00:13:09 +08:00
|
|
|
void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2012-07-22 00:13:09 +08:00
|
|
|
constexpr once_flag() noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2022-03-26 00:55:36 +08:00
|
|
|
#include <__assert> // all public C++ headers provide the assertion handler
|
2010-05-12 03:42:16 +08:00
|
|
|
#include <__config>
|
|
|
|
#include <__mutex_base>
|
2021-05-19 23:57:04 +08:00
|
|
|
#include <__threading_support>
|
2021-06-05 10:47:47 +08:00
|
|
|
#include <__utility/forward.h>
|
2019-03-21 06:55:03 +08:00
|
|
|
#include <cstdint>
|
2015-07-07 08:27:16 +08:00
|
|
|
#include <memory>
|
2017-04-19 07:05:08 +08:00
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
2021-05-19 23:57:04 +08:00
|
|
|
# include <tuple>
|
2011-05-17 03:05:11 +08:00
|
|
|
#endif
|
2018-09-13 03:41:40 +08:00
|
|
|
#include <version>
|
2010-05-12 03:42:16 +08:00
|
|
|
|
[libc++] Re-add transitive includes that had been removed since LLVM 14
This commit re-adds transitive includes that had been removed by
4cd04d1687f1, c36870c8e79c, a83f4b9cda57, 1458458b558d, 2e2f3158c604,
and 489637e66dd3. This should cover almost all the includes that had
been removed since LLVM 14 and that would contribute to breaking user
code when releasing LLVM 15.
It is possible to disable the inclusion of these headers by defining
_LIBCPP_REMOVE_TRANSITIVE_INCLUDES. The intent is that vendors will
enable that macro and start fixing downstream issues immediately. We
can then remove the macro (and the transitive includes) by default in
a future release. That way, we will break users only once by removing
transitive includes in bulk instead of doing it bit by bit a every
release, which is more disruptive for users.
Note 1: The set of headers to re-add was found by re-generating the
transitive include test on a checkout of release/14.x, which
provided the list of all transitive includes we used to provide.
Note 2: Several includes of <vector>, <optional>, <array> and <unordered_map>
have been added in this commit. These transitive inclusions were
added when we implemented boyer_moore_searcher in <functional>.
Note 3: This is a best effort patch to try and resolve downstream breakage
caused since branching LLVM 14. I wasn't able to perfectly mirror
transitive includes in LLVM 14 for a few headers, so I added a
release note explaining it. To summarize, adding boyer_moore_searcher
created a bunch of circular dependencies, so we have to break
backwards compatibility in a few cases.
Differential Revision: https://reviews.llvm.org/D128661
2022-06-28 03:53:41 +08:00
|
|
|
#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
|
|
|
|
# include <functional>
|
|
|
|
#endif
|
|
|
|
|
2011-10-18 04:05:10 +08:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-02 09:16:40 +08:00
|
|
|
# pragma GCC system_header
|
2011-10-18 04:05:10 +08:00
|
|
|
#endif
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2017-06-01 06:07:49 +08:00
|
|
|
_LIBCPP_PUSH_MACROS
|
|
|
|
#include <__undef_macros>
|
|
|
|
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2014-09-06 03:45:05 +08:00
|
|
|
#ifndef _LIBCPP_HAS_NO_THREADS
|
|
|
|
|
2013-03-07 07:30:19 +08:00
|
|
|
class _LIBCPP_TYPE_VIS recursive_mutex
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2017-01-06 01:54:45 +08:00
|
|
|
__libcpp_recursive_mutex_t __m_;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
public:
|
2021-12-08 17:57:12 +08:00
|
|
|
recursive_mutex();
|
|
|
|
~recursive_mutex();
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2021-12-08 17:57:12 +08:00
|
|
|
recursive_mutex(const recursive_mutex&) = delete;
|
|
|
|
recursive_mutex& operator=(const recursive_mutex&) = delete;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
void lock();
|
2012-07-22 00:13:09 +08:00
|
|
|
bool try_lock() _NOEXCEPT;
|
|
|
|
void unlock() _NOEXCEPT;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2017-01-06 01:54:45 +08:00
|
|
|
typedef __libcpp_recursive_mutex_t* native_handle_type;
|
|
|
|
|
2010-09-23 02:02:38 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
native_handle_type native_handle() {return &__m_;}
|
|
|
|
};
|
|
|
|
|
2013-03-07 07:30:19 +08:00
|
|
|
class _LIBCPP_TYPE_VIS timed_mutex
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
mutex __m_;
|
|
|
|
condition_variable __cv_;
|
|
|
|
bool __locked_;
|
|
|
|
public:
|
|
|
|
timed_mutex();
|
|
|
|
~timed_mutex();
|
|
|
|
|
2021-12-08 17:57:12 +08:00
|
|
|
timed_mutex(const timed_mutex&) = delete;
|
|
|
|
timed_mutex& operator=(const timed_mutex&) = delete;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
void lock();
|
2012-07-22 00:13:09 +08:00
|
|
|
bool try_lock() _NOEXCEPT;
|
2010-05-12 03:42:16 +08:00
|
|
|
template <class _Rep, class _Period>
|
2010-09-23 02:02:38 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
|
2010-11-21 03:16:30 +08:00
|
|
|
{return try_lock_until(chrono::steady_clock::now() + __d);}
|
2010-05-12 03:42:16 +08:00
|
|
|
template <class _Clock, class _Duration>
|
2017-03-02 11:22:18 +08:00
|
|
|
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
|
2010-05-12 03:42:16 +08:00
|
|
|
bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
|
2012-07-22 00:13:09 +08:00
|
|
|
void unlock() _NOEXCEPT;
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
bool
|
|
|
|
timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
|
|
|
|
{
|
|
|
|
using namespace chrono;
|
|
|
|
unique_lock<mutex> __lk(__m_);
|
|
|
|
bool no_timeout = _Clock::now() < __t;
|
|
|
|
while (no_timeout && __locked_)
|
|
|
|
no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
|
|
|
|
if (!__locked_)
|
|
|
|
{
|
|
|
|
__locked_ = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-07 07:30:19 +08:00
|
|
|
class _LIBCPP_TYPE_VIS recursive_timed_mutex
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
mutex __m_;
|
|
|
|
condition_variable __cv_;
|
|
|
|
size_t __count_;
|
2019-08-15 00:21:27 +08:00
|
|
|
__thread_id __id_;
|
2010-05-12 03:42:16 +08:00
|
|
|
public:
|
2021-12-08 17:57:12 +08:00
|
|
|
recursive_timed_mutex();
|
|
|
|
~recursive_timed_mutex();
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2021-12-08 17:57:12 +08:00
|
|
|
recursive_timed_mutex(const recursive_timed_mutex&) = delete;
|
|
|
|
recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
void lock();
|
2012-07-22 00:13:09 +08:00
|
|
|
bool try_lock() _NOEXCEPT;
|
2010-05-12 03:42:16 +08:00
|
|
|
template <class _Rep, class _Period>
|
2010-09-23 02:02:38 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
|
2010-11-21 03:16:30 +08:00
|
|
|
{return try_lock_until(chrono::steady_clock::now() + __d);}
|
2010-05-12 03:42:16 +08:00
|
|
|
template <class _Clock, class _Duration>
|
2017-03-02 11:22:18 +08:00
|
|
|
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
|
2010-05-12 03:42:16 +08:00
|
|
|
bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
|
2012-07-22 00:13:09 +08:00
|
|
|
void unlock() _NOEXCEPT;
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
bool
|
|
|
|
recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
|
|
|
|
{
|
|
|
|
using namespace chrono;
|
2019-08-15 00:21:27 +08:00
|
|
|
__thread_id __id = this_thread::get_id();
|
2010-05-12 03:42:16 +08:00
|
|
|
unique_lock<mutex> lk(__m_);
|
2019-08-15 00:21:27 +08:00
|
|
|
if (__id == __id_)
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
if (__count_ == numeric_limits<size_t>::max())
|
|
|
|
return false;
|
|
|
|
++__count_;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool no_timeout = _Clock::now() < __t;
|
|
|
|
while (no_timeout && __count_ != 0)
|
|
|
|
no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
|
|
|
|
if (__count_ == 0)
|
|
|
|
{
|
|
|
|
__count_ = 1;
|
|
|
|
__id_ = __id;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-19 07:05:08 +08:00
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_CXX03_LANG
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-05-06 22:06:29 +08:00
|
|
|
__libcpp_thread_yield();
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
unique_lock<_L1> __u1(__l1);
|
|
|
|
if (__l0.try_lock())
|
|
|
|
{
|
|
|
|
__u1.release();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-06 22:06:29 +08:00
|
|
|
__libcpp_thread_yield();
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 07:05:08 +08:00
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2011-01-13 06:56:59 +08:00
|
|
|
template <class _L0, class _L1, class _L2, class ..._L3>
|
2010-05-12 03:42:16 +08:00
|
|
|
void
|
2011-01-13 06:56:59 +08:00
|
|
|
__lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
switch (__i)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
unique_lock<_L0> __u0(__l0);
|
2011-01-13 06:56:59 +08:00
|
|
|
__i = try_lock(__l1, __l2, __l3...);
|
2010-05-12 03:42:16 +08:00
|
|
|
if (__i == -1)
|
|
|
|
{
|
|
|
|
__u0.release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++__i;
|
2016-05-06 22:06:29 +08:00
|
|
|
__libcpp_thread_yield();
|
2010-05-12 03:42:16 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
unique_lock<_L1> __u1(__l1);
|
2011-01-13 06:56:59 +08:00
|
|
|
__i = try_lock(__l2, __l3..., __l0);
|
2010-05-12 03:42:16 +08:00
|
|
|
if (__i == -1)
|
|
|
|
{
|
|
|
|
__u1.release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-01-13 06:56:59 +08:00
|
|
|
if (__i == sizeof...(_L3) + 1)
|
2010-05-12 03:42:16 +08:00
|
|
|
__i = 0;
|
|
|
|
else
|
|
|
|
__i += 2;
|
2016-05-06 22:06:29 +08:00
|
|
|
__libcpp_thread_yield();
|
2010-05-12 03:42:16 +08:00
|
|
|
break;
|
|
|
|
default:
|
2011-01-13 06:56:59 +08:00
|
|
|
__lock_first(__i - 2, __l2, __l3..., __l0, __l1);
|
2010-05-12 03:42:16 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-13 06:56:59 +08:00
|
|
|
template <class _L0, class _L1, class _L2, class ..._L3>
|
2010-09-23 02:02:38 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
void
|
2011-01-13 06:56:59 +08:00
|
|
|
lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2011-01-13 06:56:59 +08:00
|
|
|
__lock_first(0, __l0, __l1, __l2, __l3...);
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
2016-06-14 11:48:09 +08:00
|
|
|
template <class _L0>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void __unlock(_L0& __l0) {
|
|
|
|
__l0.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _L0, class _L1>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void __unlock(_L0& __l0, _L1& __l1) {
|
|
|
|
__l0.unlock();
|
|
|
|
__l1.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _L0, class _L1, class _L2, class ..._L3>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
|
|
|
|
__l0.unlock();
|
|
|
|
__l1.unlock();
|
|
|
|
_VSTD::__unlock(__l2, __l3...);
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_CXX03_LANG
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2017-03-24 13:19:15 +08:00
|
|
|
#if _LIBCPP_STD_VER > 14
|
|
|
|
template <class ..._Mutexes>
|
|
|
|
class _LIBCPP_TEMPLATE_VIS scoped_lock;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class _LIBCPP_TEMPLATE_VIS scoped_lock<> {
|
|
|
|
public:
|
|
|
|
explicit scoped_lock() {}
|
|
|
|
~scoped_lock() = default;
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
explicit scoped_lock(adopt_lock_t) {}
|
|
|
|
|
|
|
|
scoped_lock(scoped_lock const&) = delete;
|
|
|
|
scoped_lock& operator=(scoped_lock const&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Mutex>
|
2018-10-10 07:42:29 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) scoped_lock<_Mutex> {
|
2017-03-24 13:19:15 +08:00
|
|
|
public:
|
|
|
|
typedef _Mutex mutex_type;
|
|
|
|
private:
|
|
|
|
mutex_type& __m_;
|
|
|
|
public:
|
|
|
|
explicit scoped_lock(mutex_type & __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
|
|
|
|
: __m_(__m) {__m_.lock();}
|
|
|
|
|
|
|
|
~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-07-28 01:44:03 +08:00
|
|
|
explicit scoped_lock(adopt_lock_t, mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
|
2017-03-24 13:19:15 +08:00
|
|
|
: __m_(__m) {}
|
|
|
|
|
|
|
|
scoped_lock(scoped_lock const&) = delete;
|
|
|
|
scoped_lock& operator=(scoped_lock const&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ..._MArgs>
|
|
|
|
class _LIBCPP_TEMPLATE_VIS scoped_lock
|
|
|
|
{
|
|
|
|
static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
|
|
|
|
typedef tuple<_MArgs&...> _MutexTuple;
|
|
|
|
|
|
|
|
public:
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
explicit scoped_lock(_MArgs&... __margs)
|
|
|
|
: __t_(__margs...)
|
|
|
|
{
|
|
|
|
_VSTD::lock(__margs...);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-07-28 01:44:03 +08:00
|
|
|
scoped_lock(adopt_lock_t, _MArgs&... __margs)
|
2017-03-24 13:19:15 +08:00
|
|
|
: __t_(__margs...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
~scoped_lock() {
|
|
|
|
typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
|
|
|
|
__unlock_unpack(_Indices{}, __t_);
|
|
|
|
}
|
|
|
|
|
|
|
|
scoped_lock(scoped_lock const&) = delete;
|
|
|
|
scoped_lock& operator=(scoped_lock const&) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <size_t ..._Indx>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
|
|
|
|
_VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
_MutexTuple __t_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _LIBCPP_STD_VER > 14
|
2014-09-06 03:45:05 +08:00
|
|
|
#endif // !_LIBCPP_HAS_NO_THREADS
|
|
|
|
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS once_flag;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2017-04-19 07:05:08 +08:00
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
template<class _Callable, class... _Args>
|
2012-09-14 08:39:16 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
void call_once(once_flag&, _Callable&&, _Args&&...);
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2017-04-19 07:05:08 +08:00
|
|
|
#else // _LIBCPP_CXX03_LANG
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
template<class _Callable>
|
2012-09-14 08:39:16 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2015-06-13 10:23:00 +08:00
|
|
|
void call_once(once_flag&, _Callable&);
|
|
|
|
|
|
|
|
template<class _Callable>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
void call_once(once_flag&, const _Callable&);
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_CXX03_LANG
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS once_flag
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2010-09-23 02:02:38 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2012-07-22 00:13:09 +08:00
|
|
|
_LIBCPP_CONSTEXPR
|
|
|
|
once_flag() _NOEXCEPT : __state_(0) {}
|
2021-12-08 17:57:12 +08:00
|
|
|
once_flag(const once_flag&) = delete;
|
|
|
|
once_flag& operator=(const once_flag&) = delete;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2019-03-21 06:55:03 +08:00
|
|
|
#if defined(_LIBCPP_ABI_MICROSOFT)
|
|
|
|
typedef uintptr_t _State_type;
|
|
|
|
#else
|
|
|
|
typedef unsigned long _State_type;
|
|
|
|
#endif
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
private:
|
2019-03-21 06:55:03 +08:00
|
|
|
_State_type __state_;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2017-04-19 07:05:08 +08:00
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
2010-05-12 03:42:16 +08:00
|
|
|
template<class _Callable, class... _Args>
|
|
|
|
friend
|
|
|
|
void call_once(once_flag&, _Callable&&, _Args&&...);
|
2017-04-19 07:05:08 +08:00
|
|
|
#else // _LIBCPP_CXX03_LANG
|
2010-05-12 03:42:16 +08:00
|
|
|
template<class _Callable>
|
|
|
|
friend
|
2015-06-13 10:23:00 +08:00
|
|
|
void call_once(once_flag&, _Callable&);
|
|
|
|
|
|
|
|
template<class _Callable>
|
|
|
|
friend
|
|
|
|
void call_once(once_flag&, const _Callable&);
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_CXX03_LANG
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
2017-04-19 07:05:08 +08:00
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
2011-05-17 03:05:11 +08:00
|
|
|
|
2011-11-30 02:15:50 +08:00
|
|
|
template <class _Fp>
|
2011-05-17 03:05:11 +08:00
|
|
|
class __call_once_param
|
|
|
|
{
|
2015-06-13 10:23:00 +08:00
|
|
|
_Fp& __f_;
|
2011-05-17 03:05:11 +08:00
|
|
|
public:
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2015-06-13 10:23:00 +08:00
|
|
|
explicit __call_once_param(_Fp& __f) : __f_(__f) {}
|
2011-05-17 03:05:11 +08:00
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
void operator()()
|
|
|
|
{
|
2011-11-30 02:15:50 +08:00
|
|
|
typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
|
2011-05-17 03:05:11 +08:00
|
|
|
__execute(_Index());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <size_t ..._Indices>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
void __execute(__tuple_indices<_Indices...>)
|
|
|
|
{
|
2020-12-16 08:32:29 +08:00
|
|
|
_VSTD::__invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
|
2011-05-17 03:05:11 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2011-11-30 02:15:50 +08:00
|
|
|
template <class _Fp>
|
2010-05-12 03:42:16 +08:00
|
|
|
class __call_once_param
|
|
|
|
{
|
2015-06-13 10:23:00 +08:00
|
|
|
_Fp& __f_;
|
2010-05-12 03:42:16 +08:00
|
|
|
public:
|
2010-09-23 02:02:38 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2015-06-13 10:23:00 +08:00
|
|
|
explicit __call_once_param(_Fp& __f) : __f_(__f) {}
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2010-09-23 02:02:38 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
void operator()()
|
|
|
|
{
|
|
|
|
__f_();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-05-17 03:05:11 +08:00
|
|
|
#endif
|
|
|
|
|
2011-11-30 02:15:50 +08:00
|
|
|
template <class _Fp>
|
2019-11-11 23:21:57 +08:00
|
|
|
void _LIBCPP_INLINE_VISIBILITY
|
2010-05-12 03:42:16 +08:00
|
|
|
__call_once_proxy(void* __vp)
|
|
|
|
{
|
2011-11-30 02:15:50 +08:00
|
|
|
__call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
|
2010-05-12 03:42:16 +08:00
|
|
|
(*__p)();
|
|
|
|
}
|
|
|
|
|
2019-03-21 06:55:03 +08:00
|
|
|
_LIBCPP_FUNC_VIS void __call_once(volatile once_flag::_State_type&, void*,
|
|
|
|
void (*)(void*));
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2017-04-19 07:05:08 +08:00
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
template<class _Callable, class... _Args>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void
|
|
|
|
call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
|
|
|
|
{
|
2019-03-21 06:55:03 +08:00
|
|
|
if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2015-06-13 10:23:00 +08:00
|
|
|
typedef tuple<_Callable&&, _Args&&...> _Gp;
|
|
|
|
_Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
|
|
|
|
__call_once_param<_Gp> __p(__f);
|
2011-11-30 02:15:50 +08:00
|
|
|
__call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
|
2010-05-12 03:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 07:05:08 +08:00
|
|
|
#else // _LIBCPP_CXX03_LANG
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
template<class _Callable>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void
|
2015-06-13 10:23:00 +08:00
|
|
|
call_once(once_flag& __flag, _Callable& __func)
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2019-03-21 06:55:03 +08:00
|
|
|
if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
__call_once_param<_Callable> __p(__func);
|
|
|
|
__call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-13 10:23:00 +08:00
|
|
|
template<class _Callable>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void
|
|
|
|
call_once(once_flag& __flag, const _Callable& __func)
|
|
|
|
{
|
2019-03-21 06:55:03 +08:00
|
|
|
if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
|
2015-06-13 10:23:00 +08:00
|
|
|
{
|
|
|
|
__call_once_param<const _Callable> __p(__func);
|
|
|
|
__call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_CXX03_LANG
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
2017-06-01 06:07:49 +08:00
|
|
|
_LIBCPP_POP_MACROS
|
|
|
|
|
2021-04-21 00:03:32 +08:00
|
|
|
#endif // _LIBCPP_MUTEX
|