summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorTormod Volden <debian.tormod@gmail.com>2023-02-27 10:46:43 +0100
committerTormod Volden <debian.tormod@gmail.com>2023-03-12 14:52:08 +0100
commitab61296036889ab2a2a9ef77aae808766039a311 (patch)
treeb68cb8eaeaedda24fd459a335b997f721905adee /tests
parent71dd672abe52d7c3a9c61373888a32984f0792a7 (diff)
downloadlibusb-ab61296036889ab2a2a9ef77aae808766039a311.tar.gz
tests/stress_mt: Add Windows threads support
Build the test on all platforms. Closes #1128 Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am10
-rw-r--r--tests/stress_mt.c84
2 files changed, 79 insertions, 15 deletions
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 <config.h>
+
#include <libusb.h>
#include <stdio.h>
+
+#if defined(PLATFORM_POSIX)
+
#include <pthread.h>
+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 <process.h>
+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");