浏览代码

finished boilerplate code, just thread-specific stuff now

tarfeef101 6 年之前
父节点
当前提交
d8bc5a0dc5
共有 1 个文件被更改,包括 61 次插入10 次删除
  1. 61 10
      kern/synchprobs/traffic_synch.c

+ 61 - 10
kern/synchprobs/traffic_synch.c

@@ -78,8 +78,8 @@ list * newlist()
   temp->back = NULL;
 }
 
-// puts a car into the queue, at the front if front is true, the back otherwise
-void enqueue(car * newcar, bool front)
+// puts a car into the queue, at the front if old is true, the back otherwise
+void enqueue(car * newcar)
 {
   if (!(queue->back))
   {
@@ -87,7 +87,7 @@ void enqueue(car * newcar, bool front)
     queue->back = newcar;
   }
   
-  if (front)
+  if (newcar->old) // if the car is "old", let it go first. be nice to your elders
   {
     car * temp = queue->front;
     queue->front = newcar;
@@ -232,13 +232,15 @@ intersection_sync_init(void)
  * You can use it to clean up any synchronization and other variables.
  *
  */
-void
-intersection_sync_cleanup(void)
+void intersection_sync_cleanup()
 {
-  /* replace this default implementation with your own implementation */
   KASSERT(intersectionSem);
+  KASSERT(queue);
+  KASSERT(active);
   
   sem_destroy(intersectionSem);
+  dellist(queue);
+  dellist(active);
 }
 
 
@@ -255,14 +257,47 @@ intersection_sync_cleanup(void)
  * return value: none
  */
 
-void
-intersection_before_entry(Direction origin, Direction destination)
+void intersection_before_entry(Direction origin, Direction destination)
 {
   /* replace this default implementation with your own implementation */
   (void)origin;  /* avoid compiler complaint about unused parameter */
   (void)destination; /* avoid compiler complaint about unused parameter */
   KASSERT(intersectionSem);
   P(intersectionSem);
+  
+  // starting my own shit
+  
+  car * new = newcar(origin, destination);
+  
+  // Nothing in intersection, so proceed
+  if (!(active->front))
+  {
+    push(new);
+    return;
+  }
+  
+  // things are in the intersection
+  if (active->front)
+  {
+    car * temp = active->front;
+    
+    while (temp)
+    {
+      if (temp->origin == new->origin || (temp->origin == new->dest && temp->dest == new->origin) || (temp->dest != new->dest && (rightturn(new) || rightturn(temp))))
+      {
+        temp = temp->next;
+        continue;
+      }
+      else
+      {
+        enqueue(new);
+        new->old = 1; // make new "old", since now it has already waited once
+        // SLEEP AND SET TO WAKE ONCE temp is out of the intersection!
+      }
+    }
+    
+    push(new);
+  }
 }
 
 
@@ -277,12 +312,28 @@ intersection_before_entry(Direction origin, Direction destination)
  * return value: none
  */
 
-void
-intersection_after_exit(Direction origin, Direction destination)
+void intersection_after_exit(Direction origin, Direction destination)
 {
   /* replace this default implementation with your own implementation */
   (void)origin;  /* avoid compiler complaint about unused parameter */
   (void)destination; /* avoid compiler complaint about unused parameter */
   KASSERT(intersectionSem);
   V(intersectionSem);
+  
+  // My shit
+  
+  car * temp = active->first;
+  
+  while (temp)
+  {
+    if (temp->origin == origin && temp->dest == destination)
+    {
+      clearing(temp);
+      break;
+    }
+    else
+    {
+      temp = temp->next;
+    }
+  }
 }