changes to MGC class library
[IRC.git] / Robust / src / ClassLibrary / MGC / gnu / AtomicBoolean.java
index bd823bd2c232a297741ca46964eb7699d9aa7c50..50cb9a2a44e7819573be04b7bd029a15fcefc6c0 100644 (file)
@@ -4,8 +4,8 @@
  * http://creativecommons.org/licenses/publicdomain
  */
 
-package java.util.concurrent.atomic;
-import sun.misc.Unsafe;
+//package java.util.concurrent.atomic;
+//import sun.misc.Unsafe;
 
 /**
  * A <tt>boolean</tt> value that may be updated atomically. See the
@@ -21,7 +21,7 @@ import sun.misc.Unsafe;
 public class AtomicBoolean implements java.io.Serializable {
     private static final long serialVersionUID = 4654671469794556979L;
     // setup to use Unsafe.compareAndSwapInt for updates
-    private static final Unsafe unsafe = Unsafe.getUnsafe();
+    /*private static final Unsafe unsafe = Unsafe.getUnsafe();
     private static final long valueOffset;
 
     static {
@@ -29,7 +29,7 @@ public class AtomicBoolean implements java.io.Serializable {
         valueOffset = unsafe.objectFieldOffset
             (AtomicBoolean.class.getDeclaredField("value"));
       } catch (Exception ex) { throw new Error(ex); }
-    }
+    }*/
 
     private volatile int value;
 
@@ -69,7 +69,15 @@ public class AtomicBoolean implements java.io.Serializable {
     public final boolean compareAndSet(boolean expect, boolean update) {
         int e = expect ? 1 : 0;
         int u = update ? 1 : 0;
-        return unsafe.compareAndSwapInt(this, valueOffset, e, u);
+        synchronized (this) {
+            if(e == value) {
+               value = u;
+               return true;
+            } else {
+               return false;
+            }
+        }
+        //return unsafe.compareAndSwapInt(this, valueOffset, e, u);
     }
 
     /**
@@ -85,7 +93,15 @@ public class AtomicBoolean implements java.io.Serializable {
     public boolean weakCompareAndSet(boolean expect, boolean update) {
         int e = expect ? 1 : 0;
         int u = update ? 1 : 0;
-        return unsafe.compareAndSwapInt(this, valueOffset, e, u);
+        synchronized (this) {
+            if(e == value) {
+               value = u;
+               return true;
+            } else {
+               return false;
+            }
+        }
+        //return unsafe.compareAndSwapInt(this, valueOffset, e, u);*/
     }
 
     /**
@@ -105,7 +121,10 @@ public class AtomicBoolean implements java.io.Serializable {
      */
     public final void lazySet(boolean newValue) {
         int v = newValue ? 1 : 0;
-        unsafe.putOrderedInt(this, valueOffset, v);
+        synchronized (this) {
+            value = v;
+        }
+        //unsafe.putOrderedInt(this, valueOffset, v);
     }
 
     /**
@@ -126,8 +145,8 @@ public class AtomicBoolean implements java.io.Serializable {
      * Returns the String representation of the current value.
      * @return the String representation of the current value.
      */
-    public String toString() {
+    /*public String toString() {
         return Boolean.toString(get());
-    }
+    }*/
 
 }