![]() |
MySQL 26.7.0
Source Code Documentation
|
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_ptr & | get_bucket (Task_id_type id) |
| Obtains the bucket handle handling a given id, for internal use. More... | |
Private Attributes | |
| std::vector< Bucket_ptr > | m_buckets |
| Buckets, synchronized independently. More... | |
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:
Usage example:
|
private |
| using mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::Mutex = concurrency::Spin_lock_mutex |
| using mysql::scheduler::Task_registry_multi< Task_id_type, Task_object_type >::Task_object = Task_object_type |
|
inline |
Constructs buckets using defined capacity.
|
default |
Destruct registry, deinitialize if applicable.
|
inline |
Registers and activates a task.
| id | Task id |
| obj | Task object |
| std::bad_alloc | If memory allocation for the new entry fails |
|
inline |
Calls 'func' on a given task object if active.
| id | Task id |
| func | function to be applied on a given registered task |
|
inline |
Checks if the bucket for the given task ID is active (contains any tasks)
| id | Task id |
|
inline |
Deactivates a task.
| id | Task id |
|
inlineprivate |
Obtains the bucket handle handling a given id, for internal use.
| id | Task id |
|
staticconstexpr |
|
private |
Buckets, synchronized independently.