From: jzhou Date: Wed, 18 Jan 2012 01:17:20 +0000 (+0000) Subject: changes to MGC class library X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=5c990dc2f16fb12de3201155cf241f329a712a09;p=IRC.git changes to MGC class library --- diff --git a/Robust/src/ClassLibrary/MGC/gnu/AtomicBoolean.java b/Robust/src/ClassLibrary/MGC/gnu/AtomicBoolean.java index 528304ac..50cb9a2a 100644 --- a/Robust/src/ClassLibrary/MGC/gnu/AtomicBoolean.java +++ b/Robust/src/ClassLibrary/MGC/gnu/AtomicBoolean.java @@ -67,11 +67,17 @@ public class AtomicBoolean implements java.io.Serializable { * the actual value was not equal to the expected value. */ public final boolean compareAndSet(boolean expect, boolean update) { - /*int e = expect ? 1 : 0; + int e = expect ? 1 : 0; int u = update ? 1 : 0; - return unsafe.compareAndSwapInt(this, valueOffset, e, u);*/ - System.out.println("Unimplemented AtomicBoolean.compareAndSet()"); - return false; + synchronized (this) { + if(e == value) { + value = u; + return true; + } else { + return false; + } + } + //return unsafe.compareAndSwapInt(this, valueOffset, e, u); } /** @@ -85,11 +91,17 @@ public class AtomicBoolean implements java.io.Serializable { * @return true if successful. */ public boolean weakCompareAndSet(boolean expect, boolean update) { - /*int e = expect ? 1 : 0; + int e = expect ? 1 : 0; int u = update ? 1 : 0; - return unsafe.compareAndSwapInt(this, valueOffset, e, u);*/ - System.out.println("Unimplemented AtomicBoolean.weakCompareAndSet()"); - return false; + synchronized (this) { + if(e == value) { + value = u; + return true; + } else { + return false; + } + } + //return unsafe.compareAndSwapInt(this, valueOffset, e, u);*/ } /** @@ -109,8 +121,10 @@ public class AtomicBoolean implements java.io.Serializable { */ public final void lazySet(boolean newValue) { int v = newValue ? 1 : 0; + synchronized (this) { + value = v; + } //unsafe.putOrderedInt(this, valueOffset, v); - System.out.println("Unimplemented AtomicBoolean.lazySet()"); } /** diff --git a/Robust/src/ClassLibrary/MGC/gnu/AtomicInteger.java b/Robust/src/ClassLibrary/MGC/gnu/AtomicInteger.java index 2b2854a2..1a1769a6 100644 --- a/Robust/src/ClassLibrary/MGC/gnu/AtomicInteger.java +++ b/Robust/src/ClassLibrary/MGC/gnu/AtomicInteger.java @@ -76,8 +76,10 @@ public class AtomicInteger /*extends Number*/ implements /*java.io.*/Serializabl * @since 1.6 */ public final void lazySet(int newValue) { + synchronized (this) { + value = newValue; + } //unsafe.putOrderedInt(this, valueOffset, newValue); - System.out.println("Unimplemented AtomicInteger.lazySet()!"); } /** @@ -104,8 +106,15 @@ public class AtomicInteger /*extends Number*/ implements /*java.io.*/Serializabl * the actual value was not equal to the expected value. */ public final boolean compareAndSet(int expect, int update) { - System.out.println("Unimplemented AtomicInteger.compareAndSet()!"); - return false; //unsafe.compareAndSwapInt(this, valueOffset, expect, update); + synchronized (this) { + if(expect == value) { + value = update; + return true; + } else { + return false; + } + } + //unsafe.compareAndSwapInt(this, valueOffset, expect, update); } /** @@ -119,8 +128,15 @@ public class AtomicInteger /*extends Number*/ implements /*java.io.*/Serializabl * @return true if successful. */ public final boolean weakCompareAndSet(int expect, int update) { - System.out.println("Unimplemented AtomicInteger.weakCompareAndSet()!"); - return false; //unsafe.compareAndSwapInt(this, valueOffset, expect, update); + synchronized (this) { + if(expect == value) { + value = update; + return true; + } else { + return false; + } + } + //unsafe.compareAndSwapInt(this, valueOffset, expect, update); } /** diff --git a/Robust/src/ClassLibrary/MGC/gnu/AtomicReference.java b/Robust/src/ClassLibrary/MGC/gnu/AtomicReference.java index 207c3124..fbb1c890 100644 --- a/Robust/src/ClassLibrary/MGC/gnu/AtomicReference.java +++ b/Robust/src/ClassLibrary/MGC/gnu/AtomicReference.java @@ -70,8 +70,10 @@ public class AtomicReference/**/ implements /*java.io.*/Serializable { * @since 1.6 */ public final void lazySet(Object/*V*/ newValue) { + synchronized (this) { + value = newValue; + } //unsafe.putOrderedObject(this, valueOffset, newValue); - System.out.println("Unimplemented AtomicReference.lazySet()!"); } /** @@ -83,8 +85,15 @@ public class AtomicReference/**/ implements /*java.io.*/Serializable { * the actual value was not equal to the expected value. */ public final boolean compareAndSet(Object/*V*/ expect, Object/*V*/ update) { + synchronized (this) { + if(expect == value) { + value = update; + return true; + } else { + return false; + } + } //return unsafe.compareAndSwapObject(this, valueOffset, expect, update); - System.out.println("Unimplemented AtomicReference.compareAndSet()!"); } /** @@ -98,8 +107,15 @@ public class AtomicReference/**/ implements /*java.io.*/Serializable { * @return true if successful. */ public final boolean weakCompareAndSet(Object/*V*/ expect, Object/*V*/ update) { - //return unsafe.compareAndSwapObject(this, valueOffset, expect, update); - System.out.println("Unimplemented AtomicReference.weakCompareAndSet()!"); + synchronized (this) { + if(expect == value) { + value = update; + return true; + } else { + return false; + } + } + //return unsafe.compareAndSwapObject(this, valueOffset, expect, update);\ } /**