Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::lockfree::circular_array< T > Class Template Reference

Dynamic circular array for work-stealing deque. More...

#include <work_stealing_deque.h>

Inheritance diagram for kcenon::thread::lockfree::circular_array< T >:
Inheritance graph
Collaboration diagram for kcenon::thread::lockfree::circular_array< T >:
Collaboration graph

Public Member Functions

 circular_array (std::size_t log_size)
 Constructs a circular array with given capacity.
 
 ~circular_array ()
 
 circular_array (const circular_array &)=delete
 
circular_arrayoperator= (const circular_array &)=delete
 
std::size_t size () const noexcept
 Get the capacity of the array.
 
get (std::int64_t index) const noexcept
 Get element at index with relaxed memory ordering.
 
void put (std::int64_t index, T value) noexcept
 Store element at index with relaxed memory ordering.
 
circular_arraygrow (std::int64_t bottom, std::int64_t top) const
 Create a new array with double the capacity, copying elements.
 

Private Attributes

std::size_t log_size_
 
std::size_t size_
 
std::size_t mask_
 
std::atomic< T > * buffer_
 

Detailed Description

template<typename T>
class kcenon::thread::lockfree::circular_array< T >

Dynamic circular array for work-stealing deque.

This class provides a resizable circular buffer used internally by the work-stealing deque. It supports lock-free growth operations.

Template Parameters
TElement type stored in the array

Definition at line 33 of file work_stealing_deque.h.

Constructor & Destructor Documentation

◆ circular_array() [1/2]

template<typename T >
kcenon::thread::lockfree::circular_array< T >::circular_array ( std::size_t log_size)
inlineexplicit

Constructs a circular array with given capacity.

Parameters
log_sizeLog base 2 of the capacity (capacity = 2^log_size)

Definition at line 39 of file work_stealing_deque.h.

40 : log_size_(log_size)
41 , size_(1ULL << log_size)
42 , mask_(size_ - 1)
43 , buffer_(new std::atomic<T>[size_]) {
44 for (std::size_t i = 0; i < size_; ++i) {
45 buffer_[i].store(T{}, std::memory_order_relaxed);
46 }
47 }

References kcenon::thread::lockfree::circular_array< T >::buffer_, and kcenon::thread::lockfree::circular_array< T >::size_.

Referenced by kcenon::thread::lockfree::circular_array< T >::grow().

Here is the caller graph for this function:

◆ ~circular_array()

template<typename T >
kcenon::thread::lockfree::circular_array< T >::~circular_array ( )
inline

Definition at line 49 of file work_stealing_deque.h.

49 {
50 delete[] buffer_;
51 }

References kcenon::thread::lockfree::circular_array< T >::buffer_.

◆ circular_array() [2/2]

template<typename T >
kcenon::thread::lockfree::circular_array< T >::circular_array ( const circular_array< T > & )
delete

Member Function Documentation

◆ get()

template<typename T >
T kcenon::thread::lockfree::circular_array< T >::get ( std::int64_t index) const
inlinenodiscardnoexcept

Get element at index with relaxed memory ordering.

Parameters
indexArray index (will be masked for circular access)
Returns
Element at the given index

Definition at line 70 of file work_stealing_deque.h.

70 {
71 return buffer_[index & mask_].load(std::memory_order_relaxed);
72 }

References kcenon::thread::lockfree::circular_array< T >::buffer_, and kcenon::thread::lockfree::circular_array< T >::mask_.

Referenced by kcenon::thread::lockfree::circular_array< T >::grow(), kcenon::thread::lockfree::work_stealing_deque< T >::pop(), kcenon::thread::lockfree::work_stealing_deque< T >::steal(), and kcenon::thread::lockfree::work_stealing_deque< T >::steal_batch().

Here is the caller graph for this function:

◆ grow()

template<typename T >
circular_array * kcenon::thread::lockfree::circular_array< T >::grow ( std::int64_t bottom,
std::int64_t top ) const
inlinenodiscard

Create a new array with double the capacity, copying elements.

Parameters
bottomCurrent bottom index
topCurrent top index
Returns
New circular array with doubled capacity

Definition at line 89 of file work_stealing_deque.h.

89 {
90 auto* new_array = new circular_array(log_size_ + 1);
91 for (std::int64_t i = top; i < bottom; ++i) {
92 new_array->put(i, get(i));
93 }
94 return new_array;
95 }
circular_array(std::size_t log_size)
Constructs a circular array with given capacity.
T get(std::int64_t index) const noexcept
Get element at index with relaxed memory ordering.

References kcenon::thread::lockfree::circular_array< T >::circular_array(), kcenon::thread::lockfree::circular_array< T >::get(), and kcenon::thread::lockfree::circular_array< T >::log_size_.

Referenced by kcenon::thread::lockfree::work_stealing_deque< T >::push().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=()

template<typename T >
circular_array & kcenon::thread::lockfree::circular_array< T >::operator= ( const circular_array< T > & )
delete

◆ put()

template<typename T >
void kcenon::thread::lockfree::circular_array< T >::put ( std::int64_t index,
T value )
inlinenoexcept

Store element at index with relaxed memory ordering.

Parameters
indexArray index (will be masked for circular access)
valueValue to store

Definition at line 79 of file work_stealing_deque.h.

79 {
80 buffer_[index & mask_].store(value, std::memory_order_relaxed);
81 }

References kcenon::thread::lockfree::circular_array< T >::buffer_, and kcenon::thread::lockfree::circular_array< T >::mask_.

Referenced by kcenon::thread::lockfree::work_stealing_deque< T >::push().

Here is the caller graph for this function:

◆ size()

template<typename T >
std::size_t kcenon::thread::lockfree::circular_array< T >::size ( ) const
inlinenodiscardnoexcept

Get the capacity of the array.

Returns
Number of elements the array can hold

Definition at line 61 of file work_stealing_deque.h.

61 {
62 return size_;
63 }

References kcenon::thread::lockfree::circular_array< T >::size_.

Referenced by kcenon::thread::lockfree::work_stealing_deque< T >::push().

Here is the caller graph for this function:

Member Data Documentation

◆ buffer_

◆ log_size_

template<typename T >
std::size_t kcenon::thread::lockfree::circular_array< T >::log_size_
private

◆ mask_

◆ size_


The documentation for this class was generated from the following file: