2014-07-23 07:54:41 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-04-25 23:10:38 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Atomics.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
using mozilla::Atomic;
|
|
|
|
using mozilla::MemoryOrdering;
|
|
|
|
using mozilla::Relaxed;
|
|
|
|
using mozilla::ReleaseAcquire;
|
|
|
|
using mozilla::SequentiallyConsistent;
|
2018-07-21 17:20:13 +03:00
|
|
|
using mozilla::recordreplay::Behavior;
|
2013-04-25 23:10:38 +04:00
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
#define A(a, b) MOZ_RELEASE_ASSERT(a, b)
|
|
|
|
|
2013-04-25 23:10:38 +04:00
|
|
|
template <typename T, MemoryOrdering Order>
|
|
|
|
static void TestTypeWithOrdering() {
|
2018-07-21 17:20:13 +03:00
|
|
|
Atomic<T, Order, Behavior::DontPreserve> atomic(5);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == 5, "Atomic variable did not initialize");
|
2013-04-25 23:10:38 +04:00
|
|
|
|
|
|
|
// Test atomic increment
|
2014-07-23 07:54:41 +04:00
|
|
|
A(++atomic == T(6), "Atomic increment did not work");
|
|
|
|
A(atomic++ == T(6), "Atomic post-increment did not work");
|
|
|
|
A(atomic == T(7), "Atomic post-increment did not work");
|
2013-04-25 23:10:38 +04:00
|
|
|
|
|
|
|
// Test atomic decrement
|
2014-07-23 07:54:41 +04:00
|
|
|
A(--atomic == 6, "Atomic decrement did not work");
|
|
|
|
A(atomic-- == 6, "Atomic post-decrement did not work");
|
|
|
|
A(atomic == 5, "Atomic post-decrement did not work");
|
2013-04-25 23:10:38 +04:00
|
|
|
|
|
|
|
// Test other arithmetic.
|
2014-04-25 01:06:50 +04:00
|
|
|
T result;
|
2013-04-25 23:10:38 +04:00
|
|
|
result = (atomic += T(5));
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == T(10), "Atomic += did not work");
|
|
|
|
A(result == T(10), "Atomic += returned the wrong value");
|
2013-04-25 23:10:38 +04:00
|
|
|
result = (atomic -= T(3));
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == T(7), "Atomic -= did not work");
|
|
|
|
A(result == T(7), "Atomic -= returned the wrong value");
|
2013-04-25 23:10:38 +04:00
|
|
|
|
|
|
|
// Test assignment
|
|
|
|
result = (atomic = T(5));
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == T(5), "Atomic assignment failed");
|
|
|
|
A(result == T(5), "Atomic assignment returned the wrong value");
|
2013-04-25 23:10:38 +04:00
|
|
|
|
|
|
|
// Test logical operations.
|
|
|
|
result = (atomic ^= T(2));
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == T(7), "Atomic ^= did not work");
|
|
|
|
A(result == T(7), "Atomic ^= returned the wrong value");
|
2013-04-25 23:10:38 +04:00
|
|
|
result = (atomic ^= T(4));
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == T(3), "Atomic ^= did not work");
|
|
|
|
A(result == T(3), "Atomic ^= returned the wrong value");
|
2013-04-25 23:10:38 +04:00
|
|
|
result = (atomic |= T(8));
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == T(11), "Atomic |= did not work");
|
|
|
|
A(result == T(11), "Atomic |= returned the wrong value");
|
2013-04-25 23:10:38 +04:00
|
|
|
result = (atomic |= T(8));
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == T(11), "Atomic |= did not work");
|
|
|
|
A(result == T(11), "Atomic |= returned the wrong value");
|
2013-04-25 23:10:38 +04:00
|
|
|
result = (atomic &= T(12));
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == T(8), "Atomic &= did not work");
|
|
|
|
A(result == T(8), "Atomic &= returned the wrong value");
|
2013-04-25 23:10:38 +04:00
|
|
|
|
|
|
|
// Test exchange.
|
|
|
|
atomic = T(30);
|
|
|
|
result = atomic.exchange(42);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == T(42), "Atomic exchange did not work");
|
|
|
|
A(result == T(30), "Atomic exchange returned the wrong value");
|
2013-05-24 21:10:47 +04:00
|
|
|
|
|
|
|
// Test CAS.
|
|
|
|
atomic = T(1);
|
2014-04-25 01:06:50 +04:00
|
|
|
bool boolResult = atomic.compareExchange(0, 2);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(!boolResult, "CAS should have returned false.");
|
|
|
|
A(atomic == T(1), "CAS shouldn't have done anything.");
|
2013-05-24 21:10:47 +04:00
|
|
|
|
|
|
|
boolResult = atomic.compareExchange(1, 42);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(boolResult, "CAS should have succeeded.");
|
|
|
|
A(atomic == T(42), "CAS should have changed atomic's value.");
|
2013-04-25 23:10:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, MemoryOrdering Order>
|
|
|
|
static void TestPointerWithOrdering() {
|
|
|
|
T array1[10];
|
2018-07-21 17:20:13 +03:00
|
|
|
Atomic<T*, Order, Behavior::DontPreserve> atomic(array1);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == array1, "Atomic variable did not initialize");
|
2013-04-25 23:10:38 +04:00
|
|
|
|
|
|
|
// Test atomic increment
|
2014-07-23 07:54:41 +04:00
|
|
|
A(++atomic == array1 + 1, "Atomic increment did not work");
|
|
|
|
A(atomic++ == array1 + 1, "Atomic post-increment did not work");
|
|
|
|
A(atomic == array1 + 2, "Atomic post-increment did not work");
|
2013-04-25 23:10:38 +04:00
|
|
|
|
|
|
|
// Test atomic decrement
|
2014-07-23 07:54:41 +04:00
|
|
|
A(--atomic == array1 + 1, "Atomic decrement did not work");
|
|
|
|
A(atomic-- == array1 + 1, "Atomic post-decrement did not work");
|
|
|
|
A(atomic == array1, "Atomic post-decrement did not work");
|
2013-04-25 23:10:38 +04:00
|
|
|
|
|
|
|
// Test other arithmetic operations
|
2014-04-25 01:06:50 +04:00
|
|
|
T* result;
|
2013-04-25 23:10:38 +04:00
|
|
|
result = (atomic += 2);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == array1 + 2, "Atomic += did not work");
|
|
|
|
A(result == array1 + 2, "Atomic += returned the wrong value");
|
2013-04-25 23:10:38 +04:00
|
|
|
result = (atomic -= 1);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == array1 + 1, "Atomic -= did not work");
|
|
|
|
A(result == array1 + 1, "Atomic -= returned the wrong value");
|
2013-04-25 23:10:38 +04:00
|
|
|
|
|
|
|
// Test stores
|
|
|
|
result = (atomic = array1);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == array1, "Atomic assignment did not work");
|
|
|
|
A(result == array1, "Atomic assignment returned the wrong value");
|
2013-04-25 23:10:38 +04:00
|
|
|
|
|
|
|
// Test exchange
|
|
|
|
atomic = array1 + 2;
|
|
|
|
result = atomic.exchange(array1);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == array1, "Atomic exchange did not work");
|
|
|
|
A(result == array1 + 2, "Atomic exchange returned the wrong value");
|
2013-05-24 21:10:47 +04:00
|
|
|
|
|
|
|
atomic = array1;
|
2014-04-25 01:06:50 +04:00
|
|
|
bool boolResult = atomic.compareExchange(array1 + 1, array1 + 2);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(!boolResult, "CAS should have returned false.");
|
|
|
|
A(atomic == array1, "CAS shouldn't have done anything.");
|
2013-05-24 21:10:47 +04:00
|
|
|
|
|
|
|
boolResult = atomic.compareExchange(array1, array1 + 3);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(boolResult, "CAS should have succeeded.");
|
|
|
|
A(atomic == array1 + 3, "CAS should have changed atomic's value.");
|
2013-04-25 23:10:38 +04:00
|
|
|
}
|
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
enum EnumType {
|
2013-08-02 05:21:32 +04:00
|
|
|
EnumType_0 = 0,
|
|
|
|
EnumType_1 = 1,
|
|
|
|
EnumType_2 = 2,
|
|
|
|
EnumType_3 = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
template <MemoryOrdering Order>
|
|
|
|
static void TestEnumWithOrdering() {
|
2018-07-21 17:20:13 +03:00
|
|
|
Atomic<EnumType, Order, Behavior::DontPreserve> atomic(EnumType_2);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == EnumType_2, "Atomic variable did not initialize");
|
2013-08-02 05:21:32 +04:00
|
|
|
|
|
|
|
// Test assignment
|
2014-04-25 01:06:50 +04:00
|
|
|
EnumType result;
|
2013-08-02 05:21:32 +04:00
|
|
|
result = (atomic = EnumType_3);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == EnumType_3, "Atomic assignment failed");
|
|
|
|
A(result == EnumType_3, "Atomic assignment returned the wrong value");
|
2013-08-02 05:21:32 +04:00
|
|
|
|
|
|
|
// Test exchange.
|
|
|
|
atomic = EnumType_1;
|
|
|
|
result = atomic.exchange(EnumType_2);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == EnumType_2, "Atomic exchange did not work");
|
|
|
|
A(result == EnumType_1, "Atomic exchange returned the wrong value");
|
2013-08-02 05:21:32 +04:00
|
|
|
|
|
|
|
// Test CAS.
|
|
|
|
atomic = EnumType_1;
|
2014-04-25 01:06:50 +04:00
|
|
|
bool boolResult = atomic.compareExchange(EnumType_0, EnumType_2);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(!boolResult, "CAS should have returned false.");
|
|
|
|
A(atomic == EnumType_1, "CAS shouldn't have done anything.");
|
2013-08-02 05:21:32 +04:00
|
|
|
|
|
|
|
boolResult = atomic.compareExchange(EnumType_1, EnumType_3);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(boolResult, "CAS should have succeeded.");
|
|
|
|
A(atomic == EnumType_3, "CAS should have changed atomic's value.");
|
2013-08-02 05:21:32 +04:00
|
|
|
}
|
|
|
|
|
2015-11-04 00:03:26 +03:00
|
|
|
enum class EnumClass : uint32_t {
|
|
|
|
Value0 = 0,
|
|
|
|
Value1 = 1,
|
|
|
|
Value2 = 2,
|
|
|
|
Value3 = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
template <MemoryOrdering Order>
|
|
|
|
static void TestEnumClassWithOrdering() {
|
2018-07-21 17:20:13 +03:00
|
|
|
Atomic<EnumClass, Order, Behavior::DontPreserve> atomic(EnumClass::Value2);
|
2015-11-04 00:03:26 +03:00
|
|
|
A(atomic == EnumClass::Value2, "Atomic variable did not initialize");
|
|
|
|
|
|
|
|
// Test assignment
|
|
|
|
EnumClass result;
|
|
|
|
result = (atomic = EnumClass::Value3);
|
|
|
|
A(atomic == EnumClass::Value3, "Atomic assignment failed");
|
|
|
|
A(result == EnumClass::Value3, "Atomic assignment returned the wrong value");
|
|
|
|
|
|
|
|
// Test exchange.
|
|
|
|
atomic = EnumClass::Value1;
|
|
|
|
result = atomic.exchange(EnumClass::Value2);
|
|
|
|
A(atomic == EnumClass::Value2, "Atomic exchange did not work");
|
|
|
|
A(result == EnumClass::Value1, "Atomic exchange returned the wrong value");
|
|
|
|
|
|
|
|
// Test CAS.
|
|
|
|
atomic = EnumClass::Value1;
|
|
|
|
bool boolResult =
|
|
|
|
atomic.compareExchange(EnumClass::Value0, EnumClass::Value2);
|
|
|
|
A(!boolResult, "CAS should have returned false.");
|
|
|
|
A(atomic == EnumClass::Value1, "CAS shouldn't have done anything.");
|
|
|
|
|
|
|
|
boolResult = atomic.compareExchange(EnumClass::Value1, EnumClass::Value3);
|
|
|
|
A(boolResult, "CAS should have succeeded.");
|
|
|
|
A(atomic == EnumClass::Value3, "CAS should have changed atomic's value.");
|
|
|
|
}
|
|
|
|
|
2014-02-06 23:57:30 +04:00
|
|
|
template <MemoryOrdering Order>
|
|
|
|
static void TestBoolWithOrdering() {
|
2018-07-21 17:20:13 +03:00
|
|
|
Atomic<bool, Order, Behavior::DontPreserve> atomic(false);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == false, "Atomic variable did not initialize");
|
2014-02-06 23:57:30 +04:00
|
|
|
|
|
|
|
// Test assignment
|
2014-04-25 01:06:50 +04:00
|
|
|
bool result;
|
2014-02-06 23:57:30 +04:00
|
|
|
result = (atomic = true);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == true, "Atomic assignment failed");
|
|
|
|
A(result == true, "Atomic assignment returned the wrong value");
|
2014-02-06 23:57:30 +04:00
|
|
|
|
|
|
|
// Test exchange.
|
|
|
|
atomic = false;
|
|
|
|
result = atomic.exchange(true);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(atomic == true, "Atomic exchange did not work");
|
|
|
|
A(result == false, "Atomic exchange returned the wrong value");
|
2014-02-06 23:57:30 +04:00
|
|
|
|
|
|
|
// Test CAS.
|
|
|
|
atomic = false;
|
2014-04-25 01:06:50 +04:00
|
|
|
bool boolResult = atomic.compareExchange(true, false);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(!boolResult, "CAS should have returned false.");
|
|
|
|
A(atomic == false, "CAS shouldn't have done anything.");
|
2014-02-06 23:57:30 +04:00
|
|
|
|
|
|
|
boolResult = atomic.compareExchange(false, true);
|
2014-07-23 07:54:41 +04:00
|
|
|
A(boolResult, "CAS should have succeeded.");
|
|
|
|
A(atomic == true, "CAS should have changed atomic's value.");
|
2014-02-06 23:57:30 +04:00
|
|
|
}
|
|
|
|
|
2013-04-25 23:10:38 +04:00
|
|
|
template <typename T>
|
|
|
|
static void TestType() {
|
|
|
|
TestTypeWithOrdering<T, SequentiallyConsistent>();
|
|
|
|
TestTypeWithOrdering<T, ReleaseAcquire>();
|
|
|
|
TestTypeWithOrdering<T, Relaxed>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void TestPointer() {
|
|
|
|
TestPointerWithOrdering<T, SequentiallyConsistent>();
|
|
|
|
TestPointerWithOrdering<T, ReleaseAcquire>();
|
|
|
|
TestPointerWithOrdering<T, Relaxed>();
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:21:32 +04:00
|
|
|
static void TestEnum() {
|
|
|
|
TestEnumWithOrdering<SequentiallyConsistent>();
|
|
|
|
TestEnumWithOrdering<ReleaseAcquire>();
|
|
|
|
TestEnumWithOrdering<Relaxed>();
|
2015-11-04 00:03:26 +03:00
|
|
|
|
|
|
|
TestEnumClassWithOrdering<SequentiallyConsistent>();
|
|
|
|
TestEnumClassWithOrdering<ReleaseAcquire>();
|
|
|
|
TestEnumClassWithOrdering<Relaxed>();
|
2013-08-02 05:21:32 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 23:57:30 +04:00
|
|
|
static void TestBool() {
|
|
|
|
TestBoolWithOrdering<SequentiallyConsistent>();
|
|
|
|
TestBoolWithOrdering<ReleaseAcquire>();
|
|
|
|
TestBoolWithOrdering<Relaxed>();
|
|
|
|
}
|
|
|
|
|
2014-07-23 07:54:41 +04:00
|
|
|
#undef A
|
|
|
|
|
|
|
|
int main() {
|
2013-04-25 23:10:38 +04:00
|
|
|
TestType<uint32_t>();
|
|
|
|
TestType<int32_t>();
|
2015-04-18 04:40:52 +03:00
|
|
|
TestType<uint64_t>();
|
|
|
|
TestType<int64_t>();
|
2013-04-25 23:10:38 +04:00
|
|
|
TestType<intptr_t>();
|
|
|
|
TestType<uintptr_t>();
|
|
|
|
TestPointer<int>();
|
|
|
|
TestPointer<float>();
|
|
|
|
TestPointer<uint16_t*>();
|
|
|
|
TestPointer<uint32_t*>();
|
2013-08-02 05:21:32 +04:00
|
|
|
TestEnum();
|
2014-02-06 23:57:30 +04:00
|
|
|
TestBool();
|
2014-07-23 07:54:41 +04:00
|
|
|
return 0;
|
2013-04-25 23:10:38 +04:00
|
|
|
}
|