MXS-1360 Make it possible to specify thread stack size
It is now possible to specify the thread stack size to be used, when a new thread is created. This will subsequently be used for allowing the stack size to be specified for worker threads.
This commit is contained in:
@ -29,11 +29,44 @@ MXS_BEGIN_DECLS
|
|||||||
* Thread type and thread identifier function macros
|
* Thread type and thread identifier function macros
|
||||||
*/
|
*/
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#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);
|
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);
|
extern void thread_millisleep(int ms);
|
||||||
|
|
||||||
MXS_END_DECLS
|
MXS_END_DECLS
|
||||||
|
@ -2004,7 +2004,7 @@ int main(int argc, char **argv)
|
|||||||
*/
|
*/
|
||||||
log_flush_timeout_ms = 1000;
|
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.";
|
const char* logerr = "Failed to start log flushing thread.";
|
||||||
print_log_n_stderr(true, true, logerr, logerr, 0);
|
print_log_n_stderr(true, true, logerr, logerr, 0);
|
||||||
|
@ -56,7 +56,7 @@ bool hkinit()
|
|||||||
{
|
{
|
||||||
bool inited = false;
|
bool inited = false;
|
||||||
|
|
||||||
if (thread_start(&hk_thr_handle, hkthread, NULL) != NULL)
|
if (thread_start(&hk_thr_handle, hkthread, NULL, 0) != NULL)
|
||||||
{
|
{
|
||||||
inited = true;
|
inited = true;
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,7 @@ int run_test(void(*func)(void*))
|
|||||||
|
|
||||||
for (size_t i = 0; i < NTHR; i++)
|
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);
|
ss_dassert(false);
|
||||||
}
|
}
|
||||||
|
@ -115,7 +115,7 @@ test2()
|
|||||||
acquire_time = 0;
|
acquire_time = 0;
|
||||||
spinlock_init(&lck);
|
spinlock_init(&lck);
|
||||||
spinlock_acquire(&lck);
|
spinlock_acquire(&lck);
|
||||||
thread_start(&handle, test2_helper, (void *)&lck);
|
thread_start(&handle, test2_helper, (void *)&lck, 0);
|
||||||
nanosleep(&sleeptime, NULL);
|
nanosleep(&sleeptime, NULL);
|
||||||
spinlock_release(&lck);
|
spinlock_release(&lck);
|
||||||
thread_wait(handle);
|
thread_wait(handle);
|
||||||
@ -222,7 +222,7 @@ test3()
|
|||||||
{
|
{
|
||||||
threadrun[i] = 0;
|
threadrun[i] = 0;
|
||||||
tnum[i] = i;
|
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++)
|
for (i = 0; i < THREADS; i++)
|
||||||
{
|
{
|
||||||
|
@ -10,58 +10,58 @@
|
|||||||
* of this software will be governed by version 2 or later of the General
|
* of this software will be governed by version 2 or later of the General
|
||||||
* Public License.
|
* Public License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <maxscale/thread.h>
|
#include <maxscale/thread.h>
|
||||||
|
#include <maxscale/log_manager.h>
|
||||||
|
|
||||||
/**
|
THREAD *thread_start(THREAD *thd, void (*entry)(void *), void *arg, size_t stack_size)
|
||||||
* @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)
|
|
||||||
{
|
{
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void thread_wait(THREAD thd)
|
||||||
* Wait for a running threads to complete.
|
|
||||||
*
|
|
||||||
* @param thd The thread handle
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
thread_wait(THREAD thd)
|
|
||||||
{
|
{
|
||||||
void *rval;
|
void *rval;
|
||||||
|
|
||||||
pthread_join((pthread_t)thd, &rval);
|
pthread_join((pthread_t)thd, &rval);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void thread_millisleep(int ms)
|
||||||
* Put the thread to sleep for a number of milliseconds
|
|
||||||
*
|
|
||||||
* @param ms Number of milliseconds to sleep
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
thread_millisleep(int ms)
|
|
||||||
{
|
{
|
||||||
struct timespec req;
|
struct timespec req;
|
||||||
req.tv_sec = ms / 1000;
|
req.tv_sec = ms / 1000;
|
||||||
|
@ -847,7 +847,7 @@ bool Worker::start()
|
|||||||
{
|
{
|
||||||
m_started = true;
|
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;
|
m_started = false;
|
||||||
}
|
}
|
||||||
|
@ -268,7 +268,7 @@ createInstance(const char *name, char **options, MXS_CONFIG_PARAMETER *params)
|
|||||||
* Launch a thread that checks the named pipe.
|
* Launch a thread that checks the named pipe.
|
||||||
*/
|
*/
|
||||||
THREAD thread;
|
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));
|
MXS_ERROR("Couldn't create a thread to check the named pipe: %s", strerror(errno));
|
||||||
error = true;
|
error = true;
|
||||||
|
@ -217,7 +217,7 @@ startMonitor(MXS_MONITOR *mon, const MXS_CONFIG_PARAMETER *params)
|
|||||||
handle->script = config_copy_string(params, "script");
|
handle->script = config_copy_string(params, "script");
|
||||||
handle->events = config_get_enum(params, "events", mxs_monitor_event_enum_values);
|
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);
|
MXS_ERROR("Failed to start monitor thread for monitor '%s'.", mon->name);
|
||||||
auroramon_free(handle);
|
auroramon_free(handle);
|
||||||
|
@ -177,7 +177,7 @@ startMonitor(MXS_MONITOR *mon, const MXS_CONFIG_PARAMETER *params)
|
|||||||
return NULL;
|
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_ERROR("Failed to start monitor thread for monitor '%s'.", mon->name);
|
||||||
hashtable_free(handle->galera_nodes_info);
|
hashtable_free(handle->galera_nodes_info);
|
||||||
|
@ -144,7 +144,7 @@ startMonitor(MXS_MONITOR *mon, const MXS_CONFIG_PARAMETER *params)
|
|||||||
return NULL;
|
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_ERROR("Failed to start monitor thread for monitor '%s'.", mon->name);
|
||||||
MXS_FREE(handle->script);
|
MXS_FREE(handle->script);
|
||||||
|
@ -282,7 +282,7 @@ startMonitor(MXS_MONITOR *monitor, const MXS_CONFIG_PARAMETER* params)
|
|||||||
MXS_FREE(handle);
|
MXS_FREE(handle);
|
||||||
handle = NULL;
|
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);
|
MXS_ERROR("Failed to start monitor thread for monitor '%s'.", monitor->name);
|
||||||
hashtable_free(handle->server_info);
|
hashtable_free(handle->server_info);
|
||||||
|
@ -135,7 +135,7 @@ startMonitor(MXS_MONITOR *mon, const MXS_CONFIG_PARAMETER *params)
|
|||||||
return NULL;
|
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_ERROR("Failed to start monitor thread for monitor '%s'.", mon->name);
|
||||||
MXS_FREE(handle->script);
|
MXS_FREE(handle->script);
|
||||||
|
Reference in New Issue
Block a user