123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #include <types.h>
- #include <lib.h>
- #include <synchprobs.h>
- #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
- * 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;
- /*
- * 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)
- {
- /* replace this default implementation with your own implementation */
- intersectionSem = sem_create("intersectionSem",1);
- if (intersectionSem == NULL) {
- panic("could not create intersection semaphore");
- }
- return;
- }
- /*
- * The simulation driver will call this function once after
- * the simulation has finished
- *
- * You can use it to clean up any synchronization and other variables.
- *
- */
- void
- intersection_sync_cleanup(void)
- {
- /* replace this default implementation with your own implementation */
- KASSERT(intersectionSem != NULL);
- sem_destroy(intersectionSem);
- }
- /*
- * 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
- * to block until it is OK for the vehicle to enter the intersection.
- *
- * parameters:
- * * origin: the Direction from which the vehicle is arriving
- * * destination: the Direction in which the vehicle is trying to go
- *
- * return value: none
- */
- 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 != NULL);
- P(intersectionSem);
- }
- /*
- * The simulation driver will call this function each time a vehicle
- * leaves the intersection.
- *
- * parameters:
- * * origin: the Direction from which the vehicle arrived
- * * destination: the Direction in which the vehicle is going
- *
- * return value: none
- */
- 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 != NULL);
- V(intersectionSem);
- }
|