MySQL 8.3.0
Source Code Documentation
temptable::Lock_free_shared_block_pool< POOL_SIZE > Class Template Reference

Lock-free pool of POOL_SIZE Block elements. More...

#include <shared_block_pool.h>

Classes

struct  L1_dcache_aligned_block
 In the event of inability to express ourselves with something like std::array<alignas<N> Block> we have to fallback to this method. More...
 

Public Member Functions

Blocktry_acquire (size_t thd_id)
 Given the THD identifier, try to acquire an instance of Block. More...
 
bool try_release (size_t thd_id)
 Given the THD identifier, try to release an acquired instance of a Block. More...
 

Private Types

using Shared_block_slot = Lock_free_pool< unsigned long long, POOL_SIZE, Alignment::L1_DCACHE_SIZE >
 Check whether all compile-time pre-conditions are set. More...
 
using Shared_block_slot_element_type = typename Shared_block_slot::Type
 Be pedantic about the element type we used when building the slot type. More...
 

Private Attributes

std::array< L1_dcache_aligned_block, POOL_SIZE > m_shared_block
 An array of L1-dcache aligned Blocks Note: This is not the same as: alignas(L1_DCACHE_SIZE) std::array<Block, POOL_SIZE> More...
 
Shared_block_slot m_slot {FREE_SLOT}
 Lock-free slots. More...
 

Static Private Attributes

static constexpr Shared_block_slot_element_type FREE_SLOT
 Constexpr variable denoting a non-occupied (free) slot. More...
 
static constexpr size_t MODULO_MASK = POOL_SIZE - 1
 Bitmask which enables us to implement modulo-arithmetic operation in single bitwise instruction. More...
 

Detailed Description

template<size_t POOL_SIZE>
class temptable::Lock_free_shared_block_pool< POOL_SIZE >

Lock-free pool of POOL_SIZE Block elements.

Each Block element in a pool is represented by a slot. Each slot can be either free (default) or occupied. Acquiring the Block is possible only from an empty slot. Releasing the Block makes the slot free again.

Acquiring and releasing the Block is done via THD identifier on which, implicitly, modulo-arithmethic is applied in order to pick the right slot. To avoid questionable performance of modulo-arithmetic in its more general form, POOL_SIZE is constrained only to numbers which are power of two. This enables us to implement a modulo operation in single bitwise instruction. Check whether POOL_SIZE is a number which is power of two is run during the compile-time.

Given that this pool is envisioned to be used concurrently by multiple threads, both the slot and Block are aligned to the size of CPU L1 data-cache. This makes sure that we negate the effect of false-sharing (threads bouncing each ones data from cache).

Member Typedef Documentation

◆ Shared_block_slot

template<size_t POOL_SIZE>
using temptable::Lock_free_shared_block_pool< POOL_SIZE >::Shared_block_slot = Lock_free_pool<unsigned long long, POOL_SIZE, Alignment::L1_DCACHE_SIZE>
private

Check whether all compile-time pre-conditions are set.

Build a slot type: a lock-free pool of L1-DCACHE aligned unsigned long long's

◆ Shared_block_slot_element_type

template<size_t POOL_SIZE>
using temptable::Lock_free_shared_block_pool< POOL_SIZE >::Shared_block_slot_element_type = typename Shared_block_slot::Type
private

Be pedantic about the element type we used when building the slot type.

Member Function Documentation

◆ try_acquire()

template<size_t POOL_SIZE>
Block * temptable::Lock_free_shared_block_pool< POOL_SIZE >::try_acquire ( size_t  thd_id)
inline

Given the THD identifier, try to acquire an instance of Block.

In the event of success, non-nullptr instance of Block will be returned and underlying slot will be marked as occupied. Otherwise, slot must have been already occupied in which case a nullptr will be returned.

Using the same THD identifier to re-acquire the Block (from the same slot) is supported but not possible if some other THD identifier is used.

[in] thd_id THD identifier.

Returns
Returns a pointer to the Block (success) or nullptr (failure).

◆ try_release()

template<size_t POOL_SIZE>
bool temptable::Lock_free_shared_block_pool< POOL_SIZE >::try_release ( size_t  thd_id)
inline

Given the THD identifier, try to release an acquired instance of a Block.

In the event of success, slot will be marked as non-occupied. Assuming that Block is not empty, it will also be destroyed.

Trying to release the Block/slot by using some other THD identifier is not possible and will therefore render this operation as failed.

[in] thd_id THD identifier.

Returns
Returns true (success) or false (failure).

Member Data Documentation

◆ FREE_SLOT

template<size_t POOL_SIZE>
constexpr Shared_block_slot_element_type temptable::Lock_free_shared_block_pool< POOL_SIZE >::FREE_SLOT
staticconstexprprivate
Initial value:
=
std::numeric_limits<Shared_block_slot_element_type>::max()

Constexpr variable denoting a non-occupied (free) slot.

◆ m_shared_block

template<size_t POOL_SIZE>
std::array<L1_dcache_aligned_block, POOL_SIZE> temptable::Lock_free_shared_block_pool< POOL_SIZE >::m_shared_block
private

An array of L1-dcache aligned Blocks Note: This is not the same as: alignas(L1_DCACHE_SIZE) std::array<Block, POOL_SIZE>

◆ m_slot

template<size_t POOL_SIZE>
Shared_block_slot temptable::Lock_free_shared_block_pool< POOL_SIZE >::m_slot {FREE_SLOT}
private

Lock-free slots.

◆ MODULO_MASK

template<size_t POOL_SIZE>
constexpr size_t temptable::Lock_free_shared_block_pool< POOL_SIZE >::MODULO_MASK = POOL_SIZE - 1
staticconstexprprivate

Bitmask which enables us to implement modulo-arithmetic operation in single bitwise instruction.


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