Skip to content

Commit 3123e27

Browse files
committed
Atomicfu delegated properties
1 parent 543d0e6 commit 3123e27

13 files changed

+22
-44
lines changed

kotlinx-coroutines-core/api/kotlinx-coroutines-core.api

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,6 @@ public class kotlinx/coroutines/JobSupport : kotlinx/coroutines/ChildJob, kotlin
446446
public final fun getKey ()Lkotlin/coroutines/CoroutineContext$Key;
447447
protected final fun getOnAwaitInternal ()Lkotlinx/coroutines/selects/SelectClause1;
448448
public final fun getOnJoin ()Lkotlinx/coroutines/selects/SelectClause0;
449-
public fun getParent ()Lkotlinx/coroutines/Job;
450449
protected fun handleJobException (Ljava/lang/Throwable;)Z
451450
protected final fun initParentJob (Lkotlinx/coroutines/Job;)V
452451
public final fun invokeOnCompletion (Lkotlin/jvm/functions/Function1;)Lkotlinx/coroutines/DisposableHandle;
@@ -464,6 +463,7 @@ public class kotlinx/coroutines/JobSupport : kotlinx/coroutines/ChildJob, kotlin
464463
public final fun parentCancelled (Lkotlinx/coroutines/ParentJob;)V
465464
public fun plus (Lkotlin/coroutines/CoroutineContext;)Lkotlin/coroutines/CoroutineContext;
466465
public fun plus (Lkotlinx/coroutines/Job;)Lkotlinx/coroutines/Job;
466+
public final fun registerSelectClause0 (Lkotlinx/coroutines/selects/SelectInstance;Lkotlin/jvm/functions/Function1;)V
467467
public final fun start ()Z
468468
protected final fun toCancellationException (Ljava/lang/Throwable;Ljava/lang/String;)Ljava/util/concurrent/CancellationException;
469469
public static synthetic fun toCancellationException$default (Lkotlinx/coroutines/JobSupport;Ljava/lang/Throwable;Ljava/lang/String;ILjava/lang/Object;)Ljava/util/concurrent/CancellationException;

kotlinx-coroutines-core/common/src/Await.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ private class AwaitAll<T>(private val deferreds: Array<out Deferred<T>>) {
104104
lateinit var handle: DisposableHandle
105105

106106
private val _disposer = atomic<DisposeHandlersOnCancel?>(null)
107-
var disposer: DisposeHandlersOnCancel?
108-
get() = _disposer.value
109-
set(value) { _disposer.value = value }
107+
var disposer: DisposeHandlersOnCancel? by _disposer
110108

111109
override fun invoke(cause: Throwable?) {
112110
if (cause != null) {

kotlinx-coroutines-core/common/src/CancellableContinuationImpl.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ internal open class CancellableContinuationImpl<in T>(
9393
private val parentHandle: DisposableHandle?
9494
get() = _parentHandle.value
9595

96-
internal val state: Any? get() = _state.value
96+
internal val state: Any? by _state
9797

9898
public override val isActive: Boolean get() = state is NotCompleted
9999

kotlinx-coroutines-core/common/src/CompletionState.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ internal open class CompletedExceptionally(
4444
handled: Boolean = false
4545
) {
4646
private val _handled = atomic(handled)
47-
val handled: Boolean get() = _handled.value
47+
val handled: Boolean by _handled
4848
fun makeHandled(): Boolean = _handled.compareAndSet(false, true)
4949
override fun toString(): String = "$classSimpleName[$cause]"
5050
}

kotlinx-coroutines-core/common/src/EventLoop.common.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay {
185185
private val _delayed = atomic<DelayedTaskQueue?>(null)
186186

187187
private val _isCompleted = atomic(false)
188-
private var isCompleted
189-
get() = _isCompleted.value
190-
set(value) { _isCompleted.value = value }
188+
private var isCompleted: Boolean by _isCompleted
191189

192190
override val isEmpty: Boolean get() {
193191
if (!isUnconfinedQueueEmpty) return false

kotlinx-coroutines-core/common/src/JobSupport.kt

+4-12
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
127127
private val _state = atomic<Any?>(if (active) EMPTY_ACTIVE else EMPTY_NEW)
128128

129129
private val _parentHandle = atomic<ChildHandle?>(null)
130-
internal var parentHandle: ChildHandle?
131-
get() = _parentHandle.value
132-
set(value) { _parentHandle.value = value }
130+
internal var parentHandle: ChildHandle? by _parentHandle
133131

134132
override val parent: Job?
135133
get() = parentHandle?.parent
@@ -1080,19 +1078,13 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
10801078
rootCause: Throwable?
10811079
) : SynchronizedObject(), Incomplete {
10821080
private val _isCompleting = atomic(isCompleting)
1083-
var isCompleting: Boolean
1084-
get() = _isCompleting.value
1085-
set(value) { _isCompleting.value = value }
1081+
var isCompleting: Boolean by _isCompleting
10861082

10871083
private val _rootCause = atomic(rootCause)
1088-
var rootCause: Throwable? // NOTE: rootCause is kept even when SEALED
1089-
get() = _rootCause.value
1090-
set(value) { _rootCause.value = value }
1084+
var rootCause: Throwable? by _rootCause // NOTE: rootCause is kept even when SEALED
10911085

10921086
private val _exceptionsHolder = atomic<Any?>(null)
1093-
private var exceptionsHolder: Any? // Contains null | Throwable | ArrayList | SEALED
1094-
get() = _exceptionsHolder.value
1095-
set(value) { _exceptionsHolder.value = value }
1087+
private var exceptionsHolder: Any? by _exceptionsHolder // Contains null | Throwable | ArrayList | SEALED
10961088

10971089
// Note: cannot be modified when sealed
10981090
val isSealed: Boolean get() = exceptionsHolder === SEALED

kotlinx-coroutines-core/common/src/channels/ArrayBroadcastChannel.kt

+4-12
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,13 @@ internal class ArrayBroadcastChannel<E>(
5151
// head, tail, and size are guarded by bufferLock
5252

5353
private val _head = atomic(0L)
54-
private var head: Long // do modulo on use of head
55-
get() = _head.value
56-
set(value) { _head.value = value }
54+
private var head: Long by _head // do modulo on use of head
5755

5856
private val _tail = atomic(0L)
59-
private var tail: Long // do modulo on use of tail
60-
get() = _tail.value
61-
set(value) { _tail.value = value }
57+
private var tail: Long by _tail // do modulo on use of tail
6258

6359
private val _size = atomic(0)
64-
private var size: Int
65-
get() = _size.value
66-
set(value) { _size.value = value }
60+
private var size: Int by _size
6761

6862
@Suppress("DEPRECATION")
6963
private val subscribers = subscriberList<Subscriber<E>>()
@@ -196,9 +190,7 @@ internal class ArrayBroadcastChannel<E>(
196190
private val subLock = ReentrantLock()
197191

198192
private val _subHead = atomic(0L)
199-
var subHead: Long // guarded by subLock
200-
get() = _subHead.value
201-
set(value) { _subHead.value = value }
193+
var subHead: Long by _subHead // guarded by subLock
202194

203195
override val isBufferAlwaysEmpty: Boolean get() = false
204196
override val isBufferEmpty: Boolean get() = subHead >= broadcastChannel.tail

kotlinx-coroutines-core/common/src/internal/Atomic.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public abstract class AtomicOp<in T> : OpDescriptor() {
5656
private val _consensus = atomic<Any?>(NO_DECISION)
5757

5858
// Returns NO_DECISION when there is not decision yet
59-
val consensus: Any? get() = _consensus.value
59+
val consensus: Any? by _consensus
6060

6161
val isDecided: Boolean get() = _consensus.value !== NO_DECISION
6262

kotlinx-coroutines-core/common/src/internal/ConcurrentLinkedList.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ internal abstract class ConcurrentLinkedListNode<N : ConcurrentLinkedListNode<N>
9595
// Pointer to the previous node, updates in [remove] function.
9696
private val _prev = atomic(prev)
9797

98-
private val nextOrClosed get() = _next.value
98+
private val nextOrClosed: Any? by _next
9999

100100
/**
101101
* Returns the next segment or `null` of the one does not exist,
@@ -122,7 +122,7 @@ internal abstract class ConcurrentLinkedListNode<N : ConcurrentLinkedListNode<N>
122122
*/
123123
val isTail: Boolean get() = next == null
124124

125-
val prev: N? get() = _prev.value
125+
val prev: N? by _prev
126126

127127
/**
128128
* Cleans the pointer to the previous node.

kotlinx-coroutines-core/common/test/channels/ChannelUndeliveredElementTest.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ class ChannelUndeliveredElementTest : TestBase() {
116116
private class Resource(val value: String) {
117117
private val _cancelled = atomic(false)
118118

119-
val isCancelled: Boolean
120-
get() = _cancelled.value
119+
val isCancelled: Boolean by _cancelled
121120

122121
fun cancel() {
123122
check(!_cancelled.getAndSet(true)) { "Already cancelled" }

kotlinx-coroutines-core/concurrent/src/internal/LockFreeLinkedList.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public actual open class LockFreeLinkedListNode {
322322
queue.correctPrev(op) // queue head is never removed, so null result can only mean RETRY_ATOMIC
323323

324324
private val _affectedNode = atomic<Node?>(null)
325-
final override val affectedNode: Node? get() = _affectedNode.value
325+
final override val affectedNode: Node? by _affectedNode
326326
final override val originalNext: Node get() = queue
327327

328328
override fun retry(affected: Node, next: Any): Boolean = next !== queue
@@ -368,8 +368,8 @@ public actual open class LockFreeLinkedListNode {
368368
}
369369
}
370370

371-
final override val affectedNode: Node? get() = _affectedNode.value
372-
final override val originalNext: Node? get() = _originalNext.value
371+
final override val affectedNode: Node? by _affectedNode
372+
final override val originalNext: Node? by _originalNext
373373

374374
// check node predicates here, must signal failure if affect is not of type T
375375
protected override fun failure(affected: Node): Any? =

kotlinx-coroutines-core/jvm/src/debug/internal/ConcurrentWeakMap.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ internal class ConcurrentWeakMap<K : Any, V: Any>(
2222
private val core = atomic(Core(MIN_CAPACITY))
2323
private val weakRefQueue: ReferenceQueue<K>? = if (weakRefQueue) ReferenceQueue() else null
2424

25-
override val size: Int
26-
get() = _size.value
25+
override val size: Int by _size
2726

2827
private fun decrementSize() { _size.decrementAndGet() }
2928

kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ internal class CoroutineScheduler(
295295

296296
// This is used a "stop signal" for close and shutdown functions
297297
private val _isTerminated = atomic(false)
298-
val isTerminated: Boolean get() = _isTerminated.value
298+
val isTerminated: Boolean by _isTerminated
299299

300300
companion object {
301301
// A symbol to mark workers that are not in parkedWorkersStack

0 commit comments

Comments
 (0)