Skip to content

Commit 168f93c

Browse files
committed
Split implementation over multiple headers
1 parent 9083e08 commit 168f93c

19 files changed

+476
-382
lines changed

internals/testing/smoke_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ auto smoke_test = test([]() {
2424
{
2525
atomically([&]() {
2626
int x = xA;
27-
int z = zA;
27+
int z = zA.load();
2828
xA = z;
29-
zA = x;
29+
zA.store(x);
3030
});
3131

3232
verify(3 == xA.unsafe_load());
@@ -35,7 +35,7 @@ auto smoke_test = test([]() {
3535
}
3636

3737
{
38-
auto r = atomically([&]() { return xA + 1; });
38+
auto r = atomically([&]() { return xA.ref() + 1; });
3939

4040
verify(r == 4);
4141
verify(3 == xA.unsafe_load());
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include "trade_v1/private/access.hpp"
4+
5+
template <class Value>
6+
void trade_v1::Private::access_t<Value, false>::retain() {
7+
new (&m_original) Value(std::move(m_current));
8+
}
9+
10+
template <class Value>
11+
void trade_v1::Private::access_t<Value, false>::destroy() {
12+
if (INITIAL != m_state) {
13+
m_current.~Value();
14+
if (READ + WRITTEN == m_state)
15+
m_original.~Value();
16+
}
17+
}
18+
19+
template <class Value>
20+
void trade_v1::Private::access_t<Value, true>::retain() {}
21+
22+
template <class Value>
23+
void trade_v1::Private::access_t<Value, true>::destroy() {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include "trade_v1/private/private.hpp"
4+
5+
struct trade_v1::Private::access_base_t {
6+
access_base_t *m_children[2];
7+
atom_mono_t *m_atom;
8+
state_t m_state;
9+
lock_ix_t m_lock_ix;
10+
destroy_t m_destroy;
11+
};
12+
13+
template <class Value>
14+
struct trade_v1::Private::access_t<Value, false> : access_base_t {
15+
~access_t() = delete;
16+
17+
Value m_current;
18+
Value m_original;
19+
20+
void retain();
21+
void destroy();
22+
};
23+
24+
template <class Value>
25+
struct trade_v1::Private::access_t<Value, true> : access_base_t {
26+
~access_t() = delete;
27+
28+
Value m_current;
29+
30+
void retain();
31+
void destroy();
32+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#include "trade_v1/private/atom.hpp"
4+
5+
template <class Value> trade_v1::Private::atom_t<Value>::atom_t() {}
6+
7+
template <class Value>
8+
trade_v1::Private::atom_t<Value>::atom_t(const Value &value) : m_value(value) {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include "trade_v1/private/non_atomic.hpp"
4+
5+
class trade_v1::Private::atom_mono_t {
6+
friend class Private;
7+
};
8+
9+
template <class Value> class trade_v1::Private::atom_t : Private::atom_mono_t {
10+
friend class Private;
11+
template <class> friend struct trade_v1::atom;
12+
13+
static constexpr bool is_atomic = !std::is_trivially_copyable_v<Value> ||
14+
std::atomic<Value>::is_always_lock_free;
15+
16+
std::conditional_t<is_atomic, std::atomic<Value>, non_atomic_t<Value>>
17+
m_value;
18+
19+
atom_t();
20+
atom_t(const Value &value);
21+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include "trade_v1/private/backoff.hpp"
4+
5+
#include "trade_v1/config.hpp"
6+
7+
#include "intrinsics_v1/pause.hpp"
8+
9+
#include "dumpster_v1/ranqd1.hpp"
10+
11+
inline trade_v1::Private::backoff_t::backoff_t() : m_count(0) {}
12+
13+
inline void trade_v1::Private::backoff_t::operator()() {
14+
#if defined(__clang__)
15+
#pragma unroll 1
16+
#endif
17+
for (auto n = 1 + ((s_seed = dumpster::ranqd1(s_seed)) &
18+
(m_count = 2 * m_count + 1));
19+
n;
20+
--n)
21+
intrinsics::pause();
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include "trade_v1/private/private.hpp"
4+
5+
struct trade_v1::Private::backoff_t {
6+
backoff_t();
7+
void operator()();
8+
static thread_local uint32_t s_seed;
9+
uint8_t m_count;
10+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#include "trade_v1/private/private.hpp"
4+
5+
struct trade_v1::Private::lock_t {
6+
std::atomic<clock_t> m_clock;
7+
waiter_t *m_first;
8+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include "trade_v1/private/private.hpp"
4+
5+
template <class Value> trade_v1::Private::non_atomic_t<Value>::non_atomic_t() {}
6+
7+
template <class Value>
8+
trade_v1::Private::non_atomic_t<Value>::non_atomic_t(const Value &value)
9+
: m_value(value) {}
10+
11+
template <class Value>
12+
void trade_v1::Private::non_atomic_t<Value>::store(const Value &value,
13+
std::memory_order) {
14+
m_value = value;
15+
}
16+
17+
template <class Value>
18+
const Value &
19+
trade_v1::Private::non_atomic_t<Value>::load(std::memory_order) const {
20+
return m_value;
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include "trade_v1/private/private.hpp"
4+
5+
template <class Value> class trade_v1::Private::non_atomic_t {
6+
friend class Private;
7+
8+
non_atomic_t();
9+
non_atomic_t(const Value &value);
10+
11+
void store(const Value &value, std::memory_order = std::memory_order_relaxed);
12+
const Value &load(std::memory_order = std::memory_order_relaxed) const;
13+
14+
Value m_value;
15+
};

0 commit comments

Comments
 (0)