Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(internal): freeze value if AtomicRef is frozen. #115

Merged
merged 4 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- `neverFlow()` now returns `NeverFlow`.
- `takeUntil`: change `notifier` type to `Flow<Any?>`

- Internal fix for `AtomicRef`: freeze `value` if `AtomicRef` is frozen.

- Support for Apple Silicon targets
- `iosSimulatorArm64`.
- `macosArm64`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

package com.hoc081098.flowext.internal

internal expect class AtomicRef<T>(value: T) {
internal expect class AtomicRef<T>(initialValue: T) {
var value: T

fun compareAndSet(expect: T, update: T): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
* SOFTWARE.
*/

package com.hoc081098.flowext
package com.hoc081098.flowext.internal

import com.hoc081098.flowext.internal.AtomicBoolean
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* MIT License
*
* Copyright (c) 2021-2022 Petrus Nguyễn Thái Học
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.hoc081098.flowext.internal

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertSame
import kotlin.test.assertTrue

class AtomicRefTest {
@Test
fun returns_initial_value_WHEN_created() {
val ref = AtomicRef(VALUE_1)

assertEquals(VALUE_1, ref.value)
}

@Test
fun returns_updated_value() {
val ref = AtomicRef(VALUE_1)

ref.value = VALUE_2

assertEquals(VALUE_2, ref.value)
}

@Test
fun compareAndSet_success() {
val ref = AtomicRef(VALUE_1)
val result = ref.compareAndSet(VALUE_1, VALUE_2)
assertTrue(result)
assertSame(VALUE_2, ref.value)
}

@Test
fun compareAndSet_fail() {
val ref = AtomicRef(VALUE_1)
val result = ref.compareAndSet(VALUE_2, VALUE_3)
assertFalse(result)
assertSame(VALUE_1, ref.value)
}

private companion object {
private const val VALUE_1 = "a"
private const val VALUE_2 = "b"
private const val VALUE_3 = "c"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

package com.hoc081098.flowext.internal

internal actual class AtomicRef<T> actual constructor(actual var value: T) {
internal actual class AtomicRef<T> actual constructor(initialValue: T) {
actual var value: T = initialValue

actual fun compareAndSet(expect: T, update: T): Boolean = if (expect == value) {
value = update
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ package com.hoc081098.flowext.internal

import java.util.concurrent.atomic.AtomicReference as JavaAtomicReference

internal actual class AtomicRef<T> actual constructor(value: T) {
private val atomic = JavaAtomicReference(value)
internal actual class AtomicRef<T> actual constructor(initialValue: T) {
private val atomic = JavaAtomicReference(initialValue)

actual var value: T
get() = atomic.get()
Expand Down
23 changes: 18 additions & 5 deletions src/nativeMain/kotlin/com/hoc081098/flowext/internal/AtomicRef.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,25 @@

package com.hoc081098.flowext.internal

import kotlin.native.concurrent.AtomicReference as NativeAtomicReference
import kotlin.native.concurrent.FreezableAtomicReference
import kotlin.native.concurrent.freeze
import kotlin.native.concurrent.isFrozen

internal actual class AtomicRef<T> actual constructor(value: T) {
private val atomic = NativeAtomicReference(value)
internal actual class AtomicRef<T> actual constructor(initialValue: T) {
private val atomic = FreezableAtomicReference(initialValue)

actual var value: T by atomic::value
actual var value: T
get() = atomic.value
set(value) {
atomic.value = value.freezeIfNeeded()
}

actual fun compareAndSet(expect: T, update: T): Boolean = atomic.compareAndSet(expect, update)
actual fun compareAndSet(expect: T, update: T): Boolean =
atomic.compareAndSet(expect, update.freezeIfNeeded())

private inline fun T.freezeIfNeeded(): T = if (atomic.isFrozen) {
freeze()
} else {
this
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* MIT License
*
* Copyright (c) 2021-2022 Petrus Nguyễn Thái Học
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.hoc081098.flowext.internal

import kotlin.native.concurrent.freeze
import kotlin.native.concurrent.isFrozen
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class AtomicRefNativeTest {
@Test
fun accepts_not_frozen_initial_value() {
AtomicRef(Any())
}

@Test
fun accepts_frozen_initial_value() {
val ref = AtomicRef(Any().freeze())

assertTrue(ref.value.isFrozen)
}

@Test
fun does_not_freeze_initial_value() {
val ref = AtomicRef(Any())

assertFalse(ref.value.isFrozen)
}

@Test
fun freezes_current_value_WHEN_frozen() {
val ref = AtomicRef(Any())

ref.freeze()

assertTrue(ref.value.isFrozen)
}

@Test
fun does_not_freeze_new_value_IF_not_frozen() {
val ref = AtomicRef(Any())

ref.value = Any()

assertFalse(ref.value.isFrozen)
}

@Test
fun freezes_new_value_IF_frozen() {
val ref = AtomicRef(Any())

ref.freeze()
ref.value = Any()

assertTrue(ref.value.isFrozen)
}
}