1b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// Copyright 2014 the V8 project authors. All rights reserved.
2b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// Redistribution and use in source and binary forms, with or without
3b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// modification, are permitted provided that the following conditions are
4b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// met:
5b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//
6b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//     * Redistributions of source code must retain the above copyright
7b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//       notice, this list of conditions and the following disclaimer.
8b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//     * Redistributions in binary form must reproduce the above
9b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//       copyright notice, this list of conditions and the following
10b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//       disclaimer in the documentation and/or other materials provided
11b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//       with the distribution.
12b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//     * Neither the name of Google Inc. nor the names of its
13b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//       contributors may be used to endorse or promote products derived
14b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//       from this software without specific prior written permission.
15b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch//
16b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
28b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "src/v8.h"
29b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
30b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "src/base/atomicops.h"
31b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#include "test/cctest/cctest.h"
32b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
33b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochusing namespace v8::base;
34b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochusing namespace v8::internal;
35b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
36b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
37b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#define CHECK_EQU(v1, v2) \
38b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQ(static_cast<int64_t>(v1), static_cast<int64_t>(v2))
39b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
40b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#define NUM_BITS(T) (sizeof(T) * 8)
41b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
42b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
43b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochtemplate <class AtomicType>
44b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochstatic void TestAtomicIncrement() {
45b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // For now, we just test the single-threaded execution.
46b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
47b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Use a guard value to make sure that NoBarrier_AtomicIncrement doesn't
48b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // go outside the expected address bounds.  This is to test that the
49b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // 32-bit NoBarrier_AtomicIncrement doesn't do the wrong thing on 64-bit
50b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // machines.
51b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  struct {
52b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    AtomicType prev_word;
53b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    AtomicType count;
54b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    AtomicType next_word;
55b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  } s;
56b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
57b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  AtomicType prev_word_value, next_word_value;
58b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  memset(&prev_word_value, 0xFF, sizeof(AtomicType));
59b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  memset(&next_word_value, 0xEE, sizeof(AtomicType));
60b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
61b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  s.prev_word = prev_word_value;
62b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  s.count = 0;
63b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  s.next_word = next_word_value;
64b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
65b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(NoBarrier_AtomicIncrement(&s.count, 1), 1);
66b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.count, 1);
67b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.prev_word, prev_word_value);
68b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.next_word, next_word_value);
69b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
70b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(NoBarrier_AtomicIncrement(&s.count, 2), 3);
71b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.count, 3);
72b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.prev_word, prev_word_value);
73b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.next_word, next_word_value);
74b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
75b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(NoBarrier_AtomicIncrement(&s.count, 3), 6);
76b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.count, 6);
77b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.prev_word, prev_word_value);
78b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.next_word, next_word_value);
79b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
80b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(NoBarrier_AtomicIncrement(&s.count, -3), 3);
81b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.count, 3);
82b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.prev_word, prev_word_value);
83b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.next_word, next_word_value);
84b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
85b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(NoBarrier_AtomicIncrement(&s.count, -2), 1);
86b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.count, 1);
87b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.prev_word, prev_word_value);
88b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.next_word, next_word_value);
89b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
90b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(NoBarrier_AtomicIncrement(&s.count, -1), 0);
91b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.count, 0);
92b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.prev_word, prev_word_value);
93b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.next_word, next_word_value);
94b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
95b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(NoBarrier_AtomicIncrement(&s.count, -1), -1);
96b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.count, -1);
97b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.prev_word, prev_word_value);
98b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.next_word, next_word_value);
99b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
100b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(NoBarrier_AtomicIncrement(&s.count, -4), -5);
101b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.count, -5);
102b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.prev_word, prev_word_value);
103b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.next_word, next_word_value);
104b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
105b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(NoBarrier_AtomicIncrement(&s.count, 5), 0);
106b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.count, 0);
107b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.prev_word, prev_word_value);
108b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(s.next_word, next_word_value);
109b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
110b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
111b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
112b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochtemplate <class AtomicType>
113b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochstatic void TestCompareAndSwap() {
114b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  AtomicType value = 0;
115b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  AtomicType prev = NoBarrier_CompareAndSwap(&value, 0, 1);
116b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(1, value);
117b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(0, prev);
118b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
119b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Use a test value that has non-zero bits in both halves, for testing
120b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // the 64-bit implementation on 32-bit platforms.
121b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const AtomicType k_test_val =
122b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      (static_cast<AtomicType>(1) << (NUM_BITS(AtomicType) - 2)) + 11;
123b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  value = k_test_val;
124b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  prev = NoBarrier_CompareAndSwap(&value, 0, 5);
125b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(k_test_val, value);
126b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(k_test_val, prev);
127b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
128b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  value = k_test_val;
129b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  prev = NoBarrier_CompareAndSwap(&value, k_test_val, 5);
130b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(5, value);
131b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(k_test_val, prev);
132b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
133b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
134b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
135b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochtemplate <class AtomicType>
136b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochstatic void TestAtomicExchange() {
137b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  AtomicType value = 0;
138b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  AtomicType new_value = NoBarrier_AtomicExchange(&value, 1);
139b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(1, value);
140b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(0, new_value);
141b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
142b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Use a test value that has non-zero bits in both halves, for testing
143b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // the 64-bit implementation on 32-bit platforms.
144b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const AtomicType k_test_val =
145b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      (static_cast<AtomicType>(1) << (NUM_BITS(AtomicType) - 2)) + 11;
146b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  value = k_test_val;
147b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  new_value = NoBarrier_AtomicExchange(&value, k_test_val);
148b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(k_test_val, value);
149b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(k_test_val, new_value);
150b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
151b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  value = k_test_val;
152b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  new_value = NoBarrier_AtomicExchange(&value, 5);
153b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(5, value);
154b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(k_test_val, new_value);
155b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
156b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
157b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
158b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochtemplate <class AtomicType>
159b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochstatic void TestAtomicIncrementBounds() {
160b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  // Test at 32-bit boundary for 64-bit atomic type.
161014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  AtomicType test_val = static_cast<AtomicType>(1)
162014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                        << (NUM_BITS(AtomicType) / 2);
163014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  AtomicType value = test_val - 1;
164014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  AtomicType new_value = NoBarrier_AtomicIncrement(&value, 1);
165b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(test_val, value);
166b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(value, new_value);
167b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
168b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  NoBarrier_AtomicIncrement(&value, -1);
169b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(test_val - 1, value);
170b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
171b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
172b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
173b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// Return an AtomicType with the value 0xa5a5a5..
174b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochtemplate <class AtomicType>
175b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochstatic AtomicType TestFillValue() {
176b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  AtomicType val = 0;
177b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  memset(&val, 0xa5, sizeof(AtomicType));
178b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return val;
179b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
180b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
181b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
182b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// This is a simple sanity check to ensure that values are correct.
183b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// Not testing atomicity.
184b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochtemplate <class AtomicType>
185b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochstatic void TestStore() {
186b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const AtomicType kVal1 = TestFillValue<AtomicType>();
187b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const AtomicType kVal2 = static_cast<AtomicType>(-1);
188b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
189b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  AtomicType value;
190b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
191b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  NoBarrier_Store(&value, kVal1);
192b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal1, value);
193b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  NoBarrier_Store(&value, kVal2);
194b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal2, value);
195b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
196b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Acquire_Store(&value, kVal1);
197b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal1, value);
198b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Acquire_Store(&value, kVal2);
199b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal2, value);
200b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
201b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Release_Store(&value, kVal1);
202b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal1, value);
203b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Release_Store(&value, kVal2);
204b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal2, value);
205b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
206b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
207b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
208b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// Merge this test with TestStore as soon as we have Atomic8 acquire
209b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// and release stores.
210b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochstatic void TestStoreAtomic8() {
211b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const Atomic8 kVal1 = TestFillValue<Atomic8>();
212b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const Atomic8 kVal2 = static_cast<Atomic8>(-1);
213b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
214b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Atomic8 value;
215b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
216b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  NoBarrier_Store(&value, kVal1);
217b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal1, value);
218b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  NoBarrier_Store(&value, kVal2);
219b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal2, value);
220b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
221b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
222b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
223b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// This is a simple sanity check to ensure that values are correct.
224b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// Not testing atomicity.
225b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochtemplate <class AtomicType>
226b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochstatic void TestLoad() {
227b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const AtomicType kVal1 = TestFillValue<AtomicType>();
228b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const AtomicType kVal2 = static_cast<AtomicType>(-1);
229b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
230b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  AtomicType value;
231b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
232b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  value = kVal1;
233b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal1, NoBarrier_Load(&value));
234b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  value = kVal2;
235b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal2, NoBarrier_Load(&value));
236b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
237b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  value = kVal1;
238b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal1, Acquire_Load(&value));
239b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  value = kVal2;
240b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal2, Acquire_Load(&value));
241b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
242b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  value = kVal1;
243b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal1, Release_Load(&value));
244b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  value = kVal2;
245b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal2, Release_Load(&value));
246b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
247b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
248b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
249b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// Merge this test with TestLoad as soon as we have Atomic8 acquire
250b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// and release loads.
251b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochstatic void TestLoadAtomic8() {
252b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const Atomic8 kVal1 = TestFillValue<Atomic8>();
253b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  const Atomic8 kVal2 = static_cast<Atomic8>(-1);
254b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
255b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  Atomic8 value;
256b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
257b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  value = kVal1;
258b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal1, NoBarrier_Load(&value));
259b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  value = kVal2;
260b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  CHECK_EQU(kVal2, NoBarrier_Load(&value));
261b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
262b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
263b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
264b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochTEST(AtomicIncrement) {
265b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestAtomicIncrement<Atomic32>();
266b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestAtomicIncrement<AtomicWord>();
267b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
268b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
269b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
270b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochTEST(CompareAndSwap) {
271b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestCompareAndSwap<Atomic32>();
272b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestCompareAndSwap<AtomicWord>();
273b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
274b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
275b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
276b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochTEST(AtomicExchange) {
277b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestAtomicExchange<Atomic32>();
278b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestAtomicExchange<AtomicWord>();
279b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
280b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
281b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
282b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochTEST(AtomicIncrementBounds) {
283b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestAtomicIncrementBounds<Atomic32>();
284b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestAtomicIncrementBounds<AtomicWord>();
285b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
286b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
287b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
288b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochTEST(Store) {
289b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestStoreAtomic8();
290b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestStore<Atomic32>();
291b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestStore<AtomicWord>();
292b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
293b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
294b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
295b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochTEST(Load) {
296b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestLoadAtomic8();
297b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestLoad<Atomic32>();
298b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  TestLoad<AtomicWord>();
299b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch}
300