xref: /aosp_15_r20/external/abseil-cpp/absl/base/internal/spinlock_linux.inc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker// Copyright 2018 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker//
3*9356374aSAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker//
7*9356374aSAndroid Build Coastguard Worker//      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker//
9*9356374aSAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker// limitations under the License.
14*9356374aSAndroid Build Coastguard Worker//
15*9356374aSAndroid Build Coastguard Worker// This file is a Linux-specific part of spinlock_wait.cc
16*9356374aSAndroid Build Coastguard Worker
17*9356374aSAndroid Build Coastguard Worker#include <linux/futex.h>
18*9356374aSAndroid Build Coastguard Worker#include <sys/syscall.h>
19*9356374aSAndroid Build Coastguard Worker#include <unistd.h>
20*9356374aSAndroid Build Coastguard Worker
21*9356374aSAndroid Build Coastguard Worker#include <atomic>
22*9356374aSAndroid Build Coastguard Worker#include <climits>
23*9356374aSAndroid Build Coastguard Worker#include <cstdint>
24*9356374aSAndroid Build Coastguard Worker#include <ctime>
25*9356374aSAndroid Build Coastguard Worker
26*9356374aSAndroid Build Coastguard Worker#include "absl/base/attributes.h"
27*9356374aSAndroid Build Coastguard Worker#include "absl/base/internal/errno_saver.h"
28*9356374aSAndroid Build Coastguard Worker
29*9356374aSAndroid Build Coastguard Worker// The SpinLock lockword is `std::atomic<uint32_t>`. Here we assert that
30*9356374aSAndroid Build Coastguard Worker// `std::atomic<uint32_t>` is bitwise equivalent of the `int` expected
31*9356374aSAndroid Build Coastguard Worker// by SYS_futex. We also assume that reads/writes done to the lockword
32*9356374aSAndroid Build Coastguard Worker// by SYS_futex have rational semantics with regard to the
33*9356374aSAndroid Build Coastguard Worker// std::atomic<> API. C++ provides no guarantees of these assumptions,
34*9356374aSAndroid Build Coastguard Worker// but they are believed to hold in practice.
35*9356374aSAndroid Build Coastguard Workerstatic_assert(sizeof(std::atomic<uint32_t>) == sizeof(int),
36*9356374aSAndroid Build Coastguard Worker              "SpinLock lockword has the wrong size for a futex");
37*9356374aSAndroid Build Coastguard Worker
38*9356374aSAndroid Build Coastguard Worker// Some Android headers are missing these definitions even though they
39*9356374aSAndroid Build Coastguard Worker// support these futex operations.
40*9356374aSAndroid Build Coastguard Worker#ifdef __BIONIC__
41*9356374aSAndroid Build Coastguard Worker#ifndef SYS_futex
42*9356374aSAndroid Build Coastguard Worker#define SYS_futex __NR_futex
43*9356374aSAndroid Build Coastguard Worker#endif
44*9356374aSAndroid Build Coastguard Worker#ifndef FUTEX_PRIVATE_FLAG
45*9356374aSAndroid Build Coastguard Worker#define FUTEX_PRIVATE_FLAG 128
46*9356374aSAndroid Build Coastguard Worker#endif
47*9356374aSAndroid Build Coastguard Worker#endif
48*9356374aSAndroid Build Coastguard Worker
49*9356374aSAndroid Build Coastguard Worker#if defined(__NR_futex_time64) && !defined(SYS_futex_time64)
50*9356374aSAndroid Build Coastguard Worker#define SYS_futex_time64 __NR_futex_time64
51*9356374aSAndroid Build Coastguard Worker#endif
52*9356374aSAndroid Build Coastguard Worker
53*9356374aSAndroid Build Coastguard Worker#if defined(SYS_futex_time64) && !defined(SYS_futex)
54*9356374aSAndroid Build Coastguard Worker#define SYS_futex SYS_futex_time64
55*9356374aSAndroid Build Coastguard Worker#endif
56*9356374aSAndroid Build Coastguard Worker
57*9356374aSAndroid Build Coastguard Workerextern "C" {
58*9356374aSAndroid Build Coastguard Worker
59*9356374aSAndroid Build Coastguard WorkerABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)(
60*9356374aSAndroid Build Coastguard Worker    std::atomic<uint32_t> *w, uint32_t value, int,
61*9356374aSAndroid Build Coastguard Worker    absl::base_internal::SchedulingMode) {
62*9356374aSAndroid Build Coastguard Worker  absl::base_internal::ErrnoSaver errno_saver;
63*9356374aSAndroid Build Coastguard Worker  syscall(SYS_futex, w, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, value, nullptr);
64*9356374aSAndroid Build Coastguard Worker}
65*9356374aSAndroid Build Coastguard Worker
66*9356374aSAndroid Build Coastguard WorkerABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(
67*9356374aSAndroid Build Coastguard Worker    std::atomic<uint32_t> *w, bool all) {
68*9356374aSAndroid Build Coastguard Worker  syscall(SYS_futex, w, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, all ? INT_MAX : 1, 0);
69*9356374aSAndroid Build Coastguard Worker}
70*9356374aSAndroid Build Coastguard Worker
71*9356374aSAndroid Build Coastguard Worker}  // extern "C"
72