File size: 6,162 Bytes
2a504a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of libcu++, the C++ Standard Library for your entire system,
// under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCUDACXX_CXX_ATOMIC_H
#define _LIBCUDACXX_CXX_ATOMIC_H

template <typename _Tp, int _Sco>
struct __cxx_atomic_base_impl {
  using __underlying_t = _Tp;
  using __temporary_t = __cxx_atomic_base_impl<_Tp, _Sco>;
  using __wrap_t = __cxx_atomic_base_impl<_Tp, _Sco>;

  static constexpr int __sco = _Sco;

#if !defined(_LIBCUDACXX_COMPILER_GCC) || (__GNUC__ >= 5)
  static_assert(is_trivially_copyable<_Tp>::value,
    "std::atomic<Tp> requires that 'Tp' be a trivially copyable type");
#endif

  constexpr
  __cxx_atomic_base_impl() noexcept = default;
  constexpr
  __cxx_atomic_base_impl(__cxx_atomic_base_impl &&) noexcept = default;
  _LIBCUDACXX_INLINE_VISIBILITY constexpr explicit
  __cxx_atomic_base_impl(_Tp value) noexcept : __a_value(value) {}

  __cxx_atomic_base_impl& operator=(const __cxx_atomic_base_impl &) noexcept = default;

  _ALIGNAS(sizeof(_Tp)) _Tp __a_value;
};

template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
_Tp* __cxx_get_underlying_atomic(__cxx_atomic_base_impl<_Tp, _Sco> * __a) noexcept {
  return &__a->__a_value;
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
volatile _Tp* __cxx_get_underlying_atomic(__cxx_atomic_base_impl<_Tp, _Sco> volatile* __a) noexcept {
  return &__a->__a_value;
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
const _Tp* __cxx_get_underlying_atomic(__cxx_atomic_base_impl<_Tp, _Sco> const* __a) noexcept {
  return &__a->__a_value;
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
const volatile _Tp* __cxx_get_underlying_atomic(__cxx_atomic_base_impl<_Tp, _Sco> const volatile* __a) noexcept {
  return &__a->__a_value;
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
__cxx_atomic_base_impl<_Tp, _Sco>* __cxx_atomic_unwrap(__cxx_atomic_base_impl<_Tp, _Sco>* __a) noexcept {
  return __a;
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
volatile __cxx_atomic_base_impl<_Tp, _Sco>* __cxx_atomic_unwrap(__cxx_atomic_base_impl<_Tp, _Sco> volatile* __a) noexcept {
  return __a;
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
const __cxx_atomic_base_impl<_Tp, _Sco>* __cxx_atomic_unwrap(__cxx_atomic_base_impl<_Tp, _Sco> const* __a) noexcept {
  return __a;
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
const volatile __cxx_atomic_base_impl<_Tp, _Sco>* __cxx_atomic_unwrap(__cxx_atomic_base_impl<_Tp, _Sco> const volatile* __a) noexcept {
  return __a;
}

template <typename _Tp, int _Sco>
struct __cxx_atomic_ref_base_impl {
  using __underlying_t = _Tp;
  using __temporary_t = _Tp;
  using __wrap_t = _Tp;

  static constexpr int __sco = _Sco;

#if !defined(_LIBCUDACXX_COMPILER_GCC) || (__GNUC__ >= 5)
  static_assert(is_trivially_copyable<_Tp>::value,
    "std::atomic_ref<Tp> requires that 'Tp' be a trivially copyable type");
#endif

  constexpr
  __cxx_atomic_ref_base_impl() noexcept = delete;
  constexpr
  __cxx_atomic_ref_base_impl(__cxx_atomic_ref_base_impl &&) noexcept = default;
  constexpr
  __cxx_atomic_ref_base_impl(const __cxx_atomic_ref_base_impl &) noexcept = default;
  _LIBCUDACXX_INLINE_VISIBILITY constexpr explicit
  __cxx_atomic_ref_base_impl(_Tp& value) noexcept : __a_value(&value) {}

  _Tp* __a_value;
};

template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
_Tp* __cxx_get_underlying_atomic(__cxx_atomic_ref_base_impl<_Tp, _Sco>* __a) noexcept {
  return __a->__a_value;
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
volatile _Tp* __cxx_get_underlying_atomic(__cxx_atomic_ref_base_impl<_Tp, _Sco> volatile* __a) noexcept {
  return __a->__a_value;
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
const _Tp* __cxx_get_underlying_atomic(__cxx_atomic_ref_base_impl<_Tp, _Sco> const* __a) noexcept {
  return __a->__a_value;
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
const volatile _Tp* __cxx_get_underlying_atomic(__cxx_atomic_ref_base_impl<_Tp, _Sco> const volatile* __a) noexcept {
  return __a->__a_value;
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
_Tp* __cxx_atomic_unwrap(__cxx_atomic_ref_base_impl<_Tp, _Sco>* __a) noexcept {
  return __cxx_get_underlying_atomic(__a);
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
volatile _Tp* __cxx_atomic_unwrap(__cxx_atomic_ref_base_impl<_Tp, _Sco> volatile* __a) noexcept {
  return __cxx_get_underlying_atomic(__a);
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
const _Tp* __cxx_atomic_unwrap(__cxx_atomic_ref_base_impl<_Tp, _Sco> const* __a) noexcept {
  return __cxx_get_underlying_atomic(__a);
}
template <typename _Tp, int _Sco>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
const volatile _Tp* __cxx_atomic_unwrap(__cxx_atomic_ref_base_impl<_Tp, _Sco> const volatile* __a) noexcept {
  return __cxx_get_underlying_atomic(__a);
}

template <typename _Tp>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
_Tp* __cxx_get_underlying_atomic(_Tp* __a) noexcept {
  return __a;
}

template <typename _Tp, typename _Up>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
auto __cxx_atomic_wrap_to_base(_Tp*, _Up __val) noexcept -> typename _Tp::__wrap_t {
  return typename _Tp::__wrap_t(__val);
}
template <typename _Tp>
_LIBCUDACXX_INLINE_VISIBILITY constexpr
auto __cxx_atomic_base_temporary(_Tp*) noexcept -> typename _Tp::__temporary_t {
  return typename _Tp::__temporary_t();
}

template <typename _Tp>
using __cxx_atomic_underlying_t = typename _Tp::__underlying_t;

#endif //_LIBCUDACXX_CXX_ATOMIC_H