-
Notifications
You must be signed in to change notification settings - Fork 448
/
AtomicLong.kt
84 lines (69 loc) · 2.53 KB
/
AtomicLong.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
@file:OptIn(ExperimentalContracts::class)
package arrow.atomic
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
public expect class AtomicLong(initialValue: Long) {
public fun get(): Long
public fun set(newValue: Long)
public fun getAndSet(value: Long): Long
public fun incrementAndGet(): Long
public fun decrementAndGet(): Long
public fun addAndGet(delta: Long): Long
public fun compareAndSet(expected: Long, new: Long): Boolean
}
public var AtomicLong.value: Long
get() = get()
set(value) {
set(value)
}
/**
* Infinite loop that reads this atomic variable and performs the specified [action] on its value.
*/
public inline fun AtomicLong.loop(action: (Long) -> Unit): Nothing {
contract { callsInPlace(action, InvocationKind.AT_LEAST_ONCE) }
do { action(value) } while(true)
}
public inline fun AtomicLong.tryUpdate(function: (Long) -> Long): Boolean {
contract { callsInPlace(function, InvocationKind.EXACTLY_ONCE) }
return tryUpdate(function) { _, _ -> }
}
public inline fun AtomicLong.update(function: (Long) -> Long) {
contract { callsInPlace(function, InvocationKind.AT_LEAST_ONCE) }
update(function) { _, _ -> }
}
/**
* Updates variable atomically using the specified [function] of its value and returns its old value.
*/
public inline fun AtomicLong.getAndUpdate(function: (Long) -> Long): Long {
contract { callsInPlace(function, InvocationKind.AT_LEAST_ONCE) }
return update(function) { old, _ -> old }
}
/**
* Updates variable atomically using the specified [function] of its value and returns its new value.
*/
public inline fun AtomicLong.updateAndGet(function: (Long) -> Long): Long {
contract { callsInPlace(function, InvocationKind.AT_LEAST_ONCE) }
return update(function) { _, new -> new }
}
@PublishedApi
internal inline fun <R> AtomicLong.update(function: (Long) -> Long, transform: (old: Long, new: Long) -> R): R {
contract {
callsInPlace(function, InvocationKind.AT_LEAST_ONCE)
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
}
loop { cur ->
val upd = function(value)
if(compareAndSet(cur, upd)) return transform(cur, upd)
}
}
@PublishedApi
internal inline fun AtomicLong.tryUpdate(function: (Long) -> Long, onUpdated: (old: Long, new: Long) -> Unit): Boolean {
contract {
callsInPlace(function, InvocationKind.EXACTLY_ONCE)
callsInPlace(onUpdated, InvocationKind.AT_MOST_ONCE)
}
val cur = value
val upd = function(cur)
return compareAndSet(cur, upd).also { if (it) onUpdated(cur, upd) }
}