Skip to content

Commit

Permalink
Inline AtomicBoolean
Browse files Browse the repository at this point in the history
  • Loading branch information
serras committed Oct 24, 2023
1 parent 6955d01 commit b0935ef
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
4 changes: 4 additions & 0 deletions arrow-libs/core/arrow-atomic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ tasks.withType<KotlinCompile>().configureEach {
freeCompilerArgs = freeCompilerArgs + "-Xexpect-actual-classes"
}
}

tasks.test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package arrow.atomic

public class AtomicBoolean(value: Boolean) {
private val inner = AtomicInt(value.toInt())
import kotlin.jvm.JvmInline

@JvmInline
public value class AtomicBoolean private constructor(private val inner: AtomicInt) {
public constructor(value: Boolean): this(AtomicInt(value.toInt()))

public var value: Boolean
get() = inner.value != 0
get() = inner.value.toBoolean()
set(value) {
inner.value = value.toInt()
}
Expand All @@ -18,12 +21,15 @@ public class AtomicBoolean(value: Boolean) {
}

public fun getAndSet(value: Boolean): Boolean =
inner.getAndSet(value.toInt()) == 1

private fun Boolean.toInt(): Int =
if (this) 1 else 0
inner.getAndSet(value.toInt()).toBoolean()
}

private inline fun Boolean.toInt(): Int =
if (this) 1 else 0

private inline fun Int.toBoolean(): Boolean =
this != 0


/**
* Infinite loop that reads this atomic variable and performs the specified [action] on its value.
Expand Down

0 comments on commit b0935ef

Please sign in to comment.