|
@@ -4,32 +4,32 @@
|
|
|
#include <synch.h>
|
|
|
#include <opt-A1.h>
|
|
|
|
|
|
-/*
|
|
|
+/*
|
|
|
* This simple default synchronization mechanism allows only vehicle at a time
|
|
|
* into the intersection. The intersectionSem is used as a a lock.
|
|
|
* We use a semaphore rather than a lock so that this code will work even
|
|
|
* before locks are implemented.
|
|
|
*/
|
|
|
|
|
|
-/*
|
|
|
+/*
|
|
|
* Replace this default synchronization mechanism with your own (better) mechanism
|
|
|
* needed for your solution. Your mechanism may use any of the available synchronzation
|
|
|
- * primitives, e.g., semaphores, locks, condition variables. You are also free to
|
|
|
+ * primitives, e.g., semaphores, locks, condition variables. You are also free to
|
|
|
* declare other global variables if your solution requires them.
|
|
|
*/
|
|
|
|
|
|
/*
|
|
|
* replace this with declarations of any synchronization and other variables you need here
|
|
|
*/
|
|
|
-static struct semaphore *intersectionSem;
|
|
|
+static struct semaphore * intersectionSem;
|
|
|
|
|
|
|
|
|
-/*
|
|
|
+/*
|
|
|
* The simulation driver will call this function once before starting
|
|
|
* the simulation
|
|
|
*
|
|
|
* You can use it to initialize synchronization and other variables.
|
|
|
- *
|
|
|
+ *
|
|
|
*/
|
|
|
void
|
|
|
intersection_sync_init(void)
|
|
@@ -37,13 +37,14 @@ intersection_sync_init(void)
|
|
|
/* replace this default implementation with your own implementation */
|
|
|
|
|
|
intersectionSem = sem_create("intersectionSem",1);
|
|
|
- if (intersectionSem == NULL) {
|
|
|
+ if (intersectionSem == NULL)
|
|
|
+ {
|
|
|
panic("could not create intersection semaphore");
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
+/*
|
|
|
* The simulation driver will call this function once after
|
|
|
* the simulation has finished
|
|
|
*
|
|
@@ -54,7 +55,8 @@ void
|
|
|
intersection_sync_cleanup(void)
|
|
|
{
|
|
|
/* replace this default implementation with your own implementation */
|
|
|
- KASSERT(intersectionSem != NULL);
|
|
|
+ KASSERT(intersectionSem);
|
|
|
+
|
|
|
sem_destroy(intersectionSem);
|
|
|
}
|
|
|
|
|
@@ -62,7 +64,7 @@ intersection_sync_cleanup(void)
|
|
|
/*
|
|
|
* The simulation driver will call this function each time a vehicle
|
|
|
* tries to enter the intersection, before it enters.
|
|
|
- * This function should cause the calling simulation thread
|
|
|
+ * This function should cause the calling simulation thread
|
|
|
* to block until it is OK for the vehicle to enter the intersection.
|
|
|
*
|
|
|
* parameters:
|
|
@@ -73,12 +75,12 @@ intersection_sync_cleanup(void)
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
-intersection_before_entry(Direction origin, Direction destination)
|
|
|
+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 != NULL);
|
|
|
+ KASSERT(intersectionSem);
|
|
|
P(intersectionSem);
|
|
|
}
|
|
|
|
|
@@ -95,11 +97,11 @@ intersection_before_entry(Direction origin, Direction destination)
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
-intersection_after_exit(Direction origin, Direction destination)
|
|
|
+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 != NULL);
|
|
|
+ KASSERT(intersectionSem);
|
|
|
V(intersectionSem);
|
|
|
}
|