Skip to content

Commit ce6cc05

Browse files
Update internal atomic wrapper (#104)
* Add missing getAndUpdate fun Add missing return types Add couple of inlines to high-order functions Put declarations on new line * Add more restrictive gradle config * Adjust Traverse docs Co-authored-by: Rachel M. Carmena <[email protected]>
1 parent d34d65f commit ce6cc05

File tree

5 files changed

+56
-28
lines changed

5 files changed

+56
-28
lines changed

arrow-libs/core/arrow-core-data/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ plugins {
1212
apply from: "$SUBPROJECT_CONF"
1313
apply from: "$DOC_CONF"
1414
apply from: "$PUBLISH_CONF"
15+
apply plugin: 'kotlinx-atomicfu'
1516

1617
dependencies {
1718
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$KOTLIN_VERSION"

arrow-libs/core/arrow-core-data/src/main/kotlin/arrow/typeclasses/Traverse.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,7 @@ import arrow.core.ValidatedNel
352352
* import arrow.fx.IO
353353
* import arrow.fx.extensions.fx
354354
* import arrow.fx.extensions.io.concurrent.parTraverse
355-
* import arrow.fx.extensions.runBlocking
356-
* import arrow.unsafe
355+
* import arrow.fx.unsafeRunSync
357356
*
358357
* interface Profile
359358
* interface User
@@ -374,7 +373,7 @@ import arrow.core.ValidatedNel
374373
* }
375374
* //sampleEnd
376375
* fun main() {
377-
* unsafe { runBlocking { program() } }
376+
* program().unsafeRunSync()
378377
* }
379378
* ```
380379
*

arrow-libs/core/arrow-core/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ plugins {
1212
apply from: "$SUBPROJECT_CONF"
1313
apply from: "$DOC_CONF"
1414
apply from: "$PUBLISH_CONF"
15+
apply plugin: 'kotlinx-atomicfu'
1516

1617
dependencies {
1718
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$KOTLIN_VERSION"

arrow-libs/core/arrow-core/src/main/kotlin/arrow/core/internal/atomic.kt

+52-21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package arrow.core.internal
22

33
import kotlinx.atomicfu.atomic
44
import kotlinx.atomicfu.updateAndGet
5+
import kotlinx.atomicfu.getAndUpdate
56

67
/**
78
* Internal wrapper for Atomic-FU Atomics to be used as local variables
@@ -15,15 +16,23 @@ class AtomicRefW<A>(a: A) {
1516
}
1617
get() = atomicRef.value
1718

18-
fun getAndSet(a: A) = atomicRef.getAndSet(a)
19+
fun getAndSet(a: A): A =
20+
atomicRef.getAndSet(a)
1921

20-
fun updateAndGet(function: (A) -> A) = atomicRef.updateAndGet(function)
22+
fun updateAndGet(function: (A) -> A): A =
23+
atomicRef.updateAndGet(function)
2124

22-
fun compareAndSet(expect: A, update: A) = atomicRef.compareAndSet(expect, update)
25+
fun getAndUpdate(f: (A) -> A): A =
26+
atomicRef.getAndUpdate(f)
2327

24-
fun lazySet(a: A) = atomicRef.lazySet(a)
28+
fun compareAndSet(expect: A, update: A): Boolean =
29+
atomicRef.compareAndSet(expect, update)
2530

26-
override fun toString(): String = value.toString()
31+
fun lazySet(a: A): Unit =
32+
atomicRef.lazySet(a)
33+
34+
override fun toString(): String =
35+
value.toString()
2736
}
2837

2938
class AtomicBooleanW(a: Boolean) {
@@ -35,15 +44,23 @@ class AtomicBooleanW(a: Boolean) {
3544
}
3645
get() = atomicRef.value
3746

38-
fun getAndSet(a: Boolean) = atomicRef.getAndSet(a)
47+
fun getAndSet(a: Boolean): Boolean =
48+
atomicRef.getAndSet(a)
49+
50+
fun updateAndGet(function: (Boolean) -> Boolean): Boolean =
51+
atomicRef.updateAndGet(function)
3952

40-
fun updateAndGet(function: (Boolean) -> Boolean) = atomicRef.updateAndGet(function)
53+
fun getAndUpdate(f: (Boolean) -> Boolean): Boolean =
54+
atomicRef.getAndUpdate(f)
4155

42-
fun compareAndSet(expect: Boolean, update: Boolean) = atomicRef.compareAndSet(expect, update)
56+
fun compareAndSet(expect: Boolean, update: Boolean): Boolean =
57+
atomicRef.compareAndSet(expect, update)
4358

44-
fun lazySet(a: Boolean) = atomicRef.lazySet(a)
59+
fun lazySet(a: Boolean): Unit =
60+
atomicRef.lazySet(a)
4561

46-
override fun toString(): String = value.toString()
62+
override fun toString(): String =
63+
value.toString()
4764
}
4865

4966
class AtomicIntW(a: Int) {
@@ -55,25 +72,39 @@ class AtomicIntW(a: Int) {
5572
}
5673
get() = atomicRef.value
5774

58-
fun getAndSet(a: Int) = atomicRef.getAndSet(a)
75+
fun getAndSet(a: Int): Int =
76+
atomicRef.getAndSet(a)
77+
78+
fun getAndAdd(delta: Int): Int =
79+
atomicRef.getAndAdd(delta)
5980

60-
fun getAndAdd(delta: Int) = atomicRef.getAndAdd(delta)
81+
fun addAndGet(delta: Int): Int =
82+
atomicRef.addAndGet(delta)
6183

62-
fun addAndGet(delta: Int) = atomicRef.addAndGet(delta)
84+
fun getAndIncrement(): Int =
85+
atomicRef.getAndIncrement()
6386

64-
fun getAndIncrement() = atomicRef.getAndIncrement()
87+
fun getAndDecrement(): Int =
88+
atomicRef.getAndDecrement()
6589

66-
fun getAndDecrement() = atomicRef.getAndDecrement()
90+
fun incrementAndGet(): Int =
91+
atomicRef.incrementAndGet()
6792

68-
fun incrementAndGet() = atomicRef.incrementAndGet()
93+
fun decrementAndGet(): Int =
94+
atomicRef.decrementAndGet()
6995

70-
fun decrementAndGet() = atomicRef.decrementAndGet()
96+
fun updateAndGet(function: (Int) -> Int): Int =
97+
atomicRef.updateAndGet(function)
7198

72-
fun updateAndGet(function: (Int) -> Int) = atomicRef.updateAndGet(function)
99+
fun getAndUpdate(f: (Int) -> Int): Int =
100+
atomicRef.getAndUpdate(f)
73101

74-
fun compareAndSet(expect: Int, update: Int) = atomicRef.compareAndSet(expect, update)
102+
fun compareAndSet(expect: Int, update: Int): Boolean =
103+
atomicRef.compareAndSet(expect, update)
75104

76-
fun lazySet(a: Int) = atomicRef.lazySet(a)
105+
fun lazySet(a: Int): Unit =
106+
atomicRef.lazySet(a)
77107

78-
override fun toString(): String = value.toString()
108+
override fun toString(): String =
109+
value.toString()
79110
}

arrow-libs/core/build.gradle

-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ plugins {
2424

2525
apply from: "$GENERIC_CONF"
2626

27-
subprojects {
28-
apply plugin: 'kotlinx-atomicfu'
29-
}
30-
3127
configure(subprojects - project("arrow-meta")) {
3228
apply plugin: 'ru.vyarus.animalsniffer'
3329
apply plugin: 'java'

0 commit comments

Comments
 (0)