2014-03-10 06:43:21 +00:00
|
|
|
// 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.
|
|
|
|
|
2019-05-24 13:51:59 +00:00
|
|
|
#include "src/init/v8.h"
|
2014-03-10 06:43:21 +00:00
|
|
|
|
2014-06-05 12:14:47 +00:00
|
|
|
#include "src/base/atomicops.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2014-03-10 06:43:21 +00:00
|
|
|
|
2017-08-31 12:34:55 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace base {
|
2014-03-10 06:43:21 +00:00
|
|
|
|
|
|
|
#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.
|
|
|
|
|
2017-05-30 07:44:37 +00:00
|
|
|
// Use a guard value to make sure that Relaxed_AtomicIncrement doesn't
|
2014-03-10 06:43:21 +00:00
|
|
|
// go outside the expected address bounds. This is to test that the
|
2017-05-30 07:44:37 +00:00
|
|
|
// 32-bit Relaxed_AtomicIncrement doesn't do the wrong thing on 64-bit
|
2014-03-10 06:43:21 +00:00
|
|
|
// 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;
|
|
|
|
|
2017-05-30 07:44:37 +00:00
|
|
|
CHECK_EQU(Relaxed_AtomicIncrement(&s.count, 1), 1);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(s.count, 1);
|
|
|
|
CHECK_EQU(s.prev_word, prev_word_value);
|
|
|
|
CHECK_EQU(s.next_word, next_word_value);
|
|
|
|
|
2017-05-30 07:44:37 +00:00
|
|
|
CHECK_EQU(Relaxed_AtomicIncrement(&s.count, 2), 3);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(s.count, 3);
|
|
|
|
CHECK_EQU(s.prev_word, prev_word_value);
|
|
|
|
CHECK_EQU(s.next_word, next_word_value);
|
|
|
|
|
2017-05-30 07:44:37 +00:00
|
|
|
CHECK_EQU(Relaxed_AtomicIncrement(&s.count, 3), 6);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(s.count, 6);
|
|
|
|
CHECK_EQU(s.prev_word, prev_word_value);
|
|
|
|
CHECK_EQU(s.next_word, next_word_value);
|
|
|
|
|
2017-05-30 07:44:37 +00:00
|
|
|
CHECK_EQU(Relaxed_AtomicIncrement(&s.count, -3), 3);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(s.count, 3);
|
|
|
|
CHECK_EQU(s.prev_word, prev_word_value);
|
|
|
|
CHECK_EQU(s.next_word, next_word_value);
|
|
|
|
|
2017-05-30 07:44:37 +00:00
|
|
|
CHECK_EQU(Relaxed_AtomicIncrement(&s.count, -2), 1);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(s.count, 1);
|
|
|
|
CHECK_EQU(s.prev_word, prev_word_value);
|
|
|
|
CHECK_EQU(s.next_word, next_word_value);
|
|
|
|
|
2017-05-30 07:44:37 +00:00
|
|
|
CHECK_EQU(Relaxed_AtomicIncrement(&s.count, -1), 0);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(s.count, 0);
|
|
|
|
CHECK_EQU(s.prev_word, prev_word_value);
|
|
|
|
CHECK_EQU(s.next_word, next_word_value);
|
|
|
|
|
2017-05-30 07:44:37 +00:00
|
|
|
CHECK_EQU(Relaxed_AtomicIncrement(&s.count, -1), -1);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(s.count, -1);
|
|
|
|
CHECK_EQU(s.prev_word, prev_word_value);
|
|
|
|
CHECK_EQU(s.next_word, next_word_value);
|
|
|
|
|
2017-05-30 07:44:37 +00:00
|
|
|
CHECK_EQU(Relaxed_AtomicIncrement(&s.count, -4), -5);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(s.count, -5);
|
|
|
|
CHECK_EQU(s.prev_word, prev_word_value);
|
|
|
|
CHECK_EQU(s.next_word, next_word_value);
|
|
|
|
|
2017-05-30 07:44:37 +00:00
|
|
|
CHECK_EQU(Relaxed_AtomicIncrement(&s.count, 5), 0);
|
2014-03-10 06:43:21 +00:00
|
|
|
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;
|
2017-05-30 07:44:37 +00:00
|
|
|
AtomicType prev = Relaxed_CompareAndSwap(&value, 0, 1);
|
2014-03-10 06:43:21 +00:00
|
|
|
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;
|
2017-05-30 07:44:37 +00:00
|
|
|
prev = Relaxed_CompareAndSwap(&value, 0, 5);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(k_test_val, value);
|
|
|
|
CHECK_EQU(k_test_val, prev);
|
|
|
|
|
|
|
|
value = k_test_val;
|
2017-05-30 07:44:37 +00:00
|
|
|
prev = Relaxed_CompareAndSwap(&value, k_test_val, 5);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(5, value);
|
|
|
|
CHECK_EQU(k_test_val, prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class AtomicType>
|
|
|
|
static void TestAtomicExchange() {
|
|
|
|
AtomicType value = 0;
|
2017-05-30 07:44:37 +00:00
|
|
|
AtomicType new_value = Relaxed_AtomicExchange(&value, 1);
|
2014-03-10 06:43:21 +00:00
|
|
|
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;
|
2017-05-30 07:44:37 +00:00
|
|
|
new_value = Relaxed_AtomicExchange(&value, k_test_val);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(k_test_val, value);
|
|
|
|
CHECK_EQU(k_test_val, new_value);
|
|
|
|
|
|
|
|
value = k_test_val;
|
2017-05-30 07:44:37 +00:00
|
|
|
new_value = Relaxed_AtomicExchange(&value, 5);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(5, value);
|
|
|
|
CHECK_EQU(k_test_val, new_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class AtomicType>
|
|
|
|
static void TestAtomicIncrementBounds() {
|
2015-10-08 04:33:24 +00:00
|
|
|
// Test at 32-bit boundary for 64-bit atomic type.
|
2015-10-08 19:02:29 +00:00
|
|
|
AtomicType test_val = static_cast<AtomicType>(1)
|
|
|
|
<< (NUM_BITS(AtomicType) / 2);
|
|
|
|
AtomicType value = test_val - 1;
|
2017-05-30 07:44:37 +00:00
|
|
|
AtomicType new_value = Relaxed_AtomicIncrement(&value, 1);
|
2015-10-08 04:33:24 +00:00
|
|
|
CHECK_EQU(test_val, value);
|
|
|
|
CHECK_EQU(value, new_value);
|
|
|
|
|
2017-05-30 07:44:37 +00:00
|
|
|
Relaxed_AtomicIncrement(&value, -1);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(test_val - 1, value);
|
|
|
|
}
|
|
|
|
|
2017-12-02 00:30:37 +00:00
|
|
|
// Return an AtomicType with the value 0xA5A5A5..
|
2014-03-10 06:43:21 +00:00
|
|
|
template <class AtomicType>
|
|
|
|
static AtomicType TestFillValue() {
|
|
|
|
AtomicType val = 0;
|
2017-12-02 00:30:37 +00:00
|
|
|
memset(&val, 0xA5, sizeof(AtomicType));
|
2014-03-10 06:43:21 +00:00
|
|
|
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;
|
|
|
|
|
2017-05-30 07:44:37 +00:00
|
|
|
Relaxed_Store(&value, kVal1);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(kVal1, value);
|
2017-05-30 07:44:37 +00:00
|
|
|
Relaxed_Store(&value, kVal2);
|
2014-03-10 06:43:21 +00:00
|
|
|
CHECK_EQU(kVal2, value);
|
|
|
|
|
|
|
|
Release_Store(&value, kVal1);
|
|
|
|
CHECK_EQU(kVal1, value);
|
|
|
|
Release_Store(&value, kVal2);
|
|
|
|
CHECK_EQU(kVal2, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-09 08:20:10 +00:00
|
|
|
// 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;
|
|
|
|
|
2017-05-30 07:44:37 +00:00
|
|
|
Relaxed_Store(&value, kVal1);
|
2014-04-09 08:20:10 +00:00
|
|
|
CHECK_EQU(kVal1, value);
|
2017-05-30 07:44:37 +00:00
|
|
|
Relaxed_Store(&value, kVal2);
|
2014-04-09 08:20:10 +00:00
|
|
|
CHECK_EQU(kVal2, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-10 06:43:21 +00:00
|
|
|
// 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;
|
2017-05-30 07:44:37 +00:00
|
|
|
CHECK_EQU(kVal1, Relaxed_Load(&value));
|
2014-03-10 06:43:21 +00:00
|
|
|
value = kVal2;
|
2017-05-30 07:44:37 +00:00
|
|
|
CHECK_EQU(kVal2, Relaxed_Load(&value));
|
2014-03-10 06:43:21 +00:00
|
|
|
|
|
|
|
value = kVal1;
|
|
|
|
CHECK_EQU(kVal1, Acquire_Load(&value));
|
|
|
|
value = kVal2;
|
|
|
|
CHECK_EQU(kVal2, Acquire_Load(&value));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-09 08:20:10 +00:00
|
|
|
// 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;
|
2017-05-30 07:44:37 +00:00
|
|
|
CHECK_EQU(kVal1, Relaxed_Load(&value));
|
2014-04-09 08:20:10 +00:00
|
|
|
value = kVal2;
|
2017-05-30 07:44:37 +00:00
|
|
|
CHECK_EQU(kVal2, Relaxed_Load(&value));
|
2014-04-09 08:20:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-10 06:43:21 +00:00
|
|
|
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) {
|
2014-04-09 08:20:10 +00:00
|
|
|
TestStoreAtomic8();
|
2014-03-10 06:43:21 +00:00
|
|
|
TestStore<Atomic32>();
|
|
|
|
TestStore<AtomicWord>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Load) {
|
2014-04-09 08:20:10 +00:00
|
|
|
TestLoadAtomic8();
|
2014-03-10 06:43:21 +00:00
|
|
|
TestLoad<Atomic32>();
|
|
|
|
TestLoad<AtomicWord>();
|
|
|
|
}
|
2017-08-31 12:34:55 +00:00
|
|
|
|
|
|
|
} // namespace base
|
|
|
|
} // namespace v8
|