From: Brian Norris Date: Sat, 10 Mar 2012 03:18:43 +0000 (-0800) Subject: userprog: separate test 'program' out to userprog.c X-Git-Url: http://plrg.eecs.uci.edu/git/?p=c11tester.git;a=commitdiff_plain;h=b37435df867398e452d2c42d6a557d6ec140e2a1 userprog: separate test 'program' out to userprog.c --- diff --git a/Makefile b/Makefile index 073121e8..7d7d0716 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC=gcc BIN=libthreads -SOURCE=libthreads.c schedule.c libatomic.c +SOURCE=libthreads.c schedule.c libatomic.c userprog.c HEADERS=libthreads.h schedule.h common.h libatomic.h FLAGS=-Wall diff --git a/libthreads.c b/libthreads.c index 89403796..f355c9c3 100644 --- a/libthreads.c +++ b/libthreads.c @@ -122,31 +122,6 @@ struct thread *thread_current(void) return current; } -void a(int *parm) -{ - int i; - - for (i = 0; i < 10; i++) { - printf("Thread %d, magic number %d, loop %d\n", thread_current()->index, *parm, i); - if (i % 2) - thread_yield(); - } -} - -void user_main() -{ - struct thread t1, t2; - int i = 17, j = 13; - - printf("%s() creating 2 threads\n", __func__); - thread_create(&t1, &a, &i); - thread_create(&t2, &a, &j); - - thread_join(&t1); - thread_join(&t2); - printf("%s() is finished\n", __func__); -} - int main() { struct thread user_thread; diff --git a/libthreads.h b/libthreads.h index 1e2df962..c0eb7d75 100644 --- a/libthreads.h +++ b/libthreads.h @@ -17,4 +17,6 @@ void thread_join(struct thread *t); int thread_yield(void); struct thread *thread_current(void); +extern void user_main(void); + #endif /* __LIBTHREADS_H__ */ diff --git a/userprog.c b/userprog.c new file mode 100644 index 00000000..8358a58c --- /dev/null +++ b/userprog.c @@ -0,0 +1,29 @@ +#include + +#include "libthreads.h" +#include "libatomic.h" + +static void a(atomic_int *obj) +{ + int i; + + for (i = 0; i < 10; i++) { + printf("Thread %d, loop %d\n", thread_current()->index, i); + if (i % 2) + atomic_load(obj); + } +} + +void user_main() +{ + struct thread t1, t2; + atomic_int obj; + + printf("%s() creating 2 threads\n", __func__); + thread_create(&t1, &a, &obj); + thread_create(&t2, &a, &obj); + + thread_join(&t1); + thread_join(&t2); + printf("%s() is finished\n", __func__); +}