-
Notifications
You must be signed in to change notification settings - Fork 0
/
LockFreeLinkedListWithRefCount2.java
658 lines (603 loc) · 25.9 KB
/
LockFreeLinkedListWithRefCount2.java
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
package org.concurrent;
import java.lang.reflect.Field;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import sun.misc.Unsafe;
public class LockFreeLinkedListWithRefCount {
static int idxNum = 16;
static int PSLY_Record_IDXNUM = idxNum;
static int PSLY_Record_IDXBIT = ((1 << idxNum) - 1);
static int arrNum = 4;
static int PSLY_Record_ARRNUM = arrNum;
static int PSLY_Record_ARRAYNUM = (1 << arrNum);
static int PSLY_Record_ARRAYBITS = ((1 << arrNum) -1);
static int PSLY_Record_ARRBIT = (((1 << arrNum) - 1) << idxNum);
static int PSLY_Record_ARRBITR = ((1 << arrNum) - 1);
static int PSLY_Record_ARRIDXBIT = ((((1 << arrNum) - 1) << idxNum) | ((1 << idxNum) - 1));
static int PSLY_Record_NEXTIDXNUM = idxNum;
static int PSLY_Record_NEXTIDXBIT = ((1 << idxNum) - 1);
static int PSLY_Record_NEXTTAILNUM = 1;
static int PSLY_Record_NEXTTAILBIT = (((1 << 1) - 1) << idxNum);
static int PSLY_Record_NEXTVERSIONNUM = (32 - 1 - idxNum);
static int PSLY_Record_NEXTVERSIONBIT = ((~0)^((((1 << 1) - 1) << idxNum) | ((1 << idxNum) - 1)));
static int PSLY_Record_NEXTVERSIONONE = (1 + ((((1 << 1) - 1) << idxNum) | ((1 << idxNum) - 1)));
static int PSLY_Record_TAILIDXNUM = idxNum;
static int PSLY_Record_TAILIDXBIT = ((1 << idxNum) - 1);
static int PSLY_Record_TAILVERSIONNUM = (32 - idxNum);
static int PSLY_Record_TAILVERSIONBIT = ((~0) ^ ((1 << idxNum) - 1));
static int PSLY_Record_TAILVERSIONONE = (1 + ((1 << idxNum) - 1));
static int PSLY_Record_HEADIDXNUM = idxNum;
static int PSLY_Record_HEADIDXBIT = ((1 << idxNum) - 1);
static int PSLY_Record_HEADVERSIONNUM = (32 - idxNum);
static int PSLY_Record_HEADVERSIONBIT = ((~0) ^ ((1 << idxNum) - 1));
static int PSLY_Record_HEADVERSIONONE = (1 + ((1 << idxNum) - 1));
static int IDXNUM_ = idxNum;
static long IDXBIT_= ((1 << idxNum) - 1);
static int ARRNUM_ = arrNum;
static long ARRBIT_ = (((1 << arrNum) - 1) << idxNum);
static long ARRIDXBIT_= (((1 << idxNum) - 1) | (((1 << arrNum) - 1) << idxNum));
static int refNum = 16;
static int REFCB = refNum;
static int REFCBITS_ = ((1 << REFCB) -1);
static long REFCBITS = (((long)(-1)) << (64 - refNum));
static long REFONE = (((long)1) << (64 - refNum));
static long DELETED = 0x0000000000000000;
static int RECBW = (64 - refNum);
static int NODEB = ((64 - refNum - arrNum - idxNum) >> 1);
static long NODEBITS = (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)));
static long NODEONE = ((((((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))) - 1) & (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) ^ (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))));
static int NEXTB = ((64 - refNum - arrNum - idxNum) >> 1);
static long NEXTBITS = ((((((((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))) - 1) & (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) ^ (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) - 1) ^ ((((((((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))) - 1) & (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) ^ (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)));
static long NEXTONE = (((((((((((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))) - 1) & (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) ^ (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) - 1) ^ ((((((((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))) - 1) & (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) ^ (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))) - 1) & ((((((((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))) - 1) & (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) ^ (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) - 1) ^ ((((((((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))) - 1) & (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) ^ (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) ^ ((((((((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))) - 1) & (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) ^ (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) - 1) ^ ((((((((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))) - 1) & (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) ^ (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))));
static long NEXTTT = (((((((((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))) - 1) & (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) ^ (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) - 1) ^ ((((((((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))) - 1) & (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) ^ (((((long)1) << (64 - refNum)) -1 ) ^ (((((long)1) << (64 - refNum)) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1)))) - 1) >> ((64 - refNum - arrNum - idxNum) >> 1))) | ((((1 << arrNum) - 1) << idxNum)|((1 << idxNum) - 1)));
static int RESTART = 2;
static int KEEPPREV = 1;
static int NONE = 0;
static int RECORD = 0x00000004;
static int SEARCH = 0x00000002;
static int REMOVE = 0x00000001;
final static int N = 124;
final static int times = 800;
final static int LISTNUM = 512;
final static int listN = LISTNUM - 1;
static int psly_handle_records(RecordList list, long pointer, int flag) {
long key = (long) pointer;
Record head = list.head;
Record tail = list.tail;
Record my = null;
long localN = 0;
int removed = 0;
Record prev = head;
long prevNext = prev.nextRecord;
Record curr = idx_Record(prevNext);
KeepPrev:
for(;;) {
long currNext = curr.nextRecord;
long currPointer = curr.pointer;
long New = prev.nextRecord;
if((prevNext & NODEBITS) != (New & NODEBITS) || (New & REFCBITS) == DELETED) {
prev = head;
prevNext = prev.nextRecord;
curr = idx_Record(prevNext);
continue;
}
if((prevNext & NEXTTT) != (New & NEXTTT)) {
prevNext = New;
curr = idx_Record(prevNext);
continue;
}
prevNext = New;
long currKey = (long) currPointer;
if(curr == tail || currKey > key) {
if(flag == SEARCH)
return 0;
if(flag == REMOVE && (prevNext & ARRIDXBIT_) == (curr.self & ARRIDXBIT_))
return 0;
Record append;
/*CasAppend:*/
if(flag == RECORD) {
if(my == null) {
my = get_Record();
localN = (my.nextRecord);
my.pointer = pointer;
}
my.nextRecord = newNext(localN, curr);
append = my;
} else {
append = curr;
}
for(;;){
if(prev.casNextRecord(prevNext, New = nxtAddrV(prevNext, append))){
long local = prevNext;
while((local & ARRIDXBIT_) != (curr.self & ARRIDXBIT_)) {
Record first = idx_Record(local);
first.pointer = 0;
local = first.nextRecord;
return_Record(first);
}
if(flag == RECORD)
return 1;
else
return 0;
}
New = prev.nextRecord;
if((prevNext & NODEBITS) != (New & NODEBITS) || (New & REFCBITS) == DELETED) {
prev = head;
prevNext = (prev.nextRecord);
curr = idx_Record(prevNext);
continue KeepPrev;
}
if((prevNext & NEXTTT) != (New & NEXTTT)) {
prevNext = New;
curr = idx_Record(prevNext);
continue KeepPrev;
}
prevNext = New;
}
} else {
if(flag == SEARCH && currKey == key)
return (int)((currNext >> RECBW) & REFCBITS_);
New = curr.nextRecord;
if((currNext & NODEBITS) != (New & NODEBITS)) {
New = (prev.nextRecord);
if((prevNext & NODEBITS) != (New & NODEBITS) || (New & REFCBITS) == DELETED) {
prev = head;
prevNext = (prev.nextRecord);
curr = idx_Record(prevNext);
continue;
}
prevNext = New;
curr = idx_Record(prevNext);
continue;
}
currNext = New;
if((currNext & REFCBITS) == DELETED) {
curr = idx_Record(currNext);
continue;
}
if(currKey != key) {
prev = curr;
prevNext = currNext;
} else {
if(removed == 1)
return 0;
Record found = curr;
long foundNext = currNext;
if(flag == REMOVE) {
long refNuM;
if(((refNuM = found.addAndGetNextRecord(-REFONE)) & REFCBITS) != DELETED)
return (int) ((refNuM >> RECBW) & REFCBITS_);
removed = 1;
currNext = refNuM;
} else {
for(;;) {
long refNuM;
if((found.casNextRecord(foundNext, refNuM = plusRecord(foundNext)))) {
if(my != null) {
my.nextRecord = localN;
return_Record(my);
}
return (int) ((refNuM >> RECBW) & REFCBITS_);
}
New = found.nextRecord;
if((foundNext & NODEBITS) != (New & NODEBITS)) {
New = (prev.nextRecord);
if((prevNext & NODEBITS) != (New & NODEBITS) || (New & REFCBITS) == DELETED) {
prev = head;
prevNext = (prev.nextRecord);
curr = idx_Record(prevNext);
continue KeepPrev;
}
prevNext = New;
curr = idx_Record(prevNext);
continue KeepPrev;
}
if((New & REFCBITS) == DELETED) {
currNext = New;
break;
}
foundNext = New;
}
}
}
curr = idx_Record(currNext);
}
}
}
static int psly_record(long pointer) {
int key = (int) pointer;
RecordList list = map.lists[key & (LISTNUM-1)];
return psly_handle_records(list, pointer, RECORD);
}
static int psly_remove(long pointer) {
int key = (int) pointer;
RecordList list = map.lists[key & (LISTNUM-1)];
return psly_handle_records(list, pointer, REMOVE);
}
static int psly_search(long pointer) {
int key = (int) pointer;
RecordList list = map.lists[key & (LISTNUM-1)];
return psly_handle_records(list, pointer, SEARCH);
}
static long nxtAddrV(long old, Record replace) {
return (old & REFCBITS) | (old & NODEBITS) | ((old + NEXTONE) & NEXTBITS) | (replace.self & ARRIDXBIT_);
}
static long plusRecord(long old) {
return ((old + REFONE) & REFCBITS) | (old & NODEBITS) | (old & NEXTBITS) | (old & ARRIDXBIT_);
}
static long newNext(long old, Record replace) {
return REFONE | ((old + NODEONE) & NODEBITS) | (old & NEXTBITS) | (replace.self & ARRIDXBIT_);
}
static class RecordList {
volatile Record head ;
volatile Record tail ;
}
static class RecordMap {
volatile RecordList[] lists;
}
static volatile RecordMap map;
static Record idx_Record(long index) {
int arr = (((int)index) & PSLY_Record_ARRBIT) >> PSLY_Record_IDXNUM;
int idx = ((int)index) & PSLY_Record_IDXBIT;
return psly_Records[arr][idx];
}
static volatile AtomicInteger recordTake = new AtomicInteger();
static Record get_Record() {
for(int i = 0; i < PSLY_Record_ARRAYNUM; ++i) {
int array = recordTake.getAndIncrement() & PSLY_Record_ARRAYBITS;
RecordQueue queue = psly_Record_queues[array];
Record[] arr = psly_Records[array];
for(;;){
int headIndex = queue.head;
int indexHead = headIndex & PSLY_Record_HEADIDXBIT;
Record head = arr[indexHead];
int tailIndex = queue.tail;
int indexTail = tailIndex & PSLY_Record_TAILIDXBIT;
int nextIndex = head.next;
if(headIndex == queue.head) {
if(indexHead == indexTail){
if((nextIndex & PSLY_Record_NEXTTAILBIT) == PSLY_Record_NEXTTAILBIT)
break;
queue.casTail(tailIndex, (((tailIndex & PSLY_Record_TAILVERSIONBIT) + PSLY_Record_TAILVERSIONONE ) & PSLY_Record_TAILVERSIONBIT)|(nextIndex & PSLY_Record_TAILIDXBIT));
} else {
if(queue.casHead(headIndex, (((headIndex & PSLY_Record_HEADVERSIONBIT) + PSLY_Record_HEADVERSIONONE) & PSLY_Record_HEADVERSIONBIT)|(nextIndex & PSLY_Record_HEADIDXBIT))) {
// head.nextRecord = plusNodeV(head.nextRecord);
return head;
}
}
}
}
}
for(int i = 0; i < PSLY_Record_ARRAYNUM; ++i) {
int array = i;
RecordQueue queue = psly_Record_queues[array];
Record[] arr = psly_Records[array];
for(;;){
int headIndex = queue.head;
int indexHead = headIndex & PSLY_Record_HEADIDXBIT;
Record head = arr[indexHead];
int tailIndex = queue.tail;
int indexTail = tailIndex & PSLY_Record_TAILIDXBIT;
int nextIndex = head.next;
if(headIndex == queue.head) {
if(indexHead == indexTail){
if((nextIndex & PSLY_Record_NEXTTAILBIT) == PSLY_Record_NEXTTAILBIT)
break;
queue.casTail(tailIndex, (((tailIndex & PSLY_Record_TAILVERSIONBIT) + PSLY_Record_TAILVERSIONONE ) & PSLY_Record_TAILVERSIONBIT)|(nextIndex & PSLY_Record_TAILIDXBIT));
} else {
if(queue.casHead(headIndex, (((headIndex & PSLY_Record_HEADVERSIONBIT) + PSLY_Record_HEADVERSIONONE) & PSLY_Record_HEADVERSIONBIT)|(nextIndex & PSLY_Record_HEADIDXBIT))) {
// head.nextRecord = plusNodeV(head.nextRecord);
return head;
}
}
}
}
}
return null;
}
static void return_Record(Record record) {
record.next |= PSLY_Record_NEXTTAILBIT;
int self = record.self;
int array = (self >> PSLY_Record_IDXNUM) & PSLY_Record_ARRBITR;
Record[] arr = psly_Records[array];
RecordQueue queue = psly_Record_queues[array];
for(;;) {
int tailIndex = queue.tail;
int indexTail = tailIndex & PSLY_Record_TAILIDXBIT;
Record tail = arr[indexTail];
int nextIndex = tail.next;
if(tailIndex == queue.tail){
if((nextIndex & PSLY_Record_NEXTTAILBIT) == PSLY_Record_NEXTTAILBIT) {
if(tail.casNext(nextIndex, (((nextIndex & PSLY_Record_NEXTVERSIONBIT) + PSLY_Record_NEXTVERSIONONE) & PSLY_Record_NEXTVERSIONBIT)|(self & PSLY_Record_NEXTIDXBIT))){
queue.casTail(tailIndex, (((tailIndex & PSLY_Record_TAILVERSIONBIT) + PSLY_Record_TAILVERSIONONE) & PSLY_Record_TAILVERSIONBIT)|(self & PSLY_Record_TAILIDXBIT));
return;
}
} else {
queue.casTail(tailIndex, (((tailIndex & PSLY_Record_TAILVERSIONBIT) + PSLY_Record_TAILVERSIONONE) & PSLY_Record_TAILVERSIONBIT)|(nextIndex & PSLY_Record_TAILIDXBIT));
}
}
}
}
static Record[][] psly_Records;
static class Record {
volatile int next;
final int self;
public Record(int self, int next, long nextRecord, long pointer) {
super();
this.next = next;
this.self = self;
this.nextRecord = nextRecord;
this.pointer = pointer;
}
public boolean casNext(int cmp, int val) {
return UNSAFE.compareAndSwapInt(this, nextOffset, cmp, val);
}
public boolean casNextRecord(long cmp, long val) {
return UNSAFE.compareAndSwapLong(this, nextRecordOffset, cmp, val);
}
public long addAndGetNextRecord(long delta) {
return UNSAFE.getAndAddLong(this, nextRecordOffset, delta) + delta;
}
public void setPointer(long pointer) {
this.pointer = pointer;
}
volatile long pointer;
volatile long nextRecord;
private static final sun.misc.Unsafe UNSAFE;
private static final long nextRecordOffset;
private static final long nextOffset;
static {
try {
UNSAFE = UtilUnsafe.getUnsafe();
nextRecordOffset = UNSAFE.objectFieldOffset(Record.class.getDeclaredField("nextRecord"));
nextOffset = UNSAFE.objectFieldOffset(Record.class.getDeclaredField("next"));
} catch (Exception e) {
throw new Error(e);
}
}
}
static RecordQueue[] psly_Record_queues;
static class RecordQueue {
volatile int head;
volatile int tail;
public boolean casHead(int cmp, int val) {
return UNSAFE.compareAndSwapInt(this, headOffset, cmp, val);
}
public boolean casTail(int cmp, int val) {
return UNSAFE.compareAndSwapInt(this, tailOffset, cmp, val);
}
private static final sun.misc.Unsafe UNSAFE;
private static final long headOffset;
private static final long tailOffset;
static {
try {
UNSAFE = UtilUnsafe.getUnsafe();
headOffset = UNSAFE.objectFieldOffset(RecordQueue.class.getDeclaredField("head"));
tailOffset = UNSAFE.objectFieldOffset(RecordQueue.class.getDeclaredField("tail"));
} catch (Exception e) {
throw new Error(e);
}
}
}
public static void main(String[] argv) {
if(argv.length != 3)
return;
nprocs = Integer.parseInt(argv[0]);
recordremove =Integer.parseInt(argv[1]);
int times_ = Integer.parseInt(argv[2]);
psly_Records = new Record[1 << PSLY_Record_ARRNUM][1 << PSLY_Record_IDXNUM];
psly_Record_queues = new RecordQueue[1 << PSLY_Record_ARRNUM];
for(int i = 0; i < (1 << PSLY_Record_ARRNUM); ++i) {
//初始化所有记录Records
for(int j = 0; j < (1 << PSLY_Record_IDXNUM) - 1; ++j) {
Record record = new Record((i << PSLY_Record_IDXNUM) | j, j + 1, 0, 0);
psly_Records[i][j] = record;
}
Record record = new Record((i << PSLY_Record_IDXNUM) | ((1 << PSLY_Record_IDXNUM) - 1), PSLY_Record_NEXTTAILBIT, 39417, 0);
psly_Records[i][((1 << PSLY_Record_IDXNUM) - 1)] = record;
//初始化队列
psly_Record_queues[i] = new RecordQueue();
psly_Record_queues[i].head = 0;
psly_Record_queues[i].tail = (1 << PSLY_Record_IDXNUM) - 1;
}
map = new RecordMap();
map.lists = new RecordList[LISTNUM];
for(int i = 0; i < LISTNUM; ++i) {
map.lists[i] = new RecordList();
RecordList list = map.lists[i];
Record head = get_Record();
Record tail = get_Record();
head.nextRecord = newNext(head.nextRecord, tail);
list.head = head;
list.tail = tail;
}
// testRecords();
int[] errNum = new int[2];
errNum[0] = 0;
errNum[1] = 0;
for(int i = 0; i < times_; ++i)
testRecordMaps(errNum);
}
private static class UtilUnsafe {
private UtilUnsafe() {
}
/** Fetch the Unsafe. Use With Caution. */
public static Unsafe getUnsafe() {
if (UtilUnsafe.class.getClassLoader() == null)
return Unsafe.getUnsafe();
try {
final Field fld = Unsafe.class.getDeclaredField("theUnsafe");
fld.setAccessible(true);
return (Unsafe) fld.get(UtilUnsafe.class);
} catch (Exception e) {
throw new RuntimeException("Could not obtain access to sun.misc.Unsafe", e);
}
}
}
static int numOfItems = 65536;
static int nprocs = 10000;
static int recordremove = 1000;
static int refCount[] = new int[numOfItems];
static volatile int totalOperation;
private static Object L;
private static final long totalOffset;
private static final int base;
private static final int shift;
private static final sun.misc.Unsafe UNSAFE;
static {
try {
UNSAFE = UtilUnsafe.getUnsafe();
L = UNSAFE.staticFieldBase(LockFreeLinkedListWithRefCount.class.getDeclaredField("totalOperation"));;
totalOffset = UNSAFE.staticFieldOffset(LockFreeLinkedListWithRefCount.class.getDeclaredField("totalOperation"));
base = UNSAFE.arrayBaseOffset(int[].class);
int scale = UNSAFE.arrayIndexScale(int[].class);
if ((scale & (scale - 1)) != 0)
throw new Error("data type scale not a power of two");
shift = 31 - Integer.numberOfLeadingZeros(scale);
} catch (Exception e) {
throw new Error(e);
}
}
private static long byteOffset(int i) {
return ((long) i << shift) + base;
}
static void recordremoveRoutine(int k) {
ThreadLocal<Random> random = new ThreadLocal<Random>() {
protected Random initialValue() {
return new Random();
}
};
Random r = random.get();
if((k & 1) == 1){
for(int j = 0; j < recordremove; ++j) {
int key = r.nextInt() & (numOfItems - 1);
int refc = UNSAFE.getIntVolatile(refCount, byteOffset(key));
for(;;) {
if(refc <(( 1 << REFCB) -1)) {
if(UNSAFE.compareAndSwapInt(refCount, byteOffset(key), refc, refc+1)) {
psly_record(key * (listN));
UNSAFE.getAndAddInt(L, totalOffset, 1);
break;
}
refc = UNSAFE.getIntVolatile(refCount, byteOffset(key));
} else break;
}
}
} else {
int key = r.nextInt() & (numOfItems - 1);
int f = key & 1;
for(int j = 0; j < recordremove; ++j) {
int refc = UNSAFE.getIntVolatile(refCount, byteOffset(key));
for(;;) {
if(refc > 0) {
if(UNSAFE.compareAndSwapInt(refCount, byteOffset(key), refc, refc-1)) {
psly_remove(key * (listN));
UNSAFE.getAndAddInt(L, totalOffset, 1);
// break;
}
refc = UNSAFE.getIntVolatile(refCount, byteOffset(key));
} else break;
}
if( f == 1)
key = (key + 1) & (numOfItems - 1);
else
key = (key - 1) & (numOfItems - 1);
}
}
}
static void testRecordMaps(int[] errNum) {
long start = System.currentTimeMillis();
System.out.println("\n");
System.out.println(++errNum[1] + " times: ");
Thread[] tids = new Thread[nprocs];
for(int i = 0; i < nprocs; ++i) {
(tids[i] = new Thread() {
public void run() {
recordremoveRoutine(errNum[1]);
}
}).start();
}
for(int i = 0; i < nprocs; ++i) {
try {
tids[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int total = nprocs * recordremove;
int add = 0;
for(int i = 0; i < numOfItems; ++i) {
int lo = psly_search(i * listN);
int lo2 = UNSAFE.getIntVolatile(refCount, byteOffset(i));
if(i < 8) System.out.println(i + ": " + lo2 + "==" + lo);
if(lo != lo2){
++errNum[0];
System.out.println(i + ": " + lo2 + "==" + lo + " WRONG!!");
}
}
System.out.println("err numbers: " + errNum[0]);
System.out.println("total: " + total + ", adds " + add);
System.out.println("actual op: " + totalOperation);
System.out.println("total_use: " + ((System.currentTimeMillis() - start) / 1000.0) );
int k = 0;
for(int i = 0; i < LISTNUM; ++i) {
RecordList list = map.lists[i];
Record head = idx_Record(list.head.nextRecord);
Record tail = list.tail;
while(head != tail) {
++k;
head = idx_Record(head.nextRecord);
}
}
System.out.println(Integer.toString(LISTNUM) + ", " + k + ", " + ((LISTNUM ==k)? 1:0));
}
static void testRecords() {
int total = 0;
for(int i = 0; i < PSLY_Record_ARRAYNUM; ++i) {
RecordQueue q = psly_Record_queues[i];
Record[] currs = psly_Records[i];
Record curr = currs[q.head & PSLY_Record_IDXBIT];
++total;
while((curr) != currs[q.tail & PSLY_Record_IDXBIT]){
++total;
curr = currs[curr.next & PSLY_Record_IDXBIT];
}
}
System.out.println("total: " + total);
Thread[] tids = new Thread[N];
for(int i = 0; i < N; ++i) {
tids[i] = new Thread() {
public void run() {
Record[] records = new Record[times];
for(int j = 0; j < times; ++j) {
Record record = get_Record();
records[j] = record;
}
for(int j = times-1; j >= 0; --j)
return_Record(records[j]);
}
};
tids[i].start();
}
for(int i = 0; i < N; ++i) {
try {
tids[i].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
total = 0;
for(int i = 0; i < PSLY_Record_ARRAYNUM; ++i) {
RecordQueue q = psly_Record_queues[i];
Record[] currs = psly_Records[i];
Record curr = currs[q.head & PSLY_Record_IDXBIT];
++total;
while((curr) != currs[q.tail & PSLY_Record_IDXBIT]){
++total;
curr = currs[curr.next & PSLY_Record_IDXBIT];
}
}
System.out.println("total: " + (total /*+ N * (times-1)*/));
}
}