diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d0937922..3f9bc52da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -497,7 +497,6 @@ endif() if (WIN32) set(LWS_MAX_SMP 1) -set(LWS_WITH_THREADPOOL 0) endif() if (LWS_WITHOUT_SERVER) @@ -592,6 +591,10 @@ set(LWS_GLIB_INCLUDE_DIRS CACHE PATH "Path to the glib include directory") set(LWS_GLIB_LIBRARIES CACHE PATH "Path to the glib library") set(LWS_LIBMOUNT_INCLUDE_DIRS CACHE PATH "Path to the libmount include directory") set(LWS_LIBMOUNT_LIBRARIES CACHE PATH "Path to the libmount library") +# on unix, these are in the toolchain. On win32 you have to put them somewhere +# yourself and point to them here +set(LWS_EXT_PTHREAD_INCLUDE_DIR CACHE PATH "Path to an external pthreads include directory") +set(LWS_EXT_PTHREAD_LIBRARIES CACHE PATH "Path to an external pthreads library") if (NOT LWS_WITH_SSL) @@ -911,6 +914,14 @@ if (NOT LWS_HAVE_GETIFADDRS) set(LWS_BUILTIN_GETIFADDRS 1) endif() +if (LWS_EXT_PTHREAD_INCLUDE_DIR) + set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES};${LWS_EXT_PTHREAD_INCLUDE_DIR}) + include_directories(${LWS_EXT_PTHREAD_INCLUDE_DIR}) + + list(APPEND LIB_LIST ${LWS_EXT_PTHREAD_LIBRARIES}) + set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} " -DHAVE_STRUCT_TIMESPEC=1") +endif() + CHECK_INCLUDE_FILE(dlfcn.h LWS_HAVE_DLFCN_H) CHECK_INCLUDE_FILE(fcntl.h LWS_HAVE_FCNTL_H) CHECK_INCLUDE_FILE(in6addr.h LWS_HAVE_IN6ADDR_H) @@ -1015,9 +1026,9 @@ CHECK_C_SOURCE_COMPILES("#include if (LWS_HAVE_PTHREAD_H) if ((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) - set(CMAKE_REQUIRED_FLAGS "-pthread -Wno-error=unused-command-line-argument") + set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} "-pthread -Wno-error=unused-command-line-argument") else() - set(CMAKE_REQUIRED_FLAGS "-pthread") + set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} "-pthread") endif() CHECK_C_SOURCE_COMPILES("#define _GNU_SOURCE @@ -1103,8 +1114,6 @@ if (LWS_WITH_FSMOUNT AND ${CMAKE_SYSTEM_NAME} STREQUAL "Linux") list(APPEND SOURCES lib/misc/fsmount.c) endif() - - if (LWS_WITH_FILE_OPS) list(APPEND SOURCES lib/core/vfs.c) endif() @@ -1250,7 +1259,7 @@ if (LWS_ROLE_MQTT AND LWS_WITH_CLIENT) ) endif() -if (LWS_WITH_THREADPOOL AND UNIX AND LWS_HAVE_PTHREAD_H) +if (LWS_WITH_THREADPOOL AND LWS_HAVE_PTHREAD_H) list(APPEND SOURCES lib/misc/threadpool/threadpool.c) endif() @@ -1796,6 +1805,7 @@ source_group("Resources" FILES ${RESOURCES}) # set(LWS_LIBRARIES) + if (LWS_WITH_STATIC) if (LWS_STATIC_PIC) set(CMAKE_POSITION_INDEPENDENT_CODE ON) @@ -1896,6 +1906,9 @@ set(LIB_LIST) # # Find libraries. # +if (LWS_EXT_PTHREAD_INCLUDE_DIR) + list(APPEND LIB_LIST ${LWS_EXT_PTHREAD_LIBRARIES}) +endif() # # ZLIB (needed for deflate extension and if LWS_WITH_HTTP_STREAM_COMPRESSION) @@ -1977,7 +1990,7 @@ if (LWS_WITH_SSL) include_directories("${inc}" "${inc}/wolfssl") endforeach() endif() - set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIRS}) + set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${OPENSSL_INCLUDE_DIRS}) set(VARIA wolfSSL_) list(APPEND LIB_LIST "${WOLFSSL_LIBRARIES}") @@ -2020,7 +2033,7 @@ if (LWS_WITH_SSL) if (NOT LWS_WITH_MBEDTLS) # older (0.98) Openssl lacks this - set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIRS}) + set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${OPENSSL_INCLUDE_DIRS}) check_include_file(openssl/ecdh.h LWS_HAVE_OPENSSL_ECDH_H) if (LWS_SSL_SERVER_WITH_ECDH_CERT AND NOT LWS_HAVE_OPENSSL_ECDH_H) @@ -2181,7 +2194,7 @@ foreach (lib ${LWS_LIBRARIES}) endforeach() set (temp ${CMAKE_REQUIRED_LIBRARIES}) -set(CMAKE_REQUIRED_LIBRARIES ${LIB_LIST}) +set(CMAKE_REQUIRED_LIBRARIES ${LIB_LIST} ${CMAKE_REQUIRED_LIBRARIES}) if (LWS_WITH_ZLIB) if (LWS_WITH_BUNDLED_ZLIB) diff --git a/READMEs/README.build-windows.md b/READMEs/README.build-windows.md index 677e1f84a..7b8536940 100644 --- a/READMEs/README.build-windows.md +++ b/READMEs/README.build-windows.md @@ -73,3 +73,22 @@ there. ``` After that you can run the test apps OK. + +## pthreads + +It's amazing but after all these years windows doesn't offer pthreads compatibility +itself. Just like the many other missing POSIX bits like fork(). + +I downloaded the latest (2012) zip release of pthreads-win32 from here + +ftp://sourceware.org/pub/pthreads-win32 + +Then I created a dir "C:\Program Files (x86)\pthreads", and copied the `dll`, +`include` and `lib` subdirs from the `prebuilt` folder in the zip there. + +The cmake incantation to build against pthreads set up like that is + +``` + $ cmake .. -DLWS_EXT_PTHREAD_INCLUDE_DIR="C:\Program Files (x86)\pthreads\include" -DLWS_EXT_PTHREAD_LIBRARIES="C:\Program Files (x86)\pthreads\lib\x64\libpthreadGC2.a" -DLWS_WITH_MINIMAL_EXAMPLES=1 +``` + diff --git a/cmake/lws_config.h.in b/cmake/lws_config.h.in index 178857683..de5e2f675 100644 --- a/cmake/lws_config.h.in +++ b/cmake/lws_config.h.in @@ -18,6 +18,8 @@ #cmakedefine LWS_LIBRARY_VERSION_NUMBER +#cmakedefine LWS_EXT_PTHREAD_LIBRARIES + #cmakedefine LWS_AVOID_SIGPIPE_IGN #cmakedefine LWS_BUILD_HASH "${LWS_BUILD_HASH}" #cmakedefine LWS_BUILTIN_GETIFADDRS diff --git a/lib/misc/threadpool/threadpool.c b/lib/misc/threadpool/threadpool.c index deab75717..61048ba95 100644 --- a/lib/misc/threadpool/threadpool.c +++ b/lib/misc/threadpool/threadpool.c @@ -26,6 +26,12 @@ #define _GNU_SOURCE #endif +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif #include #include "private-lib-core.h" @@ -131,10 +137,10 @@ __lws_threadpool_task_dump(struct lws_threadpool_task *task, char *buf, int len) } if (task->acc_running) - runms = task->acc_running; + runms = (int)task->acc_running; if (task->acc_syncing) - syncms = task->acc_syncing; + syncms = (int)task->acc_syncing; if (!task->done) { buf += lws_snprintf(buf, end - buf, diff --git a/minimal-examples/http-client/minimal-http-client-attach/CMakeLists.txt b/minimal-examples/http-client/minimal-http-client-attach/CMakeLists.txt index a033c3d79..822bfbbe2 100644 --- a/minimal-examples/http-client/minimal-http-client-attach/CMakeLists.txt +++ b/minimal-examples/http-client/minimal-http-client-attach/CMakeLists.txt @@ -16,6 +16,12 @@ MACRO(require_pthreads result) else() message(FATAL_ERROR "threading support requires pthreads") endif() + else() + if (WIN32) + set(PTHREAD_LIB ${LWS_EXT_PTHREAD_LIBRARIES}) + else() + set(PTHREAD_LIB pthread) + endif() endif() ENDMACRO() @@ -88,9 +94,9 @@ if (requirements) add_executable(${SAMP} ${SRCS}) if (websockets_shared) - target_link_libraries(${SAMP} websockets_shared pthread) + target_link_libraries(${SAMP} websockets_shared ${PTHREAD_LIB}) add_dependencies(${SAMP} websockets_shared) else() - target_link_libraries(${SAMP} websockets pthread) + target_link_libraries(${SAMP} websockets ${PTHREAD_LIB}) endif() endif() diff --git a/minimal-examples/http-client/minimal-http-client-attach/minimal-http-client-attach.c b/minimal-examples/http-client/minimal-http-client-attach/minimal-http-client-attach.c index dfd0032bc..20a035233 100644 --- a/minimal-examples/http-client/minimal-http-client-attach/minimal-http-client-attach.c +++ b/minimal-examples/http-client/minimal-http-client-attach/minimal-http-client-attach.c @@ -15,6 +15,12 @@ #include #include #include +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif #include static struct lws_context *context; @@ -119,10 +125,7 @@ attach_callback(struct lws_context *context, int tsi, void *opaque) /* * Even though it was asked for from a different thread, we are called * back by lws from the lws event loop thread context - */ - lwsl_user("%s: called from tid %p\n", __func__, (void *)pthread_self()); - - /* + * * We can set up our operations on the lws event loop and return so * they can happen asynchronously */ @@ -218,7 +221,6 @@ int main(int argc, const char **argv) logs = atoi(p); lws_set_log_level(logs, NULL); - lwsl_user("%s: main thread tid %p\n", __func__, (void *)pthread_self()); lwsl_user("LWS minimal http client attach\n"); pthread_mutex_init(&lock, NULL); diff --git a/minimal-examples/http-server/minimal-http-server-eventlib-smp/CMakeLists.txt b/minimal-examples/http-server/minimal-http-server-eventlib-smp/CMakeLists.txt index 3611a79cc..25c0b3148 100644 --- a/minimal-examples/http-server/minimal-http-server-eventlib-smp/CMakeLists.txt +++ b/minimal-examples/http-server/minimal-http-server-eventlib-smp/CMakeLists.txt @@ -15,6 +15,12 @@ MACRO(require_pthreads result) else() message(FATAL_ERROR "threading support requires pthreads") endif() + else() + if (WIN32) + set(PTHREAD_LIB ${LWS_EXT_PTHREAD_LIBRARIES}) + else() + set(PTHREAD_LIB pthread) + endif() endif() ENDMACRO() @@ -85,9 +91,9 @@ if (requirements) add_executable(${SAMP} ${SRCS}) if (websockets_shared) - target_link_libraries(${SAMP} websockets_shared pthread) - add_dependencies(${SAMP} websockets_shared pthread) + target_link_libraries(${SAMP} websockets_shared ${PTHREAD_LIB}) + add_dependencies(${SAMP} websockets_shared ${PTHREAD_LIB}) else() - target_link_libraries(${SAMP} websockets pthread) + target_link_libraries(${SAMP} websockets ${PTHREAD_LIB}) endif() endif() diff --git a/minimal-examples/http-server/minimal-http-server-eventlib-smp/minimal-http-server-eventlib-smp.c b/minimal-examples/http-server/minimal-http-server-eventlib-smp/minimal-http-server-eventlib-smp.c index 6ea9b6eac..f82fa0485 100644 --- a/minimal-examples/http-server/minimal-http-server-eventlib-smp/minimal-http-server-eventlib-smp.c +++ b/minimal-examples/http-server/minimal-http-server-eventlib-smp/minimal-http-server-eventlib-smp.c @@ -18,6 +18,13 @@ #include #include +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif + #include #define COUNT_THREADS 8 diff --git a/minimal-examples/http-server/minimal-http-server-smp/CMakeLists.txt b/minimal-examples/http-server/minimal-http-server-smp/CMakeLists.txt index b32eb7093..ab9c8336a 100644 --- a/minimal-examples/http-server/minimal-http-server-smp/CMakeLists.txt +++ b/minimal-examples/http-server/minimal-http-server-smp/CMakeLists.txt @@ -15,6 +15,12 @@ MACRO(require_pthreads result) else() message(FATAL_ERROR "threading support requires pthreads") endif() + else() + if (WIN32) + set(PTHREAD_LIB ${LWS_EXT_PTHREAD_LIBRARIES}) + else() + set(PTHREAD_LIB pthread) + endif() endif() ENDMACRO() @@ -85,9 +91,9 @@ if (requirements) add_executable(${SAMP} ${SRCS}) if (websockets_shared) - target_link_libraries(${SAMP} websockets_shared pthread) - add_dependencies(${SAMP} websockets_shared pthread) + target_link_libraries(${SAMP} websockets_shared ${PTHREAD_LIB}) + add_dependencies(${SAMP} websockets_shared) else() - target_link_libraries(${SAMP} websockets pthread) + target_link_libraries(${SAMP} websockets ${PTHREAD_LIB}) endif() endif() \ No newline at end of file diff --git a/minimal-examples/http-server/minimal-http-server-smp/minimal-http-server-smp.c b/minimal-examples/http-server/minimal-http-server-smp/minimal-http-server-smp.c index ae07e4a8e..b6a344151 100644 --- a/minimal-examples/http-server/minimal-http-server-smp/minimal-http-server-smp.c +++ b/minimal-examples/http-server/minimal-http-server-smp/minimal-http-server-smp.c @@ -21,6 +21,12 @@ #include #include #include +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif #include #define COUNT_THREADS 8 diff --git a/minimal-examples/http-server/minimal-http-server-sse-ring/CMakeLists.txt b/minimal-examples/http-server/minimal-http-server-sse-ring/CMakeLists.txt index be14a0d57..384f0a786 100644 --- a/minimal-examples/http-server/minimal-http-server-sse-ring/CMakeLists.txt +++ b/minimal-examples/http-server/minimal-http-server-sse-ring/CMakeLists.txt @@ -15,6 +15,12 @@ MACRO(require_pthreads result) else() message(FATAL_ERROR "threading support requires pthreads") endif() + else() + if (WIN32) + set(PTHREAD_LIB ${LWS_EXT_PTHREAD_LIBRARIES}) + else() + set(PTHREAD_LIB pthread) + endif() endif() ENDMACRO() @@ -85,9 +91,9 @@ if (requirements) add_executable(${SAMP} ${SRCS}) if (websockets_shared) - target_link_libraries(${SAMP} websockets_shared pthread) + target_link_libraries(${SAMP} websockets_shared ${PTHREAD_LIB}) add_dependencies(${SAMP} websockets_shared) else() - target_link_libraries(${SAMP} websockets pthread) + target_link_libraries(${SAMP} websockets ${PTHREAD_LIB}) endif() endif() diff --git a/minimal-examples/http-server/minimal-http-server-sse-ring/minimal-http-server-sse-ring.c b/minimal-examples/http-server/minimal-http-server-sse-ring/minimal-http-server-sse-ring.c index 2e51c2f9b..1d4f210c4 100644 --- a/minimal-examples/http-server/minimal-http-server-sse-ring/minimal-http-server-sse-ring.c +++ b/minimal-examples/http-server/minimal-http-server-sse-ring/minimal-http-server-sse-ring.c @@ -19,6 +19,12 @@ #include #include #include +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif #include #include @@ -86,7 +92,11 @@ thread_spam(void *d) { struct vhd *vhd = (struct vhd *)d; struct msg amsg; - int len = 128, index = 1, n; + int len = 128, index = 1, n, whoami = 0; + + for (n = 0; n < (int)LWS_ARRAY_SIZE(vhd->pthread_spam); n++) + if (pthread_equal(pthread_self(), vhd->pthread_spam[n])) + whoami = n + 1; do { /* don't generate output if nobody connected */ @@ -108,10 +118,9 @@ thread_spam(void *d) goto wait_unlock; } n = lws_snprintf((char *)amsg.payload, len, - "%s: tid: %p, msg: %d", __func__, - (void *)pthread_self(), index++); + "%s: tid: %d, msg: %d", __func__, whoami, index++); amsg.len = n; - n = lws_ring_insert(vhd->ring, &amsg, 1); + n = (int)lws_ring_insert(vhd->ring, &amsg, 1); if (n != 1) { __minimal_destroy_message(&amsg); lwsl_user("dropping!\n"); @@ -131,7 +140,7 @@ wait: } while (!vhd->finished); - lwsl_notice("thread_spam %p exiting\n", (void *)pthread_self()); + lwsl_notice("thread_spam %d exiting\n", whoami); pthread_exit(NULL); @@ -186,8 +195,7 @@ callback_sse(struct lws *wsi, enum lws_callback_reasons reason, void *user, init_fail: vhd->finished = 1; for (n = 0; n < (int)LWS_ARRAY_SIZE(vhd->pthread_spam); n++) - if (vhd->pthread_spam[n]) - pthread_join(vhd->pthread_spam[n], &retval); + pthread_join(vhd->pthread_spam[n], &retval); if (vhd->ring) lws_ring_destroy(vhd->ring); diff --git a/minimal-examples/http-server/minimal-http-server-sse/CMakeLists.txt b/minimal-examples/http-server/minimal-http-server-sse/CMakeLists.txt index 35e819f80..781ae309a 100644 --- a/minimal-examples/http-server/minimal-http-server-sse/CMakeLists.txt +++ b/minimal-examples/http-server/minimal-http-server-sse/CMakeLists.txt @@ -14,6 +14,12 @@ MACRO(require_pthreads result) else() message(FATAL_ERROR "threading support requires pthreads") endif() + else() + if (WIN32) + set(PTHREAD_LIB ${LWS_EXT_PTHREAD_LIBRARIES}) + else() + set(PTHREAD_LIB pthread) + endif() endif() ENDMACRO() @@ -84,9 +90,9 @@ if (requirements) add_executable(${SAMP} ${SRCS}) if (websockets_shared) - target_link_libraries(${SAMP} websockets_shared) + target_link_libraries(${SAMP} websockets_shared ${PTHREAD_LIB}) add_dependencies(${SAMP} websockets_shared) else() - target_link_libraries(${SAMP} websockets) + target_link_libraries(${SAMP} websockets ${PTHREAD_LIB}) endif() endif() diff --git a/minimal-examples/http-server/minimal-http-server-sse/minimal-http-server-sse.c b/minimal-examples/http-server/minimal-http-server-sse/minimal-http-server-sse.c index cb60774be..c7f8e7d38 100644 --- a/minimal-examples/http-server/minimal-http-server-sse/minimal-http-server-sse.c +++ b/minimal-examples/http-server/minimal-http-server-sse/minimal-http-server-sse.c @@ -19,6 +19,12 @@ #include #include #include +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif #include /* diff --git a/minimal-examples/mqtt-client/minimal-mqtt-client-multi/minimal-mqtt-client-multi.c b/minimal-examples/mqtt-client/minimal-mqtt-client-multi/minimal-mqtt-client-multi.c index 1659a2160..ab6967f87 100644 --- a/minimal-examples/mqtt-client/minimal-mqtt-client-multi/minimal-mqtt-client-multi.c +++ b/minimal-examples/mqtt-client/minimal-mqtt-client-multi/minimal-mqtt-client-multi.c @@ -11,6 +11,12 @@ #include #include #include +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif #include #include diff --git a/minimal-examples/mqtt-client/minimal-mqtt-client/minimal-mqtt-client.c b/minimal-examples/mqtt-client/minimal-mqtt-client/minimal-mqtt-client.c index d5fc448a1..8cbce657b 100644 --- a/minimal-examples/mqtt-client/minimal-mqtt-client/minimal-mqtt-client.c +++ b/minimal-examples/mqtt-client/minimal-mqtt-client/minimal-mqtt-client.c @@ -11,6 +11,12 @@ #include #include #include +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif #include #include diff --git a/minimal-examples/ws-client/minimal-ws-client-ping/CMakeLists.txt b/minimal-examples/ws-client/minimal-ws-client-ping/CMakeLists.txt index f40d6309b..c6d11332b 100644 --- a/minimal-examples/ws-client/minimal-ws-client-ping/CMakeLists.txt +++ b/minimal-examples/ws-client/minimal-ws-client-ping/CMakeLists.txt @@ -15,8 +15,15 @@ MACRO(require_pthreads result) else() message(FATAL_ERROR "threading support requires pthreads") endif() + else() + if (WIN32) + set(PTHREAD_LIB ${LWS_EXT_PTHREAD_LIBRARIES}) + else() + set(PTHREAD_LIB pthread) + endif() endif() ENDMACRO() + # If we are being built as part of lws, confirm current build config supports # reqconfig, else skip building ourselves. # @@ -84,9 +91,9 @@ if (requirements) add_executable(${SAMP} ${SRCS}) if (websockets_shared) - target_link_libraries(${SAMP} websockets_shared pthread) - add_dependencies(${SAMP} websockets_shared pthread) + target_link_libraries(${SAMP} websockets_shared ${PTHREAD_LIB}) + add_dependencies(${SAMP} websockets_shared) else() - target_link_libraries(${SAMP} websockets pthread) + target_link_libraries(${SAMP} websockets ${PTHREAD_LIB}) endif() endif() diff --git a/minimal-examples/ws-client/minimal-ws-client-ping/minimal-ws-client-ping.c b/minimal-examples/ws-client/minimal-ws-client-ping/minimal-ws-client-ping.c index a8589f0ef..ff6107b60 100644 --- a/minimal-examples/ws-client/minimal-ws-client-ping/minimal-ws-client-ping.c +++ b/minimal-examples/ws-client/minimal-ws-client-ping/minimal-ws-client-ping.c @@ -14,6 +14,12 @@ #include #include #include +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif #include static struct lws_context *context; diff --git a/minimal-examples/ws-client/minimal-ws-client-spam/CMakeLists.txt b/minimal-examples/ws-client/minimal-ws-client-spam/CMakeLists.txt index da3c51b6d..42fd97063 100644 --- a/minimal-examples/ws-client/minimal-ws-client-spam/CMakeLists.txt +++ b/minimal-examples/ws-client/minimal-ws-client-spam/CMakeLists.txt @@ -15,8 +15,15 @@ MACRO(require_pthreads result) else() message(FATAL_ERROR "threading support requires pthreads") endif() + else() + if (WIN32) + set(PTHREAD_LIB ${LWS_EXT_PTHREAD_LIBRARIES}) + else() + set(PTHREAD_LIB pthread) + endif() endif() ENDMACRO() + # If we are being built as part of lws, confirm current build config supports # reqconfig, else skip building ourselves. # @@ -84,9 +91,9 @@ if (requirements) add_executable(${SAMP} ${SRCS}) if (websockets_shared) - target_link_libraries(${SAMP} websockets_shared pthread) - add_dependencies(${SAMP} websockets_shared pthread) + target_link_libraries(${SAMP} websockets_shared ${PTHREAD_LIB}) + add_dependencies(${SAMP} websockets_shared) else() - target_link_libraries(${SAMP} websockets pthread) + target_link_libraries(${SAMP} websockets ${PTHREAD_LIB}) endif() endif() diff --git a/minimal-examples/ws-client/minimal-ws-client-spam/minimal-ws-client-spam.c b/minimal-examples/ws-client/minimal-ws-client-spam/minimal-ws-client-spam.c index ec6f52344..3809ef50b 100644 --- a/minimal-examples/ws-client/minimal-ws-client-spam/minimal-ws-client-spam.c +++ b/minimal-examples/ws-client/minimal-ws-client-spam/minimal-ws-client-spam.c @@ -13,6 +13,12 @@ #include #include #include +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif #include enum { diff --git a/minimal-examples/ws-client/minimal-ws-client-tx/CMakeLists.txt b/minimal-examples/ws-client/minimal-ws-client-tx/CMakeLists.txt index 6d9c81817..70fd5e272 100644 --- a/minimal-examples/ws-client/minimal-ws-client-tx/CMakeLists.txt +++ b/minimal-examples/ws-client/minimal-ws-client-tx/CMakeLists.txt @@ -15,8 +15,15 @@ MACRO(require_pthreads result) else() message(FATAL_ERROR "threading support requires pthreads") endif() + else() + if (WIN32) + set(PTHREAD_LIB ${LWS_EXT_PTHREAD_LIBRARIES}) + else() + set(PTHREAD_LIB pthread) + endif() endif() ENDMACRO() + # If we are being built as part of lws, confirm current build config supports # reqconfig, else skip building ourselves. # @@ -84,9 +91,9 @@ if (requirements) add_executable(${SAMP} ${SRCS}) if (websockets_shared) - target_link_libraries(${SAMP} websockets_shared pthread) - add_dependencies(${SAMP} websockets_shared pthread) + target_link_libraries(${SAMP} websockets_shared ${PTHREAD_LIB}) + add_dependencies(${SAMP} websockets_shared) else() - target_link_libraries(${SAMP} websockets pthread) + target_link_libraries(${SAMP} websockets ${PTHREAD_LIB}) endif() endif() \ No newline at end of file diff --git a/minimal-examples/ws-client/minimal-ws-client-tx/minimal-ws-client.c b/minimal-examples/ws-client/minimal-ws-client-tx/minimal-ws-client.c index 137201d5b..c83e23e96 100644 --- a/minimal-examples/ws-client/minimal-ws-client-tx/minimal-ws-client.c +++ b/minimal-examples/ws-client/minimal-ws-client-tx/minimal-ws-client.c @@ -21,6 +21,12 @@ #include #include #include +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif #include static int interrupted; @@ -70,7 +76,11 @@ thread_spam(void *d) struct per_vhost_data__minimal *vhd = (struct per_vhost_data__minimal *)d; struct msg amsg; - int len = 128, index = 1, n; + int len = 128, index = 1, n, whoami = 0; + + for (n = 0; n < (int)LWS_ARRAY_SIZE(vhd->pthread_spam); n++) + if (pthread_equal(pthread_self(), vhd->pthread_spam[n])) + whoami = n + 1; do { /* don't generate output if client not connected */ @@ -92,10 +102,9 @@ thread_spam(void *d) goto wait_unlock; } n = lws_snprintf((char *)amsg.payload + LWS_PRE, len, - "tid: %p, msg: %d", - (void *)pthread_self(), index++); + "tid: %d, msg: %d", whoami, index++); amsg.len = n; - n = lws_ring_insert(vhd->ring, &amsg, 1); + n = (int)lws_ring_insert(vhd->ring, &amsg, 1); if (n != 1) { __minimal_destroy_message(&amsg); lwsl_user("dropping!\n"); @@ -114,7 +123,7 @@ wait: } while (!vhd->finished); - lwsl_notice("thread_spam %p exiting\n", (void *)pthread_self()); + lwsl_notice("thread_spam %d exiting\n", whoami); pthread_exit(NULL); @@ -188,8 +197,7 @@ callback_minimal_broker(struct lws *wsi, enum lws_callback_reasons reason, init_fail: vhd->finished = 1; for (n = 0; n < (int)LWS_ARRAY_SIZE(vhd->pthread_spam); n++) - if (vhd->pthread_spam[n]) - pthread_join(vhd->pthread_spam[n], &retval); + pthread_join(vhd->pthread_spam[n], &retval); if (vhd->ring) lws_ring_destroy(vhd->ring); diff --git a/minimal-examples/ws-server/minimal-ws-server-threadpool/CMakeLists.txt b/minimal-examples/ws-server/minimal-ws-server-threadpool/CMakeLists.txt index 111ba701c..f557ca2ba 100644 --- a/minimal-examples/ws-server/minimal-ws-server-threadpool/CMakeLists.txt +++ b/minimal-examples/ws-server/minimal-ws-server-threadpool/CMakeLists.txt @@ -15,6 +15,12 @@ MACRO(require_pthreads result) else() message(FATAL_ERROR "threading support requires pthreads") endif() + else() + if (WIN32) + set(PTHREAD_LIB ${LWS_EXT_PTHREAD_LIBRARIES}) + else() + set(PTHREAD_LIB pthread) + endif() endif() ENDMACRO() @@ -86,9 +92,9 @@ if (requirements) add_executable(${SAMP} ${SRCS}) if (websockets_shared) - target_link_libraries(${SAMP} websockets_shared pthread) + target_link_libraries(${SAMP} websockets_shared ${PTHREAD_LIB}) add_dependencies(${SAMP} websockets_shared) else() - target_link_libraries(${SAMP} websockets pthread) + target_link_libraries(${SAMP} websockets ${PTHREAD_LIB}) endif() endif() diff --git a/minimal-examples/ws-server/minimal-ws-server-threadpool/minimal-ws-server-threadpool.c b/minimal-examples/ws-server/minimal-ws-server-threadpool/minimal-ws-server-threadpool.c index e0d8a9d00..61beb54da 100644 --- a/minimal-examples/ws-server/minimal-ws-server-threadpool/minimal-ws-server-threadpool.c +++ b/minimal-examples/ws-server/minimal-ws-server-threadpool/minimal-ws-server-threadpool.c @@ -21,6 +21,12 @@ #include #include #include +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif #include #define LWS_PLUGIN_STATIC @@ -125,5 +131,7 @@ int main(int argc, const char **argv) lws_context_destroy(context); + lwsl_user("%s: exiting cleanly...\n", __func__); + return 0; } diff --git a/minimal-examples/ws-server/minimal-ws-server-threadpool/protocol_lws_minimal_threadpool.c b/minimal-examples/ws-server/minimal-ws-server-threadpool/protocol_lws_minimal_threadpool.c index 55998a794..5fd7d8739 100644 --- a/minimal-examples/ws-server/minimal-ws-server-threadpool/protocol_lws_minimal_threadpool.c +++ b/minimal-examples/ws-server/minimal-ws-server-threadpool/protocol_lws_minimal_threadpool.c @@ -43,6 +43,10 @@ struct task_data { uint64_t pos, end; }; +#if defined(WIN32) +static void usleep(unsigned long l) { Sleep(l / 1000); } +#endif + /* * Create the private data for the task * @@ -254,6 +258,8 @@ callback_minimal(struct lws *wsi, enum lws_callback_reasons reason, */ task = lws_threadpool_get_task_wsi(wsi); + if (!task) + break; n = lws_threadpool_task_status(task, &_user); lwsl_debug("%s: LWS_CALLBACK_SERVER_WRITEABLE: status %d\n", __func__, n); @@ -277,7 +283,7 @@ callback_minimal(struct lws *wsi, enum lws_callback_reasons reason, lws_set_timeout(wsi, PENDING_TIMEOUT_THREADPOOL_TASK, 5); - n = strlen(priv->result + LWS_PRE); + n = (int)strlen(priv->result + LWS_PRE); m = lws_write(wsi, (unsigned char *)priv->result + LWS_PRE, n, LWS_WRITE_TEXT); if (m < n) { diff --git a/minimal-examples/ws-server/minimal-ws-server-threads-smp/CMakeLists.txt b/minimal-examples/ws-server/minimal-ws-server-threads-smp/CMakeLists.txt index a6c798f59..bf2ef9d59 100644 --- a/minimal-examples/ws-server/minimal-ws-server-threads-smp/CMakeLists.txt +++ b/minimal-examples/ws-server/minimal-ws-server-threads-smp/CMakeLists.txt @@ -15,6 +15,12 @@ MACRO(require_pthreads result) else() message(FATAL_ERROR "threading support requires pthreads") endif() + else() + if (WIN32) + set(PTHREAD_LIB ${LWS_EXT_PTHREAD_LIBRARIES}) + else() + set(PTHREAD_LIB pthread) + endif() endif() ENDMACRO() @@ -85,9 +91,9 @@ if (requirements) add_executable(${SAMP} ${SRCS}) if (websockets_shared) - target_link_libraries(${SAMP} websockets_shared pthread) + target_link_libraries(${SAMP} websockets_shared ${PTHREAD_LIB}) add_dependencies(${SAMP} websockets_shared) else() - target_link_libraries(${SAMP} websockets pthread) + target_link_libraries(${SAMP} websockets ${PTHREAD_LIB}) endif() endif() diff --git a/minimal-examples/ws-server/minimal-ws-server-threads-smp/minimal-ws-server.c b/minimal-examples/ws-server/minimal-ws-server-threads-smp/minimal-ws-server.c index 8303f5cba..8a74eb747 100644 --- a/minimal-examples/ws-server/minimal-ws-server-threads-smp/minimal-ws-server.c +++ b/minimal-examples/ws-server/minimal-ws-server-threads-smp/minimal-ws-server.c @@ -21,6 +21,12 @@ #include #include #include +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif #include #define LWS_PLUGIN_STATIC diff --git a/minimal-examples/ws-server/minimal-ws-server-threads-smp/protocol_lws_minimal.c b/minimal-examples/ws-server/minimal-ws-server-threads-smp/protocol_lws_minimal.c index 375530c13..1ee7ce705 100644 --- a/minimal-examples/ws-server/minimal-ws-server-threads-smp/protocol_lws_minimal.c +++ b/minimal-examples/ws-server/minimal-ws-server-threads-smp/protocol_lws_minimal.c @@ -83,7 +83,11 @@ thread_spam(void *d) struct per_vhost_data__minimal *vhd = (struct per_vhost_data__minimal *)d; struct msg amsg; - int len = 128, index = 1, n; + int len = 128, index = 1, n, whoami = 0; + + for (n = 0; n < (int)LWS_ARRAY_SIZE(vhd->pthread_spam); n++) + if (pthread_equal(pthread_self(), vhd->pthread_spam[n])) + whoami = n + 1; do { /* don't generate output if nobody connected */ @@ -105,10 +109,10 @@ thread_spam(void *d) goto wait_unlock; } n = lws_snprintf((char *)amsg.payload + LWS_PRE, len, - "%s: spam tid: %p, msg: %d", vhd->config, - (void *)pthread_self(), index++); + "%s: spam tid: %d, msg: %d", vhd->config, + whoami, index++); amsg.len = n; - n = lws_ring_insert(vhd->ring, &amsg, 1); + n = (int)lws_ring_insert(vhd->ring, &amsg, 1); if (n != 1) { __minimal_destroy_message(&amsg); lwsl_user("dropping!\n"); @@ -127,7 +131,7 @@ wait: } while (!vhd->finished); - lwsl_notice("thread_spam %p exiting\n", (void *)pthread_self()); + lwsl_notice("thread_spam %d exiting\n", whoami); pthread_exit(NULL); @@ -199,8 +203,7 @@ callback_minimal(struct lws *wsi, enum lws_callback_reasons reason, init_fail: vhd->finished = 1; for (n = 0; n < (int)LWS_ARRAY_SIZE(vhd->pthread_spam); n++) - if (vhd->pthread_spam[n]) - pthread_join(vhd->pthread_spam[n], &retval); + pthread_join(vhd->pthread_spam[n], &retval); if (vhd->ring) lws_ring_destroy(vhd->ring); @@ -231,7 +234,7 @@ init_fail: } n = lws_snprintf(temp + LWS_PRE, sizeof(temp) - LWS_PRE, - "svc tid:%p, %s", (void *)pthread_self(), + "svc, %s", (char *)pmsg->payload + LWS_PRE); /* notice we allowed for LWS_PRE in the payload already */ @@ -265,8 +268,7 @@ init_fail: break; case LWS_CALLBACK_EVENT_WAIT_CANCELLED: - lwsl_notice("LWS_CALLBACK_EVENT_WAIT_CANCELLED in svc tid %p\n", - (void *)pthread_self()); + lwsl_notice("LWS_CALLBACK_EVENT_WAIT_CANCELLED in svc\n"); if (!vhd) break; /* diff --git a/minimal-examples/ws-server/minimal-ws-server-threads/CMakeLists.txt b/minimal-examples/ws-server/minimal-ws-server-threads/CMakeLists.txt index 283a1828e..c8bbb0721 100644 --- a/minimal-examples/ws-server/minimal-ws-server-threads/CMakeLists.txt +++ b/minimal-examples/ws-server/minimal-ws-server-threads/CMakeLists.txt @@ -15,6 +15,12 @@ MACRO(require_pthreads result) else() message(FATAL_ERROR "threading support requires pthreads") endif() + else() + if (WIN32) + set(PTHREAD_LIB ${LWS_EXT_PTHREAD_LIBRARIES}) + else() + set(PTHREAD_LIB pthread) + endif() endif() ENDMACRO() @@ -88,9 +94,9 @@ if (requirements) add_executable(${SAMP} ${SRCS}) if (websockets_shared) - target_link_libraries(${SAMP} websockets_shared pthread) + target_link_libraries(${SAMP} websockets_shared ${PTHREAD_LIB}) add_dependencies(${SAMP} websockets_shared) else() - target_link_libraries(${SAMP} websockets pthread) + target_link_libraries(${SAMP} websockets ${PTHREAD_LIB}) endif() endif() diff --git a/minimal-examples/ws-server/minimal-ws-server-threads/minimal-ws-server.c b/minimal-examples/ws-server/minimal-ws-server-threads/minimal-ws-server.c index 40d7fc782..fb96a1bf0 100644 --- a/minimal-examples/ws-server/minimal-ws-server-threads/minimal-ws-server.c +++ b/minimal-examples/ws-server/minimal-ws-server-threads/minimal-ws-server.c @@ -21,6 +21,12 @@ #include #include #include +#if defined(WIN32) +#define HAVE_STRUCT_TIMESPEC +#if defined(pid_t) +#undef pid_t +#endif +#endif #include #define LWS_PLUGIN_STATIC diff --git a/minimal-examples/ws-server/minimal-ws-server-threads/protocol_lws_minimal.c b/minimal-examples/ws-server/minimal-ws-server-threads/protocol_lws_minimal.c index f3d83dac6..8f230bbf3 100644 --- a/minimal-examples/ws-server/minimal-ws-server-threads/protocol_lws_minimal.c +++ b/minimal-examples/ws-server/minimal-ws-server-threads/protocol_lws_minimal.c @@ -83,7 +83,11 @@ thread_spam(void *d) struct per_vhost_data__minimal *vhd = (struct per_vhost_data__minimal *)d; struct msg amsg; - int len = 128, index = 1, n; + int len = 128, index = 1, n, whoami = 0; + + for (n = 0; n < (int)LWS_ARRAY_SIZE(vhd->pthread_spam); n++) + if (pthread_equal(pthread_self(), vhd->pthread_spam[n])) + whoami = n + 1; do { /* don't generate output if nobody connected */ @@ -105,8 +109,8 @@ thread_spam(void *d) goto wait_unlock; } n = lws_snprintf((char *)amsg.payload + LWS_PRE, len, - "%s: tid: %p, msg: %d", vhd->config, - (void *)pthread_self(), index++); + "%s: tid: %d, msg: %d", vhd->config, + whoami, index++); amsg.len = n; n = lws_ring_insert(vhd->ring, &amsg, 1); if (n != 1) { @@ -127,7 +131,7 @@ wait: } while (!vhd->finished); - lwsl_notice("thread_spam %p exiting\n", (void *)pthread_self()); + lwsl_notice("thread_spam %d exiting\n", whoami); pthread_exit(NULL);