Skip to content

Commit 5c33b8d

Browse files
committed
Code style fixes
1 parent 8646c8c commit 5c33b8d

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import kotlinx.coroutines.*
99

1010
// returns the first segment `s` with `s.id >= id` or `CLOSED`
1111
// if all the segments in this linked list have lower `id` and the list is closed for further segment additions.
12-
private inline fun <S: Segment<S>> S.findSegmentInternal(id: Long, createNewSegment: (id: Long, prev: S?) -> S): SegmentOrClosed<S> {
12+
private inline fun <S : Segment<S>> S.findSegmentInternal(id: Long, createNewSegment: (id: Long, prev: S?) -> S): SegmentOrClosed<S> {
1313
// Go through `next` references and add new segments if needed,
1414
// similarly to the `push` in the Michael-Scott queue algorithm.
1515
// The only difference is that `CAS failure` means that the
@@ -59,7 +59,7 @@ private inline fun <S : Segment<S>> AtomicRef<S>.moveForward(to: S): Boolean = l
5959
* returns the segment `s` with `s.id >= id` or `CLOSED` if all the segments in this linked list have lower `id`
6060
* and the list is closed.
6161
*/
62-
internal inline fun <S: Segment<S>> AtomicRef<S>.findSegmentAndMoveForward(id: Long, startFrom: S, createNewSegment: (id: Long, prev: S?) -> S): SegmentOrClosed<S> {
62+
internal inline fun <S : Segment<S>> AtomicRef<S>.findSegmentAndMoveForward(id: Long, startFrom: S, createNewSegment: (id: Long, prev: S?) -> S): SegmentOrClosed<S> {
6363
while (true) {
6464
val s = startFrom.findSegmentInternal(id, createNewSegment)
6565
if (s.isClosed || moveForward(s.segment)) return s
@@ -87,14 +87,15 @@ internal fun <S : Segment<S>> S.close(): S {
8787
* Essentially, this is a node in the Michael-Scott queue algorithm, but with
8888
* maintaining [prev] pointer for efficient [remove] implementation.
8989
*/
90-
internal abstract class Segment<S: Segment<S>>(val id: Long, prev: S?, pointers: Int) {
90+
internal abstract class Segment<S : Segment<S>>(val id: Long, prev: S?, pointers: Int) {
9191
// Pointer to the next segment, updates similarly to the Michael-Scott queue algorithm.
9292
private val _next = atomic<Any?>(null)
9393
val nextOrClosed: NextSegmentOrClosed<S> get() = NextSegmentOrClosed(_next.value)
9494
fun trySetNext(value: S): Boolean = _next.compareAndSet(null, value)
9595

9696
// Pointer to the previous segment, updates in [remove] function.
9797
val prev = atomic(prev)
98+
9899
/**
99100
* Cleans the pointer to the previous segment.
100101
*/
@@ -119,6 +120,7 @@ internal abstract class Segment<S: Segment<S>>(val id: Long, prev: S?, pointers:
119120

120121
// increments the number of pointers if this segment is not logically removed\
121122
inline fun tryIncPointers() = cleanedAndPointers.addConditionally(1 shl POINTERS_SHIFT) { it != maxSlots }
123+
122124
// returns `true` if this segment is logically removed after the decrement
123125
inline fun decPointers() = cleanedAndPointers.addAndGet(-(1 shl POINTERS_SHIFT)) == maxSlots
124126

@@ -188,19 +190,19 @@ internal abstract class Segment<S: Segment<S>>(val id: Long, prev: S?, pointers:
188190
}
189191

190192
private inline fun AtomicInt.addConditionally(delta: Int, condition: (cur: Int) -> Boolean): Boolean {
191-
while(true) {
193+
while (true) {
192194
val cur = this.value
193195
if (!condition(cur)) return false
194196
if (this.compareAndSet(cur, cur + delta)) return true
195197
}
196198
}
197199

198-
internal inline class SegmentOrClosed<S: Segment<S>>(private val value: Any?) {
200+
internal inline class SegmentOrClosed<S : Segment<S>>(private val value: Any?) {
199201
val isClosed: Boolean get() = value === CLOSED
200202
val segment: S get() = if (value === CLOSED) error("Does not contain segment") else value as S
201203
}
202204

203-
internal inline class NextSegmentOrClosed<S: Segment<S>>(private val value: Any?) {
205+
internal inline class NextSegmentOrClosed<S : Segment<S>>(private val value: Any?) {
204206
val isClosed: Boolean get() = value === CLOSED
205207
val segment: S? get() = if (isClosed) null else value as S?
206208
}

kotlinx-coroutines-core/common/src/sync/Semaphore.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private class SemaphoreImpl(private val permits: Int, acquiredPermits: Int) : Se
132132
cur + 1
133133
}
134134

135-
private suspend fun addToQueueAndSuspend() = suspendAtomicCancellableCoroutineReusable<Unit> sc@ { cont ->
135+
private suspend fun addToQueueAndSuspend() = suspendAtomicCancellableCoroutineReusable<Unit> sc@{ cont ->
136136
val curTail = this.tail.value
137137
val enqIdx = enqIdx.getAndIncrement()
138138
val segment = this.tail.findSegmentAndMoveForward(id = enqIdx / SEGMENT_SIZE, startFrom = curTail,
@@ -148,7 +148,7 @@ private class SemaphoreImpl(private val permits: Int, acquiredPermits: Int) : Se
148148

149149
@Suppress("UNCHECKED_CAST")
150150
internal fun resumeNextFromQueue() {
151-
try_again@while (true) {
151+
try_again@ while (true) {
152152
val curHead = this.head.value
153153
val deqIdx = deqIdx.getAndIncrement()
154154
val id = deqIdx / SEGMENT_SIZE
@@ -190,7 +190,7 @@ private class CancelSemaphoreAcquisitionHandler(
190190

191191
private fun createSegment(id: Long, prev: SemaphoreSegment?) = SemaphoreSegment(id, prev, 0)
192192

193-
private class SemaphoreSegment(id: Long, prev: SemaphoreSegment?, pointers: Int): Segment<SemaphoreSegment>(id, prev, pointers) {
193+
private class SemaphoreSegment(id: Long, prev: SemaphoreSegment?, pointers: Int) : Segment<SemaphoreSegment>(id, prev, pointers) {
194194
val acquirers = atomicArrayOfNulls<Any?>(SEGMENT_SIZE)
195195
override val maxSlots: Int get() = SEGMENT_SIZE
196196

kotlinx-coroutines-core/jvm/test/internal/SegmentQueueTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class SegmentQueueTest : TestBase() {
1414
@Test
1515
fun simpleTest() {
1616
val q = SegmentBasedQueue<Int>()
17-
assertEquals( 1, q.numberOfSegments)
17+
assertEquals(1, q.numberOfSegments)
1818
assertEquals(null, q.dequeue())
1919
q.enqueue(1)
2020
assertEquals(1, q.numberOfSegments)

0 commit comments

Comments
 (0)