From: Brian Norris Date: Wed, 7 Nov 2012 03:26:13 +0000 (-0800) Subject: test: add AB/BA deadlock test X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=e208de983fb59be56a2599e3e72d4e884d07d7eb;p=c11tester.git test: add AB/BA deadlock test --- 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; +}