summaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/test-atomicops.cc
blob: 92421138cbb1acc2586b67aa81513c67171eb5e4 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright 2014 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "src/v8.h"

#include "src/base/atomicops.h"
#include "test/cctest/cctest.h"

namespace v8 {
namespace base {

#define CHECK_EQU(v1, v2) \
  CHECK_EQ(static_cast<int64_t>(v1), static_cast<int64_t>(v2))

#define NUM_BITS(T) (sizeof(T) * 8)


template <class AtomicType>
static void TestAtomicIncrement() {
  // For now, we just test the single-threaded execution.

  // Use a guard value to make sure that Relaxed_AtomicIncrement doesn't
  // go outside the expected address bounds.  This is to test that the
  // 32-bit Relaxed_AtomicIncrement doesn't do the wrong thing on 64-bit
  // machines.
  struct {
    AtomicType prev_word;
    AtomicType count;
    AtomicType next_word;
  } s;

  AtomicType prev_word_value, next_word_value;
  memset(&prev_word_value, 0xFF, sizeof(AtomicType));
  memset(&next_word_value, 0xEE, sizeof(AtomicType));

  s.prev_word = prev_word_value;
  s.count = 0;
  s.next_word = next_word_value;

  CHECK_EQU(Relaxed_AtomicIncrement(&s.count, 1), 1);
  CHECK_EQU(s.count, 1);
  CHECK_EQU(s.prev_word, prev_word_value);
  CHECK_EQU(s.next_word, next_word_value);

  CHECK_EQU(Relaxed_AtomicIncrement(&s.count, 2), 3);
  CHECK_EQU(s.count, 3);
  CHECK_EQU(s.prev_word, prev_word_value);
  CHECK_EQU(s.next_word, next_word_value);

  CHECK_EQU(Relaxed_AtomicIncrement(&s.count, 3), 6);
  CHECK_EQU(s.count, 6);
  CHECK_EQU(s.prev_word, prev_word_value);
  CHECK_EQU(s.next_word, next_word_value);

  CHECK_EQU(Relaxed_AtomicIncrement(&s.count, -3), 3);
  CHECK_EQU(s.count, 3);
  CHECK_EQU(s.prev_word, prev_word_value);
  CHECK_EQU(s.next_word, next_word_value);

  CHECK_EQU(Relaxed_AtomicIncrement(&s.count, -2), 1);
  CHECK_EQU(s.count, 1);
  CHECK_EQU(s.prev_word, prev_word_value);
  CHECK_EQU(s.next_word, next_word_value);

  CHECK_EQU(Relaxed_AtomicIncrement(&s.count, -1), 0);
  CHECK_EQU(s.count, 0);
  CHECK_EQU(s.prev_word, prev_word_value);
  CHECK_EQU(s.next_word, next_word_value);

  CHECK_EQU(Relaxed_AtomicIncrement(&s.count, -1), -1);
  CHECK_EQU(s.count, -1);
  CHECK_EQU(s.prev_word, prev_word_value);
  CHECK_EQU(s.next_word, next_word_value);

  CHECK_EQU(Relaxed_AtomicIncrement(&s.count, -4), -5);
  CHECK_EQU(s.count, -5);
  CHECK_EQU(s.prev_word, prev_word_value);
  CHECK_EQU(s.next_word, next_word_value);

  CHECK_EQU(Relaxed_AtomicIncrement(&s.count, 5), 0);
  CHECK_EQU(s.count, 0);
  CHECK_EQU(s.prev_word, prev_word_value);
  CHECK_EQU(s.next_word, next_word_value);
}


template <class AtomicType>
static void TestCompareAndSwap() {
  AtomicType value = 0;
  AtomicType prev = Relaxed_CompareAndSwap(&value, 0, 1);
  CHECK_EQU(1, value);
  CHECK_EQU(0, prev);

  // Use a test value that has non-zero bits in both halves, for testing
  // the 64-bit implementation on 32-bit platforms.
  const AtomicType k_test_val =
      (static_cast<AtomicType>(1) << (NUM_BITS(AtomicType) - 2)) + 11;
  value = k_test_val;
  prev = Relaxed_CompareAndSwap(&value, 0, 5);
  CHECK_EQU(k_test_val, value);
  CHECK_EQU(k_test_val, prev);

  value = k_test_val;
  prev = Relaxed_CompareAndSwap(&value, k_test_val, 5);
  CHECK_EQU(5, value);
  CHECK_EQU(k_test_val, prev);
}


template <class AtomicType>
static void TestAtomicExchange() {
  AtomicType value = 0;
  AtomicType new_value = Relaxed_AtomicExchange(&value, 1);
  CHECK_EQU(1, value);
  CHECK_EQU(0, new_value);

  // Use a test value that has non-zero bits in both halves, for testing
  // the 64-bit implementation on 32-bit platforms.
  const AtomicType k_test_val =
      (static_cast<AtomicType>(1) << (NUM_BITS(AtomicType) - 2)) + 11;
  value = k_test_val;
  new_value = Relaxed_AtomicExchange(&value, k_test_val);
  CHECK_EQU(k_test_val, value);
  CHECK_EQU(k_test_val, new_value);

  value = k_test_val;
  new_value = Relaxed_AtomicExchange(&value, 5);
  CHECK_EQU(5, value);
  CHECK_EQU(k_test_val, new_value);
}


template <class AtomicType>
static void TestAtomicIncrementBounds() {
  // Test at 32-bit boundary for 64-bit atomic type.
  AtomicType test_val = static_cast<AtomicType>(1)
                        << (NUM_BITS(AtomicType) / 2);
  AtomicType value = test_val - 1;
  AtomicType new_value = Relaxed_AtomicIncrement(&value, 1);
  CHECK_EQU(test_val, value);
  CHECK_EQU(value, new_value);

  Relaxed_AtomicIncrement(&value, -1);
  CHECK_EQU(test_val - 1, value);
}

// Return an AtomicType with the value 0xA5A5A5..
template <class AtomicType>
static AtomicType TestFillValue() {
  AtomicType val = 0;
  memset(&val, 0xA5, sizeof(AtomicType));
  return val;
}


// This is a simple sanity check to ensure that values are correct.
// Not testing atomicity.
template <class AtomicType>
static void TestStore() {
  const AtomicType kVal1 = TestFillValue<AtomicType>();
  const AtomicType kVal2 = static_cast<AtomicType>(-1);

  AtomicType value;

  Relaxed_Store(&value, kVal1);
  CHECK_EQU(kVal1, value);
  Relaxed_Store(&value, kVal2);
  CHECK_EQU(kVal2, value);

  Release_Store(&value, kVal1);
  CHECK_EQU(kVal1, value);
  Release_Store(&value, kVal2);
  CHECK_EQU(kVal2, value);
}


// Merge this test with TestStore as soon as we have Atomic8 acquire
// and release stores.
static void TestStoreAtomic8() {
  const Atomic8 kVal1 = TestFillValue<Atomic8>();
  const Atomic8 kVal2 = static_cast<Atomic8>(-1);

  Atomic8 value;

  Relaxed_Store(&value, kVal1);
  CHECK_EQU(kVal1, value);
  Relaxed_Store(&value, kVal2);
  CHECK_EQU(kVal2, value);
}


// This is a simple sanity check to ensure that values are correct.
// Not testing atomicity.
template <class AtomicType>
static void TestLoad() {
  const AtomicType kVal1 = TestFillValue<AtomicType>();
  const AtomicType kVal2 = static_cast<AtomicType>(-1);

  AtomicType value;

  value = kVal1;
  CHECK_EQU(kVal1, Relaxed_Load(&value));
  value = kVal2;
  CHECK_EQU(kVal2, Relaxed_Load(&value));

  value = kVal1;
  CHECK_EQU(kVal1, Acquire_Load(&value));
  value = kVal2;
  CHECK_EQU(kVal2, Acquire_Load(&value));
}


// Merge this test with TestLoad as soon as we have Atomic8 acquire
// and release loads.
static void TestLoadAtomic8() {
  const Atomic8 kVal1 = TestFillValue<Atomic8>();
  const Atomic8 kVal2 = static_cast<Atomic8>(-1);

  Atomic8 value;

  value = kVal1;
  CHECK_EQU(kVal1, Relaxed_Load(&value));
  value = kVal2;
  CHECK_EQU(kVal2, Relaxed_Load(&value));
}


TEST(AtomicIncrement) {
  TestAtomicIncrement<Atomic32>();
  TestAtomicIncrement<AtomicWord>();
}


TEST(CompareAndSwap) {
  TestCompareAndSwap<Atomic32>();
  TestCompareAndSwap<AtomicWord>();
}


TEST(AtomicExchange) {
  TestAtomicExchange<Atomic32>();
  TestAtomicExchange<AtomicWord>();
}


TEST(AtomicIncrementBounds) {
  TestAtomicIncrementBounds<Atomic32>();
  TestAtomicIncrementBounds<AtomicWord>();
}


TEST(Store) {
  TestStoreAtomic8();
  TestStore<Atomic32>();
  TestStore<AtomicWord>();
}


TEST(Load) {
  TestLoadAtomic8();
  TestLoad<Atomic32>();
  TestLoad<AtomicWord>();
}

}  // namespace base
}  // namespace v8