-
Notifications
You must be signed in to change notification settings - Fork 0
/
Philosopher.cpp
145 lines (133 loc) · 3.22 KB
/
Philosopher.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "Philosopher.h"
#include <cstdio>
Philosopher::Philosopher(int _id2, Fork* leftFork2, Fork* rightFork2)
{
_id = _id2;
leftFork = leftFork2;
rightFork = rightFork2;
full = false;
leftNeighbour = NULL;
rightNeighbour = NULL;
//printf("\nIn the constructor, _id: %d", _id);
//printf("\tWith forks: %d AND %d", leftFork -> getId(), rightFork -> getId());
}
Philosopher::Philosopher(int _id2, Fork* leftFork2)
{
_id = _id2;
leftFork = leftFork2;
rightFork = NULL;
full = false;
leftNeighbour = NULL;
rightNeighbour = NULL;
leftFork -> lock();
}
Philosopher::~Philosopher()
{
leftNeighbour = NULL;
rightNeighbour = NULL;
full = false;
_id = -1;
leftFork = NULL;
rightFork = NULL;
}
int Philosopher::sayYourNameBitch()
{
return _id;
}
void Philosopher::setLeftNeighbour(Philosopher* left)
{
if(left != NULL)
leftNeighbour = left;
}
void Philosopher::setRightNeighbour(Philosopher* right)
{
if(right != NULL)
rightNeighbour = right;
}
Fork* Philosopher::giveLeftFork()
{
Fork* tmp = NULL;
if(leftFork != NULL) {
if((leftFork -> isDirty()) == true){
leftFork -> setAsClean();
tmp = leftFork;
//(*leftFork).unlock();
leftFork = NULL;
return tmp;
}
}
else
return NULL;
}
Fork* Philosopher::giveRightFork()
{
Fork* tmp = NULL;
if(rightFork != NULL) {
if((rightFork -> isDirty()) == true){
rightFork -> setAsClean();
tmp = rightFork;
//(*rightFork).unlock();
rightFork = NULL;
return tmp;
}
}
else
return NULL;
}
void Philosopher::lockForks(){
//printf("Locking left fork\n");
int resLockLeft = leftFork -> lock();
if( resLockLeft != 0)
printf("Problem with locking left fork: %d\n", resLockLeft);
int resLockRight = rightFork -> lock();
//printf("Locking right fork\n");
if(resLockRight != 0)
printf("Problem with locking left fork: %d\n", resLockRight);
}
//void Philosopher::releaseForks(){
//leftFork -> unlock();
//rightFork -> unlock();
//}
void Philosopher::takeLeftFork()
{
if(leftFork == NULL && leftNeighbour != NULL)
leftFork = leftNeighbour -> giveRightFork();
//int resLeftLock = leftFork -> lock();
//printf("Left fork lock %d\n", resLeftLock );
}
void Philosopher::takeRightFork()
{
if(rightFork == NULL && rightNeighbour != NULL)
rightFork = rightNeighbour -> giveLeftFork();
//int resRightLock = rightFork -> lock();
//printf("Right fork lock %d\n", resRightLock );
}
void Philosopher::acquireForks()
{
//printf("%s\n", (leftFork == NULL ? "leftFork is NULL" : "leftFork is Not NULL" ));
//printf("%s\n", (rightFork == NULL ? "rightFork is NULL" : "rightFork is Not NULL" ));
if(rightFork == NULL)
takeRightFork();
else if(leftFork == NULL)
takeLeftFork();
else if(rightFork == NULL && leftFork == NULL){
takeLeftFork();
takeRightFork();
}
}
void Philosopher::iAmEating()
{;
//printf("\nPhilosopher %d is eating...", _id);
//printf("\tPhilosopher %d is done eating.\t", _id);
rightFork -> setAsDirty();
leftFork -> setAsDirty();
full = true;
}
bool Philosopher::isDoneEating()
{
return full;
}
bool Philosopher::hasBothForks()
{
return (leftFork != NULL && rightFork != NULL);
}