1//===-- Linux implementation of the callonce function ---------------------===//
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 "futex_word.h"
10
11#include "src/__support/CPP/atomic.h"
12#include "src/__support/CPP/limits.h" // INT_MAX
13#include "src/__support/OSUtil/syscall.h" // For syscall functions.
14#include "src/__support/threads/callonce.h"
15
16#include <linux/futex.h>
17#include <sys/syscall.h> // For syscall numbers.
18
19namespace LIBC_NAMESPACE {
20
21static constexpr FutexWordType NOT_CALLED = 0x0;
22static constexpr FutexWordType START = 0x11;
23static constexpr FutexWordType WAITING = 0x22;
24static constexpr FutexWordType FINISH = 0x33;
25
26int callonce(CallOnceFlag *flag, CallOnceCallback *func) {
27 auto *futex_word = reinterpret_cast<cpp::Atomic<FutexWordType> *>(flag);
28
29 FutexWordType not_called = NOT_CALLED;
30
31 // The call_once call can return only after the called function |func|
32 // returns. So, we use futexes to synchronize calls with the same flag value.
33 if (futex_word->compare_exchange_strong(expected&: not_called, desired: START)) {
34 func();
35 auto status = futex_word->exchange(desired: FINISH);
36 if (status == WAITING) {
37 LIBC_NAMESPACE::syscall_impl<long>(number: FUTEX_SYSCALL_ID, ts: &futex_word->val,
38 FUTEX_WAKE_PRIVATE,
39 INT_MAX, // Wake all waiters.
40 ts: 0, ts: 0, ts: 0);
41 }
42 return 0;
43 }
44
45 FutexWordType status = START;
46 if (futex_word->compare_exchange_strong(expected&: status, desired: WAITING) ||
47 status == WAITING) {
48 LIBC_NAMESPACE::syscall_impl<long>(
49 number: FUTEX_SYSCALL_ID, ts: &futex_word->val, FUTEX_WAIT_PRIVATE,
50 ts: WAITING, // Block only if status is still |WAITING|.
51 ts: 0, ts: 0, ts: 0);
52 }
53
54 return 0;
55}
56
57} // namespace LIBC_NAMESPACE
58

source code of libc/src/__support/threads/linux/callonce.cpp