1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <__config>
10#include <__mutex/once_flag.h>
11#include <__utility/exception_guard.h>
12
13#if _LIBCPP_HAS_THREADS
14# include <__thread/support.h>
15#endif
16
17#include "include/atomic_support.h"
18
19_LIBCPP_BEGIN_NAMESPACE_STD
20_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
21
22// If dispatch_once_f ever handles C++ exceptions, and if one can get to it
23// without illegal macros (unexpected macros not beginning with _UpperCase or
24// __lowercase), and if it stops spinning waiting threads, then call_once should
25// call into dispatch_once_f instead of here. Relevant radar this code needs to
26// keep in sync with: 7741191.
27
28#if _LIBCPP_HAS_THREADS
29static constinit __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
30static constinit __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
31#endif
32
33void __call_once(volatile once_flag::_State_type& flag, void* arg, void (*func)(void*)) {
34#if !_LIBCPP_HAS_THREADS
35
36 if (flag == once_flag::_Unset) {
37 auto guard = std::__make_exception_guard([&flag] { flag = once_flag::_Unset; });
38 flag = once_flag::_Pending;
39 func(arg);
40 flag = once_flag::_Complete;
41 guard.__complete();
42 }
43
44#else // !_LIBCPP_HAS_THREADS
45
46 __libcpp_mutex_lock(m: &mut);
47 while (flag == once_flag::_Pending)
48 __libcpp_condvar_wait(cv: &cv, m: &mut);
49 if (flag == once_flag::_Unset) {
50 auto guard = std::__make_exception_guard(rollback: [&flag] {
51 __libcpp_mutex_lock(m: &mut);
52 __libcpp_relaxed_store(dest: &flag, val: once_flag::_Unset);
53 __libcpp_mutex_unlock(m: &mut);
54 __libcpp_condvar_broadcast(cv: &cv);
55 });
56
57 __libcpp_relaxed_store(dest: &flag, val: once_flag::_Pending);
58 __libcpp_mutex_unlock(m: &mut);
59 func(arg);
60 __libcpp_mutex_lock(m: &mut);
61 __libcpp_atomic_store(dest: &flag, val: once_flag::_Complete, order: _AO_Release);
62 __libcpp_mutex_unlock(m: &mut);
63 __libcpp_condvar_broadcast(cv: &cv);
64 guard.__complete();
65 } else {
66 __libcpp_mutex_unlock(m: &mut);
67 }
68
69#endif // !_LIBCPP_HAS_THREADS
70}
71
72_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
73_LIBCPP_END_NAMESPACE_STD
74