1 //===-- Linux implementation of the call_once 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 "src/threads/call_once.h"
10 #include "config/linux/syscall.h" // For syscall functions.
11 #include "include/sys/syscall.h"  // For syscall numbers.
12 #include "include/threads.h"      // For call_once related type definition.
13 #include "src/__support/common.h"
14 #include "src/threads/linux/Futex.h"
15 
16 #include <limits.h>
17 #include <linux/futex.h>
18 #include <stdatomic.h>
19 
20 namespace __llvm_libc {
21 
22 static constexpr unsigned START = 0x11;
23 static constexpr unsigned WAITING = 0x22;
24 static constexpr unsigned FINISH = 0x33;
25 
26 LLVM_LIBC_FUNCTION(void, call_once,
27                    (once_flag * flag, __call_once_func_t func)) {
28   FutexWord *futex_word = reinterpret_cast<FutexWord *>(flag);
29   unsigned int not_called = ONCE_FLAG_INIT;
30 
31   // The C standard wording says:
32   //
33   //     The completion of the function func synchronizes with all
34   //     previous or subsequent calls to call_once with the same
35   //     flag variable.
36   //
37   // What this means is that, the call_once call can return only after
38   // the called function |func| returns. So, we use futexes to synchronize
39   // calls with the same flag value.
40   if (::atomic_compare_exchange_strong(futex_word, &not_called, START)) {
41     func();
42     auto status = ::atomic_exchange(futex_word, FINISH);
43     if (status == WAITING) {
44       __llvm_libc::syscall(SYS_futex, futex_word, FUTEX_WAKE_PRIVATE,
45                            INT_MAX, // Wake all waiters.
46                            0, 0, 0);
47     }
48     return;
49   }
50 
51   unsigned int status = START;
52   if (::atomic_compare_exchange_strong(futex_word, &status, WAITING) ||
53       status == WAITING) {
54     __llvm_libc::syscall(SYS_futex, futex_word, FUTEX_WAIT_PRIVATE,
55                          WAITING, // Block only if status is still |WAITING|.
56                          0, 0, 0);
57   }
58 }
59 
60 } // namespace __llvm_libc
61