traffic_synch.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <types.h>
  2. #include <lib.h>
  3. #include <synchprobs.h>
  4. #include <synch.h>
  5. #include <opt-A1.h>
  6. /*
  7. * This simple default synchronization mechanism allows only vehicle at a time
  8. * into the intersection. The intersectionSem is used as a a lock.
  9. * We use a semaphore rather than a lock so that this code will work even
  10. * before locks are implemented.
  11. */
  12. /*
  13. * Replace this default synchronization mechanism with your own (better) mechanism
  14. * needed for your solution. Your mechanism may use any of the available synchronzation
  15. * primitives, e.g., semaphores, locks, condition variables. You are also free to
  16. * declare other global variables if your solution requires them.
  17. */
  18. /*
  19. * replace this with declarations of any synchronization and other variables you need here
  20. */
  21. static struct semaphore *intersectionSem;
  22. /*
  23. * The simulation driver will call this function once before starting
  24. * the simulation
  25. *
  26. * You can use it to initialize synchronization and other variables.
  27. *
  28. */
  29. void
  30. intersection_sync_init(void)
  31. {
  32. /* replace this default implementation with your own implementation */
  33. intersectionSem = sem_create("intersectionSem",1);
  34. if (intersectionSem == NULL) {
  35. panic("could not create intersection semaphore");
  36. }
  37. return;
  38. }
  39. /*
  40. * The simulation driver will call this function once after
  41. * the simulation has finished
  42. *
  43. * You can use it to clean up any synchronization and other variables.
  44. *
  45. */
  46. void
  47. intersection_sync_cleanup(void)
  48. {
  49. /* replace this default implementation with your own implementation */
  50. KASSERT(intersectionSem != NULL);
  51. sem_destroy(intersectionSem);
  52. }
  53. /*
  54. * The simulation driver will call this function each time a vehicle
  55. * tries to enter the intersection, before it enters.
  56. * This function should cause the calling simulation thread
  57. * to block until it is OK for the vehicle to enter the intersection.
  58. *
  59. * parameters:
  60. * * origin: the Direction from which the vehicle is arriving
  61. * * destination: the Direction in which the vehicle is trying to go
  62. *
  63. * return value: none
  64. */
  65. void
  66. intersection_before_entry(Direction origin, Direction destination)
  67. {
  68. /* replace this default implementation with your own implementation */
  69. (void)origin; /* avoid compiler complaint about unused parameter */
  70. (void)destination; /* avoid compiler complaint about unused parameter */
  71. KASSERT(intersectionSem != NULL);
  72. P(intersectionSem);
  73. }
  74. /*
  75. * The simulation driver will call this function each time a vehicle
  76. * leaves the intersection.
  77. *
  78. * parameters:
  79. * * origin: the Direction from which the vehicle arrived
  80. * * destination: the Direction in which the vehicle is going
  81. *
  82. * return value: none
  83. */
  84. void
  85. intersection_after_exit(Direction origin, Direction destination)
  86. {
  87. /* replace this default implementation with your own implementation */
  88. (void)origin; /* avoid compiler complaint about unused parameter */
  89. (void)destination; /* avoid compiler complaint about unused parameter */
  90. KASSERT(intersectionSem != NULL);
  91. V(intersectionSem);
  92. }