xref: /aosp_15_r20/external/llvm-libc/src/__support/threads/linux/callonce.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Linux callonce fastpath -------------------------------------------===//
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 #ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_LINUX_CALLONCE_H
9 #define LLVM_LIBC_SRC___SUPPORT_THREADS_LINUX_CALLONCE_H
10 
11 #include "src/__support/macros/config.h"
12 #include "src/__support/threads/linux/futex_utils.h"
13 
14 namespace LIBC_NAMESPACE_DECL {
15 using CallOnceFlag = Futex;
16 
17 namespace callonce_impl {
18 static constexpr FutexWordType NOT_CALLED = 0x0;
19 static constexpr FutexWordType START = 0x11;
20 static constexpr FutexWordType WAITING = 0x22;
21 static constexpr FutexWordType FINISH = 0x33;
22 
23 // Avoid cmpxchg operation if the function has already been called.
24 // The destination operand of cmpxchg may receive a write cycle without
25 // regard to the result of the comparison.
callonce_fastpath(CallOnceFlag * flag)26 LIBC_INLINE bool callonce_fastpath(CallOnceFlag *flag) {
27   return flag->load(cpp::MemoryOrder::RELAXED) == FINISH;
28 }
29 } // namespace callonce_impl
30 
31 } // namespace LIBC_NAMESPACE_DECL
32 #endif // LLVM_LIBC_SRC___SUPPORT_THREADS_LINUX_CALLONCE_H
33