@@ -9,7 +9,7 @@ import kotlinx.coroutines.*
9
9
10
10
// returns the first segment `s` with `s.id >= id` or `CLOSED`
11
11
// 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 > {
13
13
// Go through `next` references and add new segments if needed,
14
14
// similarly to the `push` in the Michael-Scott queue algorithm.
15
15
// 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
59
59
* returns the segment `s` with `s.id >= id` or `CLOSED` if all the segments in this linked list have lower `id`
60
60
* and the list is closed.
61
61
*/
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 > {
63
63
while (true ) {
64
64
val s = startFrom.findSegmentInternal(id, createNewSegment)
65
65
if (s.isClosed || moveForward(s.segment)) return s
@@ -87,14 +87,15 @@ internal fun <S : Segment<S>> S.close(): S {
87
87
* Essentially, this is a node in the Michael-Scott queue algorithm, but with
88
88
* maintaining [prev] pointer for efficient [remove] implementation.
89
89
*/
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 ) {
91
91
// Pointer to the next segment, updates similarly to the Michael-Scott queue algorithm.
92
92
private val _next = atomic<Any ?>(null )
93
93
val nextOrClosed: NextSegmentOrClosed <S > get() = NextSegmentOrClosed (_next .value)
94
94
fun trySetNext (value : S ): Boolean = _next .compareAndSet(null , value)
95
95
96
96
// Pointer to the previous segment, updates in [remove] function.
97
97
val prev = atomic(prev)
98
+
98
99
/* *
99
100
* Cleans the pointer to the previous segment.
100
101
*/
@@ -119,6 +120,7 @@ internal abstract class Segment<S: Segment<S>>(val id: Long, prev: S?, pointers:
119
120
120
121
// increments the number of pointers if this segment is not logically removed\
121
122
inline fun tryIncPointers () = cleanedAndPointers.addConditionally(1 shl POINTERS_SHIFT ) { it != maxSlots }
123
+
122
124
// returns `true` if this segment is logically removed after the decrement
123
125
inline fun decPointers () = cleanedAndPointers.addAndGet(- (1 shl POINTERS_SHIFT )) == maxSlots
124
126
@@ -188,19 +190,19 @@ internal abstract class Segment<S: Segment<S>>(val id: Long, prev: S?, pointers:
188
190
}
189
191
190
192
private inline fun AtomicInt.addConditionally (delta : Int , condition : (cur: Int ) -> Boolean ): Boolean {
191
- while (true ) {
193
+ while (true ) {
192
194
val cur = this .value
193
195
if (! condition(cur)) return false
194
196
if (this .compareAndSet(cur, cur + delta)) return true
195
197
}
196
198
}
197
199
198
- internal inline class SegmentOrClosed <S : Segment <S >>(private val value : Any? ) {
200
+ internal inline class SegmentOrClosed <S : Segment <S >>(private val value : Any? ) {
199
201
val isClosed: Boolean get() = value == = CLOSED
200
202
val segment: S get() = if (value == = CLOSED ) error(" Does not contain segment" ) else value as S
201
203
}
202
204
203
- internal inline class NextSegmentOrClosed <S : Segment <S >>(private val value : Any? ) {
205
+ internal inline class NextSegmentOrClosed <S : Segment <S >>(private val value : Any? ) {
204
206
val isClosed: Boolean get() = value == = CLOSED
205
207
val segment: S ? get() = if (isClosed) null else value as S ?
206
208
}
0 commit comments