X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Futils%2Fatomic.c;h=9ab8746f299f98ca783767ec8545aea0bab17928;hb=21656fd89f25a0dfb74d6b4918d389e6314715c8;hp=abea03e41deaad2c13255c53656cc5c8edbc365f;hpb=c17e2527d1500c35939b54a21bf770a7276b1c08;p=mono.git diff --git a/mono/utils/atomic.c b/mono/utils/atomic.c old mode 100755 new mode 100644 index abea03e41de..9ab8746f299 --- a/mono/utils/atomic.c +++ b/mono/utils/atomic.c @@ -12,12 +12,13 @@ #include #include +#include #if defined (WAPI_NO_ATOMIC_ASM) || defined (BROKEN_64BIT_ATOMICS_INTRINSIC) #include -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 +26,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 +56,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 +79,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 +100,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 +121,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 +142,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 +163,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 +184,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 +205,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 +226,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 +247,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 +268,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 +289,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); @@ -337,13 +305,51 @@ gint64 InterlockedExchangeAdd64(volatile gint64 *dest, gint64 add) return(ret); } +gint8 InterlockedRead8(volatile gint8 *src) +{ + gint8 ret; + int thr_ret; + + pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock, + (void *)&spin); + thr_ret = pthread_mutex_lock(&spin); + g_assert (thr_ret == 0); + + ret= *src; + + thr_ret = pthread_mutex_unlock(&spin); + g_assert (thr_ret == 0); + + pthread_cleanup_pop (0); + + return(ret); +} + +gint16 InterlockedRead16(volatile gint16 *src) +{ + gint16 ret; + int thr_ret; + + pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock, + (void *)&spin); + thr_ret = pthread_mutex_lock(&spin); + g_assert (thr_ret == 0); + + ret= *src; + + thr_ret = pthread_mutex_unlock(&spin); + g_assert (thr_ret == 0); + + pthread_cleanup_pop (0); + + return(ret); +} + 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); @@ -364,7 +370,25 @@ 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); + g_assert (thr_ret == 0); + + ret= *src; + + thr_ret = pthread_mutex_unlock(&spin); + g_assert (thr_ret == 0); + + pthread_cleanup_pop (0); + + return(ret); +} + +gpointer InterlockedReadPointer(volatile gpointer *src) +{ + gpointer ret; + int thr_ret; pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock, (void *)&spin); @@ -381,11 +405,43 @@ gint64 InterlockedRead64(volatile gint64 *src) return(ret); } -void InterlockedWrite(volatile gint32 *dst, gint32 val) +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); + g_assert (thr_ret == 0); + + *dst=val; + + thr_ret = pthread_mutex_unlock(&spin); + g_assert (thr_ret == 0); + + pthread_cleanup_pop (0); +} + +void InterlockedWrite16(volatile gint16 *dst, gint16 val) +{ + int thr_ret; + + pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock, + (void *)&spin); + thr_ret = pthread_mutex_lock(&spin); + g_assert (thr_ret == 0); + + *dst=val; + + thr_ret = pthread_mutex_unlock(&spin); + g_assert (thr_ret == 0); + + pthread_cleanup_pop (0); +} + +void InterlockedWrite(volatile gint32 *dst, gint32 val) +{ + int thr_ret; pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock, (void *)&spin); @@ -404,7 +460,22 @@ 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); + g_assert (thr_ret == 0); + + *dst=val; + + thr_ret = pthread_mutex_unlock(&spin); + g_assert (thr_ret == 0); + + pthread_cleanup_pop (0); +} + +void InterlockedWritePointer(volatile gpointer *dst, gpointer val) +{ + int thr_ret; pthread_cleanup_push ((void(*)(void *))pthread_mutex_unlock, (void *)&spin); @@ -445,15 +516,24 @@ 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); - pthread_mutex_lock (&spin); - - old = *dest; - if(old == comp) - *dest = exch; - - pthread_mutex_unlock (&spin); - return old; + return(old); } #endif