diff options
author | Alex Rønne Petersen <alexrp@xamarin.com> | 2013-09-06 04:16:21 +0200 |
---|---|---|
committer | Jo Shields <directhex@apebox.org> | 2013-10-23 15:14:33 +0100 |
commit | 95998569b0293376e80e8895d19725f57366ed55 (patch) | |
tree | 7e7c97b98dec35ae6055cde06b3fa6e78c5a7e55 | |
parent | 07d515c192556269183638b3715db29ed2c8e9e3 (diff) | |
download | mono-95998569b0293376e80e8895d19725f57366ed55.tar.gz |
Correct our implementation of Thread.VolatileRead ()/VolatileWrite ().
We have various issues:
* All of the functions assumed that doing volatile loads/stores was
enough, when load-acquire, store-release semantics were actually
needed. This may have worked before purely by chance. We now use
proper memory barriers so that we don't have to hope for the
compiler and CPU to do the right thing.
* Removes the hack for 64-bit quantities on 32-bit systems. This is
no longer needed now that we use explicit memory barriers in
these functions. Also, these functions are not supposed to do
atomic reads/writes in the first place - they're purely about the
memory barrier semantics.
* The VolatileWrite (object&, object) overload was not using the
volatile qualifier at all, thus not getting volatile semantics
as per: http://gcc.gnu.org/onlinedocs/gcc/Volatiles.html
(cherry picked from commit 53398c4641b2dc2f55d577778b13cca8ef223b40)
Conflicts:
mono/metadata/threads.c
-rwxr-xr-x | mono/metadata/threads.c | 56 |
1 files changed, 35 insertions, 21 deletions
diff --git a/mono/metadata/threads.c b/mono/metadata/threads.c index 04d43fc4cc..7a00d1fbb9 100755 --- a/mono/metadata/threads.c +++ b/mono/metadata/threads.c @@ -48,6 +48,7 @@ #include <mono/utils/hazard-pointer.h> #include <mono/utils/mono-tls.h> #include <mono/utils/atomic.h> +#include <mono/utils/mono-memory-model.h> #include <mono/metadata/gc-internal.h> @@ -2395,107 +2396,120 @@ void mono_thread_stop (MonoThread *thread) gint8 ves_icall_System_Threading_Thread_VolatileRead1 (void *ptr) { - return *((volatile gint8 *) (ptr)); + gint8 tmp; + mono_atomic_load_acquire (tmp, gint8, (volatile gint8 *) ptr); + return tmp; } gint16 ves_icall_System_Threading_Thread_VolatileRead2 (void *ptr) { - return *((volatile gint16 *) (ptr)); + gint16 tmp; + mono_atomic_load_acquire (tmp, gint16, (volatile gint16 *) ptr); + return tmp; } gint32 ves_icall_System_Threading_Thread_VolatileRead4 (void *ptr) { - return *((volatile gint32 *) (ptr)); + gint32 tmp; + mono_atomic_load_acquire (tmp, gint32, (volatile gint32 *) ptr); + return tmp; } gint64 ves_icall_System_Threading_Thread_VolatileRead8 (void *ptr) { -#if SIZEOF_VOID_P == 8 - return *((volatile gint64 *) (ptr)); -#else - return InterlockedCompareExchange64 (ptr, 0, 0); /*Must ensure atomicity of the operation. */ -#endif + gint64 tmp; + mono_atomic_load_acquire (tmp, gint64, (volatile gint64 *) ptr); + return tmp; } void * ves_icall_System_Threading_Thread_VolatileReadIntPtr (void *ptr) { - return (void *) *((volatile void **) ptr); + volatile void *tmp; + mono_atomic_load_acquire (tmp, volatile void *, (volatile void **) ptr); + return (void *) tmp; } double ves_icall_System_Threading_Thread_VolatileReadDouble (void *ptr) { - return *((volatile double *) (ptr)); + double tmp; + mono_atomic_load_acquire (tmp, double, (volatile double *) ptr); + return tmp; } float ves_icall_System_Threading_Thread_VolatileReadFloat (void *ptr) { - return *((volatile float *) (ptr)); + float tmp; + mono_atomic_load_acquire (tmp, float, (volatile float *) ptr); + return tmp; } MonoObject* ves_icall_System_Threading_Volatile_Read_T (void *ptr) { - return (MonoObject*)*((volatile MonoObject**)ptr); + volatile MonoObject *tmp; + mono_atomic_load_acquire (tmp, volatile MonoObject *, (volatile MonoObject **) ptr); + return (MonoObject *) tmp; } void ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8 value) { - *((volatile gint8 *) ptr) = value; + mono_atomic_store_release ((volatile gint8 *) ptr, value); } void ves_icall_System_Threading_Thread_VolatileWrite2 (void *ptr, gint16 value) { - *((volatile gint16 *) ptr) = value; + mono_atomic_store_release ((volatile gint16 *) ptr, value); } void ves_icall_System_Threading_Thread_VolatileWrite4 (void *ptr, gint32 value) { - *((volatile gint32 *) ptr) = value; + mono_atomic_store_release ((volatile gint32 *) ptr, value); } void ves_icall_System_Threading_Thread_VolatileWrite8 (void *ptr, gint64 value) { - *((volatile gint64 *) ptr) = value; + mono_atomic_store_release ((volatile gint64 *) ptr, value); } void ves_icall_System_Threading_Thread_VolatileWriteIntPtr (void *ptr, void *value) { - *((volatile void **) ptr) = value; + mono_atomic_store_release ((volatile void **) ptr, value); } void ves_icall_System_Threading_Thread_VolatileWriteObject (void *ptr, void *value) { - mono_gc_wbarrier_generic_store (ptr, value); + mono_atomic_store_release ((volatile MonoObject **) ptr, value); + mono_gc_wbarrier_generic_nostore (ptr); } void ves_icall_System_Threading_Thread_VolatileWriteDouble (void *ptr, double value) { - *((volatile double *) ptr) = value; + mono_atomic_store_release ((volatile double *) ptr, value); } void ves_icall_System_Threading_Thread_VolatileWriteFloat (void *ptr, float value) { - *((volatile float *) ptr) = value; + mono_atomic_store_release ((volatile float *) ptr, value); } void ves_icall_System_Threading_Volatile_Write_T (void *ptr, MonoObject *value) { - *((volatile MonoObject **) ptr) = value; + mono_atomic_store_release ((volatile MonoObject **) ptr, value); mono_gc_wbarrier_generic_nostore (ptr); } |