26#ifndef ROUTER_SRC_JIT_EXECUTOR_INCLUDE_MYSQLROUTER_JIT_EXECUTOR_CONTEXT_POOL_H_
27#define ROUTER_SRC_JIT_EXECUTOR_INCLUDE_MYSQLROUTER_JIT_EXECUTOR_CONTEXT_POOL_H_
29#include <condition_variable>
56 explicit Pool(
size_t size,
const std::function<
T(
size_t)> &factory,
57 const std::function<
void(
T)> &destructor = {})
59 m_item_factory{factory},
60 m_item_destructor{destructor} {}
64 std::unique_lock
lock(m_mutex);
66 if (m_teardown)
return {};
73 if (m_contention_mode && m_items.empty()) {
74 m_item_availability.wait(
75 lock, [
this]() {
return m_active_items == 0 || !m_items.empty(); });
77 if (m_active_items == 0) {
78 throw std::runtime_error(
79 "All the contexts on the pool have been released.");
83 if (!m_items.empty()) {
85 item = m_items.front();
92 T item = m_item_factory(m_created_items);
93 increase_active_items();
95 }
catch (
const std::runtime_error &) {
104 std::scoped_lock
lock(m_mutex);
106 if (!m_teardown && m_items.size() < m_pool_size) {
107 m_items.push_back(ctx);
108 m_item_availability.notify_one();
118 std::scoped_lock
lock(m_mutex);
122 while (!m_items.empty()) {
123 auto item = m_items.front();
126 discard(item,
false);
130 std::unique_lock<std::mutex>
lock(m_mutex);
131 m_item_availability.wait(
lock, [
this]() {
return m_active_items == 0; });
135 std::scoped_lock
lock(m_mutex);
136 return m_active_items;
143 decrease_active_items(set_contention_mode);
145 if (m_item_destructor) {
147 m_item_destructor(ctx);
148 }
catch (
const std::exception &e) {
157 std::scoped_lock
lock(m_mutex);
165 std::scoped_lock
lock(m_mutex);
168 if (set_contention_mode) {
169 m_contention_mode =
true;
172 m_item_availability.notify_all();
177 bool m_teardown =
false;
182 size_t m_active_items = 0;
183 size_t m_created_items = 0;
184 bool m_contention_mode =
false;
195 std::shared_ptr<PooledContextHandle>
get_context();
203 std::unique_ptr<Pool<IContext *>>
m_pool;
Specialization of the Polyglot_common_context to provide MRS specific logging functions as well as th...
Definition: jit_executor_common_context.h:46
Definition: jit_executor_context_pool.h:189
std::unique_ptr< std::thread > m_release_thread
Definition: jit_executor_context_pool.h:205
void release(IContext *ctx)
Definition: jit_executor_context_pool.cc:86
void release_thread()
Definition: jit_executor_context_pool.cc:88
~ContextPool()
Definition: jit_executor_context_pool.cc:57
CommonContext * m_common_context
Definition: jit_executor_context_pool.h:202
mysql_harness::WaitingMPSCQueue< IContext * > m_release_queue
Definition: jit_executor_context_pool.h:204
std::shared_ptr< PooledContextHandle > get_context()
Definition: jit_executor_context_pool.cc:71
std::unique_ptr< Pool< IContext * > > m_pool
Definition: jit_executor_context_pool.h:203
ContextPool(size_t size, CommonContext *common_context)
Definition: jit_executor_context_pool.cc:40
void teardown()
Definition: jit_executor_context_pool.cc:59
Base handler for Jit Executor Context instances.
Definition: jit_executor_context_handle.h:36
Definition: jit_executor_context.h:40
Generic implementation of a pool.
Definition: jit_executor_context_pool.h:54
Pool(size_t size, const std::function< T(size_t)> &factory, const std::function< void(T)> &destructor={})
Definition: jit_executor_context_pool.h:56
void discard(T ctx, bool set_contention_mode)
Discards the affected context and turns ON contention mode for the pool.
Definition: jit_executor_context_pool.h:142
std::condition_variable m_item_availability
Definition: jit_executor_context_pool.h:176
void teardown()
Definition: jit_executor_context_pool.h:116
std::deque< T > m_items
Definition: jit_executor_context_pool.h:179
void release(T ctx)
Definition: jit_executor_context_pool.h:102
std::function< void(T)> m_item_destructor
Definition: jit_executor_context_pool.h:181
std::function< T(size_t id)> m_item_factory
Definition: jit_executor_context_pool.h:180
size_t m_pool_size
Definition: jit_executor_context_pool.h:178
size_t active_items() const
Definition: jit_executor_context_pool.h:134
void decrease_active_items(bool set_contention_mode=false)
Definition: jit_executor_context_pool.h:163
T get()
Definition: jit_executor_context_pool.h:62
void increase_active_items()
Definition: jit_executor_context_pool.h:155
std::mutex m_mutex
Definition: jit_executor_context_pool.h:175
A wrapper that will return a context to the pool as soon as it is released.
Definition: jit_executor_context_pool.h:211
IContext * m_context
Definition: jit_executor_context_pool.h:221
IContext * get() override
Definition: jit_executor_context_pool.h:217
~PooledContextHandle() override
Definition: jit_executor_context_pool.h:215
PooledContextHandle(ContextPool *pool, IContext *ctx)
Definition: jit_executor_context_pool.h:213
ContextPool * m_pool
Definition: jit_executor_context_pool.h:220
provide waiting pop and push operator to thread-safe queues.
Definition: waiting_queue_adaptor.h:40
#define log_error(...)
Definition: log_client.h:155
#define T
Definition: jit_executor_value.cc:373
Logging interface for using and extending the logging subsystem.
#define IMPORT_LOG_FUNCTIONS()
convenience macro to avoid common boilerplate
Definition: logging.h:323
Definition: jit_executor_callbacks.h:36
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
size_t size(const char *const c)
Definition: base64.h:46
static std::mutex lock
Definition: net_ns.cc:56