[libcxx] Allow use of <atomic> in C++03. Try 3.

Summary:
After putting this question up on cfe-dev I have decided that it would be best to allow the use of `<atomic>` in C++03. Although static initialization is a concern the syntax required to get it is C++11 only. Meaning that C++11 constant static initialization cannot silently break in C++03, it will always cause a syntax error. Furthermore `ATOMIC_VAR_INIT` and `ATOMIC_FLAG_INIT` remain defined in C++03 even though they cannot be used because C++03 usages will cause better error messages.

The main change in this patch is to replace `__has_feature(cxx_atomic)`, which only returns true when C++ >= 11, to `__has_extension(c_atomic)` which returns true whenever clang supports the required atomic builtins.


This patch adds the following macros:
* `_LIBCPP_HAS_C_ATOMIC_IMP`      - Defined on clang versions which provide the C `_Atomic` keyword.
* `_LIBCPP_HAS_GCC_ATOMIC_IMP` - Defined on GCC > 4.7. We must use the fallback atomic implementation.
* `_LIBCPP_HAS_NO_ATOMIC_HEADER` - Defined when it is not safe to include `<atomic>`.

`_LIBCPP_HAS_C_ATOMIC_IMP` and `_LIBCPP_HAS_GCC_ATOMIC_IMP` are mutually exclusive, only one should be defined. If neither is defined then `<atomic>` is not implemented and including `<atomic>` will issue an error.

Reviewers: chandlerc, jroelofs, mclow.lists

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D11555

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@245463 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier
2015-08-19 17:21:46 +00:00
parent 8966350d61
commit 00f4a49b0b
37 changed files with 363 additions and 757 deletions

View File

@@ -22,13 +22,13 @@
int main()
{
{
std::atomic_flag f = ATOMIC_FLAG_INIT;
std::atomic_flag f(false);
f.test_and_set();
atomic_flag_clear(&f);
assert(f.test_and_set() == 0);
}
{
volatile std::atomic_flag f = ATOMIC_FLAG_INIT;
volatile std::atomic_flag f(false);
f.test_and_set();
atomic_flag_clear(&f);
assert(f.test_and_set() == 0);

View File

@@ -22,37 +22,37 @@
int main()
{
{
std::atomic_flag f = ATOMIC_FLAG_INIT;
std::atomic_flag f(false);
f.test_and_set();
atomic_flag_clear_explicit(&f, std::memory_order_relaxed);
assert(f.test_and_set() == 0);
}
{
std::atomic_flag f = ATOMIC_FLAG_INIT;
std::atomic_flag f(false);
f.test_and_set();
atomic_flag_clear_explicit(&f, std::memory_order_release);
assert(f.test_and_set() == 0);
}
{
std::atomic_flag f = ATOMIC_FLAG_INIT;
std::atomic_flag f(false);
f.test_and_set();
atomic_flag_clear_explicit(&f, std::memory_order_seq_cst);
assert(f.test_and_set() == 0);
}
{
volatile std::atomic_flag f = ATOMIC_FLAG_INIT;
volatile std::atomic_flag f(false);
f.test_and_set();
atomic_flag_clear_explicit(&f, std::memory_order_relaxed);
assert(f.test_and_set() == 0);
}
{
volatile std::atomic_flag f = ATOMIC_FLAG_INIT;
volatile std::atomic_flag f(false);
f.test_and_set();
atomic_flag_clear_explicit(&f, std::memory_order_release);
assert(f.test_and_set() == 0);
}
{
volatile std::atomic_flag f = ATOMIC_FLAG_INIT;
volatile std::atomic_flag f(false);
f.test_and_set();
atomic_flag_clear_explicit(&f, std::memory_order_seq_cst);
assert(f.test_and_set() == 0);

View File

@@ -22,49 +22,49 @@
int main()
{
{
std::atomic_flag f = ATOMIC_FLAG_INIT;
std::atomic_flag f(false);
f.test_and_set();
f.clear();
assert(f.test_and_set() == 0);
}
{
std::atomic_flag f = ATOMIC_FLAG_INIT;
std::atomic_flag f(false);
f.test_and_set();
f.clear(std::memory_order_relaxed);
assert(f.test_and_set() == 0);
}
{
std::atomic_flag f = ATOMIC_FLAG_INIT;
std::atomic_flag f(false);
f.test_and_set();
f.clear(std::memory_order_release);
assert(f.test_and_set() == 0);
}
{
std::atomic_flag f = ATOMIC_FLAG_INIT;
std::atomic_flag f(false);
f.test_and_set();
f.clear(std::memory_order_seq_cst);
assert(f.test_and_set() == 0);
}
{
volatile std::atomic_flag f = ATOMIC_FLAG_INIT;
volatile std::atomic_flag f(false);
f.test_and_set();
f.clear();
assert(f.test_and_set() == 0);
}
{
volatile std::atomic_flag f = ATOMIC_FLAG_INIT;
volatile std::atomic_flag f(false);
f.test_and_set();
f.clear(std::memory_order_relaxed);
assert(f.test_and_set() == 0);
}
{
volatile std::atomic_flag f = ATOMIC_FLAG_INIT;
volatile std::atomic_flag f(false);
f.test_and_set();
f.clear(std::memory_order_release);
assert(f.test_and_set() == 0);
}
{
volatile std::atomic_flag f = ATOMIC_FLAG_INIT;
volatile std::atomic_flag f(false);
f.test_and_set();
f.clear(std::memory_order_seq_cst);
assert(f.test_and_set() == 0);

View File

@@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// XFAIL: c++98, c++03
// <atomic>

View File

@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// <atomic>
// struct atomic_flag
// TESTING EXTENSION atomic_flag(bool)
#include <atomic>
#include <cassert>
int main()
{
std::atomic_flag f(false);
assert(f.test_and_set() == 0);
}

View File

@@ -24,10 +24,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A a;
@@ -52,37 +53,10 @@ test()
assert(a == T(2));
assert(t == T(2));
}
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
int main()
{
test<A>();
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
test<int*>();
test<const int*>();
TestEachAtomicType<TestFn>()();
}

View File

@@ -27,10 +27,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A a;
@@ -59,37 +60,10 @@ test()
assert(a == T(2));
assert(t == T(2));
}
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
int main()
{
test<A>();
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
test<int*>();
test<const int*>();
TestEachAtomicType<TestFn>()();
}

View File

@@ -25,11 +25,11 @@
#include <cassert>
#include <cmpxchg_loop.h>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A a;
@@ -54,37 +54,10 @@ test()
assert(a == T(2));
assert(t == T(2));
}
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
int main()
{
test<A>();
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
test<int*>();
test<const int*>();
TestEachAtomicType<TestFn>()();
}

View File

@@ -29,10 +29,11 @@
#include <cmpxchg_loop.h>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A a;
@@ -61,37 +62,10 @@ test()
assert(a == T(2));
assert(t == T(2));
}
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
int main()
{
test<A>();
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
test<int*>();
test<const int*>();
TestEachAtomicType<TestFn>()();
}

View File

@@ -24,10 +24,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
typedef std::atomic<T> A;
A t;
std::atomic_init(&t, T(1));
@@ -37,37 +38,11 @@ test()
std::atomic_init(&vt, T(3));
assert(std::atomic_exchange(&vt, T(4)) == T(3));
assert(vt == T(4));
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
int main()
{
test<A>();
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
test<int*>();
test<const int*>();
TestEachAtomicType<TestFn>()();
}

View File

@@ -24,10 +24,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
typedef std::atomic<T> A;
A t;
std::atomic_init(&t, T(1));
@@ -39,37 +40,11 @@ test()
assert(std::atomic_exchange_explicit(&vt, T(4), std::memory_order_seq_cst)
== T(3));
assert(vt == T(4));
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
int main()
{
test<A>();
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
test<int*>();
test<const int*>();
TestEachAtomicType<TestFn>()();
}

View File

@@ -32,10 +32,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A t;
@@ -50,62 +51,33 @@ test()
assert(std::atomic_fetch_add(&t, T(2)) == T(1));
assert(t == T(3));
}
}
template <class T>
void
testp()
{
{
typedef std::atomic<T> A;
typedef typename std::remove_pointer<T>::type X;
A t;
std::atomic_init(&t, T(1*sizeof(X)));
assert(std::atomic_fetch_add(&t, 2) == T(1*sizeof(X)));
assert(t == T(3*sizeof(X)));
}
{
typedef std::atomic<T> A;
typedef typename std::remove_pointer<T>::type X;
volatile A t;
std::atomic_init(&t, T(1*sizeof(X)));
assert(std::atomic_fetch_add(&t, 2) == T(1*sizeof(X)));
assert(t == T(3*sizeof(X)));
}
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
A(const A& a) : i(a.i) {}
A(const volatile A& a) : i(a.i) {}
void operator=(const volatile A& a) volatile {i = a.i;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
template <class T>
void testp()
{
{
typedef std::atomic<T> A;
typedef typename std::remove_pointer<T>::type X;
A t;
std::atomic_init(&t, T(1*sizeof(X)));
assert(std::atomic_fetch_add(&t, 2) == T(1*sizeof(X)));
assert(t == T(3*sizeof(X)));
}
{
typedef std::atomic<T> A;
typedef typename std::remove_pointer<T>::type X;
volatile A t;
std::atomic_init(&t, T(1*sizeof(X)));
assert(std::atomic_fetch_add(&t, 2) == T(1*sizeof(X)));
assert(t == T(3*sizeof(X)));
}
}
int main()
{
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
TestEachIntegralType<TestFn>()();
testp<int*>();
testp<const int*>();
}

View File

@@ -32,10 +32,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A t;
@@ -52,7 +53,8 @@ test()
std::memory_order_seq_cst) == T(1));
assert(t == T(3));
}
}
}
};
template <class T>
void
@@ -78,38 +80,9 @@ testp()
}
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
A(const A& a) : i(a.i) {}
A(const volatile A& a) : i(a.i) {}
void operator=(const volatile A& a) volatile {i = a.i;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
};
int main()
{
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
TestEachIntegralType<TestFn>()();
testp<int*>();
testp<const int*>();
}

View File

@@ -23,10 +23,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A t;
@@ -41,24 +42,10 @@ test()
assert(std::atomic_fetch_and(&t, T(2)) == T(3));
assert(t == T(2));
}
}
}
};
int main()
{
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
TestEachIntegralType<TestFn>()();
}

View File

@@ -23,10 +23,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A t;
@@ -43,24 +44,10 @@ test()
std::memory_order_seq_cst) == T(3));
assert(t == T(2));
}
}
}
};
int main()
{
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
TestEachIntegralType<TestFn>()();
}

View File

@@ -23,10 +23,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A t;
@@ -41,24 +42,10 @@ test()
assert(std::atomic_fetch_or(&t, T(2)) == T(3));
assert(t == T(3));
}
}
}
};
int main()
{
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
TestEachIntegralType<TestFn>()();
}

View File

@@ -23,10 +23,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A t;
@@ -43,24 +44,10 @@ test()
std::memory_order_seq_cst) == T(3));
assert(t == T(3));
}
}
}
};
int main()
{
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
TestEachIntegralType<TestFn>()();
}

View File

@@ -32,10 +32,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A t;
@@ -50,62 +51,33 @@ test()
assert(std::atomic_fetch_sub(&t, T(2)) == T(3));
assert(t == T(1));
}
}
template <class T>
void
testp()
{
{
typedef std::atomic<T> A;
typedef typename std::remove_pointer<T>::type X;
A t;
std::atomic_init(&t, T(3*sizeof(X)));
assert(std::atomic_fetch_sub(&t, 2) == T(3*sizeof(X)));
assert(t == T(1*sizeof(X)));
}
{
typedef std::atomic<T> A;
typedef typename std::remove_pointer<T>::type X;
volatile A t;
std::atomic_init(&t, T(3*sizeof(X)));
assert(std::atomic_fetch_sub(&t, 2) == T(3*sizeof(X)));
assert(t == T(1*sizeof(X)));
}
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
A(const A& a) : i(a.i) {}
A(const volatile A& a) : i(a.i) {}
void operator=(const volatile A& a) volatile {i = a.i;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
template <class T>
void testp()
{
{
typedef std::atomic<T> A;
typedef typename std::remove_pointer<T>::type X;
A t;
std::atomic_init(&t, T(3*sizeof(X)));
assert(std::atomic_fetch_sub(&t, 2) == T(3*sizeof(X)));
assert(t == T(1*sizeof(X)));
}
{
typedef std::atomic<T> A;
typedef typename std::remove_pointer<T>::type X;
volatile A t;
std::atomic_init(&t, T(3*sizeof(X)));
assert(std::atomic_fetch_sub(&t, 2) == T(3*sizeof(X)));
assert(t == T(1*sizeof(X)));
}
}
int main()
{
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
TestEachIntegralType<TestFn>()();
testp<int*>();
testp<const int*>();
}

View File

@@ -33,10 +33,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A t;
@@ -53,64 +54,35 @@ test()
std::memory_order_seq_cst) == T(3));
assert(t == T(1));
}
}
template <class T>
void
testp()
{
{
typedef std::atomic<T> A;
typedef typename std::remove_pointer<T>::type X;
A t;
std::atomic_init(&t, T(3*sizeof(X)));
assert(std::atomic_fetch_sub_explicit(&t, 2,
std::memory_order_seq_cst) == T(3*sizeof(X)));
assert(t == T(1*sizeof(X)));
}
{
typedef std::atomic<T> A;
typedef typename std::remove_pointer<T>::type X;
volatile A t;
std::atomic_init(&t, T(3*sizeof(X)));
assert(std::atomic_fetch_sub_explicit(&t, 2,
std::memory_order_seq_cst) == T(3*sizeof(X)));
assert(t == T(1*sizeof(X)));
}
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
A(const A& a) : i(a.i) {}
A(const volatile A& a) : i(a.i) {}
void operator=(const volatile A& a) volatile {i = a.i;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
template <class T>
void testp()
{
{
typedef std::atomic<T> A;
typedef typename std::remove_pointer<T>::type X;
A t;
std::atomic_init(&t, T(3*sizeof(X)));
assert(std::atomic_fetch_sub_explicit(&t, 2,
std::memory_order_seq_cst) == T(3*sizeof(X)));
assert(t == T(1*sizeof(X)));
}
{
typedef std::atomic<T> A;
typedef typename std::remove_pointer<T>::type X;
volatile A t;
std::atomic_init(&t, T(3*sizeof(X)));
assert(std::atomic_fetch_sub_explicit(&t, 2,
std::memory_order_seq_cst) == T(3*sizeof(X)));
assert(t == T(1*sizeof(X)));
}
}
int main()
{
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
TestEachIntegralType<TestFn>()();
testp<int*>();
testp<const int*>();
}

View File

@@ -23,10 +23,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A t;
@@ -41,24 +42,10 @@ test()
assert(std::atomic_fetch_xor(&t, T(2)) == T(3));
assert(t == T(1));
}
}
}
};
int main()
{
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
TestEachIntegralType<TestFn>()();
}

View File

@@ -23,10 +23,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
{
typedef std::atomic<T> A;
A t;
@@ -43,24 +44,10 @@ test()
std::memory_order_seq_cst) == T(3));
assert(t == T(1));
}
}
}
};
int main()
{
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
TestEachIntegralType<TestFn>()();
}

View File

@@ -0,0 +1,51 @@
#ifndef ATOMIC_HELPERS_H
#define ATOMIC_HELPERS_H
#include <cassert>
#include "test_macros.h"
struct UserAtomicType
{
int i;
explicit UserAtomicType(int d = 0) TEST_NOEXCEPT : i(d) {}
friend bool operator==(const UserAtomicType& x, const UserAtomicType& y)
{ return x.i == y.i; }
};
template < template <class TestArg> class TestFunctor >
struct TestEachIntegralType {
void operator()() const {
TestFunctor<char>()();
TestFunctor<signed char>()();
TestFunctor<unsigned char>()();
TestFunctor<short>()();
TestFunctor<unsigned short>()();
TestFunctor<int>()();
TestFunctor<unsigned int>()();
TestFunctor<long>()();
TestFunctor<unsigned long>()();
TestFunctor<long long>()();
TestFunctor<unsigned long long>()();
TestFunctor<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
TestFunctor<char16_t>()();
TestFunctor<char32_t>()();
#endif
}
};
template < template <class TestArg> class TestFunctor >
struct TestEachAtomicType {
void operator()() const {
TestEachIntegralType<TestFunctor>()();
TestFunctor<UserAtomicType>()();
TestFunctor<int*>()();
TestFunctor<const int*>()();
}
};
#endif // ATOMIC_HELPER_H

View File

@@ -8,7 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// ... assertion fails line 34
// ... assertion fails line 36
// <atomic>
@@ -24,10 +24,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
typedef std::atomic<T> A;
A t;
std::atomic_init(&t, T(1));
@@ -35,37 +36,10 @@ test()
volatile A vt;
std::atomic_init(&vt, T(2));
assert(vt == T(2));
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
int main()
{
test<A>();
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
test<int*>();
test<const int*>();
TestEachAtomicType<TestFn>()();
}

View File

@@ -22,17 +22,19 @@
#include <atomic>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
typedef std::atomic<T> A;
A t;
bool b1 = std::atomic_is_lock_free(static_cast<const A*>(&t));
volatile A vt;
bool b2 = std::atomic_is_lock_free(static_cast<const volatile A*>(&vt));
assert(b1 == b2);
}
}
};
struct A
{
@@ -41,23 +43,6 @@ struct A
int main()
{
test<A>();
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
test<int*>();
test<const int*>();
TestFn<A>()();
TestEachAtomicType<TestFn>()();
}

View File

@@ -8,7 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// ... assertion fails line 34
// ... assertion fails line 35
// <atomic>
@@ -24,10 +24,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
typedef std::atomic<T> A;
A t;
std::atomic_init(&t, T(1));
@@ -35,37 +36,10 @@ test()
volatile A vt;
std::atomic_init(&vt, T(2));
assert(std::atomic_load(&vt) == T(2));
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
int main()
{
test<A>();
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
test<int*>();
test<const int*>();
TestEachAtomicType<TestFn>()();
}

View File

@@ -24,10 +24,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
typedef std::atomic<T> A;
A t;
std::atomic_init(&t, T(1));
@@ -35,37 +36,10 @@ test()
volatile A vt;
std::atomic_init(&vt, T(2));
assert(std::atomic_load_explicit(&vt, std::memory_order_seq_cst) == T(2));
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
int main()
{
test<A>();
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
test<int*>();
test<const int*>();
TestEachAtomicType<TestFn>()();
}

View File

@@ -8,7 +8,6 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// ... assertion fails line 31
// <atomic>
@@ -24,10 +23,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
typedef std::atomic<T> A;
A t;
std::atomic_store(&t, T(1));
@@ -35,37 +35,11 @@ test()
volatile A vt;
std::atomic_store(&vt, T(2));
assert(vt == T(2));
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
int main()
{
test<A>();
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
test<int*>();
test<const int*>();
TestEachAtomicType<TestFn>()();
}

View File

@@ -8,7 +8,6 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// ... assertion fails line 31
// <atomic>
@@ -24,10 +23,11 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
template <class T>
void
test()
{
struct TestFn {
void operator()() const {
typedef std::atomic<T> A;
A t;
std::atomic_store_explicit(&t, T(1), std::memory_order_seq_cst);
@@ -35,37 +35,11 @@ test()
volatile A vt;
std::atomic_store_explicit(&vt, T(2), std::memory_order_seq_cst);
assert(vt == T(2));
}
struct A
{
int i;
explicit A(int d = 0) noexcept {i=d;}
friend bool operator==(const A& x, const A& y)
{return x.i == y.i;}
}
};
int main()
{
test<A>();
test<char>();
test<signed char>();
test<unsigned char>();
test<short>();
test<unsigned short>();
test<int>();
test<unsigned int>();
test<long>();
test<unsigned long>();
test<long long>();
test<unsigned long long>();
test<wchar_t>();
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
test<char16_t>();
test<char32_t>();
#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
test<int*>();
test<const int*>();
TestEachAtomicType<TestFn>()();
}

View File

@@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// XFAIL: c++98, c++03
// <atomic>

View File

@@ -22,6 +22,8 @@
#include <type_traits>
#include <cassert>
#include "atomic_helpers.h"
struct UserType {
int i;
@@ -34,27 +36,29 @@ struct UserType {
};
template <class Tp>
void test() {
typedef std::atomic<Tp> Atomic;
static_assert(std::is_literal_type<Atomic>::value, "");
constexpr Tp t(42);
{
constexpr Atomic a(t);
assert(a == t);
struct TestFunc {
void operator()() const {
typedef std::atomic<Tp> Atomic;
static_assert(std::is_literal_type<Atomic>::value, "");
constexpr Tp t(42);
{
constexpr Atomic a(t);
assert(a == t);
}
{
constexpr Atomic a{t};
assert(a == t);
}
{
constexpr Atomic a = ATOMIC_VAR_INIT(t);
assert(a == t);
}
}
{
constexpr Atomic a{t};
assert(a == t);
}
{
constexpr Atomic a = ATOMIC_VAR_INIT(t);
assert(a == t);
}
}
};
int main()
{
test<int>();
test<UserType>();
TestFunc<UserType>()();
TestEachIntegralType<TestFunc>()();
}