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
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);
9 /* Initialize run queue. */
10 list_init (&ready_list);
15 + struct thread *t = thread_current ();
18 ASSERT (!intr_context ());
20 + /* Notify our parent that we're dying. */
21 + sema_up (&t->ready_to_die);
23 + /* Notify our children that they can die. */
24 + for (e = list_begin (&t->children); e != list_end (&t->children);
27 + struct thread *child = list_entry (e, struct thread, children_elem);
28 + sema_up (&child->can_die);
31 + /* Wait until our parent is ready for us to die. */
32 + sema_down (&t->can_die);
34 /* Just set our status to dying and schedule another process.
35 We will be destroyed during the call to schedule_tail(). */
37 - thread_current ()->status = THREAD_DYING;
38 + t->status = THREAD_DYING;
43 thread_current ()->status = THREAD_BLOCKED;
47 +/* Waits for thread with tid CHILD_TID to die. */
49 +thread_join (tid_t child_tid)
51 + struct thread *cur = thread_current ();
54 + for (e = list_begin (&cur->children); e != list_end (&cur->children); )
56 + struct thread *child = list_entry (e, struct thread, children_elem);
58 + if (child->tid == child_tid)
60 + /* Wait until child is ready to die. */
61 + sema_down (&child->ready_to_die);
63 + /* Remove from list of children. */
64 + list_remove (&child->children_elem);
66 + /* Notify child that it can finish dying. */
67 + sema_up (&child->can_die);
72 /* Idle thread. Executes when no other thread is ready to run. */
76 init_thread (t, name, priority);
77 t->tid = allocate_tid ();
78 + list_push_back (&thread_current ()->children, &t->children_elem);
84 -/* Does basic initialization of T as a blocked thread named
85 +/* Does basic initialization of T as a new, blocked thread named
88 init_thread (struct thread *t, const char *name, int priority)
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;
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
105 +#include "threads/synch.h"
108 #include "userprog/addrspace.h"
110 uint8_t *stack; /* Saved stack pointer. */
111 int priority; /* Priority. */
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. */
119 /* Shared between thread.c and synch.c. */
120 list_elem elem; /* List element. */