/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jctools.util;

import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * Why should we resort to using Unsafe?<br>
 * <ol>
 * <li>To construct class fields which allow volatile/ordered/plain access: This requirement is covered by
 * {@link AtomicReferenceFieldUpdater} and similar but their performance is arguably worse than the DIY approach
 * (depending on JVM version) while Unsafe intrinsification is a far lesser challenge for JIT compilers.
 * <li>To construct flavors of {@link AtomicReferenceArray}.
 * <li>Other use cases exist but are not present in this library yet.
 * </ol>
 * 
 * @author nitsanw
 * 
 */
public class UnsafeAccess {
    public static final boolean SUPPORTS_GET_AND_SET;
    public static final Unsafe UNSAFE;
    static {
        try {
            final Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            UNSAFE = (Unsafe) field.get(null);
        } catch (Exception e) {
            SUPPORTS_GET_AND_SET = false;
            throw new RuntimeException(e);
        }
        boolean getAndSetSupport = false;
        try {
            Unsafe.class.getMethod("getAndSetObject", Object.class, Long.TYPE,Object.class);
            getAndSetSupport = true;
        } catch (Exception e) {
        }
        SUPPORTS_GET_AND_SET = getAndSetSupport;
    }

}

@@ -603,16 +603,54 @@
 nsafe;%0A%0A
+import java.lang.reflect.Constructor;%0A
 import j
@@ -1319,17 +1319,16 @@
 %3C/ol%3E%0A *
- 
 %0A * @aut
@@ -1341,17 +1341,16 @@
 tsanw%0A *
- 
 %0A */%0Apub
@@ -1480,16 +1480,41 @@
 tatic %7B%0A
+        Unsafe instance;%0A
         
@@ -1646,22 +1646,24 @@
         
-UNSAFE
+instance
  = (Unsa
@@ -1702,37 +1702,558 @@
 atch (Exception 
-e
+ignored
 ) %7B%0A
+            // Some platforms, notably Android, might not have a sun.misc.Unsafe%0A            // implementation with a private %60theUnsafe%60 static instance. In this%0A            // case we can try and call the default constructor, which proves%0A            // sufficient for Android usage.%0A            try %7B%0A                Constructor%3CUnsafe%3E c = sun.misc.Unsafe.class.getDeclaredConstructor();%0A                c.setAccessible(true);%0A                instance = c.newInstance();%0A            %7D catch (Exception e) %7B%0A    
             SUPP
@@ -2290,16 +2290,20 @@
         
+ 
+   
 throw ne
@@ -2321,24 +2321,38 @@
 ception(e);%0A
+            %7D%0A
         %7D%0A  
@@ -2341,32 +2341,33 @@
     %7D%0A        %7D%0A
+%0A
         boolean 
@@ -2555,33 +2555,39 @@
 atch (Exception 
-e
+ignored
 ) %7B%0A        %7D%0A  
@@ -2576,32 +2576,68 @@
 ed) %7B%0A        %7D%0A
+        %0A        UNSAFE = instance;%0A
         SUPPORTS
@@ -2675,11 +2675,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Patch UnsafeAccess for Android usage