changes to MGC class library
authorjzhou <jzhou>
Wed, 18 Jan 2012 01:17:20 +0000 (01:17 +0000)
committerjzhou <jzhou>
Wed, 18 Jan 2012 01:17:20 +0000 (01:17 +0000)
Robust/src/ClassLibrary/MGC/gnu/AtomicBoolean.java
Robust/src/ClassLibrary/MGC/gnu/AtomicInteger.java
Robust/src/ClassLibrary/MGC/gnu/AtomicReference.java

index 528304acfb331753c0c027fc01c08a1af079d54e..50cb9a2a44e7819573be04b7bd029a15fcefc6c0 100644 (file)
@@ -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()");
     }
 
     /**
index 2b2854a2559b4a94547afbf027b28610e23eb073..1a1769a615eff968771945460ee8b4520f1caea7 100644 (file)
@@ -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);
     }
 
     /**
index 207c31243cae5561210b002adf80382458382933..fbb1c890664a983990f8664c43905556bee88fab 100644 (file)
@@ -70,8 +70,10 @@ public class AtomicReference/*<V>*/  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/*<V>*/  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/*<V>*/  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);\
     }
 
     /**