From 7246aee2282e74601de38389e5e34142ad752eee Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Sun, 11 Dec 2016 13:27:40 +0100 Subject: [PATCH] Moved start_thread to own file --- Makefile | 1 + src/httplib_start_thread.c | 67 ++++++++++++++++++++++++++++++++++++++ src/libhttp.c | 34 ------------------- 3 files changed, 68 insertions(+), 34 deletions(-) create mode 100644 src/httplib_start_thread.c diff --git a/Makefile b/Makefile index c368e0e5..ad1b15e7 100644 --- a/Makefile +++ b/Makefile @@ -167,6 +167,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_ssl_use_pem_file.c \ src/httplib_sslize.c \ src/httplib_start.c \ + src/httplib_start_thread.c \ src/httplib_start_thread_with_id.c \ src/httplib_stop.c \ src/httplib_store_body.c \ diff --git a/src/httplib_start_thread.c b/src/httplib_start_thread.c new file mode 100644 index 00000000..5659ba4b --- /dev/null +++ b/src/httplib_start_thread.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2016 Lammert Bies + * Copyright (c) 2013-2016 the Civetweb developers + * Copyright (c) 2004-2013 Sergey Lyubka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + + +#include "libhttp-private.h" + + +#if defined(_WIN32) + +int mg_start_thread(mg_thread_func_t f, void *p) { + +#if defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1) + /* Compile-time option to control stack size, e.g. -DUSE_STACK_SIZE=16384 + */ + return ((_beginthread((void(__cdecl *)(void *))f, USE_STACK_SIZE, p) == ((uintptr_t)(-1L))) ? -1 : 0); +#else + return ( (_beginthread((void(__cdecl *)(void *))f, 0, p) == ((uintptr_t)(-1L))) ? -1 : 0); +#endif /* defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1) */ + +} /* mg_start_thread */ + +#else + +int mg_start_thread(mg_thread_func_t func, void *param) { + + pthread_t thread_id; + pthread_attr_t attr; + int result; + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + +#if defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1) + /* Compile-time option to control stack size, + * e.g. -DUSE_STACK_SIZE=16384 */ + pthread_attr_setstacksize(&attr, USE_STACK_SIZE); +#endif /* defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1) */ + + result = pthread_create(&thread_id, &attr, func, param); + pthread_attr_destroy(&attr); + + return result; +} /* mg_start_thread */ + +#endif /* _WIN32 */ diff --git a/src/libhttp.c b/src/libhttp.c index b11cb12a..c1226db8 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -2169,18 +2169,6 @@ void XX_httplib_set_close_on_exec( SOCKET sock, struct mg_connection *conn ) { } /* XX_httplib_set_close_on_exec */ -int -mg_start_thread(mg_thread_func_t f, void *p) -{ -#if defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1) - /* Compile-time option to control stack size, e.g. -DUSE_STACK_SIZE=16384 - */ - return ((_beginthread((void(__cdecl *)(void *))f, USE_STACK_SIZE, p) == ((uintptr_t)(-1L))) ? -1 : 0); -#else - return ( (_beginthread((void(__cdecl *)(void *))f, 0, p) == ((uintptr_t)(-1L))) ? -1 : 0); -#endif /* defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1) */ -} - #else int XX_httplib_stat( struct mg_connection *conn, const char *path, struct file *filep ) { @@ -2212,26 +2200,4 @@ void XX_httplib_set_close_on_exec( SOCKET fd, struct mg_connection *conn ) { } /* XX_httplib_set_close_on_exec */ - -int mg_start_thread(mg_thread_func_t func, void *param) { - - pthread_t thread_id; - pthread_attr_t attr; - int result; - - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - -#if defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1) - /* Compile-time option to control stack size, - * e.g. -DUSE_STACK_SIZE=16384 */ - pthread_attr_setstacksize(&attr, USE_STACK_SIZE); -#endif /* defined(USE_STACK_SIZE) && (USE_STACK_SIZE > 1) */ - - result = pthread_create(&thread_id, &attr, func, param); - pthread_attr_destroy(&attr); - - return result; -} /* mg_start_thread */ - #endif /* _WIN32 */