From a6f1e7377c4a20055968657f493fc0cae0222e79 Mon Sep 17 00:00:00 2001 From: Alejandro Serrano Date: Sun, 29 Oct 2023 07:38:28 +0100 Subject: [PATCH] Inline `AtomicBoolean` (#3240) * Inline AtomicBoolean * Update API files --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../core/arrow-atomic/api/arrow-atomic.api | 33 ++++++++++++------- .../kotlin/arrow/atomic/AtomicBoolean.kt | 20 +++++++---- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/arrow-libs/core/arrow-atomic/api/arrow-atomic.api b/arrow-libs/core/arrow-atomic/api/arrow-atomic.api index a077cc554d2..3e8e5de6c25 100644 --- a/arrow-libs/core/arrow-atomic/api/arrow-atomic.api +++ b/arrow-libs/core/arrow-atomic/api/arrow-atomic.api @@ -1,19 +1,28 @@ public final class arrow/atomic/AtomicBoolean { - public fun (Z)V - public final fun compareAndSet (ZZ)Z - public final fun get ()Z - public final fun getAndSet (Z)Z - public final fun getValue ()Z - public final fun set (Z)V - public final fun setValue (Z)V + public static final synthetic fun box-impl (Ljava/util/concurrent/atomic/AtomicInteger;)Larrow/atomic/AtomicBoolean; + public static final fun compareAndSet-impl (Ljava/util/concurrent/atomic/AtomicInteger;ZZ)Z + public static fun constructor-impl (Z)Ljava/util/concurrent/atomic/AtomicInteger; + public fun equals (Ljava/lang/Object;)Z + public static fun equals-impl (Ljava/util/concurrent/atomic/AtomicInteger;Ljava/lang/Object;)Z + public static final fun equals-impl0 (Ljava/util/concurrent/atomic/AtomicInteger;Ljava/util/concurrent/atomic/AtomicInteger;)Z + public static final fun get-impl (Ljava/util/concurrent/atomic/AtomicInteger;)Z + public static final fun getAndSet-impl (Ljava/util/concurrent/atomic/AtomicInteger;Z)Z + public static final fun getValue-impl (Ljava/util/concurrent/atomic/AtomicInteger;)Z + public fun hashCode ()I + public static fun hashCode-impl (Ljava/util/concurrent/atomic/AtomicInteger;)I + public static final fun set-impl (Ljava/util/concurrent/atomic/AtomicInteger;Z)V + public static final fun setValue-impl (Ljava/util/concurrent/atomic/AtomicInteger;Z)V + public fun toString ()Ljava/lang/String; + public static fun toString-impl (Ljava/util/concurrent/atomic/AtomicInteger;)Ljava/lang/String; + public final synthetic fun unbox-impl ()Ljava/util/concurrent/atomic/AtomicInteger; } public final class arrow/atomic/AtomicBooleanKt { - public static final fun getAndUpdate (Larrow/atomic/AtomicBoolean;Lkotlin/jvm/functions/Function1;)Z - public static final fun loop (Larrow/atomic/AtomicBoolean;Lkotlin/jvm/functions/Function1;)Ljava/lang/Void; - public static final fun tryUpdate (Larrow/atomic/AtomicBoolean;Lkotlin/jvm/functions/Function1;)Z - public static final fun update (Larrow/atomic/AtomicBoolean;Lkotlin/jvm/functions/Function1;)V - public static final fun updateAndGet (Larrow/atomic/AtomicBoolean;Lkotlin/jvm/functions/Function1;)Z + public static final fun getAndUpdate-NCWaHEY (Ljava/util/concurrent/atomic/AtomicInteger;Lkotlin/jvm/functions/Function1;)Z + public static final fun loop-NCWaHEY (Ljava/util/concurrent/atomic/AtomicInteger;Lkotlin/jvm/functions/Function1;)Ljava/lang/Void; + public static final fun tryUpdate-NCWaHEY (Ljava/util/concurrent/atomic/AtomicInteger;Lkotlin/jvm/functions/Function1;)Z + public static final fun update-NCWaHEY (Ljava/util/concurrent/atomic/AtomicInteger;Lkotlin/jvm/functions/Function1;)V + public static final fun updateAndGet-NCWaHEY (Ljava/util/concurrent/atomic/AtomicInteger;Lkotlin/jvm/functions/Function1;)Z } public final class arrow/atomic/AtomicIntKt { diff --git a/arrow-libs/core/arrow-atomic/src/commonMain/kotlin/arrow/atomic/AtomicBoolean.kt b/arrow-libs/core/arrow-atomic/src/commonMain/kotlin/arrow/atomic/AtomicBoolean.kt index 93e0446b946..cecd7b6f5b0 100644 --- a/arrow-libs/core/arrow-atomic/src/commonMain/kotlin/arrow/atomic/AtomicBoolean.kt +++ b/arrow-libs/core/arrow-atomic/src/commonMain/kotlin/arrow/atomic/AtomicBoolean.kt @@ -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() } @@ -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.