diff --git a/kotlin-native/konan/konan.properties b/kotlin-native/konan/konan.properties index ea3dab2a388..6a05dcf3ea2 100644 --- a/kotlin-native/konan/konan.properties +++ b/kotlin-native/konan/konan.properties @@ -770,7 +770,7 @@ clangNooptFlags.android_arm32 = -O1 targetSysRoot.android_arm32 = target-sysroot-1-android_ndk linkerKonanFlags.android_arm32 = -lm -lc++_static -lc++abi -landroid -llog -latomic runtimeDefinitions.android_arm32 = __ANDROID__ USE_GCC_UNWIND=1 USE_ELF_SYMBOLS=1 ELFSIZE=32 \ - KONAN_ANDROID=1 KONAN_ARM32=1 KONAN_NO_UNALIGNED_ACCESS=1 + KONAN_ANDROID=1 KONAN_ARM32=1 KONAN_NO_UNALIGNED_ACCESS=1 KONAN_NO_64BIT_ATOMIC=1 # Android ARM64, based on NDK. targetToolchain.macos_x64-android_arm64 = target-toolchain-2-osx-android_ndk diff --git a/kotlin-native/runtime/src/main/cpp/Atomic.cpp b/kotlin-native/runtime/src/main/cpp/Atomic.cpp index 2782d03978a..f9d02f8b4f9 100644 --- a/kotlin-native/runtime/src/main/cpp/Atomic.cpp +++ b/kotlin-native/runtime/src/main/cpp/Atomic.cpp @@ -92,14 +92,25 @@ KInt Kotlin_AtomicInt_get(KRef thiz) { return getImpl(thiz); } -KLong Kotlin_AtomicLong_addAndGet(KRef thiz, KLong delta) { - return addAndGetImpl(thiz, delta); -} - #if KONAN_NO_64BIT_ATOMIC static int lock64 = 0; #endif +KLong Kotlin_AtomicLong_addAndGet(KRef thiz, KLong delta) { +#if KONAN_NO_64BIT_ATOMIC + // Potentially huge performance penalty, but correct. + while (compareAndSwap(&lock64, 0, 1) != 0); + volatile KLong* address = getValueLocation(thiz); + KLong old = *address; + KLong newValue = old + delta; + *address = newValue; + compareAndSwap(&lock64, 1, 0); + return newValue; +#else + return addAndGetImpl(thiz, delta); +#endif +} + KLong Kotlin_AtomicLong_compareAndSwap(KRef thiz, KLong expectedValue, KLong newValue) { #if KONAN_NO_64BIT_ATOMIC // Potentially huge performance penalty, but correct.