fb39bb1d2d1df45263bd652595ad80b1bf04674b
[pintos-anon] / src / tests / threads / p1-2.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 simple_test (void);
14 static void quick_test (void);
15 static void multiple_test (void);
16
17 void
18 test (void) 
19 {
20   simple_test ();
21   quick_test ();
22   multiple_test ();
23 }
24
25 static thread_func simple_thread_func;
26 static thread_func quick_thread_func;
27
28 static void
29 simple_test (void) 
30 {
31   tid_t tid0;
32   
33   printf ("\n"
34           "Testing simple join.\n"
35           "Thread 0 should finish before thread 1 starts.\n");
36   tid0 = thread_create ("0", PRI_DEFAULT, simple_thread_func, "0");
37   thread_yield ();
38   thread_join (tid0);
39   simple_thread_func ("1");
40   printf ("Simple join test done.\n");
41 }
42
43 static void
44 quick_test (void) 
45 {
46   tid_t tid2;
47   
48   printf ("\n"
49           "Testing quick join.\n"
50           "Thread 2 should finish before thread 3 starts.\n");
51
52   tid2 = thread_create ("2", PRI_DEFAULT, quick_thread_func, "2");
53   thread_yield ();
54   thread_join (tid2);
55   simple_thread_func ("3");
56   printf ("Quick join test done.\n");
57 }
58
59 static void
60 multiple_test (void) 
61 {
62   tid_t tid4, tid5;
63   
64   printf ("\n"
65           "Testing multiple join.\n"
66           "Threads 4 and 5 should finish before thread 6 starts.\n");
67
68   tid4 = thread_create ("4", PRI_DEFAULT, simple_thread_func, "4");
69   tid5 = thread_create ("5", PRI_DEFAULT, simple_thread_func, "5");
70   thread_yield ();
71   thread_join (tid4);
72   thread_join (tid5);
73   simple_thread_func ("6");
74   printf ("Multiple join test done.\n");
75 }
76
77 void 
78 simple_thread_func (void *name_) 
79 {
80   const char *name = name_;
81   int i;
82   
83   for (i = 0; i < 5; i++) 
84     {
85       printf ("Thread %s iteration %d\n", name, i);
86       thread_yield ();
87     }
88   printf ("Thread %s done!\n", name);
89 }
90
91 void 
92 quick_thread_func (void *name_) 
93 {
94   const char *name = name_;
95   int i;
96
97   intr_disable ();
98
99   for (i = 0; i < 5; i++) 
100     {
101       printf ("Thread %s iteration %d\n", name, i);
102       thread_yield ();
103     }
104   printf ("Thread %s done!\n", name);
105 }