fc94c440f6506ed626b30545495ba943c94a843a
[pintos-anon] / grading / threads / join-nested.c
1 /* Problem 1-2: Join tests.
2
3    Based on a test originally submitted for Stanford's CS 140 in
4    winter 1998 by Rob Baesman <rbaesman@cs.stanford.edu>, Ben
5    Taskar <btaskar@cs.stanford.edu>, and Toli Kuznets
6    <tolik@cs.stanford.edu>.  Later modified by shiangc, yph, and
7    arens. */
8 #include "threads/test.h"
9 #include <stdio.h>
10 #include "threads/interrupt.h"
11 #include "threads/thread.h"
12
13 static void nested_test (void);
14
15 void
16 test (void) 
17 {
18   nested_test ();
19 }
20
21 static thread_func nested_thread_func;
22
23 static void
24 nested_test (void) 
25 {
26   tid_t tid0;
27   int zero = 0;
28   
29   printf ("\n"
30           "Testing nested join.\n"
31           "Threads 0 to 7 should start in numerical order\n"
32           "and finish in reverse order.\n");
33   tid0 = thread_create ("0", PRI_DEFAULT, nested_thread_func, &zero);
34   thread_join (tid0);
35   printf ("Nested join test done.\n");
36 }
37
38 void 
39 nested_thread_func (void *valuep_) 
40 {
41   int *valuep = valuep_;
42   int value = *valuep;
43
44   printf ("Thread %d starting.\n", value);
45   if (value < 7)
46     {
47       int next = value + 1;
48       tid_t tid_next;
49       char name_next[8];
50       snprintf (name_next, sizeof name_next, "%d", next);
51
52       tid_next = thread_create (name_next, PRI_DEFAULT,
53                                 nested_thread_func, &next);
54
55       thread_join (tid_next);
56     }
57   printf ("Thread %d done.\n", value);
58 }