diff options
| author | manuel <manuel@mausz.at> | 2012-03-26 20:18:23 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2012-03-26 20:18:23 +0200 |
| commit | 11c1519a12cdae1ce05d31a6e389a103259c6c93 (patch) | |
| tree | de8b4a9c5ae617098c274733d1b3562f1a89d680 /pintos-progos | |
| parent | b5f0874cd96ee2a62aabc645b9626c2749cb6a01 (diff) | |
| download | progos-11c1519a12cdae1ce05d31a6e389a103259c6c93.tar.gz progos-11c1519a12cdae1ce05d31a6e389a103259c6c93.tar.bz2 progos-11c1519a12cdae1ce05d31a6e389a103259c6c93.zip | |
first timer alarm implementation
Diffstat (limited to 'pintos-progos')
| -rw-r--r-- | pintos-progos/.gitignore | 6 | ||||
| -rw-r--r-- | pintos-progos/devices/timer.c | 45 | ||||
| -rw-r--r-- | pintos-progos/threads/thread.h | 16 |
3 files changed, 57 insertions, 10 deletions
diff --git a/pintos-progos/.gitignore b/pintos-progos/.gitignore new file mode 100644 index 0000000..e87c613 --- /dev/null +++ b/pintos-progos/.gitignore | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | intro/bochsout.txt | ||
| 2 | intro/bochsrc.txt | ||
| 3 | intro/build | ||
| 4 | threads/bochsout.txt | ||
| 5 | threads/bochsrc.txt | ||
| 6 | threads/build | ||
diff --git a/pintos-progos/devices/timer.c b/pintos-progos/devices/timer.c index befaaae..bf78351 100644 --- a/pintos-progos/devices/timer.c +++ b/pintos-progos/devices/timer.c | |||
| @@ -30,11 +30,16 @@ static void busy_wait (int64_t loops); | |||
| 30 | static void real_time_sleep (int64_t num, int32_t denom); | 30 | static void real_time_sleep (int64_t num, int32_t denom); |
| 31 | static void real_time_delay (int64_t num, int32_t denom); | 31 | static void real_time_delay (int64_t num, int32_t denom); |
| 32 | 32 | ||
| 33 | /* list of processes waiting for an alarm event | ||
| 34 | * and currently put to sleep */ | ||
| 35 | static struct list alarm_list; | ||
| 36 | |||
| 33 | /* Sets up the timer to interrupt TIMER_FREQ times per second, | 37 | /* Sets up the timer to interrupt TIMER_FREQ times per second, |
| 34 | and registers the corresponding interrupt. */ | 38 | and registers the corresponding interrupt. */ |
| 35 | void | 39 | void |
| 36 | timer_init (void) | 40 | timer_init (void) |
| 37 | { | 41 | { |
| 42 | list_init (&alarm_list); | ||
| 38 | pit_configure_channel (0, 2, TIMER_FREQ); | 43 | pit_configure_channel (0, 2, TIMER_FREQ); |
| 39 | intr_register_ext (0x20, timer_interrupt, "8254 Timer"); | 44 | intr_register_ext (0x20, timer_interrupt, "8254 Timer"); |
| 40 | } | 45 | } |
| @@ -89,11 +94,26 @@ timer_elapsed (int64_t then) | |||
| 89 | void | 94 | void |
| 90 | timer_sleep (int64_t ticks) | 95 | timer_sleep (int64_t ticks) |
| 91 | { | 96 | { |
| 92 | int64_t start = timer_ticks (); | 97 | struct thread *t = thread_current (); |
| 98 | enum intr_level old_level; | ||
| 93 | 99 | ||
| 94 | ASSERT (intr_get_level () == INTR_ON); | 100 | /* nothing to sleep here */ |
| 95 | while (timer_elapsed (start) < ticks) | 101 | if (ticks <= 0) |
| 96 | thread_yield (); | 102 | return; |
| 103 | |||
| 104 | t->alarm_tick = timer_ticks () + ticks; | ||
| 105 | |||
| 106 | /* add thread to alarm list | ||
| 107 | * disable interrupts as this is critical */ | ||
| 108 | old_level = intr_disable (); | ||
| 109 | ASSERT (t->status == THREAD_RUNNING); | ||
| 110 | list_push_back (&alarm_list, &t->elem); | ||
| 111 | |||
| 112 | /* block the thread */ | ||
| 113 | thread_block(); | ||
| 114 | |||
| 115 | /* restore interrupt */ | ||
| 116 | intr_set_level (old_level); | ||
| 97 | } | 117 | } |
| 98 | 118 | ||
| 99 | /* Sleeps for approximately MS milliseconds. Interrupts must be | 119 | /* Sleeps for approximately MS milliseconds. Interrupts must be |
| @@ -170,8 +190,25 @@ timer_print_stats (void) | |||
| 170 | static void | 190 | static void |
| 171 | timer_interrupt (struct intr_frame *args UNUSED) | 191 | timer_interrupt (struct intr_frame *args UNUSED) |
| 172 | { | 192 | { |
| 193 | struct list_elem *el, *next; | ||
| 194 | |||
| 173 | ticks++; | 195 | ticks++; |
| 174 | thread_tick (); | 196 | thread_tick (); |
| 197 | |||
| 198 | /* check for threads waiting for an alarm event */ | ||
| 199 | for (el = list_begin (&alarm_list); el != list_end (&alarm_list); el = next) | ||
| 200 | { | ||
| 201 | struct thread *t = list_entry (el, struct thread, elem); | ||
| 202 | if (t->alarm_tick == timer_ticks ()) | ||
| 203 | { | ||
| 204 | next = list_remove (el); | ||
| 205 | /* unblock must be called AFTER removing, | ||
| 206 | * as thread_unblock() will reuse thread.elem */ | ||
| 207 | thread_unblock (t); | ||
| 208 | } | ||
| 209 | else | ||
| 210 | next = list_next (el); | ||
| 211 | } | ||
| 175 | } | 212 | } |
| 176 | 213 | ||
| 177 | /* Returns true if LOOPS iterations waits for more than one timer | 214 | /* Returns true if LOOPS iterations waits for more than one timer |
diff --git a/pintos-progos/threads/thread.h b/pintos-progos/threads/thread.h index eebf42c..9cc5ef7 100644 --- a/pintos-progos/threads/thread.h +++ b/pintos-progos/threads/thread.h | |||
| @@ -76,11 +76,13 @@ typedef int tid_t; | |||
| 76 | set to THREAD_MAGIC. Stack overflow will normally change this | 76 | set to THREAD_MAGIC. Stack overflow will normally change this |
| 77 | value, triggering the assertion. */ | 77 | value, triggering the assertion. */ |
| 78 | /* The `elem' member has a dual purpose. It can be an element in | 78 | /* The `elem' member has a dual purpose. It can be an element in |
| 79 | the run queue (thread.c), or it can be an element in a | 79 | the run queue (thread.c), it can be an element in a semaphore |
| 80 | semaphore wait list (synch.c). It can be used these two ways | 80 | wait list (synch.c), or it can be an element in the alarm sleep |
| 81 | only because they are mutually exclusive: only a thread in the | 81 | list (timer.c). It can be used these two ways only because they |
| 82 | ready state is on the run queue, whereas only a thread in the | 82 | are mutually exclusive: only a thread in the ready state is on |
| 83 | blocked state is on a semaphore wait list. */ | 83 | the run queue, only a thread in the blocked state is on a semaphore |
| 84 | wait list, whereas only a thread waiting for an alarm event is on | ||
| 85 | the alarm sleep list. */ | ||
| 84 | struct thread | 86 | struct thread |
| 85 | { | 87 | { |
| 86 | /* Owned by thread.c. */ | 88 | /* Owned by thread.c. */ |
| @@ -91,7 +93,7 @@ struct thread | |||
| 91 | int priority; /* Priority. */ | 93 | int priority; /* Priority. */ |
| 92 | struct list_elem allelem; /* List element for all threads list. */ | 94 | struct list_elem allelem; /* List element for all threads list. */ |
| 93 | 95 | ||
| 94 | /* Shared between thread.c and synch.c. */ | 96 | /* Shared between thread.c, synch.c and timer.c. */ |
| 95 | struct list_elem elem; /* List element. */ | 97 | struct list_elem elem; /* List element. */ |
| 96 | 98 | ||
| 97 | #ifdef USERPROG | 99 | #ifdef USERPROG |
| @@ -103,6 +105,8 @@ struct thread | |||
| 103 | 105 | ||
| 104 | /* Owned by thread.c. */ | 106 | /* Owned by thread.c. */ |
| 105 | unsigned magic; /* Detects stack overflow. */ | 107 | unsigned magic; /* Detects stack overflow. */ |
| 108 | |||
| 109 | int64_t alarm_tick; /* absolute tick when to trigger an alarm event */ | ||
| 106 | }; | 110 | }; |
| 107 | 111 | ||
| 108 | /* If false (default), use round-robin scheduler. | 112 | /* If false (default), use round-robin scheduler. |
