1 /* Creates N threads, each of which sleeps a different, fixed
2 duration, M times. Records the wake-up order and verifies
6 #include "tests/threads/tests.h"
7 #include "threads/init.h"
8 #include "threads/malloc.h"
9 #include "threads/synch.h"
10 #include "threads/thread.h"
11 #include "devices/timer.h"
13 static void test_sleep (int thread_cnt, int iterations);
16 test_alarm_simultaneous (void)
21 /* Information about the test. */
24 int64_t start; /* Current time at start of test. */
25 int iterations; /* Number of iterations per thread. */
26 int *output_pos; /* Current position in output buffer. */
29 static void sleeper (void *);
31 /* Runs THREAD_CNT threads thread sleep ITERATIONS times each. */
33 test_sleep (int thread_cnt, int iterations)
35 struct sleep_test test;
39 /* This test does not work with the MLFQS. */
40 ASSERT (!thread_mlfqs);
42 msg ("Creating %d threads to sleep %d times each.", thread_cnt, iterations);
43 msg ("Each thread sleeps 10 ticks each time.");
44 msg ("Within an iteration, all threads should wake up on the same tick.");
46 /* Allocate memory. */
47 output = malloc (sizeof *output * iterations * thread_cnt * 2);
49 PANIC ("couldn't allocate memory for test");
51 /* Initialize test. */
52 test.start = timer_ticks () + 100;
53 test.iterations = iterations;
54 test.output_pos = output;
57 ASSERT (output != NULL);
58 for (i = 0; i < thread_cnt; i++)
61 snprintf (name, sizeof name, "thread %d", i);
62 thread_create (name, PRI_DEFAULT, sleeper, &test);
65 /* Wait long enough for all the threads to finish. */
66 timer_sleep (100 + iterations * 10 + 100);
68 /* Print completion order. */
69 msg ("iteration 0, thread 0: woke up after %d ticks", output[0]);
70 for (i = 1; i < test.output_pos - output; i++)
71 msg ("iteration %d, thread %d: woke up %d ticks later",
72 i / thread_cnt, i % thread_cnt, output[i] - output[i - 1]);
81 struct sleep_test *test = test_;
84 /* Make sure we're at the beginning of a timer tick. */
87 for (i = 1; i <= test->iterations; i++)
89 int64_t sleep_until = test->start + i * 10;
90 timer_sleep (sleep_until - timer_ticks ());
91 *test->output_pos++ = timer_ticks () - test->start;