Compare commits
17 Commits
gleb/BSD
...
task1/sale
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88967acdaa | ||
|
|
e74ee59e17 | ||
|
|
53b296d6c4 | ||
|
|
0d6fb2f167 | ||
|
|
24900545d4 | ||
|
|
fb268cdef0 | ||
|
|
dbb7fd56e3 | ||
|
|
949455aae5 | ||
|
|
5c09ff0149 | ||
|
|
9f53040d27 | ||
|
|
62c8818c05 | ||
|
|
fda79173c0 | ||
|
|
1c53790ca7 | ||
|
|
9bb0b758c8 | ||
|
|
8b3f9e353f | ||
|
|
83910f945c | ||
|
|
9f71c989a9 |
7
.gitignore
vendored
7
.gitignore
vendored
@@ -4,6 +4,13 @@
|
||||
#ignore pdf files (just keep source files)
|
||||
*.pdf
|
||||
|
||||
#ignore Mac OS generated files
|
||||
.DS_Store
|
||||
|
||||
#ignore code editor generated directories
|
||||
.idea
|
||||
.vscode
|
||||
|
||||
#ignore junk files from latex output
|
||||
*.out
|
||||
*.log
|
||||
|
||||
@@ -68,7 +68,8 @@ sema_down (struct semaphore *sema)
|
||||
old_level = intr_disable ();
|
||||
while (sema->value == 0)
|
||||
{
|
||||
list_push_back (&sema->waiters, &thread_current ()->elem);
|
||||
list_insert_ordered(&sema->waiters, &thread_current ()->elem,
|
||||
priority_more, NULL);
|
||||
thread_block ();
|
||||
}
|
||||
sema->value--;
|
||||
@@ -113,11 +114,22 @@ sema_up (struct semaphore *sema)
|
||||
ASSERT (sema != NULL);
|
||||
|
||||
old_level = intr_disable ();
|
||||
if (!list_empty (&sema->waiters))
|
||||
thread_unblock (list_entry (list_pop_front (&sema->waiters),
|
||||
struct thread, elem));
|
||||
|
||||
/* Wake up (unblock) the highest priority thread from the waiters list */
|
||||
struct thread *t = NULL;
|
||||
if (!list_empty (&sema->waiters))
|
||||
{
|
||||
t = list_entry (list_pop_front (&sema->waiters), struct thread, elem);
|
||||
thread_unblock (t);
|
||||
}
|
||||
|
||||
sema->value++;
|
||||
intr_set_level (old_level);
|
||||
|
||||
/* If the unblocked thread has higher priority than the current running thread
|
||||
then yield the CPU to the unblocked thread */
|
||||
if (!intr_context() && t != NULL && t->priority > thread_get_priority ())
|
||||
thread_yield ();
|
||||
}
|
||||
|
||||
static void sema_test_helper (void *sema_);
|
||||
@@ -253,6 +265,54 @@ struct semaphore_elem
|
||||
struct semaphore semaphore; /* This semaphore. */
|
||||
};
|
||||
|
||||
/* Function that compares the two *semaphores* associated with the provided
|
||||
list_elem structures. [i.e., takes list_elem of semaphore_elem, and]
|
||||
Returns true if the thread associated with the semaphore associated with a_
|
||||
has a higher priority than that of b_.
|
||||
|
||||
If aux is provided, then it is a pointer to an integer representing the
|
||||
priority of the first semaphore. This is useful when the thread has not been
|
||||
sema'd down yet. */
|
||||
static bool
|
||||
sema_priority_more(const struct list_elem *a_, const struct list_elem *b_,
|
||||
void *aux)
|
||||
{
|
||||
int a_priority, b_priority;
|
||||
|
||||
/* If an aux is provided, then use it as the priority of the first semaphore.
|
||||
Otherwise, get the priority of the first semaphore. */
|
||||
if (aux != NULL)
|
||||
a_priority = *(int *) aux;
|
||||
else
|
||||
{
|
||||
struct semaphore_elem *a = list_entry(a_, struct semaphore_elem, elem);
|
||||
|
||||
/* If waiters list is empty, return false (i.e., a has lower priority) */
|
||||
if (list_empty(&a->semaphore.waiters))
|
||||
return false;
|
||||
|
||||
/* Otherwise, get the thread with the highest priority from the waiters
|
||||
list. By design, this is the first one in the list (See sema_down). */
|
||||
struct thread *a_thread =
|
||||
list_entry(list_front(&a->semaphore.waiters), struct thread, elem);
|
||||
|
||||
a_priority = a_thread->priority;
|
||||
}
|
||||
|
||||
struct semaphore_elem *b = list_entry(b_, struct semaphore_elem, elem);
|
||||
|
||||
/* If waiters list is empty, return true (i.e., a has higher priority) */
|
||||
if (list_empty(&b->semaphore.waiters))
|
||||
return true;
|
||||
|
||||
struct thread *b_thread =
|
||||
list_entry(list_front(&b->semaphore.waiters), struct thread, elem);
|
||||
|
||||
b_priority = b_thread->priority;
|
||||
|
||||
return a_priority > b_priority;
|
||||
}
|
||||
|
||||
/* Initializes condition variable COND. A condition variable
|
||||
allows one piece of code to signal a condition and cooperating
|
||||
code to receive the signal and act upon it. */
|
||||
@@ -295,7 +355,14 @@ cond_wait (struct condition *cond, struct lock *lock)
|
||||
ASSERT (lock_held_by_current_thread (lock));
|
||||
|
||||
sema_init (&waiter.semaphore, 0);
|
||||
list_push_back (&cond->waiters, &waiter.elem);
|
||||
|
||||
/* Insert the semaphore_elem into the waiters list in order of priority.
|
||||
We pass the priority of the current thread as aux to sema_priority_more
|
||||
because the thread has not been sema'd down yet (See sema_priority_more) */
|
||||
int priority = thread_current ()->priority;
|
||||
list_insert_ordered (&cond->waiters, &waiter.elem, sema_priority_more,
|
||||
&priority);
|
||||
|
||||
lock_release (lock);
|
||||
sema_down (&waiter.semaphore);
|
||||
lock_acquire (lock);
|
||||
|
||||
@@ -220,6 +220,10 @@ thread_create (const char *name, int priority,
|
||||
/* Add to run queue. */
|
||||
thread_unblock (t);
|
||||
|
||||
/* Yield if the new thread has a higher priority than the current thread. */
|
||||
if (priority > thread_get_priority ())
|
||||
thread_yield ();
|
||||
|
||||
return tid;
|
||||
}
|
||||
|
||||
@@ -256,7 +260,10 @@ thread_unblock (struct thread *t)
|
||||
|
||||
old_level = intr_disable ();
|
||||
ASSERT (t->status == THREAD_BLOCKED);
|
||||
list_push_back (&ready_list, &t->elem);
|
||||
|
||||
/* Insert the thread back into the ready list in priority order. */
|
||||
list_insert_ordered(&ready_list, &t->elem, priority_more, NULL);
|
||||
|
||||
t->status = THREAD_READY;
|
||||
intr_set_level (old_level);
|
||||
}
|
||||
@@ -326,8 +333,11 @@ thread_yield (void)
|
||||
ASSERT (!intr_context ());
|
||||
|
||||
old_level = intr_disable ();
|
||||
|
||||
/* Insert the thread back into the ready list in priority order. */
|
||||
if (cur != idle_thread)
|
||||
list_push_back (&ready_list, &cur->elem);
|
||||
list_insert_ordered(&ready_list, &cur->elem, priority_more, NULL);
|
||||
|
||||
cur->status = THREAD_READY;
|
||||
schedule ();
|
||||
intr_set_level (old_level);
|
||||
@@ -350,11 +360,57 @@ thread_foreach (thread_action_func *func, void *aux)
|
||||
}
|
||||
}
|
||||
|
||||
/* Function that compares the two threads associated with the provided
|
||||
list_elem structures. Returns true if the thread associated with a_ has
|
||||
a higher priority than that of b_. */
|
||||
bool
|
||||
priority_more (const struct list_elem *a_, const struct list_elem *b_,
|
||||
void *aux UNUSED)
|
||||
{
|
||||
struct thread *a = list_entry (a_, struct thread, elem);
|
||||
struct thread *b = list_entry (b_, struct thread, elem);
|
||||
|
||||
return a->priority > b->priority;
|
||||
}
|
||||
|
||||
/* Sets the current thread's priority to NEW_PRIORITY. */
|
||||
void
|
||||
thread_set_priority (int new_priority)
|
||||
{
|
||||
ASSERT (new_priority >= PRI_MIN);
|
||||
ASSERT (new_priority <= PRI_MAX);
|
||||
|
||||
int old_priority = thread_get_priority ();
|
||||
if (new_priority == old_priority)
|
||||
return;
|
||||
|
||||
thread_current ()->priority = new_priority;
|
||||
|
||||
enum intr_level old_level = intr_disable ();
|
||||
|
||||
/* If the thread is in the ready list, the list must be reordered to maintain
|
||||
the priority order. */
|
||||
if (thread_current ()->status == THREAD_READY) {
|
||||
/* Remove from the ready list and reinsert it in priority order. */
|
||||
list_remove (&thread_current ()->elem);
|
||||
list_insert_ordered (&ready_list, &thread_current ()->elem, priority_more,
|
||||
NULL);
|
||||
}
|
||||
|
||||
if (new_priority < old_priority && !list_empty (&ready_list)) {
|
||||
/* If the new priority is lower than the old priority, check if the current
|
||||
thread no longer has the highest priority. If it doesn't, yield the CPU.
|
||||
*/
|
||||
|
||||
struct thread *next_thread =
|
||||
list_entry (list_front (&ready_list), struct thread, elem);
|
||||
|
||||
if (next_thread->priority > new_priority) {
|
||||
thread_yield();
|
||||
}
|
||||
}
|
||||
|
||||
intr_set_level (old_level);
|
||||
}
|
||||
|
||||
/* Returns the current thread's priority. */
|
||||
|
||||
@@ -131,6 +131,8 @@ void thread_yield (void);
|
||||
typedef void thread_action_func (struct thread *t, void *aux);
|
||||
void thread_foreach (thread_action_func *, void *);
|
||||
|
||||
bool priority_more (const struct list_elem *a_, const struct list_elem *b_,
|
||||
void *aux UNUSED);
|
||||
int thread_get_priority (void);
|
||||
void thread_set_priority (int);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user