Fix two bugs in the base Pintos code:
[pintos-anon] / src / tests / threads / mlfqs-block.c
1 /* Checks that recent_cpu and priorities are updated for blocked
2    threads.
3
4    The main thread sleeps for 25 seconds, spins for 5 seconds,
5    then releases a lock.  The "block" thread spins for 20 seconds
6    then attempts to acquire the lock, which will block for 10
7    seconds (until the main thread releases it).  If recent_cpu
8    decays properly while the "block" thread sleeps, then the
9    block thread should be immediately scheduled when the main
10    thread releases the lock. */
11
12 #include <stdio.h>
13 #include "tests/threads/tests.h"
14 #include "threads/init.h"
15 #include "threads/malloc.h"
16 #include "threads/synch.h"
17 #include "threads/thread.h"
18 #include "devices/timer.h"
19
20 static void block_thread (void *lock_);
21
22 void
23 test_mlfqs_block (void) 
24 {
25   int64_t start_time;
26   struct lock lock;
27   
28   ASSERT (thread_mlfqs);
29
30   msg ("Main thread acquiring lock.");
31   lock_init (&lock);
32   lock_acquire (&lock);
33   
34   msg ("Main thread creating block thread, sleeping 25 seconds...");
35   thread_create ("block", PRI_DEFAULT, block_thread, &lock);
36   timer_sleep (25 * TIMER_FREQ);
37
38   msg ("Main thread spinning for 5 seconds...");
39   start_time = timer_ticks ();
40   while (timer_elapsed (start_time) < 5 * TIMER_FREQ)
41     continue;
42
43   msg ("Main thread releasing lock.");
44   lock_release (&lock);
45
46   msg ("Block thread should have already acquired lock.");
47 }
48
49 static void
50 block_thread (void *lock_) 
51 {
52   struct lock *lock = lock_;
53   int64_t start_time;
54
55   msg ("Block thread spinning for 20 seconds...");
56   start_time = timer_ticks ();
57   while (timer_elapsed (start_time) < 20 * TIMER_FREQ)
58     continue;
59
60   msg ("Block thread acquiring lock...");
61   lock_acquire (lock);
62
63   msg ("...got it.");
64 }