diff --git a/include/maxscale/thread.h b/include/maxscale/thread.h index e2addc302..49f694538 100644 --- a/include/maxscale/thread.h +++ b/include/maxscale/thread.h @@ -29,11 +29,44 @@ MXS_BEGIN_DECLS * Thread type and thread identifier function macros */ #include -#define THREAD pthread_t -#define thread_self() pthread_self() -extern THREAD *thread_start(THREAD *thd, void (*entry)(void *), void *arg); +typedef pthread_t THREAD; + +/** + * Obtain a handle to the calling thread + * + * @return The thread handle of the calling thread. + */ +static inline THREAD thread_self() +{ + return pthread_self(); +} + +/** + * Start a secondary thread + * + * @param thd Pointer to the THREAD object + * @param entry The entry point to call + * @param arg The argument to pass the thread entry point + * @param stack_size The stack size of the thread. If 0, the default + * size will be used. + * + * @return The thread handle or NULL if an error occurred + */ +extern THREAD *thread_start(THREAD *thd, void (*entry)(void *), void *arg, size_t stack_size); + +/** + * Wait for a running thread to complete. + * + * @param thd The thread handle + */ extern void thread_wait(THREAD thd); + +/** + * Put the calling thread to sleep for a number of milliseconds + * + * @param ms Number of milliseconds to sleep + */ extern void thread_millisleep(int ms); MXS_END_DECLS diff --git a/server/core/gateway.cc b/server/core/gateway.cc index 362d78b1f..11391c256 100644 --- a/server/core/gateway.cc +++ b/server/core/gateway.cc @@ -2004,7 +2004,7 @@ int main(int argc, char **argv) */ log_flush_timeout_ms = 1000; - if (thread_start(&log_flush_thr, log_flush_cb, (void *) &log_flush_timeout_ms) == NULL) + if (thread_start(&log_flush_thr, log_flush_cb, (void *) &log_flush_timeout_ms, 0) == NULL) { const char* logerr = "Failed to start log flushing thread."; print_log_n_stderr(true, true, logerr, logerr, 0); diff --git a/server/core/housekeeper.cc b/server/core/housekeeper.cc index 41d365a1e..0e484e013 100644 --- a/server/core/housekeeper.cc +++ b/server/core/housekeeper.cc @@ -56,7 +56,7 @@ bool hkinit() { bool inited = false; - if (thread_start(&hk_thr_handle, hkthread, NULL) != NULL) + if (thread_start(&hk_thr_handle, hkthread, NULL, 0) != NULL) { inited = true; } diff --git a/server/core/test/testatomic.cc b/server/core/test/testatomic.cc index 30988fadd..35786d52b 100644 --- a/server/core/test/testatomic.cc +++ b/server/core/test/testatomic.cc @@ -82,7 +82,7 @@ int run_test(void(*func)(void*)) for (size_t i = 0; i < NTHR; i++) { - if (thread_start(&threads[i], func, (void*)(i + 1)) == NULL) + if (thread_start(&threads[i], func, (void*)(i + 1), 0) == NULL) { ss_dassert(false); } diff --git a/server/core/test/testspinlock.cc b/server/core/test/testspinlock.cc index 52d935cc0..f0ea61d62 100644 --- a/server/core/test/testspinlock.cc +++ b/server/core/test/testspinlock.cc @@ -115,7 +115,7 @@ test2() acquire_time = 0; spinlock_init(&lck); spinlock_acquire(&lck); - thread_start(&handle, test2_helper, (void *)&lck); + thread_start(&handle, test2_helper, (void *)&lck, 0); nanosleep(&sleeptime, NULL); spinlock_release(&lck); thread_wait(handle); @@ -222,7 +222,7 @@ test3() { threadrun[i] = 0; tnum[i] = i; - thread_start(&handle[i], test3_helper, &tnum[i]); + thread_start(&handle[i], test3_helper, &tnum[i], 0); } for (i = 0; i < THREADS; i++) { diff --git a/server/core/thread.cc b/server/core/thread.cc index b0856dce0..8b154a72a 100644 --- a/server/core/thread.cc +++ b/server/core/thread.cc @@ -10,58 +10,58 @@ * of this software will be governed by version 2 or later of the General * Public License. */ + #include +#include -/** - * @file thread.c - Implementation of thread related operations - * - * @verbatim - * Revision History - * - * Date Who Description - * 25/06/13 Mark Riddoch Initial implementation - * - * @endverbatim - */ - - -/** - * Start a polling thread - * - * @param thd Pointer to the THREAD object - * @param entry The entry point to call - * @param arg The argument to pass the thread entry point - * @return The thread handle or NULL if an error occurred - */ -THREAD *thread_start(THREAD *thd, void (*entry)(void *), void *arg) +THREAD *thread_start(THREAD *thd, void (*entry)(void *), void *arg, size_t stack_size) { - if (pthread_create(thd, NULL, (void *(*)(void *))entry, arg) != 0) + THREAD* rv = NULL; + + pthread_attr_t attr; + int error = pthread_attr_init(&attr); + + if (error == 0) { - return NULL; + if (stack_size != 0) + { + error = pthread_attr_setstacksize(&attr, stack_size); + } + + if (error == 0) + { + error = pthread_create(thd, &attr, (void *(*)(void *))entry, arg); + + if (error == 0) + { + rv = thd; + } + else + { + MXS_ERROR("Could not start thread: %s", mxs_strerror(error)); + } + } + else + { + MXS_ERROR("Could not set thread stack size to %lu: %s", stack_size, mxs_strerror(error)); + } } - return thd; + else + { + MXS_ERROR("Could not initialize thread attributes: %s", mxs_strerror(error)); + } + + return rv; } -/** - * Wait for a running threads to complete. - * - * @param thd The thread handle - */ -void -thread_wait(THREAD thd) +void thread_wait(THREAD thd) { void *rval; pthread_join((pthread_t)thd, &rval); } -/** - * Put the thread to sleep for a number of milliseconds - * - * @param ms Number of milliseconds to sleep - */ -void -thread_millisleep(int ms) +void thread_millisleep(int ms) { struct timespec req; req.tv_sec = ms / 1000; diff --git a/server/core/worker.cc b/server/core/worker.cc index 9728aea45..2b5386628 100644 --- a/server/core/worker.cc +++ b/server/core/worker.cc @@ -847,7 +847,7 @@ bool Worker::start() { m_started = true; - if (!thread_start(&m_thread, &Worker::thread_main, this)) + if (!thread_start(&m_thread, &Worker::thread_main, this, 0)) { m_started = false; } diff --git a/server/modules/filter/tpmfilter/tpmfilter.c b/server/modules/filter/tpmfilter/tpmfilter.c index 5c666e11a..c54d02eea 100644 --- a/server/modules/filter/tpmfilter/tpmfilter.c +++ b/server/modules/filter/tpmfilter/tpmfilter.c @@ -268,7 +268,7 @@ createInstance(const char *name, char **options, MXS_CONFIG_PARAMETER *params) * Launch a thread that checks the named pipe. */ THREAD thread; - if (!error && thread_start(&thread, checkNamedPipe, (void*)my_instance) == NULL) + if (!error && thread_start(&thread, checkNamedPipe, (void*)my_instance, 0) == NULL) { MXS_ERROR("Couldn't create a thread to check the named pipe: %s", strerror(errno)); error = true; diff --git a/server/modules/monitor/auroramon/auroramon.c b/server/modules/monitor/auroramon/auroramon.c index a5d48a29c..cf15d6ad1 100644 --- a/server/modules/monitor/auroramon/auroramon.c +++ b/server/modules/monitor/auroramon/auroramon.c @@ -217,7 +217,7 @@ startMonitor(MXS_MONITOR *mon, const MXS_CONFIG_PARAMETER *params) handle->script = config_copy_string(params, "script"); handle->events = config_get_enum(params, "events", mxs_monitor_event_enum_values); - if (thread_start(&handle->thread, monitorMain, handle) == NULL) + if (thread_start(&handle->thread, monitorMain, handle, 0) == NULL) { MXS_ERROR("Failed to start monitor thread for monitor '%s'.", mon->name); auroramon_free(handle); diff --git a/server/modules/monitor/galeramon/galeramon.c b/server/modules/monitor/galeramon/galeramon.c index dd87b439a..307d03dcb 100644 --- a/server/modules/monitor/galeramon/galeramon.c +++ b/server/modules/monitor/galeramon/galeramon.c @@ -177,7 +177,7 @@ startMonitor(MXS_MONITOR *mon, const MXS_CONFIG_PARAMETER *params) return NULL; } - if (thread_start(&handle->thread, monitorMain, handle) == NULL) + if (thread_start(&handle->thread, monitorMain, handle, 0) == NULL) { MXS_ERROR("Failed to start monitor thread for monitor '%s'.", mon->name); hashtable_free(handle->galera_nodes_info); diff --git a/server/modules/monitor/mmmon/mmmon.c b/server/modules/monitor/mmmon/mmmon.c index 7e2a0ca87..568cc5da0 100644 --- a/server/modules/monitor/mmmon/mmmon.c +++ b/server/modules/monitor/mmmon/mmmon.c @@ -144,7 +144,7 @@ startMonitor(MXS_MONITOR *mon, const MXS_CONFIG_PARAMETER *params) return NULL; } - if (thread_start(&handle->thread, monitorMain, handle) == NULL) + if (thread_start(&handle->thread, monitorMain, handle, 0) == NULL) { MXS_ERROR("Failed to start monitor thread for monitor '%s'.", mon->name); MXS_FREE(handle->script); diff --git a/server/modules/monitor/mysqlmon/mysql_mon.c b/server/modules/monitor/mysqlmon/mysql_mon.c index ba90c72ba..b728aaec9 100644 --- a/server/modules/monitor/mysqlmon/mysql_mon.c +++ b/server/modules/monitor/mysqlmon/mysql_mon.c @@ -282,7 +282,7 @@ startMonitor(MXS_MONITOR *monitor, const MXS_CONFIG_PARAMETER* params) MXS_FREE(handle); handle = NULL; } - else if (thread_start(&handle->thread, monitorMain, handle) == NULL) + else if (thread_start(&handle->thread, monitorMain, handle, 0) == NULL) { MXS_ERROR("Failed to start monitor thread for monitor '%s'.", monitor->name); hashtable_free(handle->server_info); diff --git a/server/modules/monitor/ndbclustermon/ndbclustermon.c b/server/modules/monitor/ndbclustermon/ndbclustermon.c index ce56d81d2..520569f9e 100644 --- a/server/modules/monitor/ndbclustermon/ndbclustermon.c +++ b/server/modules/monitor/ndbclustermon/ndbclustermon.c @@ -135,7 +135,7 @@ startMonitor(MXS_MONITOR *mon, const MXS_CONFIG_PARAMETER *params) return NULL; } - if (thread_start(&handle->thread, monitorMain, handle) == NULL) + if (thread_start(&handle->thread, monitorMain, handle, 0) == NULL) { MXS_ERROR("Failed to start monitor thread for monitor '%s'.", mon->name); MXS_FREE(handle->script);