Coverage Report

Created: 2020-09-22 08:39

/Users/buildslave/jenkins/workspace/coverage/llvm-project/libcxx/src/atomic.cpp
Line
Count
Source (jump to first uncovered line)
1
//===------------------------- atomic.cpp ---------------------------------===//
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 <__config>
10
#ifndef _LIBCPP_HAS_NO_THREADS
11
12
#include <climits>
13
#include <atomic>
14
#include <functional>
15
16
#include <iostream>
17
18
#ifdef __linux__
19
20
#include <unistd.h>
21
#include <linux/futex.h>
22
#include <sys/syscall.h>
23
24
#else // <- Add other operating systems here
25
26
// Baseline needs no new headers
27
28
#endif
29
30
_LIBCPP_BEGIN_NAMESPACE_STD
31
32
#ifdef __linux__
33
34
static void __libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr,
35
                                              __cxx_contention_t __val)
36
{
37
    static constexpr timespec __timeout = { 2, 0 };
38
    syscall(SYS_futex, __ptr, FUTEX_WAIT_PRIVATE, __val, &__timeout, 0, 0);
39
}
40
41
static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr,
42
                                              bool __notify_one)
43
{
44
    syscall(SYS_futex, __ptr, FUTEX_WAKE_PRIVATE, __notify_one ? 1 : INT_MAX, 0, 0, 0);
45
}
46
47
#elif defined(__APPLE__) && defined(_LIBCPP_USE_ULOCK)
48
49
extern "C" int __ulock_wait(uint32_t operation, void *addr, uint64_t value,
50
    uint32_t timeout); /* timeout is specified in microseconds */
51
extern "C" int __ulock_wake(uint32_t operation, void *addr, uint64_t wake_value);
52
53
#define UL_COMPARE_AND_WAIT       1
54
#define ULF_WAKE_ALL          0x00000100
55
56
static void __libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr,
57
                                              __cxx_contention_t __val)
58
{
59
    __ulock_wait(UL_COMPARE_AND_WAIT,
60
                 const_cast<__cxx_atomic_contention_t*>(__ptr), __val, 0);
61
}
62
63
static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr,
64
                                              bool __notify_one)
65
{
66
    __ulock_wake(UL_COMPARE_AND_WAIT | (__notify_one ? 0 : ULF_WAKE_ALL),
67
                 const_cast<__cxx_atomic_contention_t*>(__ptr), 0);
68
}
69
70
#else // <- Add other operating systems here
71
72
// Baseline is just a timed backoff
73
74
static void __libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr,
75
                                              __cxx_contention_t __val)
76
0
{
77
0
    __libcpp_thread_poll_with_backoff([=]() -> bool {
78
0
        return !__cxx_nonatomic_compare_equal(__cxx_atomic_load(__ptr, memory_order_relaxed), __val);
79
0
    }, __libcpp_timed_backoff_policy());
80
0
}
81
82
0
static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile*, bool) { }
83
84
#endif // __linux__
85
86
static constexpr size_t __libcpp_contention_table_size = (1 << 8);  /* < there's no magic in this number */
87
88
struct alignas(64) /*  aim to avoid false sharing */ __libcpp_contention_table_entry
89
{
90
    __cxx_atomic_contention_t __contention_state;
91
    __cxx_atomic_contention_t __platform_state;
92
    inline constexpr __libcpp_contention_table_entry() :
93
0
        __contention_state(0), __platform_state(0) { }
94
};
95
96
static __libcpp_contention_table_entry __libcpp_contention_table[ __libcpp_contention_table_size ];
97
98
static hash<void const volatile*> __libcpp_contention_hasher;
99
100
static __libcpp_contention_table_entry* __libcpp_contention_state(void const volatile * p)
101
0
{
102
0
    return &__libcpp_contention_table[__libcpp_contention_hasher(p) & (__libcpp_contention_table_size - 1)];
103
0
}
104
105
/* Given an atomic to track contention and an atomic to actually wait on, which may be
106
   the same atomic, we try to detect contention to avoid spuriously calling the platform. */
107
108
static void __libcpp_contention_notify(__cxx_atomic_contention_t volatile* __contention_state,
109
                                       __cxx_atomic_contention_t const volatile* __platform_state,
110
                                       bool __notify_one)
111
0
{
112
0
    if(0 != __cxx_atomic_load(__contention_state, memory_order_seq_cst))
113
        // We only call 'wake' if we consumed a contention bit here.
114
0
        __libcpp_platform_wake_by_address(__platform_state, __notify_one);
115
0
}
116
static __cxx_contention_t __libcpp_contention_monitor_for_wait(__cxx_atomic_contention_t volatile* __contention_state,
117
                                                               __cxx_atomic_contention_t const volatile* __platform_state)
118
0
{
119
    // We will monitor this value.
120
0
    return __cxx_atomic_load(__platform_state, memory_order_acquire);
121
0
}
122
static void __libcpp_contention_wait(__cxx_atomic_contention_t volatile* __contention_state,
123
                                     __cxx_atomic_contention_t const volatile* __platform_state,
124
                                     __cxx_contention_t __old_value)
125
0
{
126
0
    __cxx_atomic_fetch_add(__contention_state, __cxx_contention_t(1), memory_order_seq_cst);
127
    // We sleep as long as the monitored value hasn't changed.
128
0
    __libcpp_platform_wait_on_address(__platform_state, __old_value);
129
0
    __cxx_atomic_fetch_sub(__contention_state, __cxx_contention_t(1), memory_order_release);
130
0
}
131
132
/* When the incoming atomic is the wrong size for the platform wait size, need to
133
   launder the value sequence through an atomic from our table. */
134
135
static void __libcpp_atomic_notify(void const volatile* __location)
136
0
{
137
0
    auto const __entry = __libcpp_contention_state(__location);
138
    // The value sequence laundering happens on the next line below.
139
0
    __cxx_atomic_fetch_add(&__entry->__platform_state, __cxx_contention_t(1), memory_order_release);
140
0
    __libcpp_contention_notify(&__entry->__contention_state,
141
0
                               &__entry->__platform_state,
142
0
                               false /* when laundering, we can't handle notify_one */);
143
0
}
144
_LIBCPP_EXPORTED_FROM_ABI
145
void __cxx_atomic_notify_one(void const volatile* __location)
146
0
    { __libcpp_atomic_notify(__location); }
147
_LIBCPP_EXPORTED_FROM_ABI
148
void __cxx_atomic_notify_all(void const volatile* __location)
149
0
    { __libcpp_atomic_notify(__location); }
150
_LIBCPP_EXPORTED_FROM_ABI
151
__cxx_contention_t __libcpp_atomic_monitor(void const volatile* __location)
152
0
{
153
0
    auto const __entry = __libcpp_contention_state(__location);
154
0
    return __libcpp_contention_monitor_for_wait(&__entry->__contention_state, &__entry->__platform_state);
155
0
}
156
_LIBCPP_EXPORTED_FROM_ABI
157
void __libcpp_atomic_wait(void const volatile* __location, __cxx_contention_t __old_value)
158
0
{
159
0
    auto const __entry = __libcpp_contention_state(__location);
160
0
    __libcpp_contention_wait(&__entry->__contention_state, &__entry->__platform_state, __old_value);
161
0
}
162
163
/* When the incoming atomic happens to be the platform wait size, we still need to use the
164
   table for the contention detection, but we can use the atomic directly for the wait. */
165
166
_LIBCPP_EXPORTED_FROM_ABI
167
void __cxx_atomic_notify_one(__cxx_atomic_contention_t const volatile* __location)
168
0
{
169
0
    __libcpp_contention_notify(&__libcpp_contention_state(__location)->__contention_state, __location, true);
170
0
}
171
_LIBCPP_EXPORTED_FROM_ABI
172
void __cxx_atomic_notify_all(__cxx_atomic_contention_t const volatile* __location)
173
0
{
174
0
    __libcpp_contention_notify(&__libcpp_contention_state(__location)->__contention_state, __location, false);
175
0
}
176
_LIBCPP_EXPORTED_FROM_ABI
177
__cxx_contention_t __libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile* __location)
178
0
{
179
0
    return __libcpp_contention_monitor_for_wait(&__libcpp_contention_state(__location)->__contention_state, __location);
180
0
}
181
_LIBCPP_EXPORTED_FROM_ABI
182
void __libcpp_atomic_wait(__cxx_atomic_contention_t const volatile* __location, __cxx_contention_t __old_value)
183
0
{
184
0
    __libcpp_contention_wait(&__libcpp_contention_state(__location)->__contention_state, __location, __old_value);
185
0
}
186
187
_LIBCPP_END_NAMESPACE_STD
188
189
#endif //_LIBCPP_HAS_NO_THREADS