summaryrefslogtreecommitdiff
path: root/src/mongo/platform/atomic_word_test.cpp
blob: 7f55057a7c6acbc5b148502eb1a84b803e884eff (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/platform/basic.h"

#include <iostream>


#include "mongo/platform/atomic_word.h"
#include "mongo/unittest/unittest.h"

namespace mongo {
namespace {

/*
 * Modified CAS for AtomicWord to return boolean based on whether or not the swap occurred.
 * This helper function mimics the old implementation, which returned the original value of
 * expected.
 */
template <typename WordType>
WordType testAtomicWordCompareAndSwap(AtomicWord<WordType>& word,
                                      WordType expected,
                                      WordType desired) {
    auto prevWord = word.loadRelaxed();
    auto didSwap = word.compareAndSwap(&expected, desired);
    ASSERT_EQUALS(expected, prevWord);
    if (didSwap) {
        ASSERT_EQUALS(word.load(), desired);
    } else {
        ASSERT_EQUALS(word.load(), prevWord);
    }
    return expected;
}

template <typename _AtomicWordType>
void testAtomicWordBasicOperations() {
    typedef typename _AtomicWordType::WordType WordType;
    _AtomicWordType w;

    ASSERT_EQUALS(WordType(0), w.load());

    w.store(1);
    ASSERT_EQUALS(WordType(1), w.load());

    ASSERT_EQUALS(WordType(1), w.swap(2));
    ASSERT_EQUALS(WordType(2), w.load());

    ASSERT_EQUALS(WordType(2), testAtomicWordCompareAndSwap<WordType>(w, 0, 1));
    ASSERT_EQUALS(WordType(2), w.load());
    ASSERT_EQUALS(WordType(2), testAtomicWordCompareAndSwap<WordType>(w, 2, 1));
    ASSERT_EQUALS(WordType(1), w.load());

    ASSERT_EQUALS(WordType(1), w.fetchAndAdd(14));
    ASSERT_EQUALS(WordType(17), w.addAndFetch(2));
    ASSERT_EQUALS(WordType(16), w.subtractAndFetch(1));
    ASSERT_EQUALS(WordType(16), w.fetchAndSubtract(1));
    ASSERT_EQUALS(WordType(15), testAtomicWordCompareAndSwap<WordType>(w, 15, 0));
    ASSERT_EQUALS(WordType(0), w.load());
}

template <typename AtomicWordType>
void testAtomicWordBitOperations() {
    typedef typename AtomicWordType::WordType WordType;
    AtomicWordType w;

    WordType highBit = 1ull << ((sizeof(WordType) * 8) - 1);

    w.store(highBit | 0xFFull);
    ASSERT_EQUALS(WordType(highBit | 0xFFull), w.fetchAndBitAnd(highBit));
    ASSERT_EQUALS(WordType(highBit), w.fetchAndBitOr(highBit | 0xFFull));
    ASSERT_EQUALS(WordType(highBit | 0xFFull), w.fetchAndBitXor(0xFFFF));
    ASSERT_EQUALS(WordType(highBit | 0xFF00ull), w.load());
}

ASSERT_DOES_NOT_COMPILE(typename T = int, AtomicWord<T>().fetchAndBitAnd(0));
ASSERT_DOES_NOT_COMPILE(typename T = int, AtomicWord<T>().fetchAndBitOr(0));
ASSERT_DOES_NOT_COMPILE(typename T = int, AtomicWord<T>().fetchAndBitXor(0));

ASSERT_DOES_NOT_COMPILE(typename T = char, AtomicWord<T>().fetchAndBitAnd(0));
ASSERT_DOES_NOT_COMPILE(typename T = char, AtomicWord<T>().fetchAndBitOr(0));
ASSERT_DOES_NOT_COMPILE(typename T = char, AtomicWord<T>().fetchAndBitXor(0));

enum TestEnum { E0, E1, E2, E3 };

TEST(AtomicWordTests, BasicOperationsEnum) {
    MONGO_STATIC_ASSERT(sizeof(AtomicWord<TestEnum>) == sizeof(TestEnum));
    AtomicWord<TestEnum> w;
    ASSERT_EQUALS(E0, w.load());
    ASSERT_EQUALS(E0, testAtomicWordCompareAndSwap(w, E0, E1));
    ASSERT_EQUALS(E1, w.load());
    ASSERT_EQUALS(E1, testAtomicWordCompareAndSwap(w, E0, E2));
    ASSERT_EQUALS(E1, w.load());
}

TEST(AtomicWordTests, BasicOperationsUnsigned32Bit) {
    typedef unsigned WordType;
    testAtomicWordBasicOperations<AtomicWord<unsigned>>();
    testAtomicWordBitOperations<AtomicWord<unsigned>>();

    AtomicWord<unsigned> w(0xdeadbeef);
    ASSERT_EQUALS(WordType(0xdeadbeef), testAtomicWordCompareAndSwap<WordType>(w, 0, 1));
    ASSERT_EQUALS(WordType(0xdeadbeef),
                  testAtomicWordCompareAndSwap<WordType>(w, 0xdeadbeef, 0xcafe1234));
    ASSERT_EQUALS(WordType(0xcafe1234), w.fetchAndAdd(0xf000));
    ASSERT_EQUALS(WordType(0xcaff0234), w.swap(0));
    ASSERT_EQUALS(WordType(0), w.load());
}

TEST(AtomicWordTests, BasicOperationsUnsigned64Bit) {
    typedef unsigned long long WordType;
    testAtomicWordBasicOperations<AtomicWord<unsigned long long>>();
    testAtomicWordBitOperations<AtomicWord<unsigned long long>>();

    AtomicWord<unsigned long long> w(0xdeadbeefcafe1234ULL);
    ASSERT_EQUALS(WordType(0xdeadbeefcafe1234ULL), testAtomicWordCompareAndSwap<WordType>(w, 0, 1));
    ASSERT_EQUALS(
        WordType(0xdeadbeefcafe1234ULL),
        testAtomicWordCompareAndSwap<WordType>(w, 0xdeadbeefcafe1234ULL, 0xfedcba9876543210ULL));
    ASSERT_EQUALS(WordType(0xfedcba9876543210ULL), w.fetchAndAdd(0xf0000000ULL));
    ASSERT_EQUALS(WordType(0xfedcba9966543210ULL), w.swap(0));
    ASSERT_EQUALS(WordType(0), w.load());
}

TEST(AtomicWordTests, BasicOperationsSigned32Bit) {
    typedef int WordType;
    testAtomicWordBasicOperations<AtomicWord<int>>();

    AtomicWord<int> w(0xdeadbeef);
    ASSERT_EQUALS(WordType(0xdeadbeef), testAtomicWordCompareAndSwap<WordType>(w, 0, 1));
    ASSERT_EQUALS(WordType(0xdeadbeef),
                  testAtomicWordCompareAndSwap<WordType>(w, 0xdeadbeef, 0xcafe1234));
    ASSERT_EQUALS(WordType(0xcafe1234), w.fetchAndAdd(0xf000));
    ASSERT_EQUALS(WordType(0xcaff0234), w.swap(0));
    ASSERT_EQUALS(WordType(0), w.load());
}

TEST(AtomicWordTests, BasicOperationsSigned64Bit) {
    typedef long long WordType;
    testAtomicWordBasicOperations<AtomicWord<long long>>();

    AtomicWord<long long> w(0xdeadbeefcafe1234ULL);
    ASSERT_EQUALS(WordType(0xdeadbeefcafe1234LL), testAtomicWordCompareAndSwap<WordType>(w, 0, 1));
    ASSERT_EQUALS(
        WordType(0xdeadbeefcafe1234LL),
        testAtomicWordCompareAndSwap<WordType>(w, 0xdeadbeefcafe1234LL, 0xfedcba9876543210LL));
    ASSERT_EQUALS(WordType(0xfedcba9876543210LL), w.fetchAndAdd(0xf0000000LL));
    ASSERT_EQUALS(WordType(0xfedcba9966543210LL), w.swap(0));
    ASSERT_EQUALS(WordType(0), w.load());
}

TEST(AtomicWordTests, BasicOperationsFloat) {
    typedef AtomicWord<float>::WordType WordType;

    AtomicWord<float> w;

    ASSERT_EQUALS(WordType(0), w.load());

    w.store(1);
    ASSERT_EQUALS(WordType(1), w.load());

    ASSERT_EQUALS(WordType(1), w.swap(2));
    ASSERT_EQUALS(WordType(2), w.load());

    ASSERT_EQUALS(WordType(2), testAtomicWordCompareAndSwap<WordType>(w, 0, 1));
    ASSERT_EQUALS(WordType(2), w.load());
    ASSERT_EQUALS(WordType(2), testAtomicWordCompareAndSwap<WordType>(w, 2, 1));
    ASSERT_EQUALS(WordType(1), w.load());

    w.store(15);
    ASSERT_EQUALS(WordType(15), testAtomicWordCompareAndSwap<WordType>(w, 15, 0));
    ASSERT_EQUALS(WordType(0), w.load());
}

struct Chars {
    static constexpr size_t kLength = 6;

    Chars(const char* chars = "") {
        invariant(std::strlen(chars) < kLength);
        std::strncpy(_storage.data(), chars, sizeof(_storage));
    }

    std::array<char, 6> _storage = {};

    friend bool operator==(const Chars& lhs, const Chars& rhs) {
        return lhs._storage == rhs._storage;
    }

    friend bool operator!=(const Chars& lhs, const Chars& rhs) {
        return !(lhs == rhs);
    }
};

std::ostream& operator<<(std::ostream& os, const Chars& chars) {
    return (os << chars._storage.data());
}

template <typename T>
void verifyAtomicityHelper() {
    ASSERT(std::atomic<T>{}.is_lock_free());                                     // NOLINT
    ASSERT(std::atomic<typename std::make_signed<T>::type>{}.is_lock_free());    // NOLINT
    ASSERT(std::atomic<typename std::make_unsigned<T>::type>{}.is_lock_free());  // NOLINT
}

template <typename... Types>
void verifyAtomicity() {
    using expander = int[];
    (void)expander{(verifyAtomicityHelper<Types>(), 0)...};
}

TEST(AtomicWordTests, StdAtomicOfIntegralIsLockFree) {
    // 2 means that they're always atomic.  Instead of 1, that means sometimes, and 0, which means
    // never.
    ASSERT_EQUALS(2, ATOMIC_CHAR_LOCK_FREE);
    ASSERT_EQUALS(2, ATOMIC_CHAR16_T_LOCK_FREE);
    ASSERT_EQUALS(2, ATOMIC_CHAR32_T_LOCK_FREE);
    ASSERT_EQUALS(2, ATOMIC_WCHAR_T_LOCK_FREE);
    ASSERT_EQUALS(2, ATOMIC_SHORT_LOCK_FREE);
    ASSERT_EQUALS(2, ATOMIC_INT_LOCK_FREE);
    ASSERT_EQUALS(2, ATOMIC_LONG_LOCK_FREE);
    ASSERT_EQUALS(2, ATOMIC_LLONG_LOCK_FREE);
    ASSERT_EQUALS(2, ATOMIC_POINTER_LOCK_FREE);

    verifyAtomicity<char, char16_t, char32_t, wchar_t, short, int, long, long long>();
    ASSERT(std::atomic<bool>{}.is_lock_free());  // NOLINT
}

}  // namespace
}  // namespace mongo