]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/atomic/detail/futex.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / atomic / detail / futex.hpp
CommitLineData
20effc67
TL
1/*
2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
5 *
6 * Copyright (c) 2020 Andrey Semashev
7 */
8/*!
9 * \file atomic/detail/futex.hpp
10 *
11 * This header defines wrappers around futex syscall.
12 *
13 * http://man7.org/linux/man-pages/man2/futex.2.html
14 * https://man.openbsd.org/futex
15 */
16
17#ifndef BOOST_ATOMIC_DETAIL_FUTEX_HPP_INCLUDED_
18#define BOOST_ATOMIC_DETAIL_FUTEX_HPP_INCLUDED_
19
20#include <boost/atomic/detail/config.hpp>
21
22#ifdef BOOST_HAS_PRAGMA_ONCE
23#pragma once
24#endif
25
26#if defined(__linux__) || defined(__OpenBSD__) || defined(__NETBSD__) || defined(__NetBSD__)
27
28#include <sys/syscall.h>
29
20effc67
TL
30#if defined(SYS_futex)
31#define BOOST_ATOMIC_DETAIL_SYS_FUTEX SYS_futex
1e59de90
TL
32#elif defined(SYS_futex_time64)
33// On some 32-bit targets (e.g. riscv32) SYS_futex is not defined and instead SYS_futex_time64 is implemented,
34// which is equivalent to SYS_futex but uses 64-bit time_t.
35#define BOOST_ATOMIC_DETAIL_SYS_FUTEX SYS_futex_time64
20effc67 36#elif defined(__NR_futex)
1e59de90 37// Some Android NDKs (Google NDK and older Crystax.NET NDK versions) don't define SYS_futex.
20effc67
TL
38#define BOOST_ATOMIC_DETAIL_SYS_FUTEX __NR_futex
39#elif defined(SYS___futex)
40// NetBSD defines SYS___futex, which has slightly different parameters. Basically, it has decoupled timeout and val2 parameters:
41// int __futex(int *addr1, int op, int val1, const struct timespec *timeout, int *addr2, int val2, int val3);
42// https://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/sys/sys/syscall.h
43// http://bxr.su/NetBSD/sys/kern/sys_futex.c
44#define BOOST_ATOMIC_DETAIL_SYS_FUTEX SYS___futex
45#define BOOST_ATOMIC_DETAIL_NETBSD_FUTEX
46#endif
47
48#if defined(BOOST_ATOMIC_DETAIL_SYS_FUTEX)
49
50#include <cstddef>
51#if defined(__linux__)
52#include <linux/futex.h>
53#else
54#include <sys/futex.h>
55#endif
56#include <boost/atomic/detail/intptr.hpp>
57#include <boost/atomic/detail/header.hpp>
58
59#define BOOST_ATOMIC_DETAIL_HAS_FUTEX
60
61#if defined(FUTEX_PRIVATE_FLAG)
62#define BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG FUTEX_PRIVATE_FLAG
1e59de90
TL
63#elif defined(__ANDROID__)
64// On Android, futex.h is lacking many definitions, but the actual Linux kernel supports the API in full.
65#define BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG 128
20effc67
TL
66#else
67#define BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG 0
68#endif
69
70namespace boost {
71namespace atomics {
72namespace detail {
73
74//! Invokes an operation on the futex
75BOOST_FORCEINLINE int futex_invoke(void* addr1, int op, unsigned int val1, const void* timeout = NULL, void* addr2 = NULL, unsigned int val3 = 0) BOOST_NOEXCEPT
76{
77#if !defined(BOOST_ATOMIC_DETAIL_NETBSD_FUTEX)
78 return ::syscall(BOOST_ATOMIC_DETAIL_SYS_FUTEX, addr1, op, val1, timeout, addr2, val3);
79#else
80 // Pass 0 in val2.
81 return ::syscall(BOOST_ATOMIC_DETAIL_SYS_FUTEX, addr1, op, val1, timeout, addr2, 0u, val3);
82#endif
83}
84
85//! Invokes an operation on the futex
86BOOST_FORCEINLINE int futex_invoke(void* addr1, int op, unsigned int val1, unsigned int val2, void* addr2 = NULL, unsigned int val3 = 0) BOOST_NOEXCEPT
87{
88#if !defined(BOOST_ATOMIC_DETAIL_NETBSD_FUTEX)
89 return ::syscall(BOOST_ATOMIC_DETAIL_SYS_FUTEX, addr1, op, val1, static_cast< atomics::detail::uintptr_t >(val2), addr2, val3);
90#else
91 // Pass NULL in timeout.
92 return ::syscall(BOOST_ATOMIC_DETAIL_SYS_FUTEX, addr1, op, val1, static_cast< void* >(NULL), addr2, val2, val3);
93#endif
94}
95
96//! Checks that the value \c pval is \c expected and blocks
97BOOST_FORCEINLINE int futex_wait(void* pval, unsigned int expected) BOOST_NOEXCEPT
98{
99 return futex_invoke(pval, FUTEX_WAIT, expected);
100}
101
102//! Checks that the value \c pval is \c expected and blocks
103BOOST_FORCEINLINE int futex_wait_private(void* pval, unsigned int expected) BOOST_NOEXCEPT
104{
105 return futex_invoke(pval, FUTEX_WAIT | BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG, expected);
106}
107
108//! Wakes the specified number of threads waiting on the futex
109BOOST_FORCEINLINE int futex_signal(void* pval, unsigned int count = 1u) BOOST_NOEXCEPT
110{
111 return futex_invoke(pval, FUTEX_WAKE, count);
112}
113
114//! Wakes the specified number of threads waiting on the futex
115BOOST_FORCEINLINE int futex_signal_private(void* pval, unsigned int count = 1u) BOOST_NOEXCEPT
116{
117 return futex_invoke(pval, FUTEX_WAKE | BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG, count);
118}
119
120//! Wakes all threads waiting on the futex
121BOOST_FORCEINLINE int futex_broadcast(void* pval) BOOST_NOEXCEPT
122{
123 return futex_signal(pval, (~static_cast< unsigned int >(0u)) >> 1);
124}
125
126//! Wakes all threads waiting on the futex
127BOOST_FORCEINLINE int futex_broadcast_private(void* pval) BOOST_NOEXCEPT
128{
129 return futex_signal_private(pval, (~static_cast< unsigned int >(0u)) >> 1);
130}
131
132//! Wakes the wake_count threads waiting on the futex pval1 and requeues up to requeue_count of the blocked threads onto another futex pval2
133BOOST_FORCEINLINE int futex_requeue(void* pval1, void* pval2, unsigned int wake_count = 1u, unsigned int requeue_count = (~static_cast< unsigned int >(0u)) >> 1) BOOST_NOEXCEPT
134{
135 return futex_invoke(pval1, FUTEX_REQUEUE, wake_count, requeue_count, pval2);
136}
137
138//! Wakes the wake_count threads waiting on the futex pval1 and requeues up to requeue_count of the blocked threads onto another futex pval2
139BOOST_FORCEINLINE int futex_requeue_private(void* pval1, void* pval2, unsigned int wake_count = 1u, unsigned int requeue_count = (~static_cast< unsigned int >(0u)) >> 1) BOOST_NOEXCEPT
140{
141 return futex_invoke(pval1, FUTEX_REQUEUE | BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG, wake_count, requeue_count, pval2);
142}
143
144} // namespace detail
145} // namespace atomics
146} // namespace boost
147
148#include <boost/atomic/detail/footer.hpp>
149
150#endif // defined(BOOST_ATOMIC_DETAIL_SYS_FUTEX)
151
152#endif // defined(__linux__) || defined(__OpenBSD__) || defined(__NETBSD__) || defined(__NetBSD__)
153
154#endif // BOOST_ATOMIC_DETAIL_FUTEX_HPP_INCLUDED_