From d50c16456e503a46580b7cf83e5a20186c99932c Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Wed, 4 Jul 2018 02:55:47 +0300 Subject: [PATCH] Fix MIPS atomics. --- runtime/src/main/cpp/Atomic.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/runtime/src/main/cpp/Atomic.cpp b/runtime/src/main/cpp/Atomic.cpp index 8b7b24be27e..4ec1f685f92 100644 --- a/runtime/src/main/cpp/Atomic.cpp +++ b/runtime/src/main/cpp/Atomic.cpp @@ -58,7 +58,21 @@ KLong Kotlin_AtomicLong_addAndGet(KRef thiz, KLong delta) { } KLong Kotlin_AtomicLong_compareAndSwap(KRef thiz, KLong expectedValue, KLong newValue) { +#ifdef __mips + // Potentially huge performance penalty, but correct. + // TODO: reconsider, once target MIPS can do proper 64-bit CAS. + static int lock = 0; + while (compareAndSwap(&lock, 0, 1) != 0); + KLong* address = reinterpret_cast(thiz + 1); + KLong old = *address; + if (old == expectedValue) { + *address = newValue; + } + compareAndSwap(&lock, 1, 0); + return old; +#else return compareAndSwapImpl(thiz, expectedValue, newValue); +#endif } KNativePtr Kotlin_AtomicNativePtr_compareAndSwap(KRef thiz, KNativePtr expectedValue, KNativePtr newValue) { @@ -87,4 +101,4 @@ OBJ_GETTER(Kotlin_AtomicReference_get, KRef thiz) { RETURN_RESULT_OF(ReadRefLocked, &ref->value_, &ref->lock_); } -} // extern "C" \ No newline at end of file +} // extern "C"