Skip to content

Commit

Permalink
Inline AtomicBoolean (#3240)
Browse files Browse the repository at this point in the history
* Inline AtomicBoolean

* Update API files

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
serras and github-actions[bot] authored Oct 29, 2023
1 parent 1fd3cbf commit a6f1e73
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
33 changes: 21 additions & 12 deletions arrow-libs/core/arrow-atomic/api/arrow-atomic.api
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
public final class arrow/atomic/AtomicBoolean {
public fun <init> (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 {
Expand Down
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 a6f1e73

Please sign in to comment.