X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=userprog.c;h=33319b1ce873c891b4bce429790196280b3acc12;hb=8b76d39eff3f6e27d416123292641ae36f88ca06;hp=4b106d88c11e8f0829008b5d7c47dd626e093c05;hpb=be5073a74591e8a9708219dac0da7217c0800ba2;p=c11tester.git diff --git a/userprog.c b/userprog.c index 4b106d88..33319b1c 100644 --- a/userprog.c +++ b/userprog.c @@ -1,20 +1,28 @@ #include +#include #include "libthreads.h" #include "libatomic.h" +#include "librace.h" static void a(atomic_int *obj) { int i; + int ret; - for (i = 0; i < 10; i++) { + store_32(&i, 10); + printf("load 32 yields: %d\n", load_32(&i)); + + for (i = 0; i < 2; i++) { printf("Thread %d, loop %d\n", thrd_current(), i); - switch (i % 4) { + switch (i % 2) { case 1: - atomic_load(obj); + ret = atomic_load(obj); + printf("Read value: %d\n", ret); break; - case 3: - atomic_store(obj, i); + case 0: + atomic_store(obj, i + 1); + printf("Write value: %d\n", i + 1); break; } } @@ -23,13 +31,18 @@ static void a(atomic_int *obj) void user_main() { thrd_t t1, t2; - atomic_int obj; + atomic_int *obj; + + obj = malloc(sizeof(*obj)); + + atomic_init(obj, 0); - printf("Creating 2 threads\n"); - thrd_create(&t1, (thrd_start_t)&a, &obj); - thrd_create(&t2, (thrd_start_t)&a, &obj); + printf("Thread %d: creating 2 threads\n", thrd_current()); + thrd_create(&t1, (thrd_start_t)&a, obj); + thrd_create(&t2, (thrd_start_t)&a, obj); thrd_join(t1); thrd_join(t2); - printf("Thread is finished\n"); + free(obj); + printf("Thread %d is finished\n", thrd_current()); }