diff options
author | jfrijters <jfrijters> | 2011-05-13 11:29:31 +0400 |
---|---|---|
committer | jfrijters <jfrijters> | 2011-05-13 11:29:31 +0400 |
commit | ffe0dacce72115893842597a7432cbf671d989c5 (patch) | |
tree | c5300bbb30a76ee3b5d04c3d29ed8aa31edbcd0b | |
parent | 0d4bd639f583cee525ea523725ea153ef8ceda47 (diff) |
- Updated AtomicInteger, AtomicIntegerArray, AtomicLonger, AtomicLongerArray to use .NET 2.0 interlocked operations.
- Added stind_i8 opcode to remapper.
-rw-r--r-- | classpath/java/util/concurrent/atomic/AtomicInteger.java | 18 | ||||
-rw-r--r-- | classpath/java/util/concurrent/atomic/AtomicIntegerArray.java | 58 | ||||
-rw-r--r-- | classpath/java/util/concurrent/atomic/AtomicLong.java | 53 | ||||
-rw-r--r-- | classpath/java/util/concurrent/atomic/AtomicLongArray.java | 61 | ||||
-rw-r--r-- | ikvmc/remapper.cs | 10 | ||||
-rw-r--r-- | openjdk/map.xml | 179 |
6 files changed, 236 insertions, 143 deletions
diff --git a/classpath/java/util/concurrent/atomic/AtomicInteger.java b/classpath/java/util/concurrent/atomic/AtomicInteger.java index 48119cfa..bb50770a 100644 --- a/classpath/java/util/concurrent/atomic/AtomicInteger.java +++ b/classpath/java/util/concurrent/atomic/AtomicInteger.java @@ -7,7 +7,7 @@ */ /* - Parts Copyright (C) 2006 Jeroen Frijters + Parts Copyright (C) 2006-2011 Jeroen Frijters This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -150,12 +150,7 @@ public class AtomicInteger extends Number implements java.io.Serializable { * @return the previous value */ public final int getAndAdd(int delta) { - for (;;) { - int current = get(); - int next = current + delta; - if (compareAndSet(current, next)) - return current; - } + return addAndGet(delta) - delta; } /** @@ -178,14 +173,7 @@ public class AtomicInteger extends Number implements java.io.Serializable { * @param delta the value to add * @return the updated value */ - public final int addAndGet(int delta) { - for (;;) { - int current = get(); - int next = current + delta; - if (compareAndSet(current, next)) - return next; - } - } + public final native int addAndGet(int delta); /** * Returns the String representation of the current value. diff --git a/classpath/java/util/concurrent/atomic/AtomicIntegerArray.java b/classpath/java/util/concurrent/atomic/AtomicIntegerArray.java index 1acc6c4c..18fcec33 100644 --- a/classpath/java/util/concurrent/atomic/AtomicIntegerArray.java +++ b/classpath/java/util/concurrent/atomic/AtomicIntegerArray.java @@ -7,7 +7,7 @@ */ /* - Parts Copyright (C) 2006 Jeroen Frijters + Parts Copyright (C) 2006-2011 Jeroen Frijters This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -124,13 +124,7 @@ public class AtomicIntegerArray implements java.io.Serializable { * @param newValue the new value * @return the previous value */ - public final int getAndSet(int i, int newValue) { - while (true) { - int current = get(i); - if (compareAndSet(i, current, newValue)) - return current; - } - } + public final native int getAndSet(int i, int newValue); /** * Atomically sets the element at position <tt>i</tt> to the given @@ -165,12 +159,7 @@ public class AtomicIntegerArray implements java.io.Serializable { * @return the previous value */ public final int getAndIncrement(int i) { - while (true) { - int current = get(i); - int next = current + 1; - if (compareAndSet(i, current, next)) - return current; - } + return incrementAndGet(i) - 1; } /** @@ -180,12 +169,7 @@ public class AtomicIntegerArray implements java.io.Serializable { * @return the previous value */ public final int getAndDecrement(int i) { - while (true) { - int current = get(i); - int next = current - 1; - if (compareAndSet(i, current, next)) - return current; - } + return decrementAndGet(i) + 1; } /** @@ -196,12 +180,7 @@ public class AtomicIntegerArray implements java.io.Serializable { * @return the previous value */ public final int getAndAdd(int i, int delta) { - while (true) { - int current = get(i); - int next = current + delta; - if (compareAndSet(i, current, next)) - return current; - } + return addAndGet(i, delta) - delta; } /** @@ -210,14 +189,7 @@ public class AtomicIntegerArray implements java.io.Serializable { * @param i the index * @return the updated value */ - public final int incrementAndGet(int i) { - while (true) { - int current = get(i); - int next = current + 1; - if (compareAndSet(i, current, next)) - return next; - } - } + public final native int incrementAndGet(int i); /** * Atomically decrements by one the element at index <tt>i</tt>. @@ -225,14 +197,7 @@ public class AtomicIntegerArray implements java.io.Serializable { * @param i the index * @return the updated value */ - public final int decrementAndGet(int i) { - while (true) { - int current = get(i); - int next = current - 1; - if (compareAndSet(i, current, next)) - return next; - } - } + public final native int decrementAndGet(int i); /** * Atomically adds the given value to the element at index <tt>i</tt>. @@ -241,14 +206,7 @@ public class AtomicIntegerArray implements java.io.Serializable { * @param delta the value to add * @return the updated value */ - public final int addAndGet(int i, int delta) { - while (true) { - int current = get(i); - int next = current + delta; - if (compareAndSet(i, current, next)) - return next; - } - } + public final native int addAndGet(int i, int delta); /** * Returns the String representation of the current values of array. diff --git a/classpath/java/util/concurrent/atomic/AtomicLong.java b/classpath/java/util/concurrent/atomic/AtomicLong.java index e4e6b6f6..a170578b 100644 --- a/classpath/java/util/concurrent/atomic/AtomicLong.java +++ b/classpath/java/util/concurrent/atomic/AtomicLong.java @@ -7,7 +7,7 @@ */ /* - Parts Copyright (C) 2006 Jeroen Frijters + Parts Copyright (C) 2006-2011 Jeroen Frijters This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -56,7 +56,7 @@ public class AtomicLong extends Number implements java.io.Serializable { */ static final boolean VM_SUPPORTS_LONG_CAS = false; - private long value; + private volatile long value; /** * Creates a new AtomicLong with the given initial value. @@ -64,7 +64,7 @@ public class AtomicLong extends Number implements java.io.Serializable { * @param initialValue the initial value */ public AtomicLong(long initialValue) { - set(initialValue); + value = initialValue; } /** @@ -78,7 +78,7 @@ public class AtomicLong extends Number implements java.io.Serializable { * * @return the current value */ - public final synchronized long get() { + public final long get() { return value; } @@ -87,7 +87,7 @@ public class AtomicLong extends Number implements java.io.Serializable { * * @param newValue the new value */ - public final synchronized void set(long newValue) { + public final void set(long newValue) { value = newValue; } @@ -98,7 +98,7 @@ public class AtomicLong extends Number implements java.io.Serializable { * @since 1.6 */ public final void lazySet(long newValue) { - set(newValue); + value = newValue; } /** @@ -107,11 +107,7 @@ public class AtomicLong extends Number implements java.io.Serializable { * @param newValue the new value * @return the previous value */ - public final synchronized long getAndSet(long newValue) { - long v = value; - value = newValue; - return v; - } + public final native long getAndSet(long newValue); /** * Atomically sets the value to the given updated value @@ -122,13 +118,7 @@ public class AtomicLong extends Number implements java.io.Serializable { * @return true if successful. False return indicates that * the actual value was not equal to the expected value. */ - public final synchronized boolean compareAndSet(long expect, long update) { - if (value == expect) { - value = update; - return true; - } - return false; - } + public final native boolean compareAndSet(long expect, long update); /** * Atomically sets the value to the given updated value @@ -149,8 +139,8 @@ public class AtomicLong extends Number implements java.io.Serializable { * * @return the previous value */ - public final synchronized long getAndIncrement() { - return value++; + public final long getAndIncrement() { + return incrementAndGet() - 1; } /** @@ -158,8 +148,8 @@ public class AtomicLong extends Number implements java.io.Serializable { * * @return the previous value */ - public final synchronized long getAndDecrement() { - return value--; + public final long getAndDecrement() { + return decrementAndGet() + 1; } /** @@ -168,10 +158,8 @@ public class AtomicLong extends Number implements java.io.Serializable { * @param delta the value to add * @return the previous value */ - public final synchronized long getAndAdd(long delta) { - long v = value; - value += delta; - return v; + public final long getAndAdd(long delta) { + return addAndGet(delta) - delta; } /** @@ -179,18 +167,14 @@ public class AtomicLong extends Number implements java.io.Serializable { * * @return the updated value */ - public final synchronized long incrementAndGet() { - return ++value; - } + public final native long incrementAndGet(); /** * Atomically decrements by one the current value. * * @return the updated value */ - public final synchronized long decrementAndGet() { - return --value; - } + public final native long decrementAndGet(); /** * Atomically adds the given value to the current value. @@ -198,10 +182,7 @@ public class AtomicLong extends Number implements java.io.Serializable { * @param delta the value to add * @return the updated value */ - public final synchronized long addAndGet(long delta) { - value += delta; - return value; - } + public final native long addAndGet(long delta); /** * Returns the String representation of the current value. diff --git a/classpath/java/util/concurrent/atomic/AtomicLongArray.java b/classpath/java/util/concurrent/atomic/AtomicLongArray.java index 086522f7..0ddb0496 100644 --- a/classpath/java/util/concurrent/atomic/AtomicLongArray.java +++ b/classpath/java/util/concurrent/atomic/AtomicLongArray.java @@ -7,7 +7,7 @@ */ /* - Parts Copyright (C) 2006 Jeroen Frijters + Parts Copyright (C) 2006-2011 Jeroen Frijters This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -52,6 +52,9 @@ public class AtomicLongArray implements java.io.Serializable { */ public AtomicLongArray(int length) { array = new long[length]; + // must perform at least one volatile write to conform to JMM + if (length > 0) + set(0, 0); } /** @@ -66,9 +69,12 @@ public class AtomicLongArray implements java.io.Serializable { throw new NullPointerException(); int length = array.length; this.array = new long[length]; - synchronized (this) { - for (int i = 0; i < array.length; i++) + if (length > 0) { + int last = length-1; + for (int i = 0; i < last; ++i) this.array[i] = array[i]; + // Do the last write as volatile + set(last, array[last]); } } @@ -87,9 +93,7 @@ public class AtomicLongArray implements java.io.Serializable { * @param i the index * @return the current value */ - public final synchronized long get(int i) { - return array[i]; - } + public final native long get(int i); /** * Sets the element at position <tt>i</tt> to the given value. @@ -97,9 +101,7 @@ public class AtomicLongArray implements java.io.Serializable { * @param i the index * @param newValue the new value */ - public final synchronized void set(int i, long newValue) { - array[i] = newValue; - } + public final native void set(int i, long newValue); /** * Eventually sets the element at position <tt>i</tt> to the given value. @@ -121,11 +123,7 @@ public class AtomicLongArray implements java.io.Serializable { * @param newValue the new value * @return the previous value */ - public final synchronized long getAndSet(int i, long newValue) { - long v = array[i]; - array[i] = newValue; - return v; - } + public final native long getAndSet(int i, long newValue); /** * Atomically sets the value to the given updated value @@ -137,13 +135,7 @@ public class AtomicLongArray implements java.io.Serializable { * @return true if successful. False return indicates that * the actual value was not equal to the expected value. */ - public final synchronized boolean compareAndSet(int i, long expect, long update) { - if (array[i] == expect) { - array[i] = update; - return true; - } - return false; - } + public final native boolean compareAndSet(int i, long expect, long update); /** * Atomically sets the value to the given updated value @@ -166,8 +158,8 @@ public class AtomicLongArray implements java.io.Serializable { * @param i the index * @return the previous value */ - public final synchronized long getAndIncrement(int i) { - return array[i]++; + public final long getAndIncrement(int i) { + return incrementAndGet(i) - 1; } /** @@ -176,8 +168,8 @@ public class AtomicLongArray implements java.io.Serializable { * @param i the index * @return the previous value */ - public final synchronized long getAndDecrement(int i) { - return array[i]--; + public final long getAndDecrement(int i) { + return decrementAndGet(i) + 1; } /** @@ -187,10 +179,8 @@ public class AtomicLongArray implements java.io.Serializable { * @param delta the value to add * @return the previous value */ - public final synchronized long getAndAdd(int i, long delta) { - long v = array[i]; - array[i] += delta; - return v; + public final long getAndAdd(int i, long delta) { + return addAndGet(i, delta) - delta; } /** @@ -199,9 +189,7 @@ public class AtomicLongArray implements java.io.Serializable { * @param i the index * @return the updated value */ - public final synchronized long incrementAndGet(int i) { - return ++array[i]; - } + public final native long incrementAndGet(int i); /** * Atomically decrements by one the element at index <tt>i</tt>. @@ -209,9 +197,7 @@ public class AtomicLongArray implements java.io.Serializable { * @param i the index * @return the updated value */ - public final synchronized long decrementAndGet(int i) { - return --array[i]; - } + public final native long decrementAndGet(int i); /** * Atomically adds the given value to the element at index <tt>i</tt>. @@ -220,10 +206,7 @@ public class AtomicLongArray implements java.io.Serializable { * @param delta the value to add * @return the updated value */ - public synchronized long addAndGet(int i, long delta) { - array[i] += delta; - return array[i]; - } + public native long addAndGet(int i, long delta); /** * Returns the String representation of the current values of array. diff --git a/ikvmc/remapper.cs b/ikvmc/remapper.cs index e9f14f9c..1be7c6de 100644 --- a/ikvmc/remapper.cs +++ b/ikvmc/remapper.cs @@ -742,6 +742,15 @@ namespace IKVM.Internal.MapXml } } + [XmlType("stind_i8")] + public sealed class Stind_i8 : Simple + { + public Stind_i8() + : base(OpCodes.Stind_I8) + { + } + } + [XmlType("stind_ref")] public sealed class Stind_ref : Simple { @@ -1232,6 +1241,7 @@ namespace IKVM.Internal.MapXml [XmlElement(typeof(Stind_i1))] [XmlElement(typeof(Stind_i2))] [XmlElement(typeof(Stind_i4))] + [XmlElement(typeof(Stind_i8))] [XmlElement(typeof(Stind_ref))] [XmlElement(typeof(Ret))] [XmlElement(typeof(Throw))] diff --git a/openjdk/map.xml b/openjdk/map.xml index 237f844b..0ce4f42d 100644 --- a/openjdk/map.xml +++ b/openjdk/map.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8" ?> <!-- - Copyright (C) 2002-2010 Jeroen Frijters + Copyright (C) 2002-2011 Jeroen Frijters This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -1963,6 +1963,15 @@ <ret /> </body> </method> + <method name="addAndGet" sig="(I)I"> + <body> + <ldarg_0 /> + <ldflda class="java.util.concurrent.atomic.AtomicInteger" name="value" sig="I" /> + <ldarg_1 /> + <call type="System.Threading.Interlocked" name="Add" sig="System.Int32&;System.Int32" /> + <ret /> + </body> + </method> </class> <class name="java.util.concurrent.atomic.AtomicIntegerArray"> <method name="get" sig="(I)I"> @@ -1989,6 +1998,17 @@ <ret /> </body> </method> + <method name="getAndSet" sig="(II)I"> + <body> + <ldarg_0 /> + <ldfld class="java.util.concurrent.atomic.AtomicIntegerArray" name="array" sig="[I" /> + <ldarg_1 /> + <ldelema sig="I" /> + <ldarg_2 /> + <call type="System.Threading.Interlocked" name="Exchange" sig="System.Int32&;System.Int32" /> + <ret /> + </body> + </method> <method name="compareAndSet" sig="(III)Z"> <body> <ldarg_0 /> @@ -2003,11 +2023,164 @@ <ret /> </body> </method> + <method name="incrementAndGet" sig="(I)I"> + <body> + <ldarg_0 /> + <ldfld class="java.util.concurrent.atomic.AtomicIntegerArray" name="array" sig="[I" /> + <ldarg_1 /> + <ldelema sig="I" /> + <call type="System.Threading.Interlocked" name="Increment" sig="System.Int32&" /> + <ret /> + </body> + </method> + <method name="decrementAndGet" sig="(I)I"> + <body> + <ldarg_0 /> + <ldfld class="java.util.concurrent.atomic.AtomicIntegerArray" name="array" sig="[I" /> + <ldarg_1 /> + <ldelema sig="I" /> + <call type="System.Threading.Interlocked" name="Decrement" sig="System.Int32&" /> + <ret /> + </body> + </method> + <method name="addAndGet" sig="(II)I"> + <body> + <ldarg_0 /> + <ldfld class="java.util.concurrent.atomic.AtomicIntegerArray" name="array" sig="[I" /> + <ldarg_1 /> + <ldelema sig="I" /> + <ldarg_2 /> + <call type="System.Threading.Interlocked" name="Add" sig="System.Int32&;System.Int32" /> + <ret /> + </body> + </method> </class> <class name="java.util.concurrent.atomic.AtomicLong"> - <method name="VMSupportsCS8" sig="()Z"> + <method name="getAndSet" sig="(J)J"> <body> - <ldc_i4_0 /> + <ldarg_0 /> + <ldflda class="java.util.concurrent.atomic.AtomicLong" name="value" sig="J" /> + <ldarg_1 /> + <call type="System.Threading.Interlocked" name="Exchange" sig="System.Int64&;System.Int64" /> + <ret /> + </body> + </method> + <method name="compareAndSet" sig="(JJ)Z"> + <body> + <ldarg_0 /> + <ldflda class="java.util.concurrent.atomic.AtomicLong" name="value" sig="J" /> + <ldarg_2 /> + <ldarg_1 /> + <call type="System.Threading.Interlocked" name="CompareExchange" sig="System.Int64&;System.Int64;System.Int64" /> + <ldarg_1 /> + <ceq /> + <ret /> + </body> + </method> + <method name="incrementAndGet" sig="()J"> + <body> + <ldarg_0 /> + <ldflda class="java.util.concurrent.atomic.AtomicLong" name="value" sig="J" /> + <call type="System.Threading.Interlocked" name="Increment" sig="System.Int64&" /> + <ret /> + </body> + </method> + <method name="decrementAndGet" sig="()J"> + <body> + <ldarg_0 /> + <ldflda class="java.util.concurrent.atomic.AtomicLong" name="value" sig="J" /> + <call type="System.Threading.Interlocked" name="Decrement" sig="System.Int64&" /> + <ret /> + </body> + </method> + <method name="addAndGet" sig="(J)J"> + <body> + <ldarg_0 /> + <ldflda class="java.util.concurrent.atomic.AtomicLong" name="value" sig="J" /> + <ldarg_1 /> + <call type="System.Threading.Interlocked" name="Add" sig="System.Int64&;System.Int64" /> + <ret /> + </body> + </method> + </class> + <class name="java.util.concurrent.atomic.AtomicLongArray"> + <method name="get" sig="(I)J"> + <body> + <ldarg_0 /> + <ldfld class="java.util.concurrent.atomic.AtomicLongArray" name="array" sig="[J" /> + <ldarg_1 /> + <ldelema sig="J" /> + <volatile /> + <ldind_i8 /> + <ret /> + </body> + </method> + <method name="set" sig="(IJ)V"> + <body> + <ldarg_0 /> + <ldfld class="java.util.concurrent.atomic.AtomicLongArray" name="array" sig="[J" /> + <ldarg_1 /> + <ldelema sig="J" /> + <ldarg_2 /> + <volatile /> + <stind_i8 /> + <call type="System.Threading.Thread" name="MemoryBarrier" sig="" /> + <ret /> + </body> + </method> + <method name="getAndSet" sig="(IJ)J"> + <body> + <ldarg_0 /> + <ldfld class="java.util.concurrent.atomic.AtomicLongArray" name="array" sig="[J" /> + <ldarg_1 /> + <ldelema sig="J" /> + <ldarg_2 /> + <call type="System.Threading.Interlocked" name="Exchange" sig="System.Int64&;System.Int64" /> + <ret /> + </body> + </method> + <method name="compareAndSet" sig="(IJJ)Z"> + <body> + <ldarg_0 /> + <ldfld class="java.util.concurrent.atomic.AtomicLongArray" name="array" sig="[J" /> + <ldarg_1 /> + <ldelema sig="J" /> + <ldarg_3 /> + <ldarg_2 /> + <call type="System.Threading.Interlocked" name="CompareExchange" sig="System.Int64&;System.Int64;System.Int64" /> + <ldarg_2 /> + <ceq /> + <ret /> + </body> + </method> + <method name="incrementAndGet" sig="(I)J"> + <body> + <ldarg_0 /> + <ldfld class="java.util.concurrent.atomic.AtomicLongArray" name="array" sig="[J" /> + <ldarg_1 /> + <ldelema sig="J" /> + <call type="System.Threading.Interlocked" name="Increment" sig="System.Int64&" /> + <ret /> + </body> + </method> + <method name="decrementAndGet" sig="(I)J"> + <body> + <ldarg_0 /> + <ldfld class="java.util.concurrent.atomic.AtomicLongArray" name="array" sig="[J" /> + <ldarg_1 /> + <ldelema sig="J" /> + <call type="System.Threading.Interlocked" name="Decrement" sig="System.Int64&" /> + <ret /> + </body> + </method> + <method name="addAndGet" sig="(IJ)J"> + <body> + <ldarg_0 /> + <ldfld class="java.util.concurrent.atomic.AtomicLongArray" name="array" sig="[J" /> + <ldarg_1 /> + <ldelema sig="J" /> + <ldarg_2 /> + <call type="System.Threading.Interlocked" name="Add" sig="System.Int64&;System.Int64" /> <ret /> </body> </method> |