test: add AB/BA deadlock test
authorBrian Norris <banorris@uci.edu>
Wed, 7 Nov 2012 03:26:13 +0000 (19:26 -0800)
committerBrian Norris <banorris@uci.edu>
Wed, 14 Nov 2012 22:34:20 +0000 (14:34 -0800)
test/deadlock.cc [new file with mode: 0644]

diff --git a/test/deadlock.cc b/test/deadlock.cc
new file mode 100644 (file)
index 0000000..3b26bec
--- /dev/null
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <threads.h>
+#include <mutex>
+
+#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;
+}