-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathchunkAlgorithm.c
More file actions
executable file
·796 lines (648 loc) · 24.2 KB
/
chunkAlgorithm.c
File metadata and controls
executable file
·796 lines (648 loc) · 24.2 KB
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#include "chunkAlgorithm.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
/*****************************************************************************
ALGORITHM FUNCTIONS
*****************************************************************************/
Bool searchInChunk(Btree* btree, ThreadGlobals* tg, Chunk* chunk, int key, Data *data) {
assert(clearFreezeState(chunk) != NULL);
assert(clearFreezeState(chunk) == chunk);
assert(key > 0);
assert(data != NULL);
assert(validateChunk(chunk));
Bool result = FALSE;
if (find(btree, tg, chunk, key)) {
Entry* CUR = entryIn(chunk, tg->cur);
assert(CUR != NULL);
*data = getData(CUR);
result = TRUE;
}
*(tg->hp0) = NULL;
*(tg->hp1) = NULL;
return result;
}
/*****************************************************************************/
Bool insertToChunk(Btree* btree, ThreadGlobals* tg, Chunk* chunk, int key, Data data) {
assert(clearFreezeState(chunk) != NULL);
assert(clearFreezeState(chunk) == chunk);
assert(key > 0);
assert(validateChunk(chunk));
Bool result = FALSE;
//Find an available entry
Entry* current = allocateEntry(chunk,key,data);
while (current == NULL) {
// No available entry.Freeze and try again
uint64_t data64bit = LOW_32_MASK & data;
chunk = freeze(btree, tg, chunk, key, 0, data64bit, INSERT, &result);
if (chunk == NULL) {
// Freeze completed the insertion.
return result;
}
//help infant chunk
if (extractFreezeState(chunk->mergeBuddy) == INFANT) {
helpInfant(btree,tg,chunk);
}
// Otherwise, retry allocation
current = allocateEntry(chunk,key,data);
}
ReturnCode returnCode = insertEntry(btree, tg, chunk, current, key);
switch (returnCode) {
case SUCCESS_THIS:
// Increments the counter of entries in the chunk
incCount(chunk);
result = TRUE;
break;
case SUCCESS_OTHER:
// Entry was inserted by other thread due to help in freeze
result = TRUE;
break;
case SUCCESS_DELAYED_INSERT:
// Entry was inserted by other thread and then deleted (delayed insert)
result = TRUE;
break;
case EXISTED:
// This key exists - attempt to clear the entry
if (clearEntry(btree, tg, chunk, current)) {
result = FALSE;
}
else {
// Failure to clear the entry implies
// a freeze thread that eventually inserts the entry
result = TRUE;
}
break;
} //end of switch
// Clear all hazard pointers and return
*(tg->hp0) = NULL;
*(tg->hp1) = NULL;
return result;
}
/*****************************************************************************/
Bool deleteInChunk(Btree* btree, ThreadGlobals* tg, Chunk* chunk,int key, Data expectedData) {
assert(clearFreezeState(chunk) != NULL);
assert(clearFreezeState(chunk) == chunk);
assert(key > 0);
assert(validateChunk(chunk));
Bool result = FALSE;
restart_deleteInChunk:
while (!decCount(chunk)) {
assert(extractFreezeState(chunk->mergeBuddy) != INFANT);
// If too few entries in chunk; call freeze
chunk = freeze(btree, tg, chunk, key, 0, expectedData, DELETE, &result);
if (chunk == NULL ) {
// If Freeze succeeded to proceed with deletion, return
return result;
}
} // end of decrement counter while
while (TRUE) {
if (!find(btree, tg, chunk, key)) {
incCount(chunk);
*(tg->hp0) = *(tg->hp1) = NULL;
return FALSE;
}
Entry* CUR = entryIn(chunk, tg->cur);
assert(CUR != NULL);
// Mark entry as deleted, assume entry is not deleted or frozen
uint64_t clearedNext = clearFrozen(clearDeleted(CUR->nextEntry));
//deal with delayed merge threads
//only applied to non leafs
if (chunk->height != 0) {
assert(CUR != NULL);
Data index = getData(CUR);
if (index != expectedData) {
return TRUE; //someone else deleted the entry
}
}
if (!CAS(&(CUR->nextEntry), clearedNext, incVersion(markDeleted(clearedNext))) ) {
if (isFrozen(CUR->nextEntry)) {
incCount(chunk);
chunk = freeze(btree, tg, chunk, key, 0, expectedData, DELETE, &result);
if (chunk == NULL) {
return result;
}
goto restart_deleteInChunk;
} else {
continue;
}
}
uint64_t new1 = setIndex ( incVersion( tg->cur ) , getIndex(tg->next) );
//if prev was frozen keep it frozen
if (isFrozen(tg->cur)) {
new1 = markFrozen(new1); //new1 will replace prev
}
if ( CAS(tg->prev, tg->cur, new1)) {
retireEntry(btree, tg, CUR);
} else {
find(btree, tg, chunk, key);
}
*(tg->hp0) = *(tg->hp1) = NULL;
return TRUE;
}
}
/*****************************************************************************/
ReturnCode insertEntry(Btree* btree, ThreadGlobals* tg, Chunk* chunk, Entry* entry, int key) {
assert(clearFreezeState(chunk) != NULL);
assert(clearFreezeState(chunk) == chunk);
assert(key > 0);
assert(entry != NULL);
assert(validateChunk(chunk));
Entry* CUR = NULL;
while (TRUE) {
uint64_t savedNext = entry->nextEntry;
//should never insert deleted entry
assert(isDeleted(savedNext) == FALSE);
// Find insert location and pointers to previous and current entries
if (find(btree, tg, chunk, key)) {
CUR = entryIn(chunk, tg->cur);
assert(CUR != NULL);
//This key existed in the list, cur is global initiated by Find
if (entry == CUR) {
return SUCCESS_OTHER;
} else {
return EXISTED;
}
}
CUR = entryIn(chunk, tg->cur);
// Attempt setting next field (forward link)
uint64_t new1 = setIndex ( incVersion( savedNext ) , getIndex(tg->cur));
if (!CAS(&(entry->nextEntry), savedNext, new1)) {
continue;
}
// don't add frozen chunk to Btree. (happens with delayed insert)
if (chunk->height != 0) { //only for non leaf
assert(entry != NULL);
Data chunkIndex = getData(entry);
assert(chunkIndex>=0 && chunkIndex<MAX_CHUNKS);
FreezeState fs = extractFreezeState(btree->memoryManager->memoryPool[chunkIndex].mergeBuddy);
if ( (fs != INFANT) && (fs != NORMAL)) {
return SUCCESS_DELAYED_INSERT; //someone else completed the insert
}
assert(fs == INFANT || fs ==NORMAL); //allow only infant node to be inserted
}
// Attempt linking into the list (backward link)
int entryPos = getPosition(entry, chunk->entriesArray);
uint64_t new2 = setIndex ( incVersion( tg->cur ) , entryPos );
if (!CAS(tg->prev, tg->cur ,new2)) {
continue;
}
return SUCCESS_THIS; // both CASes were successful
}
assert(FALSE); //unreachable
}
/*****************************************************************************/
Bool find(Btree* btree, ThreadGlobals* tg, Chunk* chunk, int key) {
assert(clearFreezeState(chunk) != NULL);
assert(clearFreezeState(chunk) == chunk);
assert(tg);
assert(key > 0);
assert(validateChunk(chunk));
Entry* CUR = NULL;
uint64_t new1 = 0;
restart_find:
tg->prev = &(chunk->entryHead.nextEntry); // Restart pointer
tg->cur = *tg->prev;
CUR = entryIn(chunk, tg->cur);
assert(CUR || chunk->root);
if ( *tg->prev != tg->cur ) {
goto restart_find;
}
while (CUR != NULL) {
// Progress to an unprotected entry
*(tg->hp0) = CUR;
if ( *tg->prev != tg->cur ) {
goto restart_find; // Validate progress after protecting
}
tg->next = CUR->nextEntry;
if (isDeleted(tg->next)) { // Current entry is marked deleted
// Disconnect current: prev gets the value of next with the delete bit cleared
new1 = clearDeleted( setIndex(incVersion(tg->cur), getIndex(tg->next)) );
if (isFrozen(tg->cur)) {
//new1 replaces prev_nextEntry; save freeze bit
new1 = markFrozen(new1);
}
if (!CAS(tg->prev, tg->cur, new1)) {
goto restart_find;
}
retireEntry(btree, tg, CUR); //CAS succeeded - try to reclaim
tg->cur = clearDeleted(tg->next);
CUR = entryIn(chunk, tg->cur);
} else {
int ckey = getKey(CUR);
if ( *tg->prev != tg->cur ) {
goto restart_find; // Validate progress after protecting
}
if (ckey >= key) {
return (ckey == key);
}
tg->prev = &(CUR->nextEntry);
// All private. hp0,hp1 are ptrs to hazard ptrs
Entry* tmp;
tmp = *(tg->hp0);
*(tg->hp0) = *(tg->hp1);
*(tg->hp1) = tmp;
tg->cur = tg->next;
CUR = entryIn(chunk, tg->cur);
}//end of else
}//end of while
return FALSE;
}
/*****************************************************************************/
Bool replaceInChunk(Btree* btree, ThreadGlobals* tg, Chunk* chunk, \
int key, uint64_t expected, uint64_t new) {
assert(clearFreezeState(chunk) != NULL);
assert(clearFreezeState(chunk) == chunk);
assert(isFrozen(expected) == FALSE);
assert(key == ((expected & LOW_32_MASK) >> 1));
Entry* CUR = NULL;
while(TRUE) {
if( !find(btree, tg, chunk, key) ) {
*(tg->hp0) = *(tg->hp1) = NULL;
return FALSE;
}
CUR = entryIn(chunk, tg->cur);
assert(CUR != NULL);
Bool casResult = FALSE;
if (!CAS(&(CUR->keyData), expected, new)) {
// assume no freeze bit set in exp - no swap on frozen entry
if ( isFrozen(CUR->keyData) ) {
// if CAS failed due to freeze help in freeze and try again
Bool result = FALSE;
chunk = freeze(btree, tg, chunk, key, expected, new, SWAP, &result);
if ( chunk == NULL ) {
*(tg->hp0) = *(tg->hp1) = NULL;
return result;
}
continue;
}
*(tg->hp0) = *(tg->hp1) = NULL;
return FALSE; // CAS failed due to not expected data
}
*(tg->hp0) = *(tg->hp1) = NULL;
return TRUE;
}
}
/*****************************************************************************/
Chunk* freeze(Btree* btree, ThreadGlobals* tg, Chunk* chunk, uint64_t key,
uint64_t expected, uint64_t data, FreezeTriggerType trigger, Bool* result) {
assert(clearFreezeState(chunk) != NULL);
assert(clearFreezeState(chunk) == chunk);
assert(result != NULL);
assert(key >= 0);
assert(validateChunk(chunk));
assert(extractFreezeState(chunk->mergeBuddy) != INFANT);
Chunk* mergePartner = NULL;
RecoveryType decision = -1;
CAS(&(chunk->mergeBuddy), C_NORMAL, C_FREEZE);
//At this point,the freeze state must NOT be NORMAL or INFANT
switch (extractFreezeState(chunk->mergeBuddy)) {
case COPY:
decision = R_COPY;
break;
case SPLIT:
decision = R_SPLIT;
break;
case MERGE: //mergePartner is already set
decision = R_MERGE;
mergePartner = clearFreezeState(chunk->mergeBuddy);
//master expected to be in MERGE, slave in SLAVE_FREEZE and pointers correctly set
assert(extractFreezeState(mergePartner->mergeBuddy) == SLAVE_FREEZE);
assert(clearFreezeState(chunk->mergeBuddy) == mergePartner);
assert(clearFreezeState(mergePartner->mergeBuddy) == chunk);
break;
case REQUEST_SLAVE: //mergePartner unknown
decision = R_MERGE;
mergePartner = findMergeSlave(btree, tg, chunk);
if (mergePartner != NULL) {
//pointers correctly set
assert(clearFreezeState(chunk->mergeBuddy) == mergePartner);
assert(clearFreezeState(mergePartner->mergeBuddy) == chunk);
break;
}
//if partner is NULL chuck was turned to SLAVE_FREEZE, continue
assert(extractFreezeState(chunk->mergeBuddy) == SLAVE_FREEZE);
//continue to case SLAVE_FREEZE, no break
case SLAVE_FREEZE: //mergePartner is already set
decision = R_MERGE;
mergePartner = clearFreezeState(chunk->mergeBuddy);
assert(mergePartner != NULL);
markChunkFrozen(chunk);
stabilizeChunk(btree,tg,chunk);
//slave is set, change master to MERGE and recover
CAS(&mergePartner->mergeBuddy, combineFreezeState(chunk, REQUEST_SLAVE),\
combineFreezeState(chunk, MERGE));
//master expected to be in MERGE, slave in SLAVE_FREEZE and pointers correctly set
if (extractFreezeState(mergePartner->mergeBuddy) != MERGE) {
printf("### chunk=%p mergePartner=%p mergePartner->mergeBuddy=%p\n",chunk,mergePartner,mergePartner->mergeBuddy);
}
assert(extractFreezeState(mergePartner->mergeBuddy) == MERGE);
assert(clearFreezeState(chunk->mergeBuddy) == mergePartner);
assert(clearFreezeState(mergePartner->mergeBuddy) == chunk);
break;
case FREEZE: //decision unknown
markChunkFrozen(chunk);
stabilizeChunk(btree, tg, chunk);
decision = freezeDecision(chunk);
switch (decision) {
case R_COPY:
assert(clearFreezeState(chunk->mergeBuddy) == NULL); //no mergeBuddy for COPY
CAS(&chunk->mergeBuddy, C_FREEZE, C_COPY);
break;
case R_SPLIT:
assert(clearFreezeState(chunk->mergeBuddy) == NULL); //no mergeBuddy for SPLIT
CAS(&chunk->mergeBuddy, C_FREEZE, C_SPLIT);
break;
case R_MERGE:
mergePartner = findMergeSlave(btree, tg, chunk);
if (mergePartner == NULL) {
//this chunk is slave, merge partner is master
assert(extractFreezeState(chunk->mergeBuddy) == SLAVE_FREEZE);
mergePartner = clearFreezeState(chunk->mergeBuddy);
assert(mergePartner != NULL);
CAS(&mergePartner->mergeBuddy, combineFreezeState(chunk, REQUEST_SLAVE),\
combineFreezeState(chunk, MERGE));
assert(extractFreezeState(mergePartner->mergeBuddy) == MERGE); // master expected to be in MERGE
}
//pointers correctly set
assert(clearFreezeState(chunk->mergeBuddy) == mergePartner);
assert(clearFreezeState(mergePartner->mergeBuddy) == chunk);
break;
} // end of switch on decision
break; //end of case FREEZE
} //end of switch on freeze state
return freezeRecovery(btree, tg, chunk, key, expected, data, decision,\
mergePartner, trigger, result);
}
/*****************************************************************************/
static inline int shouldMove(Chunk* leftleft, Chunk* left, Chunk* right) {
Chunk* leftleft_partner = clearFreezeState(leftleft->mergeBuddy);
Chunk* left_partner = clearFreezeState(left->mergeBuddy);
Chunk* right_partner = clearFreezeState(right->mergeBuddy);
if (right_partner == left) {
if (left_partner == right) {
return 1;
}
if (left_partner == NULL) {
if (leftleft_partner == left) {
return 2;
}
return 1;
}
}
if (left_partner == right) {
return 1;
}
return 0;
}
/*****************************************************************************/
Chunk* freezeRecovery(Btree* btree, ThreadGlobals* tg, Chunk* oldChunk,
uint64_t key, uint64_t expected, uint64_t input, RecoveryType recovType,
Chunk* mergeChunk, FreezeTriggerType trigger, Bool* result) {
assert(clearFreezeState(oldChunk) != NULL);
assert(clearFreezeState(oldChunk) == oldChunk);
assert(clearFreezeState(mergeChunk) == mergeChunk);
assert(result != NULL);
assert(key >= 0);
int sepKey = MAX_KEY;
Chunk* newChunk2 = NULL;
Chunk* newChunk1 = &btree->memoryManager->memoryPool[allocateChunk(btree->memoryManager)]; //Allocate a new chunk
Chunk* lowChunk = NULL;
Chunk* highChunk = NULL;
Chunk* master = NULL;
assert(validateChunk(oldChunk));
assert(validateChunk(mergeChunk));
uint64_t oldChunkKey = 0;
uint64_t partnerKey = 0;
switch (recovType) {
case R_COPY:
assert(extractFreezeState(oldChunk->mergeBuddy) == COPY);
copyToOneChunk(oldChunk, newChunk1);
break;
case R_MERGE:
assert(mergeChunk != NULL);
//correctly pointing merge partners
assert(clearFreezeState(oldChunk->mergeBuddy) == mergeChunk);
assert(clearFreezeState(mergeChunk->mergeBuddy) == oldChunk);
//one chunk is in MERGE and its partner in SLAVE_FREEZE
assert((extractFreezeState(oldChunk->mergeBuddy) == MERGE && extractFreezeState(mergeChunk->mergeBuddy) == SLAVE_FREEZE) ||
(extractFreezeState(oldChunk->mergeBuddy) == SLAVE_FREEZE && extractFreezeState(mergeChunk->mergeBuddy) == MERGE));
oldChunkKey = getKey(getNextEntry(&oldChunk->entryHead,oldChunk->entriesArray));
partnerKey = getKey(getNextEntry(&mergeChunk->entryHead,mergeChunk->entriesArray));
if (oldChunkKey < partnerKey) {
lowChunk = oldChunk;
highChunk = mergeChunk;
} else {
lowChunk = mergeChunk;
highChunk = oldChunk;
}
if ((getEntrNum(oldChunk)+getEntrNum(mergeChunk)) >= MAX_ENTRIES ) {
newChunk2 = &btree->memoryManager->memoryPool[allocateChunk(btree->memoryManager)];
assert(newChunk1 != newChunk2);
sepKey = mergeToTwoChunks(lowChunk, highChunk, newChunk1, newChunk2);
} else {
mergeToOneChunk(lowChunk, highChunk, newChunk1);
}
break;
case R_SPLIT:
assert(extractFreezeState(oldChunk->mergeBuddy) == SPLIT);
newChunk2 = &btree->memoryManager->memoryPool[allocateChunk(btree->memoryManager)];
assert(newChunk1 != newChunk2);
sepKey = splitIntoTwoChunks(oldChunk, newChunk1, newChunk2);
break;
default:
assert(FALSE); //recovType must be known at this point
break;
}
if (newChunk2 != NULL && newChunk2->height != 0) {
//if there are two new non leaf chunks -
//do not seperate between master and slave entries
int last = newChunk1->counter -1;
assert(last >= 1);
Chunk* sepChunk0 = getSon(btree->memoryManager, &newChunk1->entriesArray[last-1]);
Chunk* sepChunk1 = getSon(btree->memoryManager, &newChunk1->entriesArray[last]);
Chunk* sepChunk2 = getSon(btree->memoryManager, &newChunk2->entriesArray[0]);
int numToMove = shouldMove(sepChunk0, sepChunk1, sepChunk2);
for (int i=0; i<numToMove; ++i) {
moveEntryFromFirstToSecond(newChunk1,newChunk2);
}
if (numToMove != 0) {
sepKey = findMaxKeyInChunk(newChunk1);
}
}
assert(validateChunk(newChunk1));
assert(validateChunk(newChunk2));
*result = FALSE;
switch (trigger) {
case DELETE:
*result = deleteInChunk(btree, tg, newChunk1,key, input);
if (newChunk2 != NULL ) {
*result = *result || deleteInChunk(btree, tg, newChunk2, key, input);
}
break;
case INSERT:
if ( ( newChunk2 != NULL) && (key > sepKey) ) {
*result = insertToChunk(btree, tg, newChunk2, key, input);
} else {
*result = insertToChunk(btree, tg, newChunk1, key, input);
}
break;
case SWAP:
if (key <= sepKey) {
*result = replaceInChunk(btree, tg, newChunk1, key, expected, input);
} else {
*result = replaceInChunk(btree, tg, newChunk2, key, expected, input);
}
break;
case ENSLAVE:
// input should be interpreted as pointer to master trying to enslave, only in case of COPY
if (recovType == R_COPY) {
master = (Chunk*)input;
newChunk1->mergeBuddy = combineFreezeState(master,SLAVE_FREEZE);
markChunkFrozen(newChunk1);
}
break;
case NONE:
break;
}
//updateTarget set as following:
//for SPLIT & COPY - oldChunk
//for MERGE - master chunk
Chunk* updateTarget = oldChunk;
if (recovType == R_MERGE) {
assert(mergeChunk);
if (extractFreezeState(oldChunk->mergeBuddy) == SLAVE_FREEZE) {
assert(extractFreezeState(mergeChunk->mergeBuddy) == MERGE);
updateTarget = mergeChunk;
}
}
Chunk* retChunk = NULL;
if ( !CAS(&(updateTarget->newChunk), NULL, newChunk1 )) {
//determine in which of the new chunks the destination key is now located
if (key <= sepKey) {
retChunk = updateTarget->newChunk;
} else {
retChunk = updateTarget->newChunk->nextChunk;
}
}
//if the new chunk is in SLAVE_FREEZE, update the master to point back to him
if (extractFreezeState(updateTarget->newChunk->mergeBuddy) == SLAVE_FREEZE) {
master = clearFreezeState(updateTarget->newChunk->mergeBuddy);
CAS(&master->mergeBuddy, combineFreezeState(updateTarget,REQUEST_SLAVE), \
combineFreezeState(updateTarget->newChunk,REQUEST_SLAVE));
}
callForUpdate(btree, tg, recovType, updateTarget, sepKey);
return retChunk; // NULL if succeeded
}
/*****************************************************************************/
void stabilizeChunk(Btree* btree, ThreadGlobals* tg, Chunk* chunk) {
assert(clearFreezeState(chunk) != NULL);
assert(clearFreezeState(chunk) == chunk);
assert(validateChunk(chunk));
int maxKey = MAX_KEY;
uint64_t eNext = 0;
int key = 0;
//Implicitly remove deleted entries
find(btree, tg, chunk, maxKey);
for(int i=0; i<MAX_ENTRIES; ++i) {
Entry* currentE = &(chunk->entriesArray[i]);
key = getKey(currentE);
eNext = currentE->nextEntry;
if ((key != NOT_DEFINED_KEY) && (!isDeleted(eNext))) {
//This entry is allocated and not deleted
if (!find(btree, tg, chunk, key)) {
//This key is not yet in the tree
insertEntry(btree, tg, chunk, currentE, key);
}
}
} //end of foreach
assert(noDeletedEntries(chunk));
}
/*****************************************************************************/
void retireEntry(Btree* btree, ThreadGlobals* tg, Entry* entry) {
assert(entry != NULL);
//Add the entry to the(local)list of to-be-retired entries
addToRetList(tg, entry);
//Scan the list and reclaim the entries if possible
handleReclamationBuffer(btree, tg);
}
/*****************************************************************************/
void handleReclamationBuffer(Btree* btree, ThreadGlobals* tg) {
// Local list for recording current hazard pointers
EntryL plist;
INIT_LIST_HEAD(&plist.list1);
//Obtain head of hazard pointers array(HPA)
HPRecord* hprec = btree->memoryManager->HPArray;
// Stage1 : Save current hazard pointers in plist(locally)
for(int i=0; i<MAX_THREADS_NUM; ++i) {
Entry* hptr0 = hprec[i].hp0;
Entry* hptr1 = hprec[i].hp1;
if (hptr0 != NULL ) {
EntryL* newEntry = (EntryL*) malloc(sizeof(EntryL));
newEntry->entry = hptr0;
list_add_tail(&newEntry->list1, &plist.list1);
}
if (hptr1 != NULL ) {
EntryL* newEntry = (EntryL*) malloc(sizeof(EntryL));
newEntry->entry = hptr1;
list_add_tail(&newEntry->list1, &plist.list1);
}
}
//Stage2: Reclaim to-be-retired entries that are not protected by a HP
EntryL tmplist;
EntryL* entry = NULL;
list_t* tmpEntry = NULL;
//Copy all local to-be-retired entires and clear RetList
popAllRetList(tg, &tmplist);
tmpEntry = list_pop(&tmplist.list1);
if (!tmpEntry ) {
entry = list_entry(tmpEntry, EntryL, list1);
}
while ( (entry != NULL) && (tmpEntry != NULL) ) {
if ( lookUpEntryLsList(&plist,entry) ) {
addEntryLToRetList(tg, entry); //entry is protected
}
else if (!isFrozen(entry->entry->nextEntry)) {
clearEntry(btree, tg , NULL, entry->entry); //attempt to reclaim
}
//advance to next entry
tmpEntry = list_pop(&tmplist.list1);
if (!tmpEntry ) {
entry = list_entry(tmpEntry, EntryL, list1);
}
}
freeEntryLsList(&plist); // normal free. just deallocate all the entries.
}
/*****************************************************************************/
Bool clearEntry(Btree* btree, ThreadGlobals* tg, Chunk* chunk, Entry* entry) {
assert(entry != NULL);
uint64_t savedKeyData = clearFrozen(entry->keyData);
uint64_t savedNext = clearFrozen(entry->nextEntry);
Bool result = FALSE;
Entry* CUR = NULL;
if ( CAS(&(entry->nextEntry), savedNext, setIndex(0, END_INDEX)) ) {
if (CAS(&(entry->keyData), savedKeyData, AVAILABLE_ENTRY)) {
return TRUE; // Both CASes were successful
}
}
assert(clearFreezeState(chunk) != NULL);
assert(clearFreezeState(chunk) == chunk);
assert(validateChunk(chunk));
//A CAS failure indicates a freeze. Help freeze before proceeding.
int key = getKey(getNextEntry(&chunk->entryHead, chunk->entriesArray));
freeze(btree, tg, chunk, key, 0, 0, NONE, &result);
if (find(btree, tg, chunk, getKey(entry))) {
CUR = entryIn(chunk, tg->cur);
//Check whether the entry to be reclaimed was linked back by the freeze
if ( entry == CUR ) {
return FALSE;// cur is global initiated by Find
}
}
return TRUE;
}
/*****************************************************************************
disabled Functions.
*****************************************************************************/
/*****************************************************************************/
void retireChunk(Btree* btree,ThreadGlobals* tg, Chunk* chunk) {}
/*****************************************************************************/
void handleChunkReclamationBuffer(Btree* btree, ThreadGlobals* tg) {}