From ab61296036889ab2a2a9ef77aae808766039a311 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Mon, 27 Feb 2023 10:46:43 +0100 Subject: tests/stress_mt: Add Windows threads support Build the test on all platforms. Closes #1128 Signed-off-by: Tormod Volden --- libusb/version_nano.h | 2 +- msvc/libusb.sln | 34 ++++++++++++++++++++ msvc/stress_mt.vcxproj | 33 ++++++++++++++++++++ tests/Makefile.am | 10 ++---- tests/stress_mt.c | 84 +++++++++++++++++++++++++++++++++++++++++++++----- 5 files changed, 147 insertions(+), 16 deletions(-) create mode 100644 msvc/stress_mt.vcxproj diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 46cf934..f0b421b 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 11786 +#define LIBUSB_NANO 11787 diff --git a/msvc/libusb.sln b/msvc/libusb.sln index b76246c..c551fc8 100644 --- a/msvc/libusb.sln +++ b/msvc/libusb.sln @@ -22,6 +22,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sam3u_benchmark", "sam3u_be EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "stress", "stress.vcxproj", "{53942EFF-C810-458D-B3CB-EE5CE9F1E781}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "stress_mt", "stress_mt.vcxproj", "{9EAF1311-2BBB-4177-882B-DA7FA0AD7912}" +EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "testlibusb", "testlibusb.vcxproj", "{70828935-325B-4749-B381-0E55EF31AEE8}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "xusb", "xusb.vcxproj", "{3F3138D0-7AB7-4268-9BF3-1A3EA5503A11}" @@ -368,6 +370,38 @@ Global {53942EFF-C810-458D-B3CB-EE5CE9F1E781}.Release-MT|Win32.Build.0 = Release|Win32 {53942EFF-C810-458D-B3CB-EE5CE9F1E781}.Release-MT|x64.ActiveCfg = Release|x64 {53942EFF-C810-458D-B3CB-EE5CE9F1E781}.Release-MT|x64.Build.0 = Release|x64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug|ARM.ActiveCfg = Debug|ARM + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug|ARM.Build.0 = Debug|ARM + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug|ARM64.Build.0 = Debug|ARM64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug|Win32.ActiveCfg = Debug|Win32 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug|Win32.Build.0 = Debug|Win32 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug|x64.ActiveCfg = Debug|x64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug|x64.Build.0 = Debug|x64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug-MT|ARM.ActiveCfg = Debug|ARM + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug-MT|ARM.Build.0 = Debug|ARM + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug-MT|ARM64.ActiveCfg = Debug|ARM64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug-MT|ARM64.Build.0 = Debug|ARM64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug-MT|Win32.ActiveCfg = Debug|Win32 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug-MT|Win32.Build.0 = Debug|Win32 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug-MT|x64.ActiveCfg = Debug|x64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Debug-MT|x64.Build.0 = Debug|x64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release|ARM.ActiveCfg = Release|ARM + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release|ARM.Build.0 = Release|ARM + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release|ARM64.ActiveCfg = Release|ARM64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release|ARM64.Build.0 = Release|ARM64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release|Win32.ActiveCfg = Release|Win32 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release|Win32.Build.0 = Release|Win32 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release|x64.ActiveCfg = Release|x64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release|x64.Build.0 = Release|x64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release-MT|ARM.ActiveCfg = Release|ARM + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release-MT|ARM.Build.0 = Release|ARM + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release-MT|ARM64.ActiveCfg = Release|ARM64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release-MT|ARM64.Build.0 = Release|ARM64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release-MT|Win32.ActiveCfg = Release|Win32 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release-MT|Win32.Build.0 = Release|Win32 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release-MT|x64.ActiveCfg = Release|x64 + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912}.Release-MT|x64.Build.0 = Release|x64 {70828935-325B-4749-B381-0E55EF31AEE8}.Debug|ARM.ActiveCfg = Debug|ARM {70828935-325B-4749-B381-0E55EF31AEE8}.Debug|ARM.Build.0 = Debug|ARM {70828935-325B-4749-B381-0E55EF31AEE8}.Debug|ARM64.ActiveCfg = Debug|ARM64 diff --git a/msvc/stress_mt.vcxproj b/msvc/stress_mt.vcxproj new file mode 100644 index 0000000..db0d3b6 --- /dev/null +++ b/msvc/stress_mt.vcxproj @@ -0,0 +1,33 @@ + + + + + {9EAF1311-2BBB-4177-882B-DA7FA0AD7912} + + + + + + + + + + + + + + + + + + + + + {349ee8f9-7d25-4909-aaf5-ff3fade72187} + false + + + + + + \ No newline at end of file diff --git a/tests/Makefile.am b/tests/Makefile.am index 18da1eb..7831494 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -3,17 +3,11 @@ LDADD = ../libusb/libusb-1.0.la LIBS = stress_SOURCES = stress.c testlib.c - +stress_mt_SOURCES = stress_mt.c set_option_SOURCES = set_option.c testlib.c noinst_HEADERS = libusb_testlib.h -noinst_PROGRAMS = stress set_option - -if PLATFORM_POSIX -stress_mt_SOURCES = stress_mt.c - -noinst_PROGRAMS += stress_mt -endif +noinst_PROGRAMS = stress stress_mt set_option if BUILD_UMOCKDEV_TEST # NOTE: We add libumockdev-preload.so so that we can run tests in-process diff --git a/tests/stress_mt.c b/tests/stress_mt.c index c59a5c7..1caaff5 100644 --- a/tests/stress_mt.c +++ b/tests/stress_mt.c @@ -1,15 +1,85 @@ +/* + * libusb multi-thread test program + * Copyright 2022-2023 Tormod Volden + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + #include #include + +#if defined(PLATFORM_POSIX) + #include +typedef pthread_t thread_t; +typedef void * thread_return_t; +#define THREAD_RETURN_VALUE NULL +#define THREAD_CALL_TYPE + +static inline int thread_create(thread_t *thread, + thread_return_t (*thread_entry)(void *arg), void *arg) +{ + return pthread_create(thread, NULL, thread_entry, arg) == 0 ? 0 : -1; +} + +static inline void thread_join(thread_t thread) +{ + (void)pthread_join(thread, NULL); +} + +#elif defined(PLATFORM_WINDOWS) + +typedef HANDLE thread_t; +#define THREAD_RETURN_VALUE 0 +#define THREAD_CALL_TYPE __stdcall + +#if defined(__CYGWIN__) +typedef DWORD thread_return_t; +#else +#include +typedef unsigned thread_return_t; +#endif + +static inline int thread_create(thread_t *thread, + thread_return_t (__stdcall *thread_entry)(void *arg), void *arg) +{ +#if defined(__CYGWIN__) + *thread = CreateThread(NULL, 0, thread_entry, arg, 0, NULL); +#else + *thread = (HANDLE)_beginthreadex(NULL, 0, thread_entry, arg, 0, NULL); +#endif + return *thread != NULL ? 0 : -1; +} + +static inline void thread_join(thread_t thread) +{ + (void)WaitForSingleObject(thread, INFINITE); + (void)CloseHandle(thread); +} +#endif /* PLATFORM_WINDOWS */ /* Test that creates and destroys contexts repeatedly */ #define NTHREADS 8 #define ITERS 64 -static void *test_init_and_exit(void * arg) +static thread_return_t THREAD_CALL_TYPE init_and_exit(void * arg) { - long int threadno = (long int) arg; + long int threadno = (long int)(uintptr_t) arg; printf("Thread %ld started\n", threadno); for (int i = 0; i < ITERS; ++i) { @@ -19,25 +89,25 @@ static void *test_init_and_exit(void * arg) r = libusb_init_context(&ctx, /*options=*/NULL, /*num_options=*/0); if (r != LIBUSB_SUCCESS) { printf("Failed to init libusb on iteration %d: %d", i, r); - return NULL; + return (thread_return_t) THREAD_RETURN_VALUE; } libusb_exit(ctx); } printf("Thread %ld done\n", threadno); - return NULL; + return (thread_return_t) THREAD_RETURN_VALUE; } int main(void) { - pthread_t threadId[NTHREADS]; + thread_t threadId[NTHREADS]; long int t; printf("Starting multithreaded init and exit test...\n"); for(t = 0; t < NTHREADS; t++) - pthread_create(&threadId[t], NULL, &test_init_and_exit, (void *) t); + thread_create(&threadId[t], &init_and_exit, (void *)(uintptr_t) t); for(t = 0; t < NTHREADS; t++) - pthread_join(threadId[t], NULL); + thread_join(threadId[t]); printf("All Done\n"); -- cgit v1.2.1