m_pool->m_worker_destroy_callback();
m_pool->m_thread_count--;
} /** This needs to be called before every IO or simple task callback.*/ void callback_prolog(thread_pool_win* pool)
{
assert(pool);
assert(!m_pool || (m_pool == pool)); if (m_pool)
{ // TLS data already initialized. return;
}
m_pool = pool;
m_pool->m_thread_count++; // Call the thread init function.
m_pool->m_worker_init_callback();
}
};
static thread_local struct tls_data tls_data; /** Timer */ class native_timer : public timer
{
std::mutex m_mtx; // protects against parallel execution
std::mutex m_shutdown_mtx; // protects m_on
PTP_TIMER m_ptp_timer;
callback_func m_func; void *m_data;
thread_pool_win& m_pool; int m_period; bool m_on;
staticvoid CALLBACK timer_callback(PTP_CALLBACK_INSTANCE callback_instance, void *context,
PTP_TIMER callback_timer)
{
native_timer *timer= (native_timer *) context;
tls_data.callback_prolog(&timer->m_pool);
std::unique_lock<std::mutex> lk(timer->m_mtx, std::defer_lock); if (!lk.try_lock())
{ /* Do not try to run timers in parallel */ return;
}
timer->m_func(timer->m_data); if (timer->m_period)
timer->set_time(timer->m_period, timer->m_period);
}
public:
native_timer(thread_pool_win& pool, callback_func func, void* data) :
m_mtx(), m_func(func), m_data(data), m_pool(pool), m_period(), m_on(true)
{
m_ptp_timer= CreateThreadpoolTimer(timer_callback, this, &pool.m_env);
} void set_time(int initial_delay_ms, int period_ms) override
{
std::unique_lock<std::mutex> lk(m_shutdown_mtx); if (!m_on) return; longlong initial_delay = -10000LL * initial_delay_ms;
SetThreadpoolTimer(m_ptp_timer, NULL, 0, 0);
SetThreadpoolTimer(m_ptp_timer, (PFILETIME)&initial_delay, 0, 100);
m_period = period_ms;
} void disarm() override
{
std::unique_lock<std::mutex> lk(m_shutdown_mtx);
m_on = false;
SetThreadpoolTimer(m_ptp_timer, NULL , 0, 0);
lk.unlock(); /* Don't do it in timer callback, that will hang*/
WaitForThreadpoolTimerCallbacks(m_ptp_timer, TRUE);
}
~native_timer()
{
disarm();
CloseThreadpoolTimer(m_ptp_timer);
}
}; /** AIO handler */ class native_aio : public aio
{
thread_pool_win& m_pool;
public:
native_aio(thread_pool_win &pool, int max_io)
: m_pool(pool)
{
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.