24#ifndef MYSQL_SCHEDULER_TASK_REGISTRY_H
25#define MYSQL_SCHEDULER_TASK_REGISTRY_H
40template <
typename Task_
id_type,
typename Task_
object_type>
55 psi_params.memory_resource)),
58 void init(std::size_t capacity) {
70 psi_params.memory_resource)),
89 [[nodiscard]]
bool apply(Task_id_type
id,
T &&func) {
92 std::scoped_lock scope_guard(entry_ptr->m_lock);
93 if (!entry_ptr->m_active.test()) {
96 func(entry_ptr->m_obj);
103 template <
typename T>
107 std::scoped_lock scope_guard(entry_ptr->m_lock);
108 entry_ptr->m_active.test_and_set();
109 func(entry_ptr->m_obj);
115 return entry_ptr->m_active.test();
122 template <
typename T>
125 for (std::size_t
id = 0;
id <
m_tasks.size(); ++
id) {
128 if (entry_ptr->m_active.test()) {
129 func(entry_ptr->m_obj);
138 std::scoped_lock scope_guard(entry_ptr->m_lock);
139 if (entry_ptr->m_active.test()) {
142 entry_ptr->m_id =
id;
143 entry_ptr->m_obj = std::move(obj);
144 entry_ptr->m_active.test_and_set();
159 if (!entry_ptr->m_active.test_and_set())
break;
160 std::this_thread::yield();
162 entry_ptr->m_id =
id;
172 entry_ptr->m_lock.lock();
174 entry_ptr->m_lock.unlock();
182 return entry_ptr->m_obj;
188 return std::scoped_lock(entry_ptr->m_lock);
194 entry_ptr->m_lock.lock();
195 return entry_ptr->m_obj;
201 entry_ptr->m_lock.unlock();
213 template <
typename Functor>
217 if (!entry_ptr->m_active.test()) {
220 bool should_deactivate = func(entry_ptr->m_obj);
221 if (should_deactivate) {
222 entry_ptr->m_active.clear();
235 if (!entry_ptr->m_active.test() || entry_ptr->m_id !=
id) {
238 auto object(entry_ptr->m_obj);
239 entry_ptr->m_active.clear();
246 for (std::size_t idx = 0; idx <
m_capacity; ++idx) {
253 struct alignas(cache_line_size)
Entry {
256 Task_id_type m_id{0};
259 std::atomic_flag m_active = ATOMIC_FLAG_INIT;
261 sizeof(m_lock) +
sizeof(m_active)) %
270 auto hash_value =
id.get() %
m_tasks.size();
276 std::vector<Entry_ptr, mysql::allocators::Allocator<Entry_ptr>>
m_tasks;
Allocator using a Memory_resource to do the allocation.
Definition: allocator.h:52
MySQL wrapper for a mutex, template which may be specialized with a specific implementation of a mute...
Definition: mutex_wrapper.h:38
This template maintains the registry of tasks, with specific object types.
Definition: task_registry.h:41
Task_registry_psi m_psi
Definition: task_registry.h:278
Task_object & get(Task_id_type id)
Unprotected access to task object. Firstly obtain the guard.
Definition: task_registry.h:179
static constexpr std::size_t cache_line_size
Definition: task_registry.h:48
Task_object_type Task_object
Definition: task_registry.h:43
void deinit()
Definition: task_registry.h:75
bool m_initialized
Definition: task_registry.h:277
bool activate_entry(Task_id_type id)
Function activates already registered entry.
Definition: task_registry.h:149
bool apply(Task_id_type id, T &&func)
Calls 'func' on a given task object.
Definition: task_registry.h:89
Task_registry(Task_registry_psi psi_params={})
Definition: task_registry.h:53
std::vector< Entry_ptr, mysql::allocators::Allocator< Entry_ptr > > m_tasks
Definition: task_registry.h:276
const Task_object & get_ref(Task_id_type id)
Gets ref of internal object.
Definition: task_registry.h:169
void unlock(Task_id_type id)
Definition: task_registry.h:198
~Task_registry()
Definition: task_registry.h:82
std::unique_ptr< Entry > Entry_ptr
Definition: task_registry.h:266
bool deactivate_entry(Task_id_type id)
Function that unregisters a task, making it inactive.
Definition: task_registry.h:205
Task_registry(std::size_t capacity, Task_registry_psi psi_params={})
Definition: task_registry.h:67
void apply_on_active(T &&func)
Calls 'func' on each registered task object.
Definition: task_registry.h:123
void init(std::size_t capacity)
Definition: task_registry.h:58
void init_tasks()
Definition: task_registry.h:244
std::size_t m_capacity
Definition: task_registry.h:275
void activate_and_apply(Task_id_type id, T &&func)
Unconditionally activates and calls 'func' on a given task object.
Definition: task_registry.h:104
bool activate_or_wait(Task_id_type id)
Function activates already registered entry, if entry is active it waits until its state changes to i...
Definition: task_registry.h:155
Entry_ptr & get_entry(Task_id_type id)
Definition: task_registry.h:268
bool deactivate_entry(Task_id_type id, Functor &&func)
Function that unregisters a task, making it inactive, with functor.
Definition: task_registry.h:214
auto scoped_lock(Task_id_type id)
Definition: task_registry.h:185
std::unique_ptr< Task_object > Task_object_ptr
Definition: task_registry.h:44
bool check_active(Task_id_type id)
Definition: task_registry.h:112
static constexpr std::size_t default_capacity
Definition: task_registry.h:51
bool register_entry(Task_id_type id, Task_object &&obj)
Function that registers a task, making it active.
Definition: task_registry.h:135
Task_object & lock(Task_id_type id)
Definition: task_registry.h:191
concurrency::Mutex_key Mt_key
Definition: task_registry.h:45
std::optional< Task_object > get_and_deactivate_entry(Task_id_type id)
Function that unregisters a task, making it inactive, returns handled object if deactivation succeede...
Definition: task_registry.h:231
#define T
Definition: jit_executor_value.cc:373
Allocator class that uses a polymorphic Memory_resource to allocate memory.
#define MYSQL_CONCURRENCY_DEFINE_MT_PSI_KEY(key)
Definition: mutex_srv.h:35
constexpr std::size_t hardware_destructive_interference_size
Definition: cache_line_size.h:42
Mutex_wrapper Mutex
Definition: mutex_srv.h:40
PSI_mutex_key Mutex_key
Definition: mutex_srv.h:41
Definition: base_dependency_tracker.h:41
Definition: task_registry.h:253
Mutex m_lock
Definition: task_registry.h:258
Task_object m_obj
Definition: task_registry.h:257
Entry(Mt_key key_mt_entry=0)
Definition: task_registry.h:254
Task_registry instrumentation parameters, packed into this structure to simplify construction of a Ta...
Definition: task_registry_psi.h:38
concurrency::Mutex_key key_mutex_entry
mutex: One key for mutex protecting each entry (1 mutex per 1 entry)
Definition: task_registry_psi.h:40
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510