1 /* Problem 1-1: Alarm Clock tests.
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.
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>. */
13 #include "threads/test.h"
15 #include "threads/synch.h"
16 #include "threads/thread.h"
17 #include "devices/timer.h"
19 static void test_sleep (int iterations);
28 struct sleep_thread_data
30 int64_t start; /* Start time. */
31 int duration; /* Number of ticks to sleep. */
32 int iterations; /* Number of iterations to run. */
33 int *product; /* Largest product so far. */
34 struct lock *lock; /* Lock on access to `product'. */
35 struct semaphore done; /* Completion semaphore. */
36 tid_t tid; /* Thread ID. */
39 static void sleeper (void *);
42 test_sleep (int iterations)
44 struct sleep_thread_data threads[5];
45 const int thread_cnt = sizeof threads / sizeof *threads;
52 "Testing %d sleeps per thread.\n"
53 "If successful, product of iteration count and\n"
54 "sleep duration will appear in nondescending order.\n",
57 /* Start all the threads. */
59 lock_init (&lock, "product");
60 start = timer_ticks ();
61 for (i = 0; i < thread_cnt; i++)
63 struct sleep_thread_data *t;
66 snprintf (name, sizeof name, "thread %d", i);
69 t->duration = (i + 1) * 10;
70 t->iterations = iterations;
71 t->product = &product;
73 sema_init (&t->done, 0, name);
74 t->tid = thread_create (name, PRI_DEFAULT, sleeper, t);
77 /* Wait for all the threads to finish. */
78 for (i = 0; i < thread_cnt; i++)
80 #ifdef THREAD_JOIN_IMPLEMENTED
81 thread_join (threads[i].tid);
83 sema_down (&threads[i].done);
93 struct sleep_thread_data *t = t_;
96 for (i = 1; i <= t->iterations; i++)
99 int new_product = i * t->duration;
101 timer_sleep ((t->start + new_product) - timer_ticks ());
103 lock_acquire (t->lock);
104 old_product = *t->product;
105 *t->product = new_product;
106 lock_release (t->lock);
108 printf ("%s: duration=%d, iteration=%d, product=%d\n",
109 thread_name (), t->duration, i, new_product);
111 if (old_product > new_product)
112 printf ("%s: Out of order sleep completion (%d > %d)!\n",
113 thread_name (), old_product, new_product);
116 /* Signal completion. */