Skip to content

Commit 02c3fd2

Browse files
committed
improve the expression of the dequeueing algorithm
1 parent e414f45 commit 02c3fd2

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

Sources/ReactiveStreams/stream-post.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,13 @@ open class PostBox<Value>: EventStream<Value>
121121
#endif
122122

123123
let requested = self.requested
124-
if requested <= 0 && CAtomicsLoad(final, .relaxed) == nil { return }
124+
let terminal = Node(storage: CAtomicsLoad(self.final, .relaxed))
125+
if requested <= 0 && terminal == nil { return }
125126

126127
// try to dequeue the next event
127128
let head = self.head
128-
let next = CAtomicsLoad(head.next, .acquire)
129-
130-
if requested <= 0 && CAtomicsLoad(final, .relaxed) != next { return }
131-
132-
if let node = Node(storage: next)
129+
if let node = Node(storage: CAtomicsLoad(head.next, .acquire)),
130+
requested > 0 || node == terminal
133131
{
134132
let event = node.move()
135133
self.head = node
@@ -140,9 +138,9 @@ open class PostBox<Value>: EventStream<Value>
140138
return
141139
}
142140

143-
// Either the queue is empty, or processing is blocked.
144-
// Either way, processing will resume once
145-
// a node has been linked after the current `head`
141+
// The queue is empty, there is no request, or processing is blocked.
142+
// In any case, processing will resume once there is either
143+
// a node linked after the current `head` or a non-zero request.
146144
}
147145

148146
open override func processAdditionalRequest(_ additional: Int64)

0 commit comments

Comments
 (0)