Permalink
Browse files

Merge branch 'p2' of https://github.com/grantborthwick/Lasagnos into …

…working
  • Loading branch information...
2 parents 5c70db0 + 11bb05d commit 089ff3a30b3926d6a044361e079aae6647f38bc2 @Frankencelery Frankencelery committed Oct 8, 2012
View
@@ -98,17 +98,13 @@ timer_elapsed (int64_t then)
void
timer_sleep (int64_t ticks)
{
-// printf("We got into sleep!\n");
int64_t start = timer_ticks ();
struct thread *t = thread_current();
t->wakeup_time = ticks + start;
intr_disable();
list_insert_ordered(&thread_list, &t->timer_list_elem, compare_threads_by_wakeup_time,NULL);
intr_enable();
sema_down(&t->sema_wakeup);
-// printf("We got woken up\n");
- /*while (timer_elapsed (start) < ticks)
- thread_yield ();*/
}
/* Sleeps for approximately MS milliseconds. Interrupts must be
@@ -2,7 +2,7 @@
# Test names.
tests/threads_TESTS = $(addprefix tests/threads/,alarm-single \
-alarm-many alarm-multiple alarm-simultaneous alarm-priority alarm-zero \
+alarm-many alarm-multiple alarm-mega alarm-onethousand alarm-simultaneous alarm-priority alarm-zero \
alarm-negative priority-change priority-donate-one \
priority-donate-multiple priority-donate-multiple2 \
priority-donate-nest priority-donate-sema priority-donate-lower \
@@ -1,6 +1,7 @@
Functionality and robustness of alarm clock:
4 alarm-single
4 alarm-multiple
+4 alarm-mega
4 alarm-many
4 alarm-simultaneous
4 alarm-priority
@@ -1,59 +1,15 @@
-/* Checks that when the alarm clock wakes up threads, the
- higher-priority threads run first. */
+/* Tests timer_sleep(-100). Only requirement is that it not crash. */
#include <stdio.h>
#include "tests/threads/tests.h"
-#include "threads/init.h"
#include "threads/malloc.h"
#include "threads/synch.h"
#include "threads/thread.h"
#include "devices/timer.h"
-static thread_func alarm_priority_thread;
-static int64_t wake_time;
-static struct semaphore wait_sema;
-
void
test_alarm_negative (void)
{
- int i;
-
- /* This test does not work with the MLFQS. */
- ASSERT (!thread_mlfqs);
-
- wake_time = timer_ticks () + 5 * TIMER_FREQ;
- sema_init (&wait_sema, 0);
-
- for (i = 0; i < 10; i++)
- {
- int priority = PRI_DEFAULT - (i + 5) % 10 - 1;
- char name[16];
- snprintf (name, sizeof name, "priority %d", priority);
- thread_create (name, priority, alarm_priority_thread, NULL);
- }
- printf("starter! (%d)%s\n",(thread_current ()->priority),(thread_current ()->name));
- thread_set_priority (0);
- printf("I am the lowest thread. Now I start some more!(%d)%s\n",(thread_current ()->priority),(thread_current ()->name));
- for (i = 27; i < 38; i++)
- {
- int priority = i; //definitely weird order and sema down before others.
- char name[16];
- snprintf (name, sizeof name, "priority %d", priority);
- thread_create (name, priority, alarm_priority_thread, NULL);
- }
- timer_sleep(1000);
- for (i = 0; i < 20; i++)
- sema_up (&wait_sema);
- printf("all accounted for.\n");
-}
-
-static void
-alarm_priority_thread (void *aux UNUSED)
-{
-
- sema_down (&wait_sema);
- /* Print a message on wake-up. */
- printf ("Thread %s woke up.\n", thread_name ());
-
- sema_up (&wait_sema);
+ timer_sleep (-100);
+ pass ();
}
@@ -31,7 +31,9 @@ test_alarm_priority (void)
snprintf (name, sizeof name, "priority %d", priority);
thread_create (name, priority, alarm_priority_thread, NULL);
}
+
thread_set_priority (PRI_MIN);
+
for (i = 0; i < 10; i++)
sema_down (&wait_sema);
}
@@ -43,6 +45,7 @@ alarm_priority_thread (void *aux UNUSED)
int64_t start_time = timer_ticks ();
while (timer_elapsed (start_time) == 0)
continue;
+
/* Now we know we're at the very beginning of a timer tick, so
we can call timer_sleep() without worrying about races
between checking the time and a timer interrupt. */
@@ -29,6 +29,16 @@ test_alarm_many (void)
{
test_sleep (5, 14);
}
+void
+test_alarm_mega (void)
+{
+ test_sleep (5, 70);
+}
+void
+test_alarm_onethousand (void)
+{
+ test_sleep (5, 100);
+}
/* Information about the test. */
struct sleep_test
@@ -104,7 +104,7 @@ test_sleep (int thread_cnt, int iterations)
t->id = i;
t->duration = (i + 1) * 10;
t->iterations = 0;
- printf("Starting Threads");
+
snprintf (name, sizeof name, "thread %d", i);
thread_create (name, PRI_DEFAULT, sleeper, t);
}
@@ -39,6 +39,9 @@ static const struct test tests[] =
{"mlfqs-nice-2", test_mlfqs_nice_2},
{"mlfqs-nice-10", test_mlfqs_nice_10},
{"mlfqs-block", test_mlfqs_block},
+ //Tests added by Grant
+ {"alarm-mega", test_alarm_mega},
+ {"alarm-onethousand", test_alarm_onethousand},
};
static const char *test_name;
@@ -33,6 +33,9 @@ extern test_func test_mlfqs_fair_20;
extern test_func test_mlfqs_nice_2;
extern test_func test_mlfqs_nice_10;
extern test_func test_mlfqs_block;
+//Tests added by Grant
+extern test_func test_alarm_mega;
+extern test_func test_alarm_onethousand;
void msg (const char *, ...);
void fail (const char *, ...);
View
@@ -118,18 +118,15 @@ sema_up (struct semaphore *sema)
struct list_elem* t;
struct thread* e2;
struct thread* t2 = NULL;
- //printf("1. [");
for (e = (list_begin (&(sema->waiters))); e!= list_end (&(sema->waiters));
e = list_next(e))
{
e2 = list_entry (e, struct thread, elem);
- //printf("(%d)%s\n",(e2->priority),(e2->name));
if (t2==NULL||(e2->priority)>(t2->priority)){
t = e;
t2 = e2;
- }
- }
- //printf("]\nremove (%d)%s\n2. [",(t2->priority),(t2->name));
+ }
+ }
list_remove(t);
for (e = (list_begin (&(sema->waiters))); e!= list_end (&(sema->waiters));
e = list_next(e))
@@ -225,9 +222,27 @@ lock_acquire (struct lock *lock)
ASSERT (lock != NULL);
ASSERT (!intr_context ());
ASSERT (!lock_held_by_current_thread (lock));
-
+ thread_current ()->want_lock = lock;
+ if ((lock->holder)!= NULL){
+ struct thread* donee = lock->holder;
+ thread_current ()->donee = donee;
+ list_push_front(&donee->benefactors, &thread_current ()->elem);
+ }
+
sema_down (&lock->semaphore);
+ //threads clear benefactors as they get rid of lock!
+ thread_current ()->donee = NULL;
+ thread_current ()->want_lock = NULL;
lock->holder = thread_current ();
+ struct list_elem *e = list_begin (&((lock->semaphore).waiters));
+ struct thread * t;
+ while (e != list_end (&((lock->semaphore).waiters)))
+ {
+ if (e!= NULL){list_push_front(&(thread_current ()->benefactors),e);}
+ e = list_next(e);
+ }
+ thread_get_priority();
+
}
/* Tries to acquires LOCK and returns true if successful or false
@@ -245,8 +260,9 @@ lock_try_acquire (struct lock *lock)
ASSERT (!lock_held_by_current_thread (lock));
success = sema_try_down (&lock->semaphore);
- if (success)
+ if (success){
lock->holder = thread_current ();
+ }
return success;
}
@@ -260,8 +276,17 @@ lock_release (struct lock *lock)
{
ASSERT (lock != NULL);
ASSERT (lock_held_by_current_thread (lock));
-
+ //thread_release_lock(lock);
lock->holder = NULL;
+ struct list_elem * e = (list_begin (&(thread_current ()->benefactors)));
+ struct thread * t;
+ while (e!= list_end (&(thread_current ()->benefactors)))
+ {
+ t = list_entry (e, struct thread, elem);
+ if ((t->want_lock) == lock){e = list_remove(e);}
+ else{e = list_next(e);}
+ }
+ thread_get_priority();
sema_up (&lock->semaphore);
}
View
@@ -251,31 +251,12 @@ thread_unblock (struct thread *t)
ASSERT (t->status == THREAD_BLOCKED);
list_push_back (&ready_list, &t->elem);
t->status = THREAD_READY;
- printf("(%s - %d, %s - %d)",(thread_current ()->name), (thread_current ()->priority),(t->name),(t->priority));
- if ((thread_current () == idle_thread) || (thread_current ()->priority) < (t->priority)){printf(" => Should yield.\n"); /*thread_yield();*/ /*printf("Back.\n");*/}
- else{printf(" => Did not yield\n");}
- /*struct list_elem* e;
- struct list_elem* u;
- struct thread* e2;
- struct thread* u2 = NULL;*/
- //printf("start\n");
- /*if (!list_empty (&ready_list)){
- for (e = (list_begin (&ready_list)); e!= list_end (&ready_list);
- e = list_next(e))
- {
- e2 = list_entry (list_begin (&ready_list), struct thread, elem);
- if (u2==NULL||(e2->priority)>(u2->priority)){
- u = e;
- u2 = e2;
- printf("!!");
- }
- //printf("%s - %d\n",u2->name,u2->priority);
- }
- //printf("%s has highest with %d\n\n",u2->name,u2->priority);
- }
- else{printf("Empty!");}*/
intr_set_level (old_level);
+ if ((thread_current () != idle_thread) && (thread_current ()->priority) < (t->priority))
+ {
+ thread_yield();
+ }
}
/* Returns the name of the running thread. */
@@ -338,19 +319,34 @@ void
thread_yield (void)
{
struct thread *cur = thread_current ();
- //printf("(%d)%s - Yielding!\n",(cur->priority),(cur->name));
enum intr_level old_level;
ASSERT (!intr_context ());
old_level = intr_disable ();
if (cur != idle_thread)
list_push_back (&ready_list, &cur->elem);
- //printf("Before schedule\n");
cur->status = THREAD_READY;
schedule ();
intr_set_level (old_level);
- //printf("(%d)%s is back from the dead.\n",(cur->priority),(cur->name));
+}
+
+
+void thread_check_yield ()
+{
+ struct list_elem* e;
+ struct thread* e2;
+ bool y = false;
+ for (e = (list_begin (&ready_list)); e!= list_end (&ready_list);
+ e = list_next(e))
+ {
+ e2 = list_entry (e, struct thread, elem);
+ if ((e2->priority) >(thread_current ()->priority)){
+ y = true;
+ break;
+ }
+ }
+ if (y){thread_yield();}
}
/* Invoke function 'func' on all threads, passing along 'aux'.
@@ -374,14 +370,23 @@ thread_foreach (thread_action_func *func, void *aux)
void
thread_set_priority (int new_priority)
{
- thread_current ()->priority = new_priority;
+ thread_current ()->original_priority = new_priority;
+ thread_get_priority();
+ thread_check_yield ();
}
/* Returns the current thread's priority. */
int
thread_get_priority (void)
{
- return thread_current ()->priority;
+ struct list_elem *e = list_begin (&(thread_current ()->benefactors));
+ struct thread * t;
+ while(e != list_end(&(thread_current ()->benefactors)))
+ {
+ struct thread *t = list_entry (e, struct thread, allelem);
+ e = list_next (e);
+ }
+ return thread_current ()->priority;
}
/* Sets the current thread's nice value to NICE. */
@@ -501,10 +506,11 @@ init_thread (struct thread *t, const char *name, int priority)
strlcpy (t->name, name, sizeof t->name);
t->stack = (uint8_t *) t + PGSIZE;
t->priority = priority;
+ t->original_priority = priority;
t->magic = THREAD_MAGIC;
+ list_init(&t->benefactors);
list_push_back (&all_list, &t->allelem);
sema_init(&t->sema_wakeup, 0);
-
}
/* Allocates a SIZE-byte frame at the top of thread T's stack and
@@ -531,7 +537,7 @@ next_thread_to_run (void)
if (list_empty (&ready_list)){return idle_thread;}
else{
struct list_elem* e;
- struct list_elem* t;
+ struct list_elem* t = NULL;
struct thread* e2;
struct thread* t2 = NULL;
for (e = (list_begin (&ready_list)); e!= list_end (&ready_list);
@@ -543,8 +549,9 @@ next_thread_to_run (void)
t2 = e2;
}
}
- list_remove(t);
- return t2;
+ if( t!= NULL){list_remove(t);return t2;}
+ printf("uh oh......\n");
+ return idle_thread;
//return list_entry (list_pop_front (&ready_list), struct thread, elem);
}
}
Oops, something went wrong.

0 comments on commit 089ff3a

Please sign in to comment.