From ac80452d8a9b319e38869c8ff1434ddebfc025a5 Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Tue, 6 Nov 2012 19:26:13 -0800 Subject: [PATCH] test: add AB/BA deadlock test --- test/deadlock.cc | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 test/deadlock.cc diff --git a/test/deadlock.cc b/test/deadlock.cc new file mode 100644 index 00000000..3b26bec6 --- /dev/null +++ b/test/deadlock.cc @@ -0,0 +1,46 @@ +#include +#include +#include + +#include "librace.h" + +std::mutex *x; +std::mutex *y; +uint32_t shared = 0; + +static void a(void *obj) +{ + x->lock(); + y->lock(); + printf("shared = %u\n", load_32(&shared)); + y->unlock(); + x->unlock(); +} + +static void b(void *obj) +{ + y->lock(); + x->lock(); + store_32(&shared, 16); + printf("write shared = 16\n"); + x->unlock(); + y->unlock(); +} + +int user_main(int argc, char **argv) +{ + thrd_t t1, t2; + + x = new std::mutex(); + y = new std::mutex(); + + printf("Thread %d: creating 2 threads\n", thrd_current()); + thrd_create(&t1, (thrd_start_t)&a, NULL); + thrd_create(&t2, (thrd_start_t)&b, NULL); + + thrd_join(t1); + thrd_join(t2); + printf("Thread %d is finished\n", thrd_current()); + + return 0; +} -- 2.34.1