summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTormod Volden <debian.tormod@gmail.com>2022-08-03 08:51:37 +0200
committerTormod Volden <debian.tormod@gmail.com>2022-12-19 21:50:51 +0100
commit47751f0b7d09fdbad1f276b46ccabb81d651169b (patch)
tree2538d9c92aa04fd695d44db257666e2c8142cf9a
parent69e4ee63c97f48728fc12292d4674cd609af61e9 (diff)
downloadlibusb-47751f0b7d09fdbad1f276b46ccabb81d651169b.tar.gz
tests: Add multithreaded stress test for Posix platforms
Thanks to Ilya Averyanov for initial version. This test detects issues like #1124. Adaptation to Windows threads is on the wishlist (#1128). References #1124 References #1128 Closes #1189 Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
-rw-r--r--libusb/version_nano.h2
-rw-r--r--tests/Makefile.am6
-rw-r--r--tests/stress_mt.c45
3 files changed, 52 insertions, 1 deletions
diff --git a/libusb/version_nano.h b/libusb/version_nano.h
index fe58632..4bdd50e 100644
--- a/libusb/version_nano.h
+++ b/libusb/version_nano.h
@@ -1 +1 @@
-#define LIBUSB_NANO 11751
+#define LIBUSB_NANO 11752
diff --git a/tests/Makefile.am b/tests/Makefile.am
index cf6237e..d47d4ed 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -6,6 +6,12 @@ stress_SOURCES = stress.c libusb_testlib.h testlib.c
noinst_PROGRAMS = stress
+if PLATFORM_POSIX
+stress_mt_SOURCES = stress_mt.c
+
+noinst_PROGRAMS += stress_mt
+endif
+
if BUILD_UMOCKDEV_TEST
# NOTE: We add libumockdev-preload.so so that we can run tests in-process
# We also use -Wl,-lxxx as the compiler doesn't need it and libtool
diff --git a/tests/stress_mt.c b/tests/stress_mt.c
new file mode 100644
index 0000000..0299bfc
--- /dev/null
+++ b/tests/stress_mt.c
@@ -0,0 +1,45 @@
+#include <libusb.h>
+#include <stdio.h>
+#include <pthread.h>
+
+/* Test that creates and destroys contexts repeatedly */
+
+#define NTHREADS 8
+#define ITERS 64
+
+static void *test_init_and_exit(void * arg)
+{
+ long int threadno = (long int) arg;
+
+ printf("Thread %ld started\n", threadno);
+ for (int i = 0; i < ITERS; ++i) {
+ libusb_context *ctx = NULL;
+ int r;
+
+ r = libusb_init(&ctx);
+ if (r != LIBUSB_SUCCESS) {
+ printf("Failed to init libusb on iteration %d: %d", i, r);
+ return NULL;
+ }
+ libusb_exit(ctx);
+ }
+ printf("Thread %ld done\n", threadno);
+ return NULL;
+}
+
+int main(void)
+{
+ pthread_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);
+
+ for(t = 0; t < NTHREADS; t++)
+ pthread_join(threadId[t], NULL);
+
+ printf("All Done\n");
+
+ return 0;
+}