diff --git a/tpool/aio_linux.cc b/tpool/aio_linux.cc index 6997cbcccab..1f52e62bd07 100644 --- a/tpool/aio_linux.cc +++ b/tpool/aio_linux.cc @@ -102,6 +102,7 @@ class aio_linux final : public aio */ constexpr unsigned MAX_EVENTS= 256; + aio->m_pool->m_worker_init_callback(); io_event events[MAX_EVENTS]; for (;;) { @@ -110,14 +111,14 @@ class aio_linux final : public aio continue; case -EINVAL: if (shutdown_in_progress) - return; + goto end; /* fall through */ default: if (ret < 0) { fprintf(stderr, "io_getevents returned %d\n", ret); abort(); - return; + goto end; } for (int i= 0; i < ret; i++) { @@ -142,6 +143,8 @@ class aio_linux final : public aio } } } +end: + aio->m_pool->m_worker_destroy_callback(); } public: diff --git a/tpool/aio_win.cc b/tpool/aio_win.cc index eec37383152..f483e3ca1e1 100644 --- a/tpool/aio_win.cc +++ b/tpool/aio_win.cc @@ -92,7 +92,9 @@ public: static void aio_completion_thread_proc(tpool_generic_win_aio* aio) { + aio->m_pool->m_worker_init_callback(); aio->completion_thread_work(); + aio->m_pool->m_worker_destroy_callback(); } ~tpool_generic_win_aio() diff --git a/tpool/tpool.h b/tpool/tpool.h index 7ac6763ae23..a5dccca5e91 100644 --- a/tpool/tpool.h +++ b/tpool/tpool.h @@ -27,6 +27,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 - 1301 USA*/ #define NOMINMAX #endif #include +#include + /** Windows-specific native file handle struct. Apart from the actual handle, contains PTP_IO @@ -202,21 +204,24 @@ protected: std::unique_ptr m_aio; virtual aio *create_native_aio(int max_io)= 0; +public: /** Functions to be called at worker thread start/end can be used for example to set some TLS variables */ - void (*m_worker_init_callback)(void); - void (*m_worker_destroy_callback)(void); + void (*m_worker_init_callback)(void)= [] {}; + void (*m_worker_destroy_callback)(void)= [] {}; -public: - thread_pool() : m_aio(), m_worker_init_callback(), m_worker_destroy_callback() + thread_pool() + : m_aio() { } virtual void submit_task(task *t)= 0; virtual timer* create_timer(callback_func func, void *data=nullptr) = 0; void set_thread_callbacks(void (*init)(), void (*destroy)()) { + assert(init); + assert(destroy); m_worker_init_callback= init; m_worker_destroy_callback= destroy; } diff --git a/tpool/tpool_generic.cc b/tpool/tpool_generic.cc index 9697194e0b2..cd4dd5f0bde 100644 --- a/tpool/tpool_generic.cc +++ b/tpool/tpool_generic.cc @@ -571,8 +571,7 @@ void thread_pool_generic::worker_main(worker_data *thread_var) { task* task; set_tls_pool(this); - if(m_worker_init_callback) - m_worker_init_callback(); + m_worker_init_callback(); tls_worker_data = thread_var; @@ -581,8 +580,7 @@ void thread_pool_generic::worker_main(worker_data *thread_var) task->execute(); } - if (m_worker_destroy_callback) - m_worker_destroy_callback(); + m_worker_destroy_callback(); worker_end(thread_var); } diff --git a/tpool/tpool_win.cc b/tpool/tpool_win.cc index 2b413b8d1b6..c012c4b6408 100644 --- a/tpool/tpool_win.cc +++ b/tpool/tpool_win.cc @@ -45,9 +45,7 @@ class thread_pool_win : public thread_pool if (!m_pool) return; - if (m_pool->m_worker_destroy_callback) - m_pool->m_worker_destroy_callback(); - + m_pool->m_worker_destroy_callback(); m_pool->m_thread_count--; } /** This needs to be called before every IO or simple task callback.*/ @@ -63,8 +61,7 @@ class thread_pool_win : public thread_pool m_pool = pool; m_pool->m_thread_count++; // Call the thread init function. - if (m_pool->m_worker_init_callback) - m_pool->m_worker_init_callback(); + m_pool->m_worker_init_callback(); } };