Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. void *car_arrive(void *arg) {
  2.  
  3. struct lane* l = arg;
  4.  
  5. while (1) {
  6.  
  7. pthread_mutex_lock(&l->lock);
  8.  
  9. // When the process is finished.
  10. if ((l->inc <= 0) || (l->in_cars == NULL)) {
  11. pthread_cond_signal(&l->consumer_cv);
  12. pthread_mutex_unlock(&l->lock);
  13. return NULL; // Stopping the infinite loop under the correct conditions.
  14. }
  15.  
  16. // If the buffer is full.
  17. while (l->in_buf == l->capacity) {
  18. pthread_cond_wait(&l->consumer_cv, &l->lock);
  19. }
  20.  
  21. // Putting the car in the buffer.
  22. l->buffer[l->tail] = l->in_cars;
  23. l->tail++;
  24.  
  25. if (l->tail >= l->capacity) {
  26. l->tail = 0;
  27. }
  28.  
  29. l->in_buf++;
  30. l->in_cars = l->in_cars->next;
  31.  
  32. pthread_cond_signal(&l->producer_cv);
  33.  
  34. pthread_mutex_unlock(&l->lock);
  35.  
  36. }
  37.  
  38. }
  39.  
  40. /**
  41. * TODO: Fill in this function
  42. *
  43. * Moves cars from a single lane across the intersection. Cars
  44. * crossing the intersection must abide the rules of the road
  45. * and cross along the correct path. Ensure to notify the
  46. * arrival thread as room becomes available in the lane.
  47. *
  48. * Note: After crossing the intersection the car should be added
  49. * to the out_cars list of the lane that corresponds to the car's
  50. * out_dir. Do not free the cars!
  51. *
  52. *
  53. * Note: For testing purposes, each car which gets to cross the
  54. * intersection should print the following three numbers on a
  55. * new line, separated by spaces:
  56. * - the car's 'in' direction, 'out' direction, and id.
  57. *
  58. * You may add other print statements, but in the end, please
  59. * make sure to clear any prints other than the one specified above,
  60. * before submitting your final code.
  61. */
  62. void *car_cross(void *arg) {
  63.  
  64. struct lane *l = arg;
  65.  
  66. while (1) {
  67.  
  68. pthread_mutex_lock(&l->lock);
  69.  
  70. // When the process is finished.
  71. if (l->inc <= 0) {
  72. pthread_mutex_unlock(&l->lock);
  73. return NULL; // Stopping the infinite loop under the correct conditions.
  74. }
  75.  
  76. // While the buffer is empty.
  77. while (l->in_buf == 0) {
  78. pthread_cond_wait(&l->producer_cv, &l->lock);
  79. }
  80.  
  81. // Get the car from the buffer.
  82. struct car *cur_car = l->buffer[l->head];
  83.  
  84. l->head++;
  85.  
  86. if (l->head >= l->capacity) {
  87. l->head = 0;
  88. }
  89.  
  90. l->in_buf--;
  91. l->inc--;
  92.  
  93. pthread_cond_signal(&l->consumer_cv);
  94.  
  95. pthread_mutex_unlock(&l->lock);
  96.  
  97. // Print statement for testing purposes.
  98. printf("in_dir: %d || out_dir: %d || ID: %d\n", cur_car->in_dir, cur_car->out_dir, cur_car->id);
  99.  
  100. // Find the path.
  101. int* path = compute_path(cur_car->in_dir, cur_car->out_dir);
  102.  
  103. int x = 0;
  104. while (x < (sizeof(path)/sizeof(int))) {
  105. pthread_mutex_lock(&isection.quad[path[x]]);
  106. x++;
  107. }
  108.  
  109. struct lane* exit = &isection.lanes[cur_car->out_dir];
  110.  
  111. pthread_mutex_lock(&exit->lock);
  112.  
  113. cur_car->next = exit->out_cars;
  114. exit->out_cars = cur_car;
  115. exit->passed++;
  116.  
  117. pthread_mutex_unlock(&exit->lock);
  118.  
  119. int y = 0;
  120. while (y < (sizeof(path)/sizeof(int))) {
  121. pthread_mutex_unlock(&isection.quad[path[y]]);
  122. y++;
  123. }
  124.  
  125. // Free up the path for the next loop.
  126. free(path);
  127.  
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement