Skip to content

Commit daa76cb

Browse files
committed
Internal duckdb#6607: IEJoin Task Locking
* Fix Windows atomic failure(?)
1 parent 84c5c5c commit daa76cb

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/common/sort/sorted_run.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ void SortedRunScanState::TemplatedScan(const SortedRun &sorted_run, const Vector
143143
//===--------------------------------------------------------------------===//
144144
// SortedRun
145145
//===--------------------------------------------------------------------===//
146+
sort_key_ptr_t::Atomic sort_key_ptr_t::lock_test;
147+
146148
SortedRun::SortedRun(ClientContext &context_p, const Sort &sort_p, bool is_index_sort_p)
147149
: context(context_p), sort(sort_p), key_data(make_uniq<TupleDataCollection>(context, sort.key_layout)),
148150
payload_data(sort.payload_layout && sort.payload_layout->ColumnCount() != 0

src/include/duckdb/common/sorting/sort_key.hpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ enum class SortKeyType : uint8_t {
3434

3535
//! Forces a pointer size of 8 bytes (even on 32-bit)
3636
struct sort_key_ptr_t { // NOLINT: match stl case
37+
// For some reason, is_lock_free is not a class member...
38+
using Atomic = atomic<data_ptr_t>;
39+
static Atomic lock_test;
40+
41+
inline data_ptr_t GetPtr() const {
42+
if (lock_test.is_lock_free()) {
43+
auto a = new ((void *)&u.ptr) Atomic;
44+
return a->load();
45+
} else {
46+
return u.ptr;
47+
}
48+
}
49+
50+
inline void SetPtr(const data_ptr_t &ptr) {
51+
if (lock_test.is_lock_free()) {
52+
auto a = new ((void *)&u.ptr) Atomic;
53+
a->store(ptr);
54+
} else {
55+
u.ptr = ptr;
56+
}
57+
}
58+
3759
union {
3860
data_ptr_t ptr;
3961
uint64_t pad;
@@ -69,17 +91,16 @@ struct SortKeyPayload {
6991

7092
public:
7193
static constexpr bool HAS_PAYLOAD = true;
94+
using Atomic = sort_key_ptr_t::Atomic;
7295

7396
data_ptr_t GetPayload() const {
7497
auto &sort_key = static_cast<const SORT_KEY &>(*this);
75-
auto a = new ((void *)&sort_key.payload.u.ptr) atomic<data_ptr_t>;
76-
return a->load();
98+
return sort_key.payload.GetPtr();
7799
}
78100

79101
void SetPayload(const data_ptr_t &payload) {
80102
auto &sort_key = static_cast<SORT_KEY &>(*this);
81-
auto a = new ((void *)&sort_key.payload.u.ptr) atomic<data_ptr_t>;
82-
a->store(payload);
103+
sort_key.payload.SetPtr(payload);
83104
}
84105
};
85106

0 commit comments

Comments
 (0)