aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/cxxsupp/libcxx/src/call_once.cpp
blob: 7f5aef1db5cadf1f879e3a5596ce09f4926044a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include <__mutex/once_flag.h>
#include <__utility/exception_guard.h>

#ifndef _LIBCPP_HAS_NO_THREADS
#  include <__threading_support>
#endif

#include "include/atomic_support.h"

_LIBCPP_BEGIN_NAMESPACE_STD

// 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 constinit __libcpp_mutex_t mut  = _LIBCPP_MUTEX_INITIALIZER;
static constinit __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
#endif

#ifdef _LIBCPP_ABI_MICROSOFT
void __call_once(volatile std::atomic<once_flag::_State_type>& flag, void* arg, void (*func)(void*))
#else
void __call_once(volatile once_flag::_State_type& flag, void* arg, void (*func)(void*))
#endif
{
#if defined(_LIBCPP_HAS_NO_THREADS)

  if (flag == once_flag::_Unset) {
    auto guard = std::__make_exception_guard([&flag] { flag = once_flag::_Unset; });
    flag       = once_flag::_Pending;
    func(arg);
    flag = once_flag::_Complete;
    guard.__complete();
  }

#else // !_LIBCPP_HAS_NO_THREADS

  __libcpp_mutex_lock(&mut);
  while (flag == once_flag::_Pending)
    __libcpp_condvar_wait(&cv, &mut);
  if (flag == once_flag::_Unset) {
    auto guard = std::__make_exception_guard([&flag] {
      __libcpp_mutex_lock(&mut);
#  ifdef _LIBCPP_ABI_MICROSOFT
      flag.store(once_flag::_Unset);
#  else
      __libcpp_relaxed_store(&flag, once_flag::_Unset);
#  endif
      __libcpp_mutex_unlock(&mut);
      __libcpp_condvar_broadcast(&cv);
    });

#  ifdef _LIBCPP_ABI_MICROSOFT
    flag.store(once_flag::_Pending, memory_order_relaxed);
#  else
    __libcpp_relaxed_store(&flag, once_flag::_Pending);
#  endif
    __libcpp_mutex_unlock(&mut);
    func(arg);
    __libcpp_mutex_lock(&mut);
#  ifdef _LIBCPP_ABI_MICROSOFT
    flag.store(once_flag::_Complete, memory_order_release);
#  else
    __libcpp_atomic_store(&flag, once_flag::_Complete, _AO_Release);
#  endif
    __libcpp_mutex_unlock(&mut);
    __libcpp_condvar_broadcast(&cv);
    guard.__complete();
  } else {
    __libcpp_mutex_unlock(&mut);
  }

#endif // !_LIBCPP_HAS_NO_THREADS
}

_LIBCPP_END_NAMESPACE_STD