-
Notifications
You must be signed in to change notification settings - Fork 0
/
occ.c
190 lines (167 loc) · 9.55 KB
/
occ.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <semaphore.h>
#include "occ.h"
ValidationList validationList_default = {
.writeMutex = PTHREAD_MUTEX_INITIALIZER,
.readMutex = PTHREAD_MUTEX_INITIALIZER,
.turnMutex = PTHREAD_MUTEX_INITIALIZER,
.numberReaders = 0
};
void read_lock(ValidationList *validationList) {
// turn mutex
pthread_mutex_lock(&(validationList->turnMutex));
pthread_mutex_unlock(&(validationList->turnMutex));
// stop writes from entering
pthread_mutex_lock(&(validationList->readMutex));
validationList->numberReaders += 1;
if (validationList->numberReaders == 1) pthread_mutex_lock(&(validationList->writeMutex));
pthread_mutex_unlock(&(validationList->readMutex));
}
void read_unlock(ValidationList *validationList) {
// allow writes if last leaving
pthread_mutex_lock(&(validationList->readMutex));
validationList->numberReaders -= 1;
if (validationList->numberReaders == 0) pthread_mutex_unlock(&(validationList->writeMutex));
pthread_mutex_unlock(&(validationList->readMutex));
}
void write_lock(ValidationList *validationList) {
pthread_mutex_lock(&(validationList->turnMutex));
pthread_mutex_lock(&(validationList->writeMutex));
}
void write_unlock(ValidationList *validationList) {
pthread_mutex_unlock(&(validationList->turnMutex));
pthread_mutex_unlock(&(validationList->writeMutex));
}
void get_shadow_copy(BankAccount *bankAccount, BankAccountData *bankAccountData) {
pthread_mutex_lock(&(bankAccount->mutex));
*bankAccountData = (BankAccountData)
{
.bankAccount = bankAccount,
.copyTimestamp = bankAccount->writeBackTimestamp,
.id = bankAccount->id,
.balance = bankAccount->balance
};
pthread_mutex_unlock(&(bankAccount->mutex));
}
// Always use 1 or greater as the additiveList offset to distinguish between 1st entry verified (1 or greater) or not (0 or greater)
uint64_t get_start_time(ValidationList *validationList) {
read_lock(validationList);
uint64_t startTime;
for (startTime = validationList->additiveList.offset + validationList->additiveList.listLength - 1;
startTime >= validationList->additiveList.offset; startTime--) {
if (((ValidationEntry *) additive_list_get(&validationList->additiveList, startTime))->hasWrittenBack) {
break;
}
}
read_unlock(validationList);
return startTime;
}
bool read_validate(BankAccountData *bankAccountData1, BankAccountData *bankAccountData2, uint64_t startTime) {
if (bankAccountData1 != NULL && bankAccountData1->copyTimestamp > startTime) return false;
if (bankAccountData2 != NULL && bankAccountData2->copyTimestamp > startTime) return false;
return true;
}
bool serialisability_validate(BankAccountData *bankAccountData1, BankAccountData *bankAccountData2, uint64_t startTime,
ValidationList *validationList) {
ValidationEntry *validationEntry = NULL;
for (uint64_t offsetIndex = validationList->additiveList.offset + validationList->additiveList.listLength - 1;
offsetIndex >= validationList->additiveList.offset; offsetIndex--) {
validationEntry = additive_list_get(&validationList->additiveList, offsetIndex);
if (startTime == offsetIndex) break;
if (bankAccountData1 != NULL && bankAccountData1->bankAccount == validationEntry->bankAccount1) return false;
if (bankAccountData2 != NULL && bankAccountData2->bankAccount == validationEntry->bankAccount2) return false;
}
return true;
}
bool submit_validation_request(ValidationRequest *validationRequest, CircularQueue *validationCircularQueue) {
bool result;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t conditionVariable = PTHREAD_COND_INITIALIZER;
ValidationWriteBackResult validationResult = {.validationRequest = validationRequest, .result = &result,
.resultMutex = &mutex, .resultConditionVariable = &conditionVariable};
pthread_mutex_lock(&mutex);
circular_queue_push(validationCircularQueue, &validationResult);
pthread_cond_wait(&conditionVariable, &mutex);
pthread_mutex_unlock(&mutex);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&conditionVariable);
return result;
}
void validate_transaction(ValidationList *validationList, CircularQueue *validationCircularQueue,
CircularQueue *writeBackCircularQueue) {
#pragma clang diagnostic push
#pragma ide diagnostic ignored "EndlessLoop"
while (true) {
ValidationWriteBackResult validationWriteBackResult;
circular_queue_pop(validationCircularQueue, &validationWriteBackResult);
read_lock(validationList);
bool isValidated = read_validate(validationWriteBackResult.validationRequest->bankAccountData1,
validationWriteBackResult.validationRequest->bankAccountData2,
validationWriteBackResult.validationRequest->startTime) &&
serialisability_validate(validationWriteBackResult.validationRequest->bankAccountData1,
validationWriteBackResult.validationRequest->bankAccountData2,
validationWriteBackResult.validationRequest->startTime,
validationList);
read_unlock(validationList);
if (isValidated) {
ValidationEntry validationEntry = {
.bankAccount1 = (validationWriteBackResult.validationRequest->bankAccountData1 != NULL &&
validationWriteBackResult.validationRequest->isBankAccount1Updated)
? validationWriteBackResult.validationRequest->bankAccountData1->bankAccount
: NULL,
.bankAccount2 = (validationWriteBackResult.validationRequest->bankAccountData2 != NULL &&
validationWriteBackResult.validationRequest->isBankAccount2Updated)
? validationWriteBackResult.validationRequest->bankAccountData2->bankAccount
: NULL,
.hasWrittenBack = false};
write_lock(validationList);
validationWriteBackResult.validationTimestamp = validationList->additiveList.offset +
validationList->additiveList.listLength;
additive_list_add(&validationList->additiveList, &validationEntry);
write_unlock(validationList);
circular_queue_push(writeBackCircularQueue, &validationWriteBackResult);
} else {
pthread_mutex_lock(validationWriteBackResult.resultMutex);
*(validationWriteBackResult.result) = false;
pthread_mutex_unlock(validationWriteBackResult.resultMutex);
pthread_cond_signal(validationWriteBackResult.resultConditionVariable);
}
}
#pragma clang diagnostic pop
}
void write_back_transaction(ValidationList *validationList, CircularQueue *writeBackCircularQueue) {
#pragma clang diagnostic push
#pragma ide diagnostic ignored "EndlessLoop"
while (true) {
ValidationWriteBackResult validationWriteBackResult;
circular_queue_pop(writeBackCircularQueue, &validationWriteBackResult);
if (validationWriteBackResult.validationRequest->bankAccountData1 != NULL &&
validationWriteBackResult.validationRequest->isBankAccount1Updated) {
pthread_mutex_lock(&(validationWriteBackResult.validationRequest->bankAccountData1->bankAccount->mutex));
// TODO: Add more changes than balance
validationWriteBackResult.validationRequest->bankAccountData1->bankAccount->balance =
validationWriteBackResult.validationRequest->bankAccountData1->balance;
validationWriteBackResult.validationRequest->bankAccountData1->bankAccount->writeBackTimestamp =
validationWriteBackResult.validationTimestamp;
pthread_mutex_unlock(&(validationWriteBackResult.validationRequest->bankAccountData1->bankAccount->mutex));
}
if (validationWriteBackResult.validationRequest->bankAccountData2 != NULL &&
validationWriteBackResult.validationRequest->isBankAccount2Updated) {
pthread_mutex_lock(&(validationWriteBackResult.validationRequest->bankAccountData2->bankAccount->mutex));
// TODO: Add more changes than balance
validationWriteBackResult.validationRequest->bankAccountData2->bankAccount->balance =
validationWriteBackResult.validationRequest->bankAccountData2->balance;
validationWriteBackResult.validationRequest->bankAccountData2->bankAccount->writeBackTimestamp =
validationWriteBackResult.validationTimestamp;
pthread_mutex_unlock(&(validationWriteBackResult.validationRequest->bankAccountData2->bankAccount->mutex));
}
write_lock(validationList);
((ValidationEntry *) additive_list_get(&validationList->additiveList,
validationWriteBackResult.validationTimestamp))->hasWrittenBack = true;
write_unlock(validationList);
pthread_mutex_lock(validationWriteBackResult.resultMutex);
*(validationWriteBackResult.result) = true;
pthread_mutex_unlock(validationWriteBackResult.resultMutex);
pthread_cond_signal(validationWriteBackResult.resultConditionVariable);
}
#pragma clang diagnostic pop
}