Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <condition_variable>
  4.  
  5. #include "AirportServer.h"
  6.  
  7.  
  8. /**
  9. * Called by an Airplane when it wishes to land on a runway
  10. */
  11. void AirportServer::reserveRunway(int airplaneNum, AirportRunways::RunwayNumber runway)
  12. {
  13. // Acquire runway(s)
  14. { // Begin critical region
  15.  
  16. //unique_lock<mutex> runwaysLock(runwaysMutex);
  17.  
  18. {
  19. lock_guard<mutex> lk(AirportRunways::checkMutex);
  20.  
  21. cout << "Airplane #" << airplaneNum << " is acquiring any needed runway(s) for landing on Runway "
  22. << AirportRunways::runwayName(runway) << endl;
  23.  
  24. }
  25.  
  26. /**
  27. * ***** Add your synchronization here! *****
  28. */
  29.  
  30.  
  31.  
  32. muticee[runway].lock();
  33.  
  34. switch (runway) {
  35. case AirportRunways::RUNWAY_14:
  36. break;
  37. case AirportRunways::RUNWAY_4L:
  38. muticee[AirportRunways::RUNWAY_15L].lock();
  39. muticee[AirportRunways::RUNWAY_15R].lock();
  40. break;
  41. case AirportRunways::RUNWAY_4R:
  42. muticee[AirportRunways::RUNWAY_15L].lock();
  43. muticee[AirportRunways::RUNWAY_15R].lock();
  44. break;
  45. case AirportRunways::RUNWAY_15L:
  46. muticee[AirportRunways::RUNWAY_4L].lock();
  47. muticee[AirportRunways::RUNWAY_4R].lock();
  48. break;
  49. case AirportRunways::RUNWAY_9:
  50. muticee[AirportRunways::RUNWAY_4R].lock();
  51. muticee[AirportRunways::RUNWAY_15R].lock();
  52. break;
  53. case AirportRunways::RUNWAY_15R:
  54. muticee[AirportRunways::RUNWAY_4L].lock();
  55. muticee[AirportRunways::RUNWAY_4R].lock();
  56. break;
  57. default:
  58. throw "Invalid runway";
  59. }
  60.  
  61. // Check status of the airport for any rule violations
  62. AirportRunways::checkAirportStatus(runway);
  63.  
  64. //runwaysLock.unlock();
  65.  
  66. } // End critical region
  67.  
  68. // obtain a seed from the system clock:
  69. unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
  70. std::default_random_engine generator(seed);
  71.  
  72. // Taxi for a random number of milliseconds
  73. std::uniform_int_distribution<int> taxiTimeDistribution(1, MAX_TAXI_TIME);
  74. int taxiTime = taxiTimeDistribution(generator);
  75.  
  76. {
  77. lock_guard<mutex> lk(AirportRunways::checkMutex);
  78.  
  79. cout << "Airplane #" << airplaneNum << " is taxiing on Runway " << AirportRunways::runwayName(runway)
  80. << " for " << taxiTime << " milliseconds\n";
  81. }
  82.  
  83. std::this_thread::sleep_for(std::chrono::milliseconds(taxiTime));
  84.  
  85. } // end AirportServer::reserveRunway()
  86.  
  87.  
  88. /**
  89. * Called by an Airplane when it is finished landing
  90. */
  91. void AirportServer::releaseRunway(int airplaneNum, AirportRunways::RunwayNumber runway)
  92. {
  93. // Release the landing runway and any other needed runways
  94. { // Begin critical region
  95.  
  96. //unique_lock<mutex> runwaysLock(runwaysMutex);
  97.  
  98. {
  99. lock_guard<mutex> lk(AirportRunways::checkMutex);
  100.  
  101. cout << "Airplane #" << airplaneNum << " is releasing any needed runway(s) after landing on Runway "
  102. << AirportRunways::runwayName(runway) << endl;
  103. }
  104.  
  105. /**
  106. * ***** Add your synchronization here! *****
  107. */
  108.  
  109. // Update the status of the airport to indicate that the landing is complete
  110. AirportRunways::finishedWithRunway(runway);
  111.  
  112. muticee[runway].unlock();
  113.  
  114. switch (runway) {
  115. case AirportRunways::RUNWAY_14:
  116. break;
  117. case AirportRunways::RUNWAY_4L:
  118. muticee[AirportRunways::RUNWAY_15L].unlock();
  119. muticee[AirportRunways::RUNWAY_15R].unlock();
  120. break;
  121. case AirportRunways::RUNWAY_4R:
  122. muticee[AirportRunways::RUNWAY_15L].unlock();
  123. muticee[AirportRunways::RUNWAY_15R].unlock();
  124. break;
  125. case AirportRunways::RUNWAY_15L:
  126. muticee[AirportRunways::RUNWAY_4L].unlock();
  127. muticee[AirportRunways::RUNWAY_4R].unlock();
  128. break;
  129. case AirportRunways::RUNWAY_9:
  130. muticee[AirportRunways::RUNWAY_4R].unlock();
  131. muticee[AirportRunways::RUNWAY_15R].unlock();
  132. break;
  133. case AirportRunways::RUNWAY_15R:
  134. muticee[AirportRunways::RUNWAY_4L].unlock();
  135. muticee[AirportRunways::RUNWAY_4R].unlock();
  136. break;
  137. default:
  138. throw "Invalid runway";
  139. }
  140.  
  141.  
  142. //runwaysLock.unlock();
  143.  
  144. } // End critical region
  145.  
  146. // obtain a seed from the system clock:
  147. unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
  148. std::default_random_engine generator(seed);
  149.  
  150. // Wait for a random number of milliseconds before requesting the next landing for this Airplane
  151. std::uniform_int_distribution<int> waitTimeDistribution(1, MAX_WAIT_TIME);
  152. int waitTime = waitTimeDistribution(generator);
  153.  
  154. {
  155. lock_guard<mutex> lk(AirportRunways::checkMutex);
  156.  
  157. cout << "Airplane #" << airplaneNum << " is waiting for " << waitTime << " milliseconds before landing again\n";
  158. }
  159.  
  160. std::this_thread::sleep_for(std::chrono::milliseconds(waitTime));
  161.  
  162. } // end AirportServer::releaseRunway()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement