Skip to content

Commit afccd7f

Browse files
committed
Replacement for java.util.concurrent.atomic.AtomicInteger without using of Unsafe.
1 parent 0a417f0 commit afccd7f

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package de.inetsoftware.jwebassembly.api.java.util.concurrent.atomic;
2+
3+
import java.util.function.IntBinaryOperator;
4+
import java.util.function.IntUnaryOperator;
5+
6+
import de.inetsoftware.jwebassembly.api.annotation.Replace;
7+
8+
/**
9+
* Replacement for java.util.concurrent.atomic.AtomicInteger without using of Unsafe.
10+
* TODO this class is not thread safe anymore. This must be rewritten if we supports threads.
11+
*
12+
* @author Volker Berlin
13+
*/
14+
@Replace( "java/util/concurrent/atomic/AtomicInteger" )
15+
public class ReplacementForAtomicInteger extends Number implements java.io.Serializable {
16+
17+
private volatile int value;
18+
19+
public ReplacementForAtomicInteger( int initialValue ) {
20+
value = initialValue;
21+
}
22+
23+
public ReplacementForAtomicInteger() {
24+
}
25+
26+
public final int get() {
27+
return value;
28+
}
29+
30+
public final void set( int newValue ) {
31+
value = newValue;
32+
}
33+
34+
public final void lazySet( int newValue ) {
35+
value = newValue;
36+
}
37+
38+
public final int getAndSet( int newValue ) {
39+
int oldValue = value;
40+
value = newValue;
41+
return oldValue;
42+
}
43+
44+
public final boolean compareAndSet( int expect, int update ) {
45+
if( value == expect ) {
46+
value = update;
47+
return true;
48+
}
49+
return false;
50+
}
51+
52+
public final boolean weakCompareAndSet( int expect, int update ) {
53+
if( value == expect ) {
54+
value = update;
55+
return true;
56+
}
57+
return false;
58+
}
59+
60+
public final int getAndIncrement() {
61+
return value++;
62+
}
63+
64+
public final int getAndDecrement() {
65+
return value--;
66+
}
67+
68+
public final int getAndAdd( int delta ) {
69+
int oldValue = value;
70+
value = oldValue + delta;
71+
return oldValue;
72+
}
73+
74+
public final int incrementAndGet() {
75+
return ++value;
76+
}
77+
78+
public final int decrementAndGet() {
79+
return --value;
80+
}
81+
82+
public final int addAndGet( int delta ) {
83+
int oldValue = value + delta;
84+
value = oldValue;
85+
return oldValue;
86+
}
87+
88+
public final int getAndUpdate( IntUnaryOperator updateFunction ) {
89+
int prev, next;
90+
do {
91+
prev = get();
92+
next = updateFunction.applyAsInt( prev );
93+
} while( !compareAndSet( prev, next ) );
94+
return prev;
95+
}
96+
97+
public final int updateAndGet( IntUnaryOperator updateFunction ) {
98+
int prev, next;
99+
do {
100+
prev = get();
101+
next = updateFunction.applyAsInt( prev );
102+
} while( !compareAndSet( prev, next ) );
103+
return next;
104+
}
105+
106+
public final int getAndAccumulate( int x, IntBinaryOperator accumulatorFunction ) {
107+
int prev, next;
108+
do {
109+
prev = get();
110+
next = accumulatorFunction.applyAsInt( prev, x );
111+
} while( !compareAndSet( prev, next ) );
112+
return prev;
113+
}
114+
115+
public final int accumulateAndGet( int x, IntBinaryOperator accumulatorFunction ) {
116+
int prev, next;
117+
do {
118+
prev = get();
119+
next = accumulatorFunction.applyAsInt( prev, x );
120+
} while( !compareAndSet( prev, next ) );
121+
return next;
122+
}
123+
124+
public String toString() {
125+
return Integer.toString( value );
126+
}
127+
128+
public int intValue() {
129+
return value;
130+
}
131+
132+
public long longValue() {
133+
return (long)value;
134+
}
135+
136+
public float floatValue() {
137+
return (float)value;
138+
}
139+
140+
public double doubleValue() {
141+
return (double)value;
142+
}
143+
}

0 commit comments

Comments
 (0)