Merge pull request #2802 from BrzVlad/feature-evacuation-opt2
[mono.git] / mono / utils / atomic.c
old mode 100755 (executable)
new mode 100644 (file)
index 7cd884e..86afad5
@@ -17,7 +17,7 @@
 
 #include <pthread.h>
 
-static pthread_mutex_t spin = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t spin G_GNUC_UNUSED = PTHREAD_MUTEX_INITIALIZER;
 
 #define NEED_64BIT_CMPXCHG_FALLBACK
 
@@ -25,21 +25,12 @@ static pthread_mutex_t spin = PTHREAD_MUTEX_INITIALIZER;
 
 #ifdef WAPI_NO_ATOMIC_ASM
 
-static mono_once_t spin_once=MONO_ONCE_INIT;
-
-static void spin_init(void)
-{
-       g_warning("Using non-atomic functions!  Expect race conditions when using process-shared handles!");
-}
-
 gint32 InterlockedCompareExchange(volatile gint32 *dest, gint32 exch,
                                  gint32 comp)
 {
        gint32 old;
        int ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        ret = pthread_mutex_lock(&spin);
@@ -64,8 +55,6 @@ gpointer InterlockedCompareExchangePointer(volatile gpointer *dest,
        gpointer old;
        int ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        ret = pthread_mutex_lock(&spin);
@@ -89,8 +78,6 @@ gint32 InterlockedAdd(volatile gint32 *dest, gint32 add)
        gint32 ret;
        int thr_ret;
 
-       mono_once(&spin_once, spin_init);
-
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -112,8 +99,6 @@ gint64 InterlockedAdd64(volatile gint64 *dest, gint64 add)
        gint64 ret;
        int thr_ret;
 
-       mono_once(&spin_once, spin_init);
-
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -135,8 +120,6 @@ gint32 InterlockedIncrement(volatile gint32 *dest)
        gint32 ret;
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -158,8 +141,6 @@ gint64 InterlockedIncrement64(volatile gint64 *dest)
        gint64 ret;
        int thr_ret;
 
-       mono_once(&spin_once, spin_init);
-
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -181,8 +162,6 @@ gint32 InterlockedDecrement(volatile gint32 *dest)
        gint32 ret;
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -204,8 +183,6 @@ gint64 InterlockedDecrement64(volatile gint64 *dest)
        gint64 ret;
        int thr_ret;
 
-       mono_once(&spin_once, spin_init);
-
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -227,8 +204,6 @@ gint32 InterlockedExchange(volatile gint32 *dest, gint32 exch)
        gint32 ret;
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -250,8 +225,6 @@ gint64 InterlockedExchange64(volatile gint64 *dest, gint64 exch)
        gint64 ret;
        int thr_ret;
 
-       mono_once(&spin_once, spin_init);
-
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -273,8 +246,6 @@ gpointer InterlockedExchangePointer(volatile gpointer *dest, gpointer exch)
        gpointer ret;
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -296,8 +267,6 @@ gint32 InterlockedExchangeAdd(volatile gint32 *dest, gint32 add)
        gint32 ret;
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -319,8 +288,6 @@ gint64 InterlockedExchangeAdd64(volatile gint64 *dest, gint64 add)
        gint64 ret;
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -342,8 +309,6 @@ gint8 InterlockedRead8(volatile gint8 *src)
        gint8 ret;
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -364,8 +329,6 @@ gint16 InterlockedRead16(volatile gint16 *src)
        gint16 ret;
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -386,8 +349,6 @@ gint32 InterlockedRead(volatile gint32 *src)
        gint32 ret;
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -408,8 +369,6 @@ gint64 InterlockedRead64(volatile gint64 *src)
        gint64 ret;
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -430,8 +389,6 @@ gpointer InterlockedReadPointer(volatile gpointer *src)
        gpointer ret;
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -451,8 +408,6 @@ void InterlockedWrite(volatile gint8 *dst, gint8 val)
 {
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -470,8 +425,6 @@ void InterlockedWrite16(volatile gint16 *dst, gint16 val)
 {
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -489,8 +442,6 @@ void InterlockedWrite(volatile gint32 *dst, gint32 val)
 {
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -508,8 +459,6 @@ void InterlockedWrite64(volatile gint64 *dst, gint64 val)
 {
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -527,8 +476,6 @@ void InterlockedWritePointer(volatile gpointer *dst, gpointer val)
 {
        int thr_ret;
        
-       mono_once(&spin_once, spin_init);
-       
        pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
                              (void *)&spin);
        thr_ret = pthread_mutex_lock(&spin);
@@ -556,27 +503,82 @@ InterlockedCompareExchange64(volatile gint64 *dest, gint64 exch, gint64 comp)
        return __sync_val_compare_and_swap (dest, comp, exch);
 }
 
-#elif defined (HAVE_64BIT_CMPXCHG_FALLBACK)
+#elif defined (__arm__) && defined (HAVE_ARMV7) && (defined(TARGET_IOS) || defined(TARGET_WATCHOS) || defined(TARGET_ANDROID))
 
-#ifdef ENABLE_EXTENSION_MODULE
-#include "../../../mono-extensions/mono/utils/atomic.c"
+#if defined (TARGET_IOS) || defined (TARGET_WATCHOS)
+
+#ifndef __clang__
+#error "Not supported."
 #endif
 
-#else
+gint64
+InterlockedCompareExchange64(volatile gint64 *dest, gint64 exch, gint64 comp)
+{
+       return  __sync_val_compare_and_swap (dest, comp, exch);
+}
+
+#elif defined (TARGET_ANDROID)
+
+/* Some Android systems can't find the 64-bit CAS intrinsic at runtime,
+ * so we have to roll our own...
+ */
+
+gint64 InterlockedCompareExchange64(volatile gint64 *dest, gint64 exch, gint64 comp) __attribute__ ((naked));
 
 gint64
 InterlockedCompareExchange64(volatile gint64 *dest, gint64 exch, gint64 comp)
 {
-       gint64 old;
+       __asm__ (
+               "push           {r4, r5, r6, r7}\n"
+               "ldrd           r4, [sp, #16]\n"
+               "dmb            sy\n"
+       "1:\n"
+               "ldrexd         r6, [r0]\n"
+               "cmp            r7, r5\n"
+               "cmpeq          r6, r4\n"
+               "bne            2f\n"
+               "strexd         r1, r2, [r0]\n"
+               "cmp            r1, #0\n"
+               "bne            1b\n"
+       "2:\n"
+               "dmb            sy\n"
+               "mov            r0, r6\n"
+               "mov            r1, r7\n"
+               "pop            {r4, r5, r6, r7}\n"
+               "bx                     lr\n"
+       );
+}
+
+#else
 
-       pthread_mutex_lock (&spin);
+#error "Need a 64-bit CAS fallback!"
 
-       old = *dest;
-       if(old == comp)
-               *dest = exch;
+#endif
 
-       pthread_mutex_unlock (&spin);
-       return old;
+#else
+
+gint64
+InterlockedCompareExchange64(volatile gint64 *dest, gint64 exch, gint64 comp)
+{
+       gint64 old;
+       int ret;
+       
+       pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock,
+                             (void *)&spin);
+       ret = pthread_mutex_lock(&spin);
+       g_assert (ret == 0);
+       
+       old= *dest;
+       if(old==comp) {
+               *dest=exch;
+       }
+       
+       ret = pthread_mutex_unlock(&spin);
+       g_assert (ret == 0);
+       
+       pthread_cleanup_pop (0);
+
+       return(old);
 }
 
 #endif