Cleaned up the use of thread types

The THREAD type was not used everywhere and pthread_t was used instead.
The thread creation function also returned the address of a stack allocated
value which isn't guaranteed to be usable.
This commit is contained in:
Markus Makela
2016-01-23 02:56:04 +02:00
parent da1a717dd8
commit 0111df3767
13 changed files with 79 additions and 43 deletions

View File

@ -1059,13 +1059,13 @@ int main(int argc, char **argv)
int daemon_pipe[2] = {-1, -1}; int daemon_pipe[2] = {-1, -1};
bool parent_process; bool parent_process;
int child_status; int child_status;
void** threads = NULL; /*< thread list */ THREAD* threads = NULL; /*< thread list */
char mysql_home[PATH_MAX+1]; char mysql_home[PATH_MAX+1];
char datadir_arg[10+PATH_MAX+1]; /*< '--datadir=' + PATH_MAX */ char datadir_arg[10+PATH_MAX+1]; /*< '--datadir=' + PATH_MAX */
char language_arg[11+PATH_MAX+1]; /*< '--language=' + PATH_MAX */ char language_arg[11+PATH_MAX+1]; /*< '--language=' + PATH_MAX */
char* cnf_file_path = NULL; /*< conf file, to be freed */ char* cnf_file_path = NULL; /*< conf file, to be freed */
char* cnf_file_arg = NULL; /*< conf filename from cmd-line arg */ char* cnf_file_arg = NULL; /*< conf filename from cmd-line arg */
void* log_flush_thr = NULL; THREAD log_flush_thr;
char* tmp_path; char* tmp_path;
char* tmp_var; char* tmp_var;
int option_index; int option_index;
@ -1940,9 +1940,14 @@ int main(int argc, char **argv)
* Start periodic log flusher thread. * Start periodic log flusher thread.
*/ */
log_flush_timeout_ms = 1000; log_flush_timeout_ms = 1000;
log_flush_thr = thread_start(
log_flush_cb, if (thread_start(&log_flush_thr, log_flush_cb, (void *) &log_flush_timeout_ms) == NULL)
(void *)&log_flush_timeout_ms); {
char* logerr = "Failed to start log flushing thread.";
print_log_n_stderr(true, !daemon_mode, logerr, logerr, 0);
rc = MAXSCALE_INTERNALERROR;
goto return_main;
}
/* /*
* Start the housekeeper thread * Start the housekeeper thread
@ -1954,14 +1959,22 @@ int main(int argc, char **argv)
* configured as the main thread will also poll. * configured as the main thread will also poll.
*/ */
n_threads = config_threadcount(); n_threads = config_threadcount();
threads = (void **)calloc(n_threads, sizeof(void *)); threads = calloc(n_threads, sizeof(THREAD));
/*< /*<
* Start server threads. * Start server threads.
*/ */
for (thread_id = 0; thread_id < n_threads - 1; thread_id++) for (thread_id = 0; thread_id < n_threads - 1; thread_id++)
{ {
threads[thread_id] = thread_start(worker_thread_main, (void *)(thread_id + 1)); if (thread_start(&threads[thread_id], worker_thread_main,
(void *)(thread_id + 1)) == NULL)
{
char* logerr = "Failed to start worker thread.";
print_log_n_stderr(true, !daemon_mode, logerr, logerr, 0);
rc = MAXSCALE_INTERNALERROR;
goto return_main;
} }
}
MXS_NOTICE("MaxScale started with %d server threads.", config_threadcount()); MXS_NOTICE("MaxScale started with %d server threads.", config_threadcount());
/** /**
* Successful start, notify the parent process that it can exit. * Successful start, notify the parent process that it can exit.

View File

@ -20,6 +20,7 @@
#include <housekeeper.h> #include <housekeeper.h>
#include <thread.h> #include <thread.h>
#include <spinlock.h> #include <spinlock.h>
#include <log_manager.h>
/** /**
* @file housekeeper.c Provide a mechanism to run periodic tasks * @file housekeeper.c Provide a mechanism to run periodic tasks
@ -54,6 +55,7 @@ static SPINLOCK tasklock = SPINLOCK_INIT;
static int do_shutdown = 0; static int do_shutdown = 0;
long hkheartbeat = 0; /*< One heartbeat is 100 milliseconds */ long hkheartbeat = 0; /*< One heartbeat is 100 milliseconds */
static THREAD hk_thr_handle;
static void hkthread(void *); static void hkthread(void *);
@ -63,7 +65,10 @@ static void hkthread(void *);
void void
hkinit() hkinit()
{ {
thread_start(hkthread, NULL); if (thread_start(&hk_thr_handle, hkthread, NULL) == NULL)
{
MXS_ERROR("Failed to start housekeeper thread.");
}
} }
/** /**

View File

@ -87,7 +87,7 @@ spinlock_acquire(SPINLOCK *lock)
} }
} }
lock->acquired++; lock->acquired++;
lock->owner = THREAD_SHELF(); lock->owner = thread_self();
atomic_add(&(lock->waiting), -1); atomic_add(&(lock->waiting), -1);
#endif #endif
} }
@ -112,7 +112,7 @@ spinlock_acquire_nowait(SPINLOCK *lock)
#endif #endif
#if SPINLOCK_PROFILE #if SPINLOCK_PROFILE
lock->acquired++; lock->acquired++;
lock->owner = THREAD_SHELF(); lock->owner = thread_self();
#endif #endif
return TRUE; return TRUE;
} }

View File

@ -111,7 +111,7 @@ static int
test2() test2()
{ {
SPINLOCK lck; SPINLOCK lck;
void *handle; THREAD handle;
struct timespec sleeptime; struct timespec sleeptime;
sleeptime.tv_sec = 10; sleeptime.tv_sec = 10;
@ -120,7 +120,7 @@ struct timespec sleeptime;
acquire_time = 0; acquire_time = 0;
spinlock_init(&lck); spinlock_init(&lck);
spinlock_acquire(&lck); spinlock_acquire(&lck);
handle = thread_start(test2_helper, (void *)&lck); thread_start(&handle, test2_helper, (void *)&lck);
nanosleep(&sleeptime, NULL); nanosleep(&sleeptime, NULL);
spinlock_release(&lck); spinlock_release(&lck);
thread_wait(handle); thread_wait(handle);
@ -206,7 +206,7 @@ static int
test3() test3()
{ {
// SPINLOCK lck; // SPINLOCK lck;
void *handle[THREADS]; THREAD handle[THREADS];
int i; int i;
int tnum[THREADS]; int tnum[THREADS];
time_t rawtime; time_t rawtime;
@ -220,7 +220,7 @@ time_t rawtime;
for (i = 0; i<THREADS; i++) { for (i = 0; i<THREADS; i++) {
threadrun[i] = 0; threadrun[i] = 0;
tnum[i] = i; tnum[i] = i;
handle[i] = thread_start(test3_helper, &tnum[i]); thread_start(&handle[i], test3_helper, &tnum[i]);
} }
for (i = 0; i<THREADS; i++) { for (i = 0; i<THREADS; i++) {
fprintf(stderr, "spinlock_test 3 thread %d ran %d times, no wait %d times before waits.\n", i, threadrun[i], nowait[i]); fprintf(stderr, "spinlock_test 3 thread %d ran %d times, no wait %d times before waits.\n", i, threadrun[i], nowait[i]);

View File

@ -16,7 +16,7 @@
* Copyright MariaDB Corporation Ab 2013-2014 * Copyright MariaDB Corporation Ab 2013-2014
*/ */
#include <thread.h> #include <thread.h>
#include <pthread.h>
/** /**
* @file thread.c - Implementation of thread related operations * @file thread.c - Implementation of thread related operations
* *
@ -33,20 +33,18 @@
/** /**
* Start a polling thread * Start a polling thread
* *
* @param thd Pointer to the THREAD object
* @param entry The entry point to call * @param entry The entry point to call
* @param arg The argument to pass the thread entry point * @param arg The argument to pass the thread entry point
* @return The thread handle * @return The thread handle or NULL if an error occurred
*/ */
void * THREAD *thread_start(THREAD *thd, void (*entry)(void *), void *arg)
thread_start(void (*entry)(void *), void *arg)
{ {
pthread_t thd; if (pthread_create(thd, NULL, (void *(*)(void *))entry, arg) != 0)
if (pthread_create(&thd, NULL, (void *(*)(void *))entry, arg) != 0)
{ {
return NULL; return NULL;
} }
return (void *)thd; return thd;
} }
/** /**
@ -55,7 +53,7 @@ thread_start(void (*entry)(void *), void *arg)
* @param thd The thread handle * @param thd The thread handle
*/ */
void void
thread_wait(void *thd) thread_wait(THREAD thd)
{ {
void *rval; void *rval;
@ -71,7 +69,6 @@ void
thread_millisleep(int ms) thread_millisleep(int ms)
{ {
struct timespec req; struct timespec req;
req.tv_sec = ms / 1000; req.tv_sec = ms / 1000;
req.tv_nsec = (ms % 1000) * 1000000; req.tv_nsec = (ms % 1000) * 1000000;
nanosleep(&req, NULL); nanosleep(&req, NULL);

View File

@ -17,22 +17,25 @@
* *
* Copyright MariaDB Corporation Ab 2013-2014 * Copyright MariaDB Corporation Ab 2013-2014
*/ */
#include <pthread.h>
/** /**
* @file thread.h The gateway threading interface * @file thread.h The gateway threading interface
* *
* An encapsulation of the threading used by the gateway. This is designed to * An encapsulation of the threading used by the gateway. This is designed to
* isolate the majority of the gateway code from th epthread library, enabling * isolate the majority of the gateway code from the pthread library, enabling
* the gateway to be ported to a different threading package with the minimum * the gateway to be ported to a different threading package with the minimum
* of changes. * of changes.
*/ */
/**
* Thread type and thread identifier function macros
*/
#include <pthread.h>
#define THREAD pthread_t #define THREAD pthread_t
#define THREAD_SHELF pthread_self #define thread_self() pthread_self()
extern void *thread_start(void (*entry)(void *), void *arg); extern THREAD *thread_start(THREAD *thd, void (*entry)(void *), void *arg);
extern void thread_wait(void *thd); extern void thread_wait(THREAD thd);
extern void thread_millisleep(int ms); extern void thread_millisleep(int ms);
#endif #endif

View File

@ -200,7 +200,11 @@ startMonitor(void *arg, void* opt)
memset(handle->events, true, sizeof(handle->events)); memset(handle->events, true, sizeof(handle->events));
} }
handle->tid = (THREAD) thread_start(monitorMain, mon); if (thread_start(&handle->thread, monitorMain, mon) == NULL)
{
MXS_ERROR("Failed to start monitor thread for monitor '%s'.", mon->name);
}
return handle; return handle;
} }
@ -216,7 +220,7 @@ stopMonitor(void *arg)
GALERA_MONITOR *handle = (GALERA_MONITOR *) mon->handle; GALERA_MONITOR *handle = (GALERA_MONITOR *) mon->handle;
handle->shutdown = 1; handle->shutdown = 1;
thread_wait((void *) handle->tid); thread_wait(handle->thread);
} }
/** /**

View File

@ -51,7 +51,7 @@
typedef struct typedef struct
{ {
SPINLOCK lock; /**< The monitor spinlock */ SPINLOCK lock; /**< The monitor spinlock */
pthread_t tid; /**< id of monitor thread */ THREAD thread; /**< Monitor thread */
int shutdown; /**< Flag to shutdown the monitor thread */ int shutdown; /**< Flag to shutdown the monitor thread */
int status; /**< Monitor status */ int status; /**< Monitor status */
unsigned long id; /**< Monitor ID */ unsigned long id; /**< Monitor ID */

View File

@ -173,7 +173,12 @@ startMonitor(void *arg, void* opt)
{ {
memset(handle->events, true, sizeof(handle->events)); memset(handle->events, true, sizeof(handle->events));
} }
handle->tid = (THREAD) thread_start(monitorMain, mon);
if (thread_start(handle->thread, monitorMain, mon) == NULL)
{
MXS_ERROR("Failed to start monitor thread for monitor '%s'.", mon->name);
}
return handle; return handle;
} }
@ -189,7 +194,7 @@ stopMonitor(void *arg)
MM_MONITOR *handle = (MM_MONITOR *) mon->handle; MM_MONITOR *handle = (MM_MONITOR *) mon->handle;
handle->shutdown = 1; handle->shutdown = 1;
thread_wait((void *) handle->tid); thread_wait((void *) handle->thread);
} }
/** /**

View File

@ -44,7 +44,7 @@
typedef struct typedef struct
{ {
SPINLOCK lock; /**< The monitor spinlock */ SPINLOCK lock; /**< The monitor spinlock */
pthread_t tid; /**< id of monitor thread */ THREAD thread; /**< Monitor thread */
int shutdown; /**< Flag to shutdown the monitor thread */ int shutdown; /**< Flag to shutdown the monitor thread */
int status; /**< Monitor status */ int status; /**< Monitor status */
unsigned long id; /**< Monitor ID */ unsigned long id; /**< Monitor ID */

View File

@ -205,7 +205,11 @@ startMonitor(void *arg, void* opt)
memset(handle->events, true, sizeof(handle->events)); memset(handle->events, true, sizeof(handle->events));
} }
handle->tid = (THREAD) thread_start(monitorMain, monitor); if (thread_start(&handle->thread, monitorMain, monitor) == NULL)
{
MXS_ERROR("Failed to start monitor thread for monitor '%s'.", monitor->name);
}
return handle; return handle;
} }
@ -221,7 +225,7 @@ stopMonitor(void *arg)
MYSQL_MONITOR *handle = (MYSQL_MONITOR *) mon->handle; MYSQL_MONITOR *handle = (MYSQL_MONITOR *) mon->handle;
handle->shutdown = 1; handle->shutdown = 1;
thread_wait((void *) handle->tid); thread_wait(handle->thread);
} }
/** /**

View File

@ -60,7 +60,7 @@
typedef struct typedef struct
{ {
SPINLOCK lock; /**< The monitor spinlock */ SPINLOCK lock; /**< The monitor spinlock */
pthread_t tid; /**< id of monitor thread */ THREAD thread; /**< Monitor thread */
int shutdown; /**< Flag to shutdown the monitor thread */ int shutdown; /**< Flag to shutdown the monitor thread */
int status; /**< Monitor status */ int status; /**< Monitor status */
unsigned long id; /**< Monitor ID */ unsigned long id; /**< Monitor ID */

View File

@ -164,7 +164,12 @@ startMonitor(void *arg, void* opt)
{ {
memset(handle->events, true, sizeof(handle->events)); memset(handle->events, true, sizeof(handle->events));
} }
handle->tid = (THREAD) thread_start(monitorMain, mon);
if (thread_start(&handle->thread, monitorMain, mon) == NULL)
{
MXS_ERROR("Failed to start monitor thread for monitor '%s'.", mon->name);
}
return handle; return handle;
} }
@ -180,7 +185,7 @@ stopMonitor(void *arg)
MYSQL_MONITOR *handle = (MYSQL_MONITOR *) mon->handle; MYSQL_MONITOR *handle = (MYSQL_MONITOR *) mon->handle;
handle->shutdown = 1; handle->shutdown = 1;
thread_wait((void *) handle->tid); thread_wait(handle->thread);
} }
/** /**