MySQL 26.7.0
Source Code Documentation
mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type > Class Template Reference

This template class provides a concurrent registry for tasks that handles hash conflicts by allowing multiple task entries to coexist in the same hash bucket. More...

#include <task_registry_multi.h>

Classes

struct  Bucket
 Represents a bucket, capable of handling many subentries. More...
 
struct  Sub_entry
 Each bucket is capable of handling many subentries. More...
 

Public Types

using Task_object = Task_object_type
 
using Mutex = concurrency::Spin_lock_mutex
 

Public Member Functions

 Task_registry_multi (std::size_t capacity=default_capacity)
 Constructs buckets using defined capacity. More...
 
 ~Task_registry_multi ()=default
 Destruct registry, deinitialize if applicable. More...
 
template<typename T >
bool apply (Task_id_type id, T &&func)
 Calls 'func' on a given task object if active. More...
 
bool activate (Task_id_type id, Task_object &&obj)
 Registers and activates a task. More...
 
bool deactivate (Task_id_type id)
 Deactivates a task. More...
 
bool bucket_active (Task_id_type id) const
 Checks if the bucket for the given task ID is active (contains any tasks) More...
 

Static Public Attributes

static constexpr std::size_t default_capacity = 16384
 

Private Types

using Bucket_ptr = std::unique_ptr< Bucket >
 

Private Member Functions

Bucket_ptrget_bucket (Task_id_type id)
 Obtains the bucket handle handling a given id, for internal use. More...
 

Private Attributes

std::vector< Bucket_ptrm_buckets
 Buckets, synchronized independently. More...
 

Detailed Description

template<typename Task_id_type, typename Task_object_type>
class mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >

This template class provides a concurrent registry for tasks that handles hash conflicts by allowing multiple task entries to coexist in the same hash bucket.

It implements thread-safe access to task objects using a bucketed hash table with locks per bucket.

Threads do not contend when they operate on separate buckets. When a hash conflict occurs, threads use spin locks to lock requested resources (bucket).

Spin locks excel when locks are held for very brief periods, such as in this registry, where operations like checking task activation or updating entries are quick atomic operations. The spin lock avoids expensive context switches that std::mutex would incur.

Each bucket (determined by task ID hash) contains an unordered_map mapping task IDs to their associated objects and state. This design resolves collisions that could occur with simple hashing, ensuring that tasks with IDs mapping to the same bucket index can be stored and accessed efficiently.

Key features:

  • Hash conflict resolution: Multiple tasks per bucket using std::unordered_map.
  • Thread-safe: Each bucket has its own mutex.
  • Limited API: Only essential operations for task management.
  • Template-based: Supports various task ID and object types.

Usage example:

Task_registry_multi<Task_id, Task_info> registry(1000);
registry.activate(task_id, Task_info{...});
registry.apply(task_id, [](Task_info& info) { /* operate on info */ });
registry.deactivate(task_id);

Member Typedef Documentation

◆ Bucket_ptr

template<typename Task_id_type , typename Task_object_type >
using mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::Bucket_ptr = std::unique_ptr<Bucket>
private

◆ Mutex

template<typename Task_id_type , typename Task_object_type >
using mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::Mutex = concurrency::Spin_lock_mutex

◆ Task_object

template<typename Task_id_type , typename Task_object_type >
using mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::Task_object = Task_object_type

Constructor & Destructor Documentation

◆ Task_registry_multi()

template<typename Task_id_type , typename Task_object_type >
mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::Task_registry_multi ( std::size_t  capacity = default_capacity)
inline

Constructs buckets using defined capacity.

◆ ~Task_registry_multi()

template<typename Task_id_type , typename Task_object_type >
mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::~Task_registry_multi ( )
default

Destruct registry, deinitialize if applicable.

Member Function Documentation

◆ activate()

template<typename Task_id_type , typename Task_object_type >
bool mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::activate ( Task_id_type  id,
Task_object &&  obj 
)
inline

Registers and activates a task.

Parameters
idTask id
objTask object
Returns
true if the previous state was inactive (i.e., the task was newly registered), false if the previous state was active (task already present)
Exceptions
std::bad_allocIf memory allocation for the new entry fails

◆ apply()

template<typename Task_id_type , typename Task_object_type >
template<typename T >
bool mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::apply ( Task_id_type  id,
T &&  func 
)
inline

Calls 'func' on a given task object if active.

Parameters
idTask id
funcfunction to be applied on a given registered task
Returns
true if task was found and active, false otherwise

◆ bucket_active()

template<typename Task_id_type , typename Task_object_type >
bool mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::bucket_active ( Task_id_type  id) const
inline

Checks if the bucket for the given task ID is active (contains any tasks)

Parameters
idTask id
Returns
true if bucket has at least one active task, false otherwise

◆ deactivate()

template<typename Task_id_type , typename Task_object_type >
bool mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::deactivate ( Task_id_type  id)
inline

Deactivates a task.

Parameters
idTask id
Returns
true if deactivated, false if not found or not active

◆ get_bucket()

template<typename Task_id_type , typename Task_object_type >
Bucket_ptr & mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::get_bucket ( Task_id_type  id)
inlineprivate

Obtains the bucket handle handling a given id, for internal use.

Parameters
idTask id
Returns
Bucket handle

Member Data Documentation

◆ default_capacity

template<typename Task_id_type , typename Task_object_type >
constexpr std::size_t mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::default_capacity = 16384
staticconstexpr

◆ m_buckets

template<typename Task_id_type , typename Task_object_type >
std::vector<Bucket_ptr> mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::m_buckets
private

Buckets, synchronized independently.


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