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

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