/
AtomicQueueUtil.java
101 lines (81 loc) · 2.43 KB
/
AtomicQueueUtil.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
package org.jctools.queues.atomic.unpadded;
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.concurrent.atomic.AtomicReferenceArray;
final class AtomicQueueUtil
{
static <E> E lvRefElement(AtomicReferenceArray<E> buffer, int offset)
{
return buffer.get(offset);
}
static <E> E lpRefElement(AtomicReferenceArray<E> buffer, int offset)
{
return buffer.get(offset); // no weaker form available
}
static <E> void spRefElement(AtomicReferenceArray<E> buffer, int offset, E value)
{
buffer.lazySet(offset, value); // no weaker form available
}
static void soRefElement(AtomicReferenceArray buffer, int offset, Object value)
{
buffer.lazySet(offset, value);
}
static <E> void svRefElement(AtomicReferenceArray<E> buffer, int offset, E value)
{
buffer.set(offset, value);
}
static int calcRefElementOffset(long index)
{
return (int) index;
}
static int calcCircularRefElementOffset(long index, long mask)
{
return (int) (index & mask);
}
static <E> AtomicReferenceArray<E> allocateRefArray(int capacity)
{
return new AtomicReferenceArray<E>(capacity);
}
static void spLongElement(AtomicLongArray buffer, int offset, long e)
{
buffer.lazySet(offset, e);
}
static void soLongElement(AtomicLongArray buffer, int offset, long e)
{
buffer.lazySet(offset, e);
}
static long lpLongElement(AtomicLongArray buffer, int offset)
{
return buffer.get(offset);
}
static long lvLongElement(AtomicLongArray buffer, int offset)
{
return buffer.get(offset);
}
static int calcLongElementOffset(long index)
{
return (int) index;
}
static int calcCircularLongElementOffset(long index, int mask)
{
return (int) (index & mask);
}
static AtomicLongArray allocateLongArray(int capacity)
{
return new AtomicLongArray(capacity);
}
static int length(AtomicReferenceArray<?> buf)
{
return buf.length();
}
/**
* This method assumes index is actually (index << 1) because lower bit is used for resize hence the >> 1
*/
static int modifiedCalcCircularRefElementOffset(long index, long mask)
{
return (int) (index & mask) >> 1;
}
static int nextArrayOffset(AtomicReferenceArray<?> curr)
{
return length(curr) - 1;
}
}