-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_queue.c
151 lines (140 loc) · 4.61 KB
/
user_queue.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
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
146
147
148
149
150
151
#include "user_queue.h"
// Put data unless queue is full
void putUser(UserQueue *queue, User user) {
// 1. Lock
pthread_mutex_lock(&queue->lockQueue);
// 2. Wait until queue is not full
while (queue->length == QSIZE) {
pthread_cond_wait(&queue->condNotFull, &queue->lockQueue);
}
// 3. Put Algorithm for Circular Array
// 3.a. Assign the value `data` to the position pointed by `put_index`
queue->UserArr[queue->putIndex] = user;
// 3.b. Modularly increase `put_index` by 1 so that it is within `capacity`
queue->putIndex = (queue->putIndex + 1) % QSIZE;
// at all times 3.c. Increase `length` by 1
queue->length++;
// 4. Send a signal that queue is not empty
pthread_cond_signal(&queue->condNotEmpty);
// 5. Unlock
pthread_mutex_unlock(&queue->lockQueue);
}
// Get data unless queue is empty
User getUser(UserQueue *queue) {
User user;
// 1. Lock
pthread_mutex_lock(&queue->lockQueue);
// 2: Wait until queue is not empty
while (queue->length == 0) {
pthread_cond_wait(&queue->condNotEmpty, &queue->lockQueue);
}
// 3. Get Algorithm for Circular Array
// 3.a. Get the data at index `get_index` and then modularly add `get_index`
// by
// so that it is within capacity at all times
user = queue->UserArr[queue->getIndex];
// 3.b. Decrease `length` by 1
queue->length--;
// 3.c. When `length` is zero, set `get_index` and `put_index` to 0
if (queue->length == 0) {
queue->getIndex = queue->putIndex = 0;
} else {
queue->getIndex = (queue->getIndex + 1) % QSIZE;
}
// 4. Send a signal that queue is not full
pthread_cond_signal(&queue->condNotFull);
// 5. Unlock
// Note: Signal before releasing the lock because the signal thread might
// acquire the lock again.
pthread_mutex_unlock(&queue->lockQueue);
// 6. Return data
return user;
}
bool userLoggedIn(UserQueue *queue, uint32_t userID) {
pthread_mutex_lock(&queue->lockQueue);
// condition variable 쓸 여지가 있음
// from getIndex to putIndex because the queue is circular
for (int i = queue->getIndex;; i = (i + 1) % QSIZE) {
if (i == queue->putIndex) {
break;
}
if (queue->UserArr[i].userID == userID && queue->UserArr[i].isLoggedIn) {
pthread_mutex_unlock(&queue->lockQueue);
return true;
}
}
pthread_mutex_unlock(&queue->lockQueue);
return false;
}
bool isClientActive(UserQueue *queue, int clientID) {
pthread_mutex_lock(&queue->lockQueue);
// Condition variable 쓸 여지가 있음
// from getIndex to putIndex because the queue is circular
for (int i = queue->getIndex;; i = (i + 1) % QSIZE) {
if (i == queue->putIndex) {
break;
}
if (queue->UserArr[i].clientID == clientID) {
pthread_mutex_unlock(&queue->lockQueue);
return true;
}
}
pthread_mutex_unlock(&queue->lockQueue);
return false;
}
bool userExistsInQueue(UserQueue *queue, uint32_t userID) {
pthread_mutex_lock(&queue->lockQueue);
// Condition variable 쓸 여지가 있음
// from getIndex to putIndex because the queue is circular
for (int i = queue->getIndex;; i = (i + 1) % QSIZE) {
if (i == queue->putIndex) {
break;
}
if (queue->UserArr[i].userID == userID) {
pthread_mutex_unlock(&queue->lockQueue);
return true;
}
}
pthread_mutex_unlock(&queue->lockQueue);
return false;
}
int getIndexOfUser(UserQueue *queue, uint32_t userID) {
pthread_mutex_lock(&queue->lockQueue);
// Condition variable 쓸 여지가 있음
// from getIndex to putIndex because the queue is circular
for (int i = queue->getIndex;; i = (i + 1) % QSIZE) {
if (i == queue->putIndex) {
break;
}
if (queue->UserArr[i].userID == userID) {
pthread_mutex_unlock(&queue->lockQueue);
return i;
}
}
pthread_mutex_unlock(&queue->lockQueue);
return -1;
}
UserQueue *initUserQueue(int capacity) {
UserQueue *queue =
(UserQueue *)malloc(sizeof(UserQueue) + sizeof(User[capacity]));
queue->capacity = capacity;
queue->putIndex = queue->getIndex = queue->length = 0;
for (size_t i = 0; i < QSIZE; i++) {
queue->UserArr[i].userID = 0;
queue->UserArr[i].clientID = 0;
queue->UserArr[i].isLoggedIn = false;
queue->UserArr[i].numBookedSeats = 0;
memset(queue->UserArr[i].bookedSeats, 0x00,
sizeof(queue->UserArr[i].bookedSeats));
}
pthread_mutex_init(&queue->lockQueue, NULL);
pthread_cond_init(&queue->condNotFull, NULL);
pthread_cond_init(&queue->condNotEmpty, NULL);
return queue;
}
void deleteUserQueue(UserQueue *queue) {
pthread_mutex_destroy(&queue->lockQueue);
pthread_cond_destroy(&queue->condNotFull);
pthread_cond_destroy(&queue->condNotEmpty);
free(queue);
}