Skip to content

Instantly share code, notes, and snippets.

@amwatson
Created April 15, 2014 00:35
Show Gist options
  • Save amwatson/10692575 to your computer and use it in GitHub Desktop.
Save amwatson/10692575 to your computer and use it in GitHub Desktop.
P3 Patches
commit 41a7fcc7399f718f3121ab34649191d7385c1a1d
Author: Amanda M. Watson <[email protected]>
Date: Mon Apr 14 19:57:47 2014 -0400
added back in kundeschedule
diff --git a/kern/drivers/keyboard.c b/kern/drivers/keyboard.c
index 6862f81..3886241 100644
--- a/kern/drivers/keyboard.c
+++ b/kern/drivers/keyboard.c
@@ -85,7 +85,7 @@ void keyboard_handler() {
if (c == '\n') {
rl.active->state = RUNNING;
- schedule(rl.active, FRONT);
+ kundeschedule(rl.active);
context_switch_needed = 1;
}
}
diff --git a/kern/inc/schedule.h b/kern/inc/schedule.h
index 1cb2260..711f828 100644
--- a/kern/inc/schedule.h
+++ b/kern/inc/schedule.h
@@ -68,6 +68,6 @@ void insert_sleep_queue(TCB_t *thread, int ticks);
void wakeup_sleep_queue(int ticks);
void run_thread(TCB_t *thread);
void validate_esp();
-
+void kundeschedule (TCB_t *thread);
#endif /* _SCHEDULE_H */
diff --git a/kern/locks/kcond.c b/kern/locks/kcond.c
index 3a9b998..b0565bf 100644
--- a/kern/locks/kcond.c
+++ b/kern/locks/kcond.c
@@ -6,7 +6,7 @@
* @bug None known.
*
* Our kcondvars function largely in the same manner as our P2 condvars, with
- * the exception that we have finer grained scheduler control and so can more
+ * the exception that we have finer grained kundescheduler control and so can more
* accurately handle sleeping/waking up of threads.
*
* We have no use at present for _broadcast, so it has not been ported. One
@@ -151,7 +151,7 @@ void kcond_signal(kcond *kv) {
wakeup->tcb->cond_ref = NULL;
wakeup->tcb->state = READY;
LPRINTF("signaling %d", wakeup->tcb->tid);
- schedule(wakeup->tcb, FRONT);
+ kundeschedule(wakeup->tcb);
)
}
diff --git a/kern/locks/kmut.c b/kern/locks/kmut.c
index 37a59c1..c34701e 100644
--- a/kern/locks/kmut.c
+++ b/kern/locks/kmut.c
@@ -158,8 +158,8 @@ void kmut_unlock(kmut *km) {
if (run_this != NULL) {
LPRINTF("LOCK: waking up %d in %p", run_this->tcb->tid, km);
- /* scheduler is protected here */
- schedule(run_this->tcb, FRONT);
+ /* kundescheduler is protected here */
+ kundeschedule(run_this->tcb);
}
km->lock_avail = 1; /* kxchg not currently needed here */
diff --git a/kern/schedule/schedule.c b/kern/schedule/schedule.c
index b4e3712..03ca0bb 100644
--- a/kern/schedule/schedule.c
+++ b/kern/schedule/schedule.c
@@ -154,6 +154,21 @@ void insert_sleep_queue(TCB_t *thread, int ticks) {
return;
}
+void kundeschedule (TCB_t *thread) {
+ assert(thread != NULL);
+ assert(thread != proc_master.scheduler.running);
+
+ Q_REMOVE(&(proc_master.scheduler.desc_queue), thread, desc_queue_link);
+
+ LPRINTF("LOCK SCHEDULE: undescheduled %d from desc queue (now length %d)",
+ thread->tid,
+ proc_master.scheduler.desc_queue.count);
+
+ schedule(thread, FRONT);
+ validate_esp();
+
+}
+
/* @brief given a running/runnable thread, insert it into the deschedule queue
* to be woken up at another time.
*
commit 0d13b81fe6f762594b8edb96b0d90fc1b9b116b1
Author: Amanda M. Watson <[email protected]>
Date: Mon Apr 14 20:18:19 2014 -0400
wait now checks to see if there are too many threads already waiting
diff --git a/kern/inc/pcb.h b/kern/inc/pcb.h
index dcddbe6..92fa960 100644
--- a/kern/inc/pcb.h
+++ b/kern/inc/pcb.h
@@ -54,6 +54,7 @@ typedef struct pcb_node {
Q_NEW_LINK(pcb_node) child_queue_link;
int child_ref; // how many children are still active (even re-parented)
+ int wait_count; // how many peer threads are currently descheduled in wait
exit_status_queue child_exits; // holds exit statuses of exited children
kcond proc_cond; // used for sleep when proc calls wait
// the process' exit status, to be collected when the proc vanishes and
diff --git a/kern/proc/fork.c b/kern/proc/fork.c
index cbce9b6..43790b9 100644
--- a/kern/proc/fork.c
+++ b/kern/proc/fork.c
@@ -291,8 +291,10 @@ int wait(void *status_ptr) {
LPRINTF("%d returning dead exit status", thread->tid);
return tid;
}
- // if there are still live children, deschedule until one exits
- if (proc->child_ref) {
+ // if there are still live children and no peer threads are already waiting
+ // on them
+ if (proc->child_ref && proc->wait_count < proc->child_ref) {
+ proc->wait_count++;
kcond_wait(&(proc->proc_cond), &(proc->proc_lock));
LPRINTF("%d waking up from cond_wait", thread->tid);
// we come back holding the lock, and should now have an exit
@@ -300,6 +302,7 @@ int wait(void *status_ptr) {
exit_status = Q_GET_FRONT(&(proc->child_exits));
assert(exit_status != NULL);
Q_REMOVE(&(proc->child_exits), exit_status, exit_status_link);
+ proc->wait_count--;
kmut_unlock(&(proc->proc_lock));
/* if wait fails, it's because I'm bad at pointers */
if (copy_to_user(status_ptr, &(exit_status->exit_status),
diff --git a/kern/structures/pcb.c b/kern/structures/pcb.c
index ce7aadf..1cec851 100644
--- a/kern/structures/pcb.c
+++ b/kern/structures/pcb.c
@@ -56,6 +56,7 @@ int PCB_init(PCB_t *proc) {
/* init status info: process is initially alive with
* exit status 0 (as per fork spec) and no parent */
proc->state = P_ALIVE;
+ proc->wait_count = 0;
proc->parent = NULL;
proc->child_ref = 0;
proc->thread_count = 0;
commit caf89664c4fef21052373925de6ebc893bc4ec77
Author: Amanda M. Watson <[email protected]>
Date: Mon Apr 14 20:11:18 2014 -0400
fixed cr2 race
diff --git a/kern/interrupts/page_fault_handler.c b/kern/interrupts/page_fault_handler.c
index f5e7ef3..5c773bd 100644
--- a/kern/interrupts/page_fault_handler.c
+++ b/kern/interrupts/page_fault_handler.c
@@ -51,6 +51,10 @@ int page_fault_handler(ureg_t *ureg) {
GET_FLAGS(phys_addr) | READ_WRITE_BIT) >= 0);
kmut_unlock(&(PD->PD_lock));
return 0;
+ /* in the case that the memory allocation has already been resolved,
+ * simply return without setting the page */
+ } else if ((void *)GET_ALIGNED_ADDR(phys_addr) != NULL) {
+ return 0;
}
/* we don't take page faults in the kernel! That'd be absurd! */
assert (ureg->ss == SEGSEL_USER_DS);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment