Browse Source

adding data structs, some functions to help with eventual solution

tarfeef101 6 years ago
parent
commit
bd6fa3ef2f
1 changed files with 181 additions and 0 deletions
  1. 181 0
      kern/synchprobs/traffic_synch.c

+ 181 - 0
kern/synchprobs/traffic_synch.c

@@ -22,7 +22,160 @@
  * replace this with declarations of any synchronization and other variables you need here
  */
 static struct semaphore * intersectionSem;
+static struct lock * globlock;
+static struct semaphore * nsem;
+static struct semaphore * esem;
+static struct semaphore * ssem;
+static struct semaphore * ssem;
 
+// This section contains global vars and useful functions to work with them. As well as the structs used for them
+typedef struct car
+{
+  Direction origin;
+  Direction dest;
+  bool old;
+  car * next;
+} car;
+
+typedef struct list
+{
+  car * front;
+  car * back;
+} list;
+
+list * queue = NULL;
+list * active = NULL;
+
+// car initializer/allocator
+car * newcar(Direction origin, Direction dest)
+{
+  car * temp = kmalloc(sizeof(car));
+  
+  if(!(temp))
+  {
+    kprintf("Failed to create a car.");
+    panic();
+  }
+  
+  temp->origin = origin;
+  temp->dest = dest;
+  temp->old = 0;
+  temp->next = NULL;
+}
+
+// list initializer/allocator
+list * newlist()
+{
+  list * temp = kmalloc(sizeof(list));
+  
+  if(!(temp))
+  {
+    kprintf("Could not allocate list.");
+    panic();
+  }
+  
+  temp->front = NULL;
+  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)
+{
+  if (!(queue->back))
+  {
+    queue->front = newcar;
+    queue->back = newcar;
+  }
+  
+  if (front)
+  {
+    car * temp = queue->front;
+    queue->front = newcar;
+    newcar->next = temp;
+    
+    return;
+  }
+  
+  queue->back->next = newcar;
+  queue->back = newcar;
+}
+
+// push a car to the end of the active list
+void push(car * newcar)
+{
+  if (!(active->front))
+  {
+    active->front = newcar;
+    active->back = newcar;
+  }
+  
+  active->back->next = newcar;
+  active->back = newcar;
+}
+
+// remove an element from the front of the queue
+car * pop()
+{
+  if (!(queue->front))
+  {
+    return NULL;
+  }
+  
+  if (queue->front == queue->back)
+  {
+    queue->back == NULL;
+  }
+  
+  car * temp = queue->front;
+  queue->front = queue->front->next;
+  return temp;
+}
+
+// called when a car clears the intersection
+void clearint(car * done)
+{
+  car * temp = active->front();
+  car * temp2 = NULL;
+  
+  while(temp != done)
+  {
+    temp = active->next;
+    temp2 = temp;
+  }
+  
+  // first element of the list is being removed
+  if (!(temp2))
+  {
+    active->front = active->front->next;
+    goto SKIP1;
+  }
+  
+  temp2->next = temp->next;
+  SKIP1:
+  kfree(temp);
+}
+
+// cleans up a list
+void dellist(list * dead)
+{
+  car * temp = dead->front;
+  
+  while (temp)
+  {
+    car * temp2 = temp->next;
+    kfree(temp);
+    temp = temp2;
+  }
+  
+  kree(dead);
+}
+
+// returns true if a car is turning right
+bool rightturn(car * car)
+{
+  int temp = car->origin - car->dest;
+  return (temp == 1 || temp == -3)
+}
 
 /*
  * The simulation driver will call this function once before starting
@@ -42,6 +195,34 @@ intersection_sync_init(void)
     panic("could not create intersection semaphore");
   }
   return;
+  
+  nsem = sem_create("nsem",1);
+  if (intersectionSem == NULL)
+  {
+    panic("could not create intersection semaphore");
+  }
+  return;
+  
+  intersectionSem = sem_create("intersectionSem",1);
+  if (intersectionSem == NULL)
+  {
+    panic("could not create intersection semaphore");
+  }
+  return;
+  
+  intersectionSem = sem_create("intersectionSem",1);
+  if (intersectionSem == NULL)
+  {
+    panic("could not create intersection semaphore");
+  }
+  return;
+  
+  intersectionSem = sem_create("intersectionSem",1);
+  if (intersectionSem == NULL)
+  {
+    panic("could not create intersection semaphore");
+  }
+  return;
 }
 
 /*