From 8c795e46244df91bf9f5ad65b6268767dec054fc Mon Sep 17 00:00:00 2001 From: Brian Demsky Date: Thu, 11 Oct 2012 23:56:40 -0700 Subject: [PATCH] add two test cases --- test/condvar.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ test/mutextest.cc | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 test/condvar.cc create mode 100644 test/mutextest.cc diff --git a/test/condvar.cc b/test/condvar.cc new file mode 100644 index 0000000..488f9be --- /dev/null +++ b/test/condvar.cc @@ -0,0 +1,44 @@ +#include + +#include "threads.h" +#include "librace.h" +#include "stdatomic.h" +#include +#include "conditionvariable.h" + +std::mutex * m; +std::condition_variable *v; +int shareddata; + +static void a(void *obj) +{ + + m->lock(); + while(load_32(&shareddata)==0) + v->wait(*m); + m->unlock(); + +} + +static void b(void *obj) +{ + m->lock(); + store_32(&shareddata, (unsigned int) 1); + v->notify_all(); + m->unlock(); +} + +int user_main(int argc, char **argv) +{ + thrd_t t1, t2; + store_32(&shareddata, (unsigned int) 0); + m=new std::mutex(); + v=new std::condition_variable(); + + thrd_create(&t1, (thrd_start_t)&a, NULL); + thrd_create(&t2, (thrd_start_t)&b, NULL); + + thrd_join(t1); + thrd_join(t2); + return 0; +} diff --git a/test/mutextest.cc b/test/mutextest.cc new file mode 100644 index 0000000..968d9c4 --- /dev/null +++ b/test/mutextest.cc @@ -0,0 +1,38 @@ +#include + +#include "threads.h" +#include "librace.h" +#include "stdatomic.h" +#include +std::mutex * m; +int shareddata; + +static void a(void *obj) +{ + int i; + for(i=0;i<2;i++) { + if ((i%2)==0) { + m->lock(); + store_32(&shareddata,(unsigned int)i); + m->unlock(); + } else { + while(!m->try_lock()) + ; + store_32(&shareddata,(unsigned int)i); + m->unlock(); + } + } +} + +int user_main(int argc, char **argv) +{ + thrd_t t1, t2; + m=new std::mutex(); + + thrd_create(&t1, (thrd_start_t)&a, NULL); + thrd_create(&t2, (thrd_start_t)&a, NULL); + + thrd_join(t1); + thrd_join(t2); + return 0; +} -- 2.34.1