From f37c7224fbaf1d50369297840f33d23eebc04ef8 Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Fri, 30 Mar 2018 11:40:26 -0600 Subject: [PATCH] fix threading in example server --- examples/server/server.c | 8 +++-- tests/testsuite.c | 65 ---------------------------------- wolfssh/test.h | 75 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 77 insertions(+), 71 deletions(-) diff --git a/examples/server/server.c b/examples/server/server.c index 9af3f7f6..afec6476 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -592,7 +592,9 @@ THREAD_RETURN WOLFSSH_THREAD server_test(void* args) SOCKET_T clientFd = 0; SOCKADDR_IN_T clientAddr; socklen_t clientAddrSz = sizeof(clientAddr); +#ifndef SINGLE_THREADED THREAD_TYPE thread; +#endif WOLFSSH* ssh; thread_ctx_t* threadCtx; @@ -626,12 +628,12 @@ THREAD_RETURN WOLFSSH_THREAD server_test(void* args) threadCtx->id = threadCount++; #ifndef SINGLE_THREADED - start_thread(server_worker, threadCtx, &thread); + ThreadStart(server_worker, threadCtx, &thread); if (multipleConnections) - detach_thread(thread); + ThreadDetach(thread); else - join_thread(thread); + ThreadJoin(thread); #else server_worker(threadCtx); (void)thread; diff --git a/tests/testsuite.c b/tests/testsuite.c index ff1e19c0..c3b9aa1d 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -166,68 +166,3 @@ void WaitTcpReady(func_args* args) #endif } - -void ThreadStart(THREAD_FUNC fun, void* args, THREAD_TYPE* thread) -{ -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) - pthread_create(thread, 0, fun, args); - return; -#elif defined(WOLFSSL_TIRTOS) - /* Initialize the defaults and set the parameters. */ - Task_Params taskParams; - Task_Params_init(&taskParams); - taskParams.arg0 = (UArg)args; - taskParams.stackSize = 65535; - *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL); - if (*thread == NULL) { - printf("Failed to create new Task\n"); - } - Task_yield(); -#else - *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0); -#endif -} - - -void ThreadJoin(THREAD_TYPE thread) -{ -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) - pthread_join(thread, 0); -#elif defined(WOLFSSL_TIRTOS) - while(1) { - if (Task_getMode(thread) == Task_Mode_TERMINATED) { - Task_sleep(5); - break; - } - Task_yield(); - } -#else - int res = WaitForSingleObject((HANDLE)thread, INFINITE); - assert(res == WAIT_OBJECT_0); - res = CloseHandle((HANDLE)thread); - assert(res); - (void)res; /* Suppress un-used variable warning */ -#endif -} - - -void ThreadDetach(THREAD_TYPE thread) -{ -#if defined(_POSIX_THREADS) && !defined(__MINGW32__) - pthread_detach(thread); -#elif defined(WOLFSSL_TIRTOS) -#if 0 - while(1) { - if (Task_getMode(thread) == Task_Mode_TERMINATED) { - Task_sleep(5); - break; - } - Task_yield(); - } -#endif -#else - int res = CloseHandle((HANDLE)thread); - assert(res); - (void)res; /* Suppress un-used variable warning */ -#endif -} diff --git a/wolfssh/test.h b/wolfssh/test.h index 6585dd89..d8acccd9 100644 --- a/wolfssh/test.h +++ b/wolfssh/test.h @@ -171,9 +171,6 @@ typedef struct func_args { typedef THREAD_RETURN WOLFSSH_THREAD THREAD_FUNC(void*); -void ThreadStart(THREAD_FUNC, void*, THREAD_TYPE*); -void ThreadJoin(THREAD_TYPE); -void ThreadDetach(THREAD_TYPE); void WaitTcpReady(func_args*); @@ -521,5 +518,77 @@ static INLINE void tcp_listen(SOCKET_T* sockfd, word16* port, int useAnyAddr) #endif /* !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOL FSSL_TIRTOS) */ +static INLINE void ThreadStart(THREAD_FUNC fun, void* args, THREAD_TYPE* thread) +{ +#ifdef SINGLE_THREADED + (void)fun; + (void)args; + (void)thread; +#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) + pthread_create(thread, 0, fun, args); + return; +#elif defined(WOLFSSL_TIRTOS) + /* Initialize the defaults and set the parameters. */ + Task_Params taskParams; + Task_Params_init(&taskParams); + taskParams.arg0 = (UArg)args; + taskParams.stackSize = 65535; + *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL); + if (*thread == NULL) { + printf("Failed to create new Task\n"); + } + Task_yield(); +#else + *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0); +#endif +} + + +static INLINE void ThreadJoin(THREAD_TYPE thread) +{ +#ifdef SINGLE_THREADED + (void)thread; +#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) + pthread_join(thread, 0); +#elif defined(WOLFSSL_TIRTOS) + while(1) { + if (Task_getMode(thread) == Task_Mode_TERMINATED) { + Task_sleep(5); + break; + } + Task_yield(); + } +#else + int res = WaitForSingleObject((HANDLE)thread, INFINITE); + assert(res == WAIT_OBJECT_0); + res = CloseHandle((HANDLE)thread); + assert(res); + (void)res; /* Suppress un-used variable warning */ +#endif +} + + +static INLINE void ThreadDetach(THREAD_TYPE thread) +{ +#ifdef SINGLE_THREADED + (void)thread; +#elif defined(_POSIX_THREADS) && !defined(__MINGW32__) + pthread_detach(thread); +#elif defined(WOLFSSL_TIRTOS) +#if 0 + while(1) { + if (Task_getMode(thread) == Task_Mode_TERMINATED) { + Task_sleep(5); + break; + } + Task_yield(); + } +#endif +#else + int res = CloseHandle((HANDLE)thread); + assert(res); + (void)res; /* Suppress un-used variable warning */ +#endif +} #endif /* _WOLFSSH_TEST_H_ */