userprog: separate test 'program' out to userprog.c
authorBrian Norris <banorris@uci.edu>
Sat, 10 Mar 2012 03:18:43 +0000 (19:18 -0800)
committerBrian Norris <banorris@uci.edu>
Sat, 10 Mar 2012 03:22:37 +0000 (19:22 -0800)
Makefile
libthreads.c
libthreads.h
userprog.c [new file with mode: 0644]

index 073121e867eb926d30e3683bec67bd4d96e54342..7d7d0716a30ffe967435e08ec659a231994fd3eb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 CC=gcc
 BIN=libthreads
 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
 
 HEADERS=libthreads.h schedule.h common.h libatomic.h
 FLAGS=-Wall
 
index 894037960fc70f634414c6d1bb4844c0912254bd..f355c9c303446fefa4e8ab206bcfab2c5d768f3e 100644 (file)
@@ -122,31 +122,6 @@ struct thread *thread_current(void)
        return current;
 }
 
        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;
 int main()
 {
        struct thread user_thread;
index 1e2df96292bf365d087d7a7d8fe8cf5405f56b87..c0eb7d75c658e2e5d3de1842cefa75b67ec0b383 100644 (file)
@@ -17,4 +17,6 @@ void thread_join(struct thread *t);
 int thread_yield(void);
 struct thread *thread_current(void);
 
 int thread_yield(void);
 struct thread *thread_current(void);
 
+extern void user_main(void);
+
 #endif /* __LIBTHREADS_H__ */
 #endif /* __LIBTHREADS_H__ */
diff --git a/userprog.c b/userprog.c
new file mode 100644 (file)
index 0000000..8358a58
--- /dev/null
@@ -0,0 +1,29 @@
+#include <stdio.h>
+
+#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__);
+}