-
Notifications
You must be signed in to change notification settings - Fork 3
/
caltrain.c
84 lines (70 loc) · 2.37 KB
/
caltrain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <pthread.h>
#include "caltrain.h"
/**
Initializes all the mutexes and condition-variables.
*/
void
station_init(struct station *station)
{
station->out_passengers = 0;
station->in_passengers = 0;
pthread_mutex_init(&(station->lock), NULL);
pthread_cond_init(&(station->train_arrived_cond), NULL);
pthread_cond_init(&(station->passengers_seated_cond), NULL);
pthread_cond_init(&(station->train_is_full_cond), NULL);
}
/**
Loads the train with passengers. When a passenger robot arrives in a station, it first invokes this function.
The function must not return until the train is satisfactorily loaded.
Params:
stattion: current station pointer
count: indicates how many seats are available on the train
*/
void
station_load_train(struct station *station, int count)
{
// Enter critical region
pthread_mutex_lock(&(station->lock));
while ((station->out_passengers > 0) && (count > 0)){
pthread_cond_signal(&(station->train_arrived_cond));
count--;
pthread_cond_wait(&(station->passengers_seated_cond), &(station->lock));
}
if (station->in_passengers > 0)
pthread_cond_wait(&(station->train_is_full_cond), &(station->lock));
// Leave critical region
pthread_mutex_unlock(&(station->lock));
}
/**
This function must not return until a train is in the station and there are enough free seats on
the train for this passenger. Once this function returns, the passenger robot will move the
passenger on board the train and into a seat.
Once the passenger is seated, it will call the function: station_on_board
Params:
stattion: current station pointe
*/
void
station_wait_for_train(struct station *station)
{
pthread_mutex_lock(&(station->lock));
station->out_passengers++;
pthread_cond_wait(&(station->train_arrived_cond), &(station->lock));
station->out_passengers--;
station->in_passengers++;
pthread_mutex_unlock(&(station->lock));
pthread_cond_signal(&(station->passengers_seated_cond));
}
/**
Use this function to let the train know that it’s on board.
Params:
stattion: current station pointer
*/
void
station_on_board(struct station *station)
{
pthread_mutex_lock(&(station->lock));
station->in_passengers--;
pthread_mutex_unlock(&(station->lock));
if (station->in_passengers == 0)
pthread_cond_broadcast(&(station->train_is_full_cond));
}