File: LockFreeQueue.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (195 lines) | stat: -rw-r--r-- 6,115 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

// A lock-free concurrent queue implementation adapted from
// M. Michael and M. Scott's 1996 paper [Michael 1996].
//
// [Michael 1996]: https://doi.org/10.1145/248052.248106
//
// While this is a nice illustration of the use of atomic strong references,
// this is a somewhat sloppy implementation of an old algorithm. If you need a
// lock-free queue for actual production use, it would probably be a good idea
// to look at some more recent algorithms before deciding on this one.
//
// Note: because this implementation uses reference counting, we don't need
// to implement a free list to resolve the original algorithm's use-after-free
// problem.

import XCTest
import Dispatch
import Atomics

private var iterations: Int {
  #if SWIFT_ATOMICS_LONG_TESTS
  return 1_000_000
  #else
  return 50_000
  #endif
}

private let nodeCount = ManagedAtomic<Int>(0)

class LockFreeQueue<Element> {
  final class Node: AtomicReference {
    let next: ManagedAtomic<Node?>
    var value: Element?

    init(value: Element?, next: Node?) {
      self.value = value
      self.next = ManagedAtomic(next)
      nodeCount.wrappingIncrement(ordering: .relaxed)
    }

    deinit {
      var values = 0
      // Prevent stack overflow when reclaiming a long queue
      var node = self.next.exchange(nil, ordering: .relaxed)
      while node != nil && isKnownUniquelyReferenced(&node) {
        let next = node!.next.exchange(nil, ordering: .relaxed)
        withExtendedLifetime(node) {
          values += 1
        }
        node = next
      }
      if values > 0 {
        print(values)
      }
      nodeCount.wrappingDecrement(ordering: .relaxed)
    }
  }

  let head: ManagedAtomic<Node>
  let tail: ManagedAtomic<Node>

  // Used to distinguish removed nodes from active nodes with a nil `next`.
  let marker = Node(value: nil, next: nil)

  init() {
    let dummy = Node(value: nil, next: nil)
    self.head = ManagedAtomic(dummy)
    self.tail = ManagedAtomic(dummy)
  }

  func enqueue(_ newValue: Element) {
    let new = Node(value: newValue, next: nil)

    var tail = self.tail.load(ordering: .acquiring)
    while true {
      let next = tail.next.load(ordering: .acquiring)
      if tail === marker || next === marker {
        // The node we loaded has been unlinked by a dequeue on another thread.
        // Try again.
        tail = self.tail.load(ordering: .acquiring)
        continue
      }
      if let next = next {
        // Assist competing threads by nudging `self.tail` forward a step.
        let (exchanged, original) = self.tail.compareExchange(
          expected: tail,
          desired: next,
          ordering: .acquiringAndReleasing)
        tail = (exchanged ? next : original)
        continue
      }
      let (exchanged, current) = tail.next.compareExchange(
        expected: nil,
        desired: new,
        ordering: .acquiringAndReleasing
      )
      if exchanged {
        _ = self.tail.compareExchange(expected: tail, desired: new, ordering: .releasing)
        return
      }
      tail = current!
    }
  }

  func dequeue() -> Element? {
    while true {
      let head = self.head.load(ordering: .acquiring)
      let next = head.next.load(ordering: .acquiring)
      if next === marker { continue }
      guard let n = next else { return nil }
      let tail = self.tail.load(ordering: .acquiring)
      if head === tail {
        // Nudge `tail` forward a step to make sure it doesn't fall off the
        // list when we unlink this node.
        _ = self.tail.compareExchange(expected: tail, desired: n, ordering: .acquiringAndReleasing)
      }
      if self.head.compareExchange(expected: head, desired: n, ordering: .releasing).exchanged {
        let result = n.value!
        n.value = nil
        // To prevent threads that are suspended in `enqueue`/`dequeue` from
        // holding onto arbitrarily long chains of removed nodes, we unlink
        // removed nodes by replacing their `next` value with the special
        // `marker`.
        head.next.store(marker, ordering: .releasing)
        return result
      }
    }
  }
}

class QueueTests: XCTestCase {
  override func tearDown() {
    XCTAssertEqual(nodeCount.load(ordering: .relaxed), 0)
  }

  func check(readers: Int, writers: Int, count: Int) {
    let queue = LockFreeQueue<(writer: Int, value: Int)>()
    let num = ManagedAtomic(0)
    DispatchQueue.concurrentPerform(iterations: writers + readers) { id in
      if id < writers {
        // Writer
        for i in 0 ..< count {
          queue.enqueue((id, i))
        }
      } else {
        // Reader
        var values = (0 ..< writers).map { _ in -1 }
        while num.load(ordering: .relaxed) < writers * count {
          // Spin until we get a value
          guard let (writer, value) = queue.dequeue() else { continue }
          precondition(writer >= 0 && writer < writers)
          precondition(readers == 1 ? value == values[writer] + 1 : value > values[writer])
          values[writer] = value
          num.wrappingIncrement(ordering: .relaxed)
        }
      }
    }
  }

  func test01_10() {
    check(readers: 1, writers: 10, count: iterations)
  }

  func test02_10() {
    check(readers: 2, writers: 10, count: iterations)
  }

  func test04_10() {
    check(readers: 2, writers: 10, count: iterations)
  }

  func test16_16() {
    check(readers: 16, writers: 16, count: iterations)
  }

#if MANUAL_TEST_DISCOVERY
  public static var allTests = [
    ("test01_10", test01_10),
    ("test02_10", test02_10),
    ("test04_10", test04_10),
    ("test16_16", test16_16),
  ]
#endif
}