2020-12-25 03:18:36 +01:00
|
|
|
#pragma once
|
2016-05-13 16:01:48 +02:00
|
|
|
|
2020-12-12 13:01:29 +01:00
|
|
|
#include "util/types.hpp"
|
2019-07-27 00:34:10 +02:00
|
|
|
#include "util/atomic.hpp"
|
2023-08-19 12:30:46 +02:00
|
|
|
#include "util/bless.hpp"
|
2016-05-13 16:01:48 +02:00
|
|
|
|
2024-11-15 12:25:37 +01:00
|
|
|
// Simple unshrinkable array base for concurrent access. Only growths automatically.
|
|
|
|
// There is no way to know the current size. The smaller index is, the faster it's accessed.
|
|
|
|
//
|
|
|
|
// T is the type of elements. Currently, default constructor of T shall be constexpr.
|
|
|
|
// N is initial element count, available without any memory allocation and only stored contiguously.
|
|
|
|
// Let's have around 256 bytes or less worth of preallocated elements
|
|
|
|
template <typename T, usz N = std::max<usz>(256 / sizeof(T), 1)>
|
2016-05-13 16:01:48 +02:00
|
|
|
class lf_array
|
|
|
|
{
|
|
|
|
// Data (default-initialized)
|
|
|
|
T m_data[N]{};
|
|
|
|
|
|
|
|
// Next array block
|
|
|
|
atomic_t<lf_array*> m_next{};
|
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr lf_array() = default;
|
|
|
|
|
|
|
|
~lf_array()
|
|
|
|
{
|
2020-02-05 08:00:08 +01:00
|
|
|
for (auto ptr = m_next.raw(); ptr;)
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
|
|
|
delete std::exchange(ptr, std::exchange(ptr->m_next.raw(), nullptr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 08:39:54 +01:00
|
|
|
T& operator [](usz index)
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
2024-11-15 18:10:59 +01:00
|
|
|
lf_array* _this = this;
|
|
|
|
|
|
|
|
T* result{};
|
|
|
|
bool installed = false;
|
|
|
|
|
|
|
|
for (usz i = 0;; i += N)
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
2024-11-15 18:10:59 +01:00
|
|
|
if (index - i < N)
|
|
|
|
{
|
2024-11-18 16:23:47 +01:00
|
|
|
result = std::addressof(_this->m_data[index - i]);
|
2024-11-15 18:10:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
lf_array* next = m_next;
|
|
|
|
|
|
|
|
if (!next)
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
2024-11-18 15:18:27 +01:00
|
|
|
// Do not allow access beyond many element more at a time
|
|
|
|
ensure(!installed && index - i < N * 2);
|
2024-11-15 18:10:59 +01:00
|
|
|
|
|
|
|
installed = true;
|
|
|
|
|
|
|
|
for (auto _new = new lf_array, ptr = _this; ptr;)
|
|
|
|
{
|
|
|
|
// Install the pointer. If failed, go deeper.
|
|
|
|
ptr = ptr->m_next.compare_and_swap(nullptr, _new);
|
|
|
|
|
|
|
|
if (!next)
|
|
|
|
{
|
|
|
|
// Determine the next pointer (if null then the new memory has been installed)
|
|
|
|
next = ptr ? ptr : _new;
|
|
|
|
}
|
|
|
|
}
|
2016-05-13 16:01:48 +02:00
|
|
|
}
|
2024-11-15 18:10:59 +01:00
|
|
|
|
|
|
|
_this = next;
|
2016-05-13 16:01:48 +02:00
|
|
|
}
|
|
|
|
|
2024-11-15 18:10:59 +01:00
|
|
|
return *result;
|
2016-05-13 16:01:48 +02:00
|
|
|
}
|
2022-06-10 13:27:02 +02:00
|
|
|
|
2024-11-16 08:32:41 +01:00
|
|
|
template <typename F> requires (std::is_invocable_v<F, T&>)
|
|
|
|
auto for_each(F&& func, bool is_finite = true)
|
|
|
|
{
|
|
|
|
lf_array* _this = this;
|
|
|
|
|
2024-11-18 15:18:27 +01:00
|
|
|
using return_t = std::invoke_result_t<F, T&>;
|
2024-11-16 08:32:41 +01:00
|
|
|
|
2024-11-16 13:05:07 +01:00
|
|
|
while (_this)
|
2024-11-16 08:32:41 +01:00
|
|
|
{
|
|
|
|
for (usz j = 0; j < N; j++)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_void_v<return_t>)
|
|
|
|
{
|
|
|
|
std::invoke(func, _this->m_data[j]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto ret = std::invoke(func, _this->m_data[j]);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
return std::make_pair(std::addressof(_this->m_data[j]), std::move(ret));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lf_array* next = m_next;
|
|
|
|
|
2024-11-16 13:05:07 +01:00
|
|
|
if constexpr (!std::is_void_v<return_t>)
|
2024-11-16 08:32:41 +01:00
|
|
|
{
|
2024-11-16 13:05:07 +01:00
|
|
|
if (!next && !is_finite)
|
2024-11-16 08:32:41 +01:00
|
|
|
{
|
2024-11-16 13:05:07 +01:00
|
|
|
for (auto _new = new lf_array, ptr = _this; ptr;)
|
2024-11-16 08:32:41 +01:00
|
|
|
{
|
2024-11-16 13:05:07 +01:00
|
|
|
// Install the pointer. If failed, go deeper.
|
|
|
|
ptr = ptr->m_next.compare_and_swap(nullptr, _new);
|
|
|
|
|
|
|
|
if (!next)
|
|
|
|
{
|
|
|
|
// Determine the next pointer (if null then the new memory has been installed)
|
|
|
|
next = ptr ? ptr : _new;
|
|
|
|
}
|
2024-11-16 08:32:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_this = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if constexpr (!std::is_void_v<return_t>)
|
|
|
|
{
|
|
|
|
return std::make_pair(std::add_pointer_t<T>{}, return_t());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 13:27:02 +02:00
|
|
|
u64 size() const
|
|
|
|
{
|
|
|
|
u64 size_n = 0;
|
|
|
|
|
|
|
|
for (auto ptr = this; ptr; ptr = ptr->m_next)
|
|
|
|
{
|
|
|
|
size_n += N;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size_n;
|
|
|
|
}
|
2016-05-13 16:01:48 +02:00
|
|
|
};
|
|
|
|
|
2024-11-15 12:25:37 +01:00
|
|
|
// Simple lock-free FIFO queue base. Based on lf_array<T, N> itself. Currently uses 32-bit counters.
|
|
|
|
// There is no "push_end" or "pop_begin" provided, the queue element must signal its state on its own.
|
|
|
|
template<typename T, usz N = std::max<usz>(256 / sizeof(T), 1)>
|
2016-05-13 16:01:48 +02:00
|
|
|
class lf_fifo : public lf_array<T, N>
|
|
|
|
{
|
2020-01-15 23:09:35 +01:00
|
|
|
// LSB 32-bit: push, MSB 32-bit: pop
|
|
|
|
atomic_t<u64> m_ctrl{};
|
2016-05-13 16:01:48 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr lf_fifo() = default;
|
|
|
|
|
2020-01-15 23:09:35 +01:00
|
|
|
// Get number of elements in the queue
|
2017-02-22 10:57:08 +01:00
|
|
|
u32 size() const
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
2020-01-15 23:09:35 +01:00
|
|
|
const u64 ctrl = m_ctrl.load();
|
|
|
|
return static_cast<u32>(ctrl - (ctrl >> 32));
|
2016-05-13 16:01:48 +02:00
|
|
|
}
|
2018-09-21 19:38:52 +02:00
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Acquire the place for one or more elements.
|
|
|
|
u32 push_begin(u32 count = 1)
|
|
|
|
{
|
2020-02-18 12:59:11 +01:00
|
|
|
return static_cast<u32>(m_ctrl.fetch_add(count));
|
2016-05-13 16:01:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get current "pop" position
|
2017-02-22 10:57:08 +01:00
|
|
|
u32 peek() const
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
2020-01-15 23:09:35 +01:00
|
|
|
return static_cast<u32>(m_ctrl >> 32);
|
2016-05-13 16:01:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Acknowledge processed element, return number of the next one.
|
|
|
|
// Perform clear if possible, zero is returned in this case.
|
|
|
|
u32 pop_end(u32 count = 1)
|
|
|
|
{
|
2020-01-15 23:09:35 +01:00
|
|
|
return m_ctrl.atomic_op([&](u64& ctrl)
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
2020-01-15 23:09:35 +01:00
|
|
|
ctrl += u64{count} << 32;
|
2016-05-13 16:01:48 +02:00
|
|
|
|
2020-01-15 23:09:35 +01:00
|
|
|
if (ctrl >> 32 == static_cast<u32>(ctrl))
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
|
|
|
// Clean if possible
|
2020-01-15 23:09:35 +01:00
|
|
|
ctrl = 0;
|
2016-05-13 16:01:48 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 23:09:35 +01:00
|
|
|
return static_cast<u32>(ctrl >> 32);
|
2016-05-13 16:01:48 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2016-06-02 17:16:01 +02:00
|
|
|
|
2018-09-21 19:38:52 +02:00
|
|
|
// Helper type, linked list element
|
|
|
|
template <typename T>
|
2019-01-03 16:18:31 +01:00
|
|
|
class lf_queue_item final
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
2019-01-03 16:18:31 +01:00
|
|
|
lf_queue_item* m_link = nullptr;
|
2018-09-21 19:38:52 +02:00
|
|
|
|
|
|
|
T m_data;
|
|
|
|
|
2019-01-03 16:18:31 +01:00
|
|
|
template <typename U>
|
|
|
|
friend class lf_queue_iterator;
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
friend class lf_queue_slice;
|
|
|
|
|
2018-09-21 19:38:52 +02:00
|
|
|
template <typename U>
|
|
|
|
friend class lf_queue;
|
|
|
|
|
2019-10-25 14:15:45 +02:00
|
|
|
template <typename U>
|
|
|
|
friend class lf_bunch;
|
|
|
|
|
2019-01-03 16:18:31 +01:00
|
|
|
constexpr lf_queue_item() = default;
|
2018-09-21 19:38:52 +02:00
|
|
|
|
|
|
|
template <typename... Args>
|
2019-01-03 16:18:31 +01:00
|
|
|
constexpr lf_queue_item(lf_queue_item* link, Args&&... args)
|
2018-09-21 19:38:52 +02:00
|
|
|
: m_link(link)
|
|
|
|
, m_data(std::forward<Args>(args)...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2019-01-03 16:18:31 +01:00
|
|
|
lf_queue_item(const lf_queue_item&) = delete;
|
2018-09-21 19:38:52 +02:00
|
|
|
|
2019-01-03 16:18:31 +01:00
|
|
|
lf_queue_item& operator=(const lf_queue_item&) = delete;
|
2018-09-21 19:38:52 +02:00
|
|
|
|
2019-01-03 16:18:31 +01:00
|
|
|
~lf_queue_item()
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
2019-01-03 16:18:31 +01:00
|
|
|
for (lf_queue_item* ptr = m_link; ptr;)
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
|
|
|
delete std::exchange(ptr, std::exchange(ptr->m_link, nullptr));
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 16:18:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Forward iterator: non-owning pointer to the list element in lf_queue_slice<>
|
|
|
|
template <typename T>
|
|
|
|
class lf_queue_iterator
|
|
|
|
{
|
|
|
|
lf_queue_item<T>* m_ptr = nullptr;
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
friend class lf_queue_slice;
|
|
|
|
|
2019-10-25 14:15:45 +02:00
|
|
|
template <typename U>
|
|
|
|
friend class lf_bunch;
|
|
|
|
|
2019-01-03 16:18:31 +01:00
|
|
|
public:
|
|
|
|
constexpr lf_queue_iterator() = default;
|
|
|
|
|
|
|
|
bool operator ==(const lf_queue_iterator& rhs) const
|
|
|
|
{
|
|
|
|
return m_ptr == rhs.m_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator *() const
|
|
|
|
{
|
|
|
|
return m_ptr->m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator ->() const
|
|
|
|
{
|
|
|
|
return &m_ptr->m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
lf_queue_iterator& operator ++()
|
|
|
|
{
|
|
|
|
m_ptr = m_ptr->m_link;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
lf_queue_iterator operator ++(int)
|
|
|
|
{
|
|
|
|
lf_queue_iterator result;
|
|
|
|
result.m_ptr = m_ptr;
|
|
|
|
m_ptr = m_ptr->m_link;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Owning pointer to the linked list taken from the lf_queue<>
|
|
|
|
template <typename T>
|
|
|
|
class lf_queue_slice
|
|
|
|
{
|
|
|
|
lf_queue_item<T>* m_head = nullptr;
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
friend class lf_queue;
|
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr lf_queue_slice() = default;
|
|
|
|
|
|
|
|
lf_queue_slice(const lf_queue_slice&) = delete;
|
|
|
|
|
|
|
|
lf_queue_slice(lf_queue_slice&& r) noexcept
|
|
|
|
: m_head(r.m_head)
|
|
|
|
{
|
|
|
|
r.m_head = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
lf_queue_slice& operator =(const lf_queue_slice&) = delete;
|
|
|
|
|
|
|
|
lf_queue_slice& operator =(lf_queue_slice&& r) noexcept
|
|
|
|
{
|
|
|
|
if (this != &r)
|
|
|
|
{
|
|
|
|
delete m_head;
|
|
|
|
m_head = r.m_head;
|
|
|
|
r.m_head = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~lf_queue_slice()
|
|
|
|
{
|
|
|
|
delete m_head;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator *() const
|
|
|
|
{
|
|
|
|
return m_head->m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator ->() const
|
|
|
|
{
|
|
|
|
return &m_head->m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator bool() const
|
|
|
|
{
|
|
|
|
return m_head != nullptr;
|
|
|
|
}
|
2018-09-21 19:38:52 +02:00
|
|
|
|
2019-01-05 11:34:02 +01:00
|
|
|
T* get() const
|
|
|
|
{
|
|
|
|
return m_head ? &m_head->m_data : nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-03 16:18:31 +01:00
|
|
|
lf_queue_iterator<T> begin() const
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
2019-01-03 16:18:31 +01:00
|
|
|
lf_queue_iterator<T> result;
|
|
|
|
result.m_ptr = m_head;
|
|
|
|
return result;
|
2018-09-21 19:38:52 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 16:18:31 +01:00
|
|
|
lf_queue_iterator<T> end() const
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
2019-01-03 16:18:31 +01:00
|
|
|
return {};
|
2018-09-21 19:38:52 +02:00
|
|
|
}
|
|
|
|
|
2023-09-01 14:07:46 +02:00
|
|
|
const T& operator[](usz index) const noexcept
|
|
|
|
{
|
|
|
|
lf_queue_iterator<T> result = begin();
|
|
|
|
|
|
|
|
while (--index != umax)
|
|
|
|
{
|
|
|
|
result++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator[](usz index) noexcept
|
|
|
|
{
|
|
|
|
lf_queue_iterator<T> result = begin();
|
|
|
|
|
|
|
|
while (--index != umax)
|
|
|
|
{
|
|
|
|
result++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
2019-01-03 16:18:31 +01:00
|
|
|
lf_queue_slice& pop_front()
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
2019-01-03 16:18:31 +01:00
|
|
|
delete std::exchange(m_head, std::exchange(m_head->m_link, nullptr));
|
|
|
|
return *this;
|
2018-09-21 19:38:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-03 16:18:31 +01:00
|
|
|
// Linked list-based multi-producer queue (the consumer drains the whole queue at once)
|
2018-09-21 19:38:52 +02:00
|
|
|
template <typename T>
|
2019-10-23 12:11:06 +02:00
|
|
|
class lf_queue final
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
2023-07-31 22:57:26 +02:00
|
|
|
atomic_t<u64> m_head{0};
|
|
|
|
|
|
|
|
lf_queue_item<T>* load(u64 value) const noexcept
|
|
|
|
{
|
|
|
|
return reinterpret_cast<lf_queue_item<T>*>(value >> 16);
|
|
|
|
}
|
2018-09-21 19:38:52 +02:00
|
|
|
|
|
|
|
// Extract all elements and reverse element order (FILO to FIFO)
|
2019-01-03 16:18:31 +01:00
|
|
|
lf_queue_item<T>* reverse() noexcept
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
2023-07-31 22:57:26 +02:00
|
|
|
if (auto* head = load(m_head) ? load(m_head.exchange(0)) : nullptr)
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
2019-01-03 16:18:31 +01:00
|
|
|
if (auto* prev = head->m_link)
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
|
|
|
head->m_link = nullptr;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2019-01-03 16:18:31 +01:00
|
|
|
auto* pprev = prev->m_link;
|
|
|
|
prev->m_link = head;
|
|
|
|
head = std::exchange(prev, pprev);
|
|
|
|
}
|
|
|
|
while (prev);
|
2018-09-21 19:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr lf_queue() = default;
|
|
|
|
|
2023-09-01 14:07:46 +02:00
|
|
|
lf_queue(lf_queue&& other) noexcept
|
|
|
|
{
|
|
|
|
m_head.release(other.m_head.exchange(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
lf_queue& operator=(lf_queue&& other) noexcept
|
|
|
|
{
|
|
|
|
if (this == std::addressof(other))
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete load(m_head);
|
|
|
|
m_head.release(other.m_head.exchange(0));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-09-21 19:38:52 +02:00
|
|
|
~lf_queue()
|
|
|
|
{
|
2023-07-31 22:57:26 +02:00
|
|
|
delete load(m_head);
|
2018-09-21 19:38:52 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 20:05:37 +01:00
|
|
|
void wait(std::nullptr_t /*null*/ = nullptr) noexcept
|
2019-09-08 23:55:48 +02:00
|
|
|
{
|
2023-07-31 22:57:26 +02:00
|
|
|
if (m_head == 0)
|
2019-09-08 23:55:48 +02:00
|
|
|
{
|
2023-07-31 22:57:26 +02:00
|
|
|
utils::bless<atomic_t<u32>>(&m_head)[1].wait(0);
|
2019-09-08 23:55:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 16:06:51 +01:00
|
|
|
const volatile void* observe() const noexcept
|
|
|
|
{
|
2023-07-31 22:57:26 +02:00
|
|
|
return load(m_head);
|
2020-12-15 16:06:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator bool() const noexcept
|
|
|
|
{
|
2023-07-31 22:57:26 +02:00
|
|
|
return m_head != 0;
|
2020-12-15 16:06:51 +01:00
|
|
|
}
|
|
|
|
|
2024-02-28 07:28:49 +01:00
|
|
|
template <bool Notify = true, typename... Args>
|
|
|
|
bool push(Args&&... args)
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
2023-07-31 22:57:26 +02:00
|
|
|
auto oldv = m_head.load();
|
2023-08-03 04:18:58 +02:00
|
|
|
auto item = new lf_queue_item<T>(load(oldv), std::forward<Args>(args)...);
|
2019-01-11 18:44:10 +01:00
|
|
|
|
2023-07-31 22:57:26 +02:00
|
|
|
while (!m_head.compare_exchange(oldv, reinterpret_cast<u64>(item) << 16))
|
2019-01-11 18:44:10 +01:00
|
|
|
{
|
2023-08-03 04:18:58 +02:00
|
|
|
item->m_link = load(oldv);
|
2018-09-21 19:38:52 +02:00
|
|
|
}
|
2019-10-12 21:45:17 +02:00
|
|
|
|
2024-02-28 07:28:49 +01:00
|
|
|
if (!oldv && Notify)
|
2019-10-12 21:45:17 +02:00
|
|
|
{
|
|
|
|
// Notify only if queue was empty
|
2024-02-28 07:28:49 +01:00
|
|
|
notify(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !oldv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void notify(bool force = false)
|
|
|
|
{
|
|
|
|
if (force || operator bool())
|
|
|
|
{
|
2023-07-31 22:57:26 +02:00
|
|
|
utils::bless<atomic_t<u32>>(&m_head)[1].notify_one();
|
2019-10-12 21:45:17 +02:00
|
|
|
}
|
2018-09-21 19:38:52 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 16:18:31 +01:00
|
|
|
// Withdraw the list, supports range-for loop: for (auto&& x : y.pop_all()) ...
|
|
|
|
lf_queue_slice<T> pop_all()
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
2019-01-03 16:18:31 +01:00
|
|
|
lf_queue_slice<T> result;
|
|
|
|
result.m_head = reverse();
|
|
|
|
return result;
|
2018-09-21 19:38:52 +02:00
|
|
|
}
|
|
|
|
|
2023-02-14 00:27:47 +01:00
|
|
|
// Withdraw the list in reverse order (LIFO/FILO)
|
|
|
|
lf_queue_slice<T> pop_all_reversed()
|
|
|
|
{
|
|
|
|
lf_queue_slice<T> result;
|
2023-07-31 22:57:26 +02:00
|
|
|
result.m_head = load(m_head.exchange(0));
|
2023-02-14 00:27:47 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-01-03 16:18:31 +01:00
|
|
|
// Apply func(data) to each element, return the total length
|
2018-09-21 19:38:52 +02:00
|
|
|
template <typename F>
|
2020-12-18 08:39:54 +01:00
|
|
|
usz apply(F func)
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
2020-12-18 08:39:54 +01:00
|
|
|
usz count = 0;
|
2018-09-21 19:38:52 +02:00
|
|
|
|
2019-01-03 16:18:31 +01:00
|
|
|
for (auto slice = pop_all(); slice; slice.pop_front())
|
2018-09-21 19:38:52 +02:00
|
|
|
{
|
2019-08-07 02:56:47 +02:00
|
|
|
std::invoke(func, *slice);
|
2018-09-21 19:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-25 14:15:45 +02:00
|
|
|
// Concurrent linked list, elements remain until destroyed.
|
|
|
|
template <typename T>
|
|
|
|
class lf_bunch final
|
|
|
|
{
|
|
|
|
atomic_t<lf_queue_item<T>*> m_head{nullptr};
|
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr lf_bunch() noexcept = default;
|
|
|
|
|
|
|
|
~lf_bunch()
|
|
|
|
{
|
|
|
|
delete m_head.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add unconditionally
|
|
|
|
template <typename... Args>
|
|
|
|
T* push(Args&&... args) noexcept
|
|
|
|
{
|
|
|
|
auto _old = m_head.load();
|
|
|
|
auto item = new lf_queue_item<T>(_old, std::forward<Args>(args)...);
|
|
|
|
|
|
|
|
while (!m_head.compare_exchange(_old, item))
|
|
|
|
{
|
|
|
|
item->m_link = _old;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &item->m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add if pred(item, all_items) is true for all existing items
|
|
|
|
template <typename F, typename... Args>
|
|
|
|
T* push_if(F pred, Args&&... args) noexcept
|
|
|
|
{
|
|
|
|
auto _old = m_head.load();
|
|
|
|
auto _chk = _old;
|
|
|
|
auto item = new lf_queue_item<T>(_old, std::forward<Args>(args)...);
|
|
|
|
|
|
|
|
_chk = nullptr;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
item->m_link = _old;
|
|
|
|
|
|
|
|
// Check all items in the queue
|
|
|
|
for (auto ptr = _old; ptr != _chk; ptr = ptr->m_link)
|
|
|
|
{
|
|
|
|
if (!pred(item->m_data, ptr->m_data))
|
|
|
|
{
|
|
|
|
item->m_link = nullptr;
|
|
|
|
delete item;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set to not check already checked items
|
|
|
|
_chk = _old;
|
|
|
|
}
|
|
|
|
while (!m_head.compare_exchange(_old, item));
|
|
|
|
|
|
|
|
return &item->m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
lf_queue_iterator<T> begin() const
|
|
|
|
{
|
|
|
|
lf_queue_iterator<T> result;
|
|
|
|
result.m_ptr = m_head.load();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
lf_queue_iterator<T> end() const
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|