Add threads tests.
[pintos-anon] / src / tests / threads / p1-1.c
1 /* Problem 1-1: Alarm Clock tests.
2
3    These tests will work with Pintos "out of the box" because an
4    implementation of timer_sleep() that "busy-waits" is
5    included.  You need to replace that implementation with one
6    that doesn't busy-wait.
7
8    Based on a test originally submitted for Stanford's CS 140 in
9    winter 1998 by Rob Baesman <rbaesman@cs.stanford.edu>, Ben
10    Taskar <btaskar@cs.stanford.edu>, and Toli Kuznets
11    <tolik@cs.stanford.edu>. */
12
13 /* If you've implemented thread_join(), you can uncomment this. */
14 /*#define THREAD_JOIN_IMPLEMENTED*/
15
16 #include "threads/test.h"
17 #include <stdio.h>
18 #include "threads/synch.h"
19 #include "threads/thread.h"
20 #include "devices/timer.h"
21
22 static void test_sleep (int iterations);
23
24 void
25 test (void) 
26 {
27   test_sleep (1);
28   test_sleep (7);
29 }
30
31 struct sleep_thread_data 
32   {
33     int64_t start;              /* Start time. */
34     int duration;               /* Number of ticks to sleep. */
35     int iterations;             /* Number of iterations to run. */
36     int *product;               /* Largest product so far. */
37     struct lock *lock;          /* Lock on access to `product'. */
38     struct semaphore done;      /* Completion semaphore. */
39     tid_t tid;                  /* Thread ID. */
40   };
41
42 static void sleeper (void *);
43
44 static void
45 test_sleep (int iterations) 
46 {
47   struct sleep_thread_data threads[5];
48   const int thread_cnt = sizeof threads / sizeof *threads;
49   struct lock lock;
50   int64_t start;
51   int product;
52   int i;
53
54   printf ("\n"
55           "Testing %d sleeps per thread.\n"
56           "If successful, product of iteration count and\n"
57           "sleep duration will appear in nondescending order.\n",
58           iterations);
59
60   /* Start all the threads. */
61   product = 0;
62   lock_init (&lock, "product");
63   start = timer_ticks ();
64   for (i = 0; i < thread_cnt; i++)
65     {
66       struct sleep_thread_data *t;
67       char name[16];
68       
69       snprintf (name, sizeof name, "thread %d", i);
70       t = threads + i;
71       t->start = start;
72       t->duration = (i + 1) * 10;
73       t->iterations = iterations;
74       t->product = &product;
75       t->lock = &lock;
76       sema_init (&t->done, 0, name);
77       t->tid = thread_create (name, PRI_DEFAULT, sleeper, t);
78     }
79   
80   /* Wait for all the threads to finish. */
81   for (i = 0; i < thread_cnt; i++) 
82     {
83 #ifdef THREAD_JOIN_IMPLEMENTED
84       thread_join (threads[i].tid);
85 #else
86       sema_down (&threads[i].done);
87 #endif
88     }
89
90   printf ("...done\n");
91 }
92
93 static void
94 sleeper (void *t_) 
95 {
96   struct sleep_thread_data *t = t_;
97   int i;
98
99   for (i = 1; i <= t->iterations; i++) 
100     {
101       int old_product;
102       int new_product = i * t->duration;
103
104       timer_sleep ((t->start + new_product) - timer_ticks ());
105
106       lock_acquire (t->lock);
107       old_product = *t->product;
108       *t->product = new_product;
109       lock_release (t->lock);
110
111       printf ("%s: duration=%d, iteration=%d, product=%d\n",
112               thread_name (), t->duration, i, new_product);
113
114       if (old_product > new_product)
115         printf ("%s: Out of order sleep completion (%d > %d)!\n",
116                 thread_name (), old_product, new_product);
117     }
118   
119   /* Signal completion. */
120   sema_up (&t->done);
121 }