aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/memory/free_list-inl.h
blob: 45bb2b8f1067ed535f51013ea7150d4b013f9a15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#ifndef FREE_LIST_INL_H_
#error "Direct inclusion of this file is not allowed, include free_list.h"
// For the sake of sane code completion.
#include "free_list.h"
#endif

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

namespace NDetail {

template <class T1, class T2>
Y_FORCE_INLINE bool CompareAndSet(
    TAtomicUint128* atomic,
    T1& expected1,
    T2& expected2,
    T1 new1,
    T2 new2)
{
#if defined(__x86_64__)
    bool success;
    __asm__ __volatile__
    (
        "lock cmpxchg16b %1\n"
        "setz %0"
        : "=q"(success)
        , "+m"(*atomic)
        , "+a"(expected1)
        , "+d"(expected2)
        : "b"(new1)
        , "c"(new2)
        : "cc"
    );
    return success;
#elif defined(__arm64__) || (defined(__aarch64__) && defined(RTE_ARM_FEATURE_ATOMICS))
    register ui64 x0 __asm("x0") = (ui64)expected1;
    register ui64 x1 __asm("x1") = (ui64)expected2;
    register ui64 x2 __asm("x2") = (ui64)new1;
    register ui64 x3 __asm("x3") = (ui64)new2;
    ui64 old1 = (ui64)expected1;
    ui64 old2 = (ui64)expected2;
    asm volatile
    (
#if defined(RTE_CC_CLANG)
        ".arch armv8-a+lse\n"
#endif
        "caspal %[old0], %[old1], %[upd0], %[upd1], [%[dst]]"
        : [old0] "+r" (x0)
        , [old1] "+r" (x1)
        : [upd0] "r" (x2)
        , [upd1] "r" (x3)
        , [dst] "r" (atomic)
        : "memory"
    );
    expected1 = (T1)x0;
    expected2 = (T2)x1;
    return x0 == old1 && x1 == old2;
#elif defined(__aarch64__)
    ui64 exp1 = reinterpret_cast<ui64>(expected1);
    ui64 exp2 = reinterpret_cast<ui64>(expected2);
    ui32 fail = 0;

    do {
        ui64 current1 = 0;
        ui64 current2 = 0;
        asm volatile (
            "ldaxp %[cur1], %[cur2], [%[src]]"
            : [cur1] "=r" (current1)
            , [cur2] "=r" (current2)
            : [src] "r" (atomic)
            : "memory"
        );

        if (current1 != exp1 || current2 != exp2) {
            expected1 = reinterpret_cast<T1>(current1);
            expected2 = reinterpret_cast<T2>(current2);
            return false;
        }

        asm volatile (
            "stlxp %w[fail], %[new1], %[new2], [%[dst]]"
            : [fail] "=&r" (fail)
            : [new1] "r" (new1)
            , [new2] "r" (new2)
            , [dst] "r" (atomic)
            : "memory"
        );

    } while (Y_UNLIKELY(fail));
    return true;
#else
#    error Unsupported platform
#endif
}

} // namespace NDetail

////////////////////////////////////////////////////////////////////////////////

template <class TItem>
TFreeList<TItem>::THead::THead(TItem* pointer)
    : Pointer(pointer)
{ }

template <class TItem>
TFreeList<TItem>::TFreeList()
    : Head_()
{ }

template <class TItem>
TFreeList<TItem>::TFreeList(TFreeList<TItem>&& other)
    : Head_(other.ExtractAll())
{ }

template <class TItem>
TFreeList<TItem>::~TFreeList()
{
    YT_VERIFY(IsEmpty());
}

template <class TItem>
template <class TPredicate>
Y_NO_SANITIZE("thread")
bool TFreeList<TItem>::PutIf(TItem* head, TItem* tail, TPredicate predicate)
{
    auto* current = Head_.Pointer.load(std::memory_order::relaxed);
    auto popCount = Head_.PopCount.load(std::memory_order::relaxed);

    while (predicate(current)) {
        tail->Next.store(current, std::memory_order::release);
        if (NYT::NDetail::CompareAndSet(&AtomicHead_, current, popCount, head, popCount)) {
            return true;
        }
    }

    tail->Next.store(nullptr, std::memory_order::release);

    return false;
}


template <class TItem>
Y_NO_SANITIZE("thread")
void TFreeList<TItem>::Put(TItem* head, TItem* tail)
{
    auto* current = Head_.Pointer.load(std::memory_order::relaxed);
    auto popCount = Head_.PopCount.load(std::memory_order::relaxed);

    do {
        tail->Next.store(current, std::memory_order::release);
    } while (!NYT::NDetail::CompareAndSet(&AtomicHead_, current, popCount, head, popCount));
}

template <class TItem>
void TFreeList<TItem>::Put(TItem* item)
{
    Put(item, item);
}

template <class TItem>
Y_NO_SANITIZE("thread")
TItem* TFreeList<TItem>::Extract()
{
    auto* current = Head_.Pointer.load(std::memory_order::relaxed);
    auto popCount = Head_.PopCount.load(std::memory_order::relaxed);

    while (current) {
        // If current node is already extracted by other thread
        // there can be any writes at address &current->Next.
        // The only guaranteed thing is that address is valid (memory is not freed).
        auto next = current->Next.load(std::memory_order::acquire);
        if (NYT::NDetail::CompareAndSet(&AtomicHead_, current, popCount, next, popCount + 1)) {
            current->Next.store(nullptr, std::memory_order::release);
            return current;
        }
    }

    return nullptr;
}

template <class TItem>
TItem* TFreeList<TItem>::ExtractAll()
{
    auto* current = Head_.Pointer.load(std::memory_order::relaxed);
    auto popCount = Head_.PopCount.load(std::memory_order::relaxed);

    while (current) {
        if (NYT::NDetail::CompareAndSet<TItem*, size_t>(&AtomicHead_, current, popCount, nullptr, popCount + 1)) {
            return current;
        }
    }

    return nullptr;
}

template <class TItem>
bool TFreeList<TItem>::IsEmpty() const
{
    return Head_.Pointer.load() == nullptr;
}

template <class TItem>
void TFreeList<TItem>::Append(TFreeList<TItem>& other)
{
    auto* head = other.ExtractAll();

    if (!head) {
        return;
    }

    auto* tail = head;
    while (tail->Next) {
        tail = tail->Next;
    }

    Put(head, tail);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT