Compare commits
13 Commits
virtual-me
...
task1/them
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd5143110f | ||
|
|
e38e1400a2 | ||
|
|
810a5376b9 | ||
|
|
c32a69a368 | ||
|
|
163b7f9016 | ||
|
|
3c1a26b668 | ||
|
|
54b46806ba | ||
|
|
6855a48603 | ||
|
|
79061fcb9b | ||
|
|
d4d5a7a937 | ||
|
|
f1fa7d2ffb | ||
|
|
1821d73b09 | ||
|
|
4ed64cf173 |
@@ -125,11 +125,7 @@ sema_up (struct semaphore *sema)
|
|||||||
|
|
||||||
sema->value++;
|
sema->value++;
|
||||||
intr_set_level (old_level);
|
intr_set_level (old_level);
|
||||||
|
thread_yield ();
|
||||||
/* 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_);
|
static void sema_test_helper (void *sema_);
|
||||||
@@ -324,6 +320,38 @@ cond_init (struct condition *cond)
|
|||||||
list_init (&cond->waiters);
|
list_init (&cond->waiters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Returns true iff the priority of the only thread in the first singleton
|
||||||
|
semaphore is greater than the priority of the only thread in the second
|
||||||
|
singleton semaphore.
|
||||||
|
|
||||||
|
Where this function is used for insertion in a singleton semaphore list, the
|
||||||
|
third argument may specify a list_elem * to assume corresponds to the thread
|
||||||
|
waiting for the inserting semaphore. For correctness, ensure this thread
|
||||||
|
calls sema_down () for this semaphore before future list accesses. */
|
||||||
|
|
||||||
|
static bool
|
||||||
|
singleton_sema_priority_greater (const struct list_elem *a,
|
||||||
|
const struct list_elem *b,
|
||||||
|
void *insertingThread)
|
||||||
|
{
|
||||||
|
struct list_elem *te_a, *te_b;
|
||||||
|
|
||||||
|
te_b = list_front (
|
||||||
|
&list_entry (b, struct semaphore_elem, elem)->semaphore.waiters);
|
||||||
|
|
||||||
|
if (insertingThread == NULL)
|
||||||
|
{
|
||||||
|
te_a = list_front (
|
||||||
|
&list_entry (a, struct semaphore_elem, elem)->semaphore.waiters);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
te_a = insertingThread;
|
||||||
|
}
|
||||||
|
|
||||||
|
return thread_priority_greater (te_a, te_b, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
/* Atomically releases LOCK and waits for COND to be signaled by
|
/* Atomically releases LOCK and waits for COND to be signaled by
|
||||||
some other piece of code. After COND is signaled, LOCK is
|
some other piece of code. After COND is signaled, LOCK is
|
||||||
reacquired before returning. LOCK must be held before calling
|
reacquired before returning. LOCK must be held before calling
|
||||||
|
|||||||
@@ -219,6 +219,7 @@ thread_create (const char *name, int priority,
|
|||||||
|
|
||||||
/* Add to run queue. */
|
/* Add to run queue. */
|
||||||
thread_unblock (t);
|
thread_unblock (t);
|
||||||
|
thread_yield ();
|
||||||
|
|
||||||
/* Yield if the new thread has a higher priority than the current thread. */
|
/* Yield if the new thread has a higher priority than the current thread. */
|
||||||
if (priority > thread_get_priority ())
|
if (priority > thread_get_priority ())
|
||||||
@@ -377,40 +378,12 @@ priority_more (const struct list_elem *a_, const struct list_elem *b_,
|
|||||||
void
|
void
|
||||||
thread_set_priority (int new_priority)
|
thread_set_priority (int new_priority)
|
||||||
{
|
{
|
||||||
ASSERT (new_priority >= PRI_MIN);
|
ASSERT (PRI_MIN <= new_priority && new_priority <= PRI_MAX);
|
||||||
ASSERT (new_priority <= PRI_MAX);
|
|
||||||
|
|
||||||
int old_priority = thread_get_priority ();
|
int old_priority = thread_get_priority ();
|
||||||
if (new_priority == old_priority)
|
|
||||||
return;
|
|
||||||
|
|
||||||
thread_current ()->priority = new_priority;
|
thread_current ()->priority = new_priority;
|
||||||
|
if (new_priority < old_priority)
|
||||||
enum intr_level old_level = intr_disable ();
|
thread_yield ();
|
||||||
|
|
||||||
/* 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. */
|
/* Returns the current thread's priority. */
|
||||||
@@ -654,6 +627,17 @@ allocate_tid (void)
|
|||||||
return tid;
|
return tid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Returns true iff the priority of the first list element's thread is greater
|
||||||
|
than that of the second list element's thread. */
|
||||||
|
bool
|
||||||
|
thread_priority_greater (const struct list_elem *a, const struct list_elem *b,
|
||||||
|
void *aux UNUSED)
|
||||||
|
{
|
||||||
|
struct thread *ta = list_entry (a, struct thread, elem);
|
||||||
|
struct thread *tb = list_entry (b, struct thread, elem);
|
||||||
|
return ta->priority > tb->priority;
|
||||||
|
}
|
||||||
|
|
||||||
/* Offset of `stack' member within `struct thread'.
|
/* Offset of `stack' member within `struct thread'.
|
||||||
Used by switch.S, which can't figure it out on its own. */
|
Used by switch.S, which can't figure it out on its own. */
|
||||||
uint32_t thread_stack_ofs = offsetof (struct thread, stack);
|
uint32_t thread_stack_ofs = offsetof (struct thread, stack);
|
||||||
|
|||||||
@@ -141,4 +141,7 @@ void thread_set_nice (int);
|
|||||||
int thread_get_recent_cpu (void);
|
int thread_get_recent_cpu (void);
|
||||||
int thread_get_load_avg (void);
|
int thread_get_load_avg (void);
|
||||||
|
|
||||||
|
/* Returns true iff the priority of the first list element's thread is greater
|
||||||
|
than that of the second list element's thread. */
|
||||||
|
list_less_func thread_priority_greater;
|
||||||
#endif /* threads/thread.h */
|
#endif /* threads/thread.h */
|
||||||
|
|||||||
Reference in New Issue
Block a user