My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
/*
* Copyright 2012 LMAX Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lmax.disruptor;

import com.lmax.disruptor.util.Util;

import sun.misc.Unsafe;

public class Sequence
{
private static final Unsafe unsafe;
private static final long valueOffset;

static
{
unsafe = Util.getUnsafe();
final int base = unsafe.arrayBaseOffset(long[].class);
final int scale = unsafe.arrayIndexScale(long[].class);
valueOffset = base + (scale * 7);
}

private final long[] paddedValue = new long[15];

public Sequence()
{
setOrdered(-1);
}

public Sequence(final long initialValue)
{
setOrdered(initialValue);
}

public long get()
{
return unsafe.getLongVolatile(paddedValue, valueOffset);
}

public void set(final long value)
{
unsafe.putOrderedLong(paddedValue, valueOffset, value);
}

private void setOrdered(final long value)
{
unsafe.putOrderedLong(paddedValue, valueOffset, value);
}

public boolean compareAndSet(final long expectedValue, final long newValue)
{
return unsafe.compareAndSwapLong(paddedValue, valueOffset, expectedValue, newValue);
}

public String toString()
{
return Long.toString(get());
}

public long incrementAndGet()
{
return addAndGet(1L);
}

public long addAndGet(final long increment)
{
long currentValue;
long newValue;

do
{
currentValue = get();
newValue = currentValue + increment;
}
while (!compareAndSet(currentValue, newValue));

return newValue;
}
}

Change log

r551 by mikeb01 on May 13, 2012   Diff
Add license information to Sequence.java
Go to: 
Project members, sign in to write a code review

Older revisions

r542 by mikeb01 on Apr 19, 2012   Diff
Removed old PaddedAtomicLong and use
Sequence in all places
r537 by mikeb01 on Apr 15, 2012   Diff
Fix bug with IBM JDK.  Use Unsafe
instead of Atomic classes
r496 by mjpt777 on Dec 15, 2011   Diff
Added Javadoc for compareAndSet
All revisions of this file

File info

Size: 2216 bytes, 92 lines
Powered by Google Project Hosting