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:
Johan Wikman 2017-08-14 14:17:17 +03:00
parent bdb0f7d8d7
commit e9b2a560b8
13 changed files with 87 additions and 54 deletions

View File

@ -29,11 +29,44 @@ MXS_BEGIN_DECLS
* Thread type and thread identifier function macros
*/
#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);
/**
* 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

View File

@ -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);

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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++)
{

View File

@ -10,58 +10,58 @@
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <maxscale/thread.h>
#include <maxscale/log_manager.h>
/**
* @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;

View File

@ -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;
}

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);