From: Brian Norris <banorris@uci.edu>
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=ac80452d8a9b319e38869c8ff1434ddebfc025a5;p=cdsspec-compiler.git

test: add AB/BA deadlock test
---

diff --git a/test/deadlock.cc b/test/deadlock.cc
new file mode 100644
index 0000000..3b26bec
--- /dev/null
+++ b/test/deadlock.cc
@@ -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;
+}