1 #include "threads/test.h"
3 #include "threads/synch.h"
4 #include "threads/thread.h"
5 #include "devices/timer.h"
7 static void test_sleep_once (void);
13 // test_sleep_multiple ();
16 struct sleep_once_data
18 int duration; /* Number of ticks to sleep. */
19 volatile int *counter; /* Counter to check. */
20 struct semaphore done; /* Completion semaphore. */
21 struct thread *thread; /* Thread. */
24 static void sleep_once (void *);
27 test_sleep_once (void)
29 struct sleep_once_data t[5];
30 const int t_cnt = sizeof t / sizeof *t;
34 printf ("\nTesting one sleep per thread...\n");
36 /* Start all the threads. */
38 for (i = 0; i < t_cnt; i++)
41 snprintf (name, sizeof name, "once %d", i);
43 t[i].duration = i * 10;
44 t[i].counter = &counter;
45 sema_init (&t[i].done, 0, name);
46 t[i].thread = thread_create (name, sleep_once, &t[i]);
49 /* Wait for all the threads to finish. */
50 for (i = 0; i < t_cnt; i++)
52 #ifdef THREAD_JOIN_IMPLEMENTED
53 thread_join (t[i].thread);
55 sema_down (&t[i].done);
65 struct sleep_once_data *t = t_;
68 timer_sleep (t->duration);
71 if (*t->counter > t->duration)
72 printf ("%s: Out of order sleep completion (%d > %d)!\n",
73 thread_name (thread_current ()),
74 *t->counter, t->duration);
77 *t->counter = t->duration;
78 printf ("%s: Sleep %d complete\n",
79 thread_name (thread_current ()), t->duration);
82 /* Signal completion. */