Update.
[pintos-anon] / solutions / p2-null.patch
1 diff -urp -X pat src/threads/init.c~ src/threads/init.c
2 --- src/threads/init.c~ 2005-03-30 10:26:02.000000000 -0800
3 +++ src/threads/init.c  2005-03-30 20:05:20.000000000 -0800
4 @@ -120,8 +120,11 @@ main (void)
5    /* Run a user program. */
6    if (initial_program != NULL)
7      {
8 +      struct semaphore done;
9 +      sema_init (&done, 0, "done");
10        printf ("\nExecuting '%s':\n", initial_program);
11 -      process_wait (process_execute (initial_program));
12 +      process_execute (initial_program, &done);
13 +      sema_down (&done);
14      }
15  #else
16    /* Run the compiled-in test function. */
17 diff -urp -X pat src/threads/synch.c~ src/threads/synch.c
18 --- src/threads/synch.c~        2005-03-29 22:23:20.000000000 -0800
19 +++ src/threads/synch.c 2005-03-30 20:01:45.000000000 -0800
20 @@ -117,7 +117,7 @@ sema_self_test (void) 
21    printf ("Testing semaphores...");
22    sema_init (&sema[0], 0, "ping");
23    sema_init (&sema[1], 0, "pong");
24 -  thread_create ("sema-test", PRI_DEFAULT, sema_test_helper, &sema);
25 +  thread_create ("sema-test", PRI_DEFAULT, sema_test_helper, &sema, NULL);
26    for (i = 0; i < 10; i++) 
27      {
28        sema_up (&sema[0]);
29 diff -urp -X pat src/threads/test.c~ src/threads/test.c
30 --- src/threads/test.c~ 2005-01-22 11:14:57.000000000 -0800
31 +++ src/threads/test.c  2005-03-30 20:03:11.000000000 -0800
32 @@ -93,7 +93,7 @@ test_sleep (int thread_cnt, int iteratio
33        t->iterations = 0;
34  
35        snprintf (name, sizeof name, "thread %d", i);
36 -      thread_create (name, PRI_DEFAULT, sleeper, t);
37 +      thread_create (name, PRI_DEFAULT, sleeper, t, NULL);
38      }
39    
40    /* Wait long enough for all the threads to finish. */
41 diff -urp -X pat src/threads/thread.c~ src/threads/thread.c
42 --- src/threads/thread.c~       2005-03-30 10:26:13.000000000 -0800
43 +++ src/threads/thread.c        2005-03-30 20:08:17.000000000 -0800
44 @@ -51,7 +51,8 @@ static void kernel_thread (thread_func *
45  static void idle (void *aux UNUSED);
46  static struct thread *running_thread (void);
47  static struct thread *next_thread_to_run (void);
48 -static void init_thread (struct thread *, const char *name, int priority);
49 +static void init_thread (struct thread *, const char *name, int priority,
50 +                         struct semaphore *);
51  static bool is_thread (struct thread *);
52  static void *alloc_frame (struct thread *, size_t size);
53  static void schedule (void);
54 @@ -78,7 +79,7 @@ thread_init (void) 
55  
56    /* Set up a thread structure for the running thread. */
57    initial_thread = running_thread ();
58 -  init_thread (initial_thread, "main", PRI_DEFAULT);
59 +  init_thread (initial_thread, "main", PRI_DEFAULT, NULL);
60    initial_thread->status = THREAD_RUNNING;
61    initial_thread->tid = allocate_tid ();
62  }
63 @@ -88,7 +89,7 @@ thread_init (void) 
64  void
65  thread_start (void) 
66  {
67 -  thread_create ("idle", PRI_DEFAULT, idle, NULL);
68 +  thread_create ("idle", PRI_DEFAULT, idle, NULL, NULL);
69    intr_enable ();
70  }
71  
72 @@ -133,7 +134,8 @@ thread_print_stats (void) 
73     Priority scheduling is the goal of Problem 1-3. */
74  tid_t
75  thread_create (const char *name, int priority,
76 -               thread_func *function, void *aux) 
77 +               thread_func *function, void *aux,
78 +               struct semaphore *completion)
79  {
80    struct thread *t;
81    struct kernel_thread_frame *kf;
82 @@ -149,7 +151,7 @@ thread_create (const char *name, int pri
83      return TID_ERROR;
84  
85    /* Initialize thread. */
86 -  init_thread (t, name, priority);
87 +  init_thread (t, name, priority, completion);
88    tid = t->tid = allocate_tid ();
89  
90    /* Stack frame for kernel_thread(). */
91 @@ -245,6 +247,9 @@ thread_exit (void) 
92  {
93    ASSERT (!intr_context ());
94  
95 +  if (thread_current ()->completion)
96 +    sema_up (thread_current ()->completion);
97 +
98  #ifdef USERPROG
99    process_exit ();
100  #endif
101 @@ -342,7 +347,8 @@ is_thread (struct thread *t) 
102  /* Does basic initialization of T as a blocked thread named
103     NAME. */
104  static void
105 -init_thread (struct thread *t, const char *name, int priority)
106 +init_thread (struct thread *t, const char *name, int priority,
107 +             struct semaphore *completion)
108  {
109    ASSERT (t != NULL);
110    ASSERT (PRI_MIN <= priority && priority <= PRI_MAX);
111 @@ -353,6 +359,7 @@ init_thread (struct thread *t, const cha
112    strlcpy (t->name, name, sizeof t->name);
113    t->stack = (uint8_t *) t + PGSIZE;
114    t->priority = priority;
115 +  t->completion = completion;
116    t->magic = THREAD_MAGIC;
117  }
118  
119 diff -urp -X pat src/threads/thread.h~ src/threads/thread.h
120 --- src/threads/thread.h~       2005-03-30 10:26:13.000000000 -0800
121 +++ src/threads/thread.h        2005-03-30 20:03:11.000000000 -0800
122 @@ -92,6 +92,9 @@ struct thread
123      /* Shared between thread.c and synch.c. */
124      struct list_elem elem;              /* List element. */
125  
126 +    /* Completion semaphore. */
127 +    struct semaphore *completion;
128 +
129  #ifdef USERPROG
130      /* Owned by userprog/process.c. */
131      uint32_t *pagedir;                  /* Page directory. */
132 @@ -107,7 +110,8 @@ void thread_tick (void);
133  void thread_print_stats (void);
134  
135  typedef void thread_func (void *aux);
136 -tid_t thread_create (const char *name, int priority, thread_func *, void *);
137 +tid_t thread_create (const char *name, int priority, thread_func *, void *,
138 +                     struct semaphore *);
139  
140  void thread_block (void);
141  void thread_unblock (struct thread *);
142 diff -urp -X pat src/userprog/process.c~ src/userprog/process.c
143 --- src/userprog/process.c~     2005-03-30 20:06:20.000000000 -0800
144 +++ src/userprog/process.c      2005-03-30 20:06:27.000000000 -0800
145 @@ -21,11 +21,10 @@ static thread_func execute_thread NO_RET
146  static bool load (const char *cmdline, void (**eip) (void), void **esp);
147  
148  /* Starts a new thread running a user program loaded from
149 -   FILENAME.  The new thread may be scheduled (and may even exit)
150 -   before process_execute() returns.  Returns the new process's
151 -   thread id, or TID_ERROR if the thread cannot be created. */
152 +   FILENAME.  The new thread may be scheduled before
153 +   process_execute() returns.*/
154  tid_t
155 -process_execute (const char *filename) 
156 +process_execute (const char *filename, struct semaphore *completion) 
157  {
158    char *fn_copy;
159    tid_t tid;
160 @@ -38,7 +37,8 @@ process_execute (const char *filename) 
161    strlcpy (fn_copy, filename, PGSIZE);
162  
163    /* Create a new thread to execute FILENAME. */
164 -  tid = thread_create (filename, PRI_DEFAULT, execute_thread, fn_copy);
165 +  tid = thread_create (filename, PRI_DEFAULT, execute_thread, fn_copy,
166 +                       completion);
167    if (tid == TID_ERROR)
168      palloc_free_page (fn_copy); 
169    return tid;
170 diff -urp -X pat src/userprog/process.h~ src/userprog/process.h
171 --- src/userprog/process.h~     2005-03-30 15:09:53.000000000 -0800
172 +++ src/userprog/process.h      2005-03-30 20:02:51.000000000 -0800
173 @@ -2,8 +2,9 @@
174  #define USERPROG_PROCESS_H
175  
176  #include "threads/thread.h"
177 +#include "threads/synch.h"
178  
179 -tid_t process_execute (const char *filename);
180 +tid_t process_execute (const char *filename, struct semaphore *);
181  int process_wait (tid_t);
182  void process_exit (void);
183  void process_activate (void);