Fix comments.
[pintos-anon] / solutions / p1-2.patch
1 --- cs140/pintos/src/threads/thread.c   2004-09-17 23:16:15.000000000 -0700
2 +++ cs140/ref/pintos/src/threads/thread.c       2004-09-17 23:19:03.000000000 -0700
3 @@ -75,6 +75,7 @@
4    init_thread (initial_thread, "main", PRI_DEFAULT);
5    initial_thread->status = THREAD_RUNNING;
6    initial_thread->tid = allocate_tid ();
7 +  sema_up (&initial_thread->can_die);
8  
9    /* Initialize run queue. */
10    list_init (&ready_list);
11 @@ -244,12 +245,29 @@
12  void
13  thread_exit (void) 
14  {
15 +  struct thread *t = thread_current ();
16 +  list_elem *e;
17 +
18    ASSERT (!intr_context ());
19  
20 +  /* Notify our parent that we're dying. */
21 +  sema_up (&t->ready_to_die);
22 +
23 +  /* Notify our children that they can die. */
24 +  for (e = list_begin (&t->children); e != list_end (&t->children);
25 +       e = list_next (e)) 
26 +    {
27 +      struct thread *child = list_entry (e, struct thread, children_elem);
28 +      sema_up (&child->can_die); 
29 +    }
30 +
31 +  /* Wait until our parent is ready for us to die. */
32 +  sema_down (&t->can_die);
33 +
34    /* Just set our status to dying and schedule another process.
35       We will be destroyed during the call to schedule_tail(). */
36    intr_disable ();
37 -  thread_current ()->status = THREAD_DYING;
38 +  t->status = THREAD_DYING;
39    schedule ();
40    NOT_REACHED ();
41  }
42 @@ -286,6 +304,31 @@
43    thread_current ()->status = THREAD_BLOCKED;
44    schedule ();
45  }
46 +
47 +/* Waits for thread with tid CHILD_TID to die. */
48 +void
49 +thread_join (tid_t child_tid) 
50 +{
51 +  struct thread *cur = thread_current ();
52 +  list_elem *e;
53 +
54 +  for (e = list_begin (&cur->children); e != list_end (&cur->children); ) 
55 +    {
56 +      struct thread *child = list_entry (e, struct thread, children_elem);
57 +      e = list_next (e);
58 +      if (child->tid == child_tid) 
59 +        {
60 +          /* Wait until child is ready to die. */
61 +          sema_down (&child->ready_to_die);
62 +
63 +          /* Remove from list of children. */
64 +          list_remove (&child->children_elem);
65 +
66 +          /* Notify child that it can finish dying. */
67 +          sema_up (&child->can_die); 
68 +        }
69 +    }
70 +}
71  \f
72  /* Idle thread.  Executes when no other thread is ready to run. */
73  static void
74 @@ -348,12 +391,13 @@
75      {
76        init_thread (t, name, priority);
77        t->tid = allocate_tid ();
78 +      list_push_back (&thread_current ()->children, &t->children_elem);
79      }
80  
81    return t;
82  }
83  
84 -/* Does basic initialization of T as a blocked thread named
85 +/* Does basic initialization of T as a new, blocked thread named
86     NAME. */
87  static void
88  init_thread (struct thread *t, const char *name, int priority)
89 @@ -367,6 +411,9 @@
90    strlcpy (t->name, name, sizeof t->name);
91    t->stack = (uint8_t *) t + PGSIZE;
92    t->priority = priority;
93 +  sema_init (&t->ready_to_die, 0, "ready-to-die");
94 +  sema_init (&t->can_die, 0, "can-die");
95 +  list_init (&t->children);
96    t->magic = THREAD_MAGIC;
97  }
98  
99 --- cs140/pintos/src/threads/thread.h   2004-09-16 21:42:35.000000000 -0700
100 +++ cs140/ref/pintos/src/threads/thread.h       2004-09-17 23:21:14.000000000 -0700
101 @@ -4,6 +4,7 @@
102  #include <debug.h>
103  #include <list.h>
104  #include <stdint.h>
105 +#include "threads/synch.h"
106  
107  #ifdef USERPROG
108  #include "userprog/addrspace.h"
109 @@ -93,6 +94,12 @@
110      uint8_t *stack;                     /* Saved stack pointer. */
111      int priority;                       /* Priority. */
112  
113 +    /* Members for implementing thread_join(). */
114 +    struct semaphore ready_to_die;      /* Up when thread about to die. */
115 +    struct semaphore can_die;           /* Up when thread allowed to die. */
116 +    struct list children;               /* List of child threads. */
117 +    list_elem children_elem;            /* Element of `children' list. */
118 +
119      /* Shared between thread.c and synch.c. */
120      list_elem elem;                     /* List element. */
121