-
Notifications
You must be signed in to change notification settings - Fork 2
/
Fine_Grained_BST_Lock.cpp
456 lines (409 loc) · 11.7 KB
/
Fine_Grained_BST_Lock.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include "Fine_Grained_BST.h"
#include "cycle_timer.h"
extern pthread_mutex_t tree_lock;
extern FG_BST_Node *g_root;
void search(int val, FG_BST_Node *root, FG_BST_Node *parent)
{
if(parent == NULL) { //I am at the root
pthread_mutex_lock(&tree_lock);
if(g_root == NULL) {
printf("Search failed for node with value %d\n", val);
pthread_mutex_unlock(&tree_lock);
return;
}
pthread_mutex_lock(&g_root->lock);
root = g_root;
pthread_mutex_unlock(&tree_lock);
}
if(val < root->value) {
if (root->left == NULL) {
printf("Search failed for node with value %d\n", val);
pthread_mutex_unlock(&root->lock);
return;
} else {
pthread_mutex_lock(&root->left->lock);
pthread_mutex_unlock(&root->lock);
search(val, root->left, root);
}
}
else if (val > root->value) {
if (root->right == NULL) {
printf("Search failed for node with value %d\n", val);
pthread_mutex_unlock(&root->lock);
return;
} else {
pthread_mutex_lock(&root->right->lock);
pthread_mutex_unlock(&root->lock);
search(val, root->right, root);
}
} else {
pthread_mutex_unlock(&root->lock);
}
}
/**
* insert:
* This entered with lock on root held except for the very first call.
*/
void insert(int val, FG_BST_Node* root, FG_BST_Node *parent, int thread_num) {
if(parent == NULL) { //I am at the root
pthread_mutex_lock(&tree_lock);
if(g_root == NULL) {
g_root = createNode(val, parent);
pthread_mutex_unlock(&tree_lock);
return;
}
pthread_mutex_lock(&g_root->lock);
root = g_root;
pthread_mutex_unlock(&tree_lock);
}
if(val < root->value) {
if (root->left == NULL) {
root->left = createNode(val, root);
pthread_mutex_unlock(&root->lock);
} else {
pthread_mutex_lock(&root->left->lock);
pthread_mutex_unlock(&root->lock);
insert(val, root->left, root, thread_num);
}
}
else if (val > root->value) {
if (root->right == NULL) {
root->right = createNode(val, root);
pthread_mutex_unlock(&root->lock);
} else {
pthread_mutex_lock(&root->right->lock);
pthread_mutex_unlock(&root->lock);
insert(val, root->right, root, thread_num);
}
} else {
printf("Duplicates not allowed");
assert(0);
}
}
FG_BST_Node* createNode(int val, FG_BST_Node *parent) {
printf("creating a new node\n");
FG_BST_Node* node = (FG_BST_Node *) malloc(sizeof(FG_BST_Node));
if(node == NULL) {
fprintf(stderr, "Failed to allocate memory for new node");
return node;
}
node->value = val;
node->left = NULL;
node->right = NULL;
node->parent = parent;
pthread_mutex_init(&node->lock, NULL);
return node;
}
/**
* del_search:
* search for the first node that matches the value val.
* It is assumed that this function will be called with a lock on root held
* If del_search() finds the required node it grabs the lock on the node and
* returns.
* If it doesn't find the node it releases all the locks and returns
*/
FG_BST_Node* del_search(int val, FG_BST_Node* root, int thread_num)
{
if(val == root->value) {
/*
* We will come in here if we want to delete the root node.
* No need to lock the root because it is already locked by
* the caller.
*/
return root;
} else if (val < root->value) {
if (root->left == NULL) {
pthread_mutex_unlock(&root->lock);
return NULL;
} else {
pthread_mutex_lock(&root->left->lock);
if (val == root->left->value) {
return root->left;
} else {
pthread_mutex_unlock(&root->lock);
return del_search(val, root->left, thread_num);
}
}
} else {
if (root->right == NULL) {
/*
* Could not find the node, unlock current root and return
*/
pthread_mutex_unlock(&root->lock);
return NULL;
} else {
pthread_mutex_lock(&root->right->lock);
if (val == root->right->value) {
return root->right;
} else {
pthread_mutex_unlock(&root->lock);
return del_search(val, root->right, thread_num);
}
}
}
return NULL;
}
int remove(int val, FG_BST_Node *root, int thread_num)
{
FG_BST_Node *to_be_deleted, *parent, *successor_parent, *successor;
FG_BST_Node *predecessor, *predecessor_parent;
pthread_mutex_lock(&tree_lock);
if (g_root == NULL) {
pthread_mutex_unlock(&tree_lock);
return -EINVAL;
}
root = g_root;
pthread_mutex_lock(&root->lock);
/*
* Check if we are deleting the root and that the root is the only node
* in the tree
*/
if (val == root->value && root->left == NULL && root->right == NULL) {
/*
* free the root node,
* set the tree's global root as NULL,
* unlock the treelock and return
*/
free(root);
g_root = NULL;
pthread_mutex_unlock(&tree_lock);
return 0;
}
to_be_deleted = del_search(val, root, thread_num);
if (to_be_deleted == NULL) {
/*
* if del_search() returns NULL we can be sure that it is not
* holding any locks. So we can just return from here.
*/
printf("Could not find the item (%d) to be deleted\n", val);
pthread_mutex_unlock(&tree_lock);
return 0;
}
/*
* If we found the node to be deleted we can be sure that we have a lock
* on the node to be deleted and its parent (if parent exists)
*/
// store the parent
parent = to_be_deleted->parent;
/*
* Because we use the data swapping mechanism we need the lock on parent only
* if the node to be deleted is a leaf node. Because that is the only case
* where we need to update to_be_deleted's parent's pointer to NULL
*/
if (to_be_deleted->left == NULL && to_be_deleted->right == NULL &&
parent == NULL) {
free(to_be_deleted);
g_root = NULL;
pthread_mutex_unlock(&tree_lock);
return 0;
}
pthread_mutex_unlock(&tree_lock);
// Leaf node to be deleted
if (to_be_deleted->left == NULL && to_be_deleted->right == NULL) {
if (to_be_deleted->value < parent->value) {
// node to be deleted is the left child of its parent
/*
* Unlock to_be_deleted. We can safely unlock here because we hold
* a lock on the parent and nobody else can come and modify to_be_deleted
*/
// free the node
free(to_be_deleted);
// set parent's left child as NULL
parent->left = NULL;
// unlock the parent and return
pthread_mutex_unlock(&parent->lock);
return 0;
} else {
// node to be deleted is the right child of its parent
// Follow the same procedure as above
free(to_be_deleted);
parent->right = NULL;
pthread_mutex_unlock(&parent->lock);
return 0;
}
}
/*
* At this point, we are either deleting the root or an internal node.
* If deleting an internal node, then unlock the parent.
*/
if (parent != NULL) {
pthread_mutex_unlock(&parent->lock);
}
/*
* Find inorder successor or inorder predecessor (whichever exists) for the
* node to be deleted
*/
if (to_be_deleted->right != NULL) {
// perform pre-emptive check
pthread_mutex_lock(&to_be_deleted->right->lock);
if (to_be_deleted->right->left == NULL) {
/*
* The right node is the successor,
* copy the value,
* lock successor's right --- we had not discussed this. But this is needed
* update pointers,
* unlock successor's right,
* unlock successor,
* free successor,
* unlock to_be_deleted and return.
*/
successor = to_be_deleted->right;
if (successor->right != NULL) {
pthread_mutex_lock(&successor->right->lock);
to_be_deleted->value = successor->value;
to_be_deleted->right = successor->right;
successor->right->parent = to_be_deleted;
pthread_mutex_unlock(&successor->right->lock);
} else {
to_be_deleted->value = successor->value;
to_be_deleted->right = NULL;
}
//pthread_mutex_unlock(&successor->lock);
free(successor);
pthread_mutex_unlock(&to_be_deleted->lock);
return 0;
}
/*
* Else,
* Find the successor,
* copy the value,
* update successor's parent AND successor->right,
* unlock successor,
* free successor,
* unlock successor's parent,
* unlock to_be_deleted and return
*/
successor = get_inorder_successor(to_be_deleted);
successor_parent = successor->parent;
if (successor->right != NULL) {
pthread_mutex_lock(&successor->right->lock);
// the successor will always be the left child of it's parent
successor_parent->left = successor->right;
successor->right->parent = successor_parent;
to_be_deleted->value = successor->value;
pthread_mutex_unlock(&successor->right->lock);
} else {
to_be_deleted->value = successor->value;
successor_parent->left = NULL;
}
free(successor);
pthread_mutex_unlock(&successor_parent->lock);
pthread_mutex_unlock(&to_be_deleted->lock);
return 0;
}
/*
* The node to be deleted doesn't have a successor. We need to find
* its predecessor.
* Very similar logic to finding successor.
*/
if (to_be_deleted->left != NULL) {
pthread_mutex_lock(&to_be_deleted->left->lock);
// perform pre-emptive check
if (to_be_deleted->left->right == NULL) {
/*
* The left node is the predecessor.
* copy the value,
* lock predecessor's left ---- We had discussed this but it is needed
* update pointers,
* unlock predecessor,
* free predecessor,
* unlock to_be_deleted and return.
*/
predecessor = to_be_deleted->left;
if (predecessor->left != NULL) {
pthread_mutex_lock(&predecessor->left->lock);
to_be_deleted->value = predecessor->value;
to_be_deleted->left = predecessor->left;
predecessor->left->parent = to_be_deleted;
pthread_mutex_unlock(&predecessor->left->lock);
} else {
to_be_deleted->value = predecessor->value;
to_be_deleted->left = NULL;
}
free(predecessor);
pthread_mutex_unlock(&to_be_deleted->lock);
return 0;
}
/*
* Else,
* Find the predecessor,
* Copy the value,
* Update predecessor's parent AND predecessor->left,
* unlock predecessor,
* free predecessor,
* unlock predecessor's parent,
* unlock to_be_deleted and return.
*/
predecessor = get_inorder_predecessor(to_be_deleted);
predecessor_parent = predecessor->parent;
if (predecessor->left != NULL) {
pthread_mutex_lock(&predecessor->left->lock);
// predecessor will always be the right child of its parent
predecessor_parent->right = predecessor->left;
predecessor->left->parent = predecessor_parent;
to_be_deleted->value = predecessor->value;
pthread_mutex_unlock(&predecessor->left->lock);
} else {
to_be_deleted->value = predecessor->value;
predecessor_parent->right = NULL;
}
free(predecessor);
pthread_mutex_unlock(&predecessor_parent->lock);
pthread_mutex_unlock(&to_be_deleted->lock);
return 0;
}
return -1;
}
/**
* get_inorder_successor:
* This function is expected to return with a lock on both
* 1) the successor and
* 2) the successor's parent
*/
FG_BST_Node *get_inorder_successor(FG_BST_Node *node)
{
FG_BST_Node *parent, *successor;
parent = node->right;
successor = parent->left;
// lock the successor
pthread_mutex_lock(&successor->lock);
while (successor->left != NULL) {
successor = successor->left;
// unlock the old parent
pthread_mutex_unlock(&parent->lock);
// lock the new successor
pthread_mutex_lock(&successor->lock);
// update the parent
parent = successor->parent;
}
return successor;
}
/**
* get_inorder_predecessor:
* This function is expected to return with a lock on both
* 1) the predecessor and
* 2) the predecessor's parent
*/
FG_BST_Node *get_inorder_predecessor(FG_BST_Node *node)
{
FG_BST_Node *parent, *predecessor;
parent = node->left;
predecessor = parent->right;
// lock the predecessor
pthread_mutex_lock(&predecessor->lock);
while (predecessor->right != NULL) {
predecessor = predecessor->right;
// unlock the old parent
pthread_mutex_unlock(&parent->lock);
// lock the new predecessor
pthread_mutex_lock(&predecessor->lock);
// update the parent
parent = predecessor->parent;
}
return predecessor;
}