Skip to content

Commit 95cf856

Browse files
committed
Atomicfu delegated properties
1 parent 35cd6d4 commit 95cf856

File tree

14 files changed

+25
-46
lines changed

14 files changed

+25
-46
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class kotlinx/coroutines/CancellableContinuationImpl : kotlin/coroutines/
6262
public fun getContinuationCancellationCause (Lkotlinx/coroutines/Job;)Ljava/lang/Throwable;
6363
public final fun getResult ()Ljava/lang/Object;
6464
public fun getStackTraceElement ()Ljava/lang/StackTraceElement;
65+
public final fun getState$kotlinx_coroutines_core ()Ljava/lang/Object;
6566
public fun initCancellability ()V
6667
public fun invokeOnCancellation (Lkotlin/jvm/functions/Function1;)V
6768
public fun isActive ()Z
@@ -433,6 +434,7 @@ public class kotlinx/coroutines/JobSupport : kotlinx/coroutines/ChildJob, kotlin
433434
public final fun getCompletionExceptionOrNull ()Ljava/lang/Throwable;
434435
public final fun getKey ()Lkotlin/coroutines/CoroutineContext$Key;
435436
public final fun getOnJoin ()Lkotlinx/coroutines/selects/SelectClause0;
437+
public final fun getParentHandle$kotlinx_coroutines_core ()Lkotlinx/coroutines/ChildHandle;
436438
protected fun handleJobException (Ljava/lang/Throwable;)Z
437439
protected final fun initParentJob (Lkotlinx/coroutines/Job;)V
438440
public final fun invokeOnCompletion (Lkotlin/jvm/functions/Function1;)Lkotlinx/coroutines/DisposableHandle;
@@ -451,6 +453,7 @@ public class kotlinx/coroutines/JobSupport : kotlinx/coroutines/ChildJob, kotlin
451453
public fun plus (Lkotlin/coroutines/CoroutineContext;)Lkotlin/coroutines/CoroutineContext;
452454
public fun plus (Lkotlinx/coroutines/Job;)Lkotlinx/coroutines/Job;
453455
public final fun registerSelectClause0 (Lkotlinx/coroutines/selects/SelectInstance;Lkotlin/jvm/functions/Function1;)V
456+
public final fun setParentHandle$kotlinx_coroutines_core (Lkotlinx/coroutines/ChildHandle;)V
454457
public final fun start ()Z
455458
protected final fun toCancellationException (Ljava/lang/Throwable;Ljava/lang/String;)Ljava/util/concurrent/CancellationException;
456459
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
@@ -74,7 +74,7 @@ internal open class CancellableContinuationImpl<in T>(
7474

7575
private var parentHandle: DisposableHandle? = null
7676

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

7979
public override val isActive: Boolean get() = state is NotCompleted
8080

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
@@ -189,9 +189,7 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay {
189189
private val _delayed = atomic<DelayedTaskQueue?>(null)
190190

191191
private val _isCompleted = atomic(false)
192-
private var isCompleted
193-
get() = _isCompleted.value
194-
set(value) { _isCompleted.value = value }
192+
private var isCompleted: Boolean by _isCompleted
195193

196194
override val isEmpty: Boolean get() {
197195
if (!isUnconfinedQueueEmpty) return false

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

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

131131
private val _parentHandle = atomic<ChildHandle?>(null)
132-
internal var parentHandle: ChildHandle?
133-
get() = _parentHandle.value
134-
set(value) { _parentHandle.value = value }
132+
internal var parentHandle: ChildHandle? by _parentHandle
135133

136134
// ------------ initialization ------------
137135

@@ -1077,19 +1075,13 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
10771075
rootCause: Throwable?
10781076
) : SynchronizedObject(), Incomplete {
10791077
private val _isCompleting = atomic(isCompleting)
1080-
var isCompleting: Boolean
1081-
get() = _isCompleting.value
1082-
set(value) { _isCompleting.value = value }
1078+
var isCompleting: Boolean by _isCompleting
10831079

10841080
private val _rootCause = atomic(rootCause)
1085-
var rootCause: Throwable? // NOTE: rootCause is kept even when SEALED
1086-
get() = _rootCause.value
1087-
set(value) { _rootCause.value = value }
1081+
var rootCause: Throwable? by _rootCause // NOTE: rootCause is kept even when SEALED
10881082

10891083
private val _exceptionsHolder = atomic<Any?>(null)
1090-
private var exceptionsHolder: Any? // Contains null | Throwable | ArrayList | SEALED
1091-
get() = _exceptionsHolder.value
1092-
set(value) { _exceptionsHolder.value = value }
1084+
private var exceptionsHolder: Any? by _exceptionsHolder // Contains null | Throwable | ArrayList | SEALED
10931085

10941086
// Note: cannot be modified when sealed
10951087
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>>()
@@ -217,9 +211,7 @@ internal class ArrayBroadcastChannel<E>(
217211
private val subLock = ReentrantLock()
218212

219213
private val _subHead = atomic(0L)
220-
var subHead: Long // guarded by subLock
221-
get() = _subHead.value
222-
set(value) { _subHead.value = value }
214+
var subHead: Long by _subHead // guarded by subLock
223215

224216
override val isBufferAlwaysEmpty: Boolean get() = false
225217
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
@@ -58,7 +58,7 @@ public abstract class AtomicOp<in T> : OpDescriptor() {
5858
private val _consensus = atomic<Any?>(NO_DECISION)
5959

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

6363
val isDecided: Boolean get() = _consensus.value !== NO_DECISION
6464

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

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

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

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

126-
val prev: N? get() = _prev.value
126+
val prev: N? by _prev
127127

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

kotlinx-coroutines-core/common/src/selects/Select.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,7 @@ internal class SelectBuilderImpl<in R>(
250250

251251
// cancellability support
252252
private val _parentHandle = atomic<DisposableHandle?>(null)
253-
private var parentHandle: DisposableHandle?
254-
get() = _parentHandle.value
255-
set(value) { _parentHandle.value = value }
253+
private var parentHandle: DisposableHandle? by _parentHandle
256254

257255
/* Result state machine
258256

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
@@ -325,7 +325,7 @@ public actual open class LockFreeLinkedListNode {
325325
queue.correctPrev(op) // queue head is never removed, so null result can only mean RETRY_ATOMIC
326326

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

331331
override fun retry(affected: Node, next: Any): Boolean = next !== queue
@@ -371,8 +371,8 @@ public actual open class LockFreeLinkedListNode {
371371
}
372372
}
373373

374-
final override val affectedNode: Node? get() = _affectedNode.value
375-
final override val originalNext: Node? get() = _originalNext.value
374+
final override val affectedNode: Node? by _affectedNode
375+
final override val originalNext: Node? by _originalNext
376376

377377
// check node predicates here, must signal failure if affect is not of type T
378378
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
@@ -23,8 +23,7 @@ internal class ConcurrentWeakMap<K : Any, V: Any>(
2323
private val core = atomic(Core(MIN_CAPACITY))
2424
private val weakRefQueue: ReferenceQueue<K>? = if (weakRefQueue) ReferenceQueue() else null
2525

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

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

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)