123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- //===----------------------------------------------------------------------===//
- //
- // 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
- //
- //===----------------------------------------------------------------------===//
- #ifndef _LIBCPP___MUTEX_ONCE_FLAG_H
- #define _LIBCPP___MUTEX_ONCE_FLAG_H
- #include <__config>
- #include <__functional/invoke.h>
- #include <__memory/shared_ptr.h> // __libcpp_acquire_load
- #include <__tuple/tuple_indices.h>
- #include <__tuple/tuple_size.h>
- #include <__utility/forward.h>
- #include <__utility/move.h>
- #include <cstdint>
- #ifndef _LIBCPP_CXX03_LANG
- # include <tuple>
- #endif
- #if !defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_CXX03_LANG)
- #include <atomic>
- #endif
- #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
- # pragma GCC system_header
- #endif
- _LIBCPP_BEGIN_NAMESPACE_STD
- struct _LIBCPP_TEMPLATE_VIS once_flag;
- #ifndef _LIBCPP_CXX03_LANG
- template <class _Callable, class... _Args>
- _LIBCPP_HIDE_FROM_ABI void call_once(once_flag&, _Callable&&, _Args&&...);
- #else // _LIBCPP_CXX03_LANG
- template <class _Callable>
- _LIBCPP_HIDE_FROM_ABI void call_once(once_flag&, _Callable&);
- template <class _Callable>
- _LIBCPP_HIDE_FROM_ABI void call_once(once_flag&, const _Callable&);
- #endif // _LIBCPP_CXX03_LANG
- struct _LIBCPP_TEMPLATE_VIS once_flag {
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR once_flag() _NOEXCEPT : __state_(_Unset) {}
- once_flag(const once_flag&) = delete;
- once_flag& operator=(const once_flag&) = delete;
- #if defined(_LIBCPP_ABI_MICROSOFT)
- typedef uintptr_t _State_type;
- #else
- typedef unsigned long _State_type;
- #endif
- static const _State_type _Unset = 0;
- static const _State_type _Pending = 1;
- static const _State_type _Complete = ~_State_type(0);
- private:
- #if defined(_LIBCPP_ABI_MICROSOFT)
- atomic<_State_type> __state_;
- #else
- _State_type __state_;
- #endif
- #ifndef _LIBCPP_CXX03_LANG
- template <class _Callable, class... _Args>
- friend void call_once(once_flag&, _Callable&&, _Args&&...);
- #else // _LIBCPP_CXX03_LANG
- template <class _Callable>
- friend void call_once(once_flag&, _Callable&);
- template <class _Callable>
- friend void call_once(once_flag&, const _Callable&);
- #endif // _LIBCPP_CXX03_LANG
- };
- #ifndef _LIBCPP_CXX03_LANG
- template <class _Fp>
- class __call_once_param {
- _Fp& __f_;
- public:
- _LIBCPP_HIDE_FROM_ABI explicit __call_once_param(_Fp& __f) : __f_(__f) {}
- _LIBCPP_HIDE_FROM_ABI void operator()() {
- typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
- __execute(_Index());
- }
- private:
- template <size_t... _Indices>
- _LIBCPP_HIDE_FROM_ABI void __execute(__tuple_indices<_Indices...>) {
- _VSTD::__invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
- }
- };
- #else
- template <class _Fp>
- class __call_once_param {
- _Fp& __f_;
- public:
- _LIBCPP_HIDE_FROM_ABI explicit __call_once_param(_Fp& __f) : __f_(__f) {}
- _LIBCPP_HIDE_FROM_ABI void operator()() { __f_(); }
- };
- #endif
- template <class _Fp>
- void _LIBCPP_HIDE_FROM_ABI __call_once_proxy(void* __vp) {
- __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
- (*__p)();
- }
- #ifdef _LIBCPP_ABI_MICROSOFT
- _LIBCPP_EXPORTED_FROM_ABI void __call_once(volatile atomic<once_flag::_State_type>&, void*, void (*)(void*));
- #else
- _LIBCPP_EXPORTED_FROM_ABI void __call_once(volatile once_flag::_State_type&, void*, void (*)(void*));
- #endif
- #ifndef _LIBCPP_CXX03_LANG
- template <class _Callable, class... _Args>
- inline _LIBCPP_HIDE_FROM_ABI void call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args) {
- #if defined(_LIBCPP_ABI_MICROSOFT)
- if (__flag.__state_.load(memory_order_acquire) != ~once_flag::_State_type(0)) {
- #else
- if (__libcpp_acquire_load(&__flag.__state_) != once_flag::_Complete) {
- #endif
- typedef tuple<_Callable&&, _Args&&...> _Gp;
- _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
- __call_once_param<_Gp> __p(__f);
- std::__call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
- }
- }
- #else // _LIBCPP_CXX03_LANG
- template <class _Callable>
- inline _LIBCPP_HIDE_FROM_ABI void call_once(once_flag& __flag, _Callable& __func) {
- #if defined(_LIBCPP_ABI_MICROSOFT)
- if (__flag.__state_.load(memory_order_acquire) != ~once_flag::_State_type(0)) {
- #else
- if (__libcpp_acquire_load(&__flag.__state_) != once_flag::_Complete) {
- #endif
- __call_once_param<_Callable> __p(__func);
- std::__call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
- }
- }
- template <class _Callable>
- inline _LIBCPP_HIDE_FROM_ABI void call_once(once_flag& __flag, const _Callable& __func) {
- #if defined(_LIBCPP_ABI_MICROSOFT)
- if (__flag.__state_.load(memory_order_relaxed) != ~once_flag::_State_type(0)) {
- #else
- if (__libcpp_acquire_load(&__flag.__state_) != once_flag::_Complete) {
- #endif
- __call_once_param<const _Callable> __p(__func);
- std::__call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
- }
- }
- #endif // _LIBCPP_CXX03_LANG
- _LIBCPP_END_NAMESPACE_STD
- #endif // _LIBCPP___MUTEX_ONCE_FLAG_H
|