File: StrongReferenceShuffle.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 (291 lines) | stat: -rw-r--r-- 9,680 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//===----------------------------------------------------------------------===//
//
// 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 linked list concurrency test. `n` threads are each
// racing on a linked list containing `n` values; in each iteration,
// each thread unlinks its corresponding value then reinserts it at
// the end of the list.

import XCTest
import Atomics
import Dispatch

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

private let nodeCount = ManagedAtomic<Int>(0)

private class List<Value: Equatable> {
  final class Node: AtomicReference {
    private let _next: UnsafeAtomic<Node?>
    private let _value: UnsafeAtomic<UnsafeMutablePointer<Value>?>

    init(pointer: UnsafeMutablePointer<Value>?, next: Node? = nil) {
      self._next = .create(next)
      self._value = .create(pointer)
      nodeCount.wrappingIncrement(ordering: .relaxed)
    }

    convenience init(_ value: Value?, next: Node? = nil) {
      if let value = value {
        let p = UnsafeMutablePointer<Value>.allocate(capacity: 1)
        p.initialize(to: value)
        self.init(pointer: p, next: next)
      } else {
        self.init(pointer: nil, next: next)
      }
    }

    deinit {
      // Prevent stack overflow when deinitializing a long chain
      var node = self._next.destroy()
      while node != nil && isKnownUniquelyReferenced(&node) {
        let next = node!._next.exchange(nil, ordering: .relaxed)
        withExtendedLifetime(node) {}
        node = next
      }

      if let p = self._value.destroy() {
        p.deinitialize(count: 1)
        p.deallocate()
      }
      nodeCount.wrappingDecrement(ordering: .relaxed)
    }

    var value: Value? { _value.load(ordering: .sequentiallyConsistent)?.pointee }

    func clearValue() -> UnsafeMutablePointer<Value>? {
      withExtendedLifetime(self) {
        _value.exchange(nil, ordering: .sequentiallyConsistent)
      }
    }

    var next: Node? { _next.load(ordering: .sequentiallyConsistent) }

    func clearNext() -> Node? {
      withExtendedLifetime(self) {
        _next.exchange(nil, ordering: .sequentiallyConsistent)
      }
    }

    func compareExchangeNext(expected: Node?, desired: Node?) -> (exchanged: Bool, original: Node?) {
      withExtendedLifetime(self) {
        _next.compareExchange(expected: expected, desired: desired, ordering: .sequentiallyConsistent)
      }
    }
  }

  let head: Node

  init<Elements: Collection>(from elements: Elements)
  where Elements.Element == Value {
    var n: Node? = nil
    for value in elements.reversed() {
      n = Node(value, next: n)
    }
    self.head = Node(nil, next: n)
  }

  deinit {
    // Prevent a stack overflow while recursively releasing list nodes.
    var node = head.clearNext()
    while let n = node {
      let next = n.clearNext()
      node = next
    }
  }
}

extension List {
  /// Move the given value to the end of this list. It is safe to call
  /// this concurrently on multiple threads as long as all invocations
  /// are using distinct values.
  func sink(_ value: Value) {
    var current = self.head
    var next = self.head.next
    var anchor = current
    var anchorNext = next
    var found = false

    var new: Node?
    while true {
      if let n = next {
        var v = n.value
        if !found, v == value {
          // Mark this node as deleted.
          found = true
          new = Node(pointer: n.clearValue(), next: nil)
          precondition(new?.value == value, "Concurrent sink invocations with the same value")
          v = nil
        }
        current = n
        next = n.next
        if v != nil {
          if current !== anchor {
            // Opportunistically unlink the chain of deleted nodes between `anchor` and `n`.
            _ = anchor.compareExchangeNext(expected: anchorNext, desired: n)
          }
          anchor = current
          anchorNext = next
        }
      } else {
        // `next` is nil. Append `new` to the end of the list.
        precondition(found, "Lost value \(value) in \(read())")
        let (exchanged, original) = current.compareExchangeNext(expected: nil, desired: new)
        if exchanged {
          if current !== anchor {
            // Opportunistically unlink the chain of deleted nodes between `anchor` and `new`.
            _ = anchor.compareExchangeNext(expected: anchorNext, desired: new)
          }
          return
        }
        next = original
      }
    }
  }

  /// Read out and return the contents of this list in an array. Note
  /// that this may return duplicate or missing elements if it is
  /// called concurrently with `sink`. (This is still safe -- the
  /// results may be useless, but the returned values are still
  /// valid.)
  @inline(never)
  func read() -> [Value?] {
    var result: [Value?] = []
    var node = head
    while let next = node.next {
      result.append(next.value)
      node = next
    }
    withExtendedLifetime(node) {}
    return result
  }
}

extension List where Value: Hashable {
  @inline(never)
  func readUnique(expectedCount: Int = 0) -> Set<Value> {
    var result: Set<Value> = []
    result.reserveCapacity(expectedCount)
    var node = head
    while let next = node.next {
      if let value = next.value {
        result.insert(value)
      }
      node = next
    }
    withExtendedLifetime(node) {}
    return result
  }
}


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

  func checkSink(
    writers: Int,
    readers: Int,
    iterations: Int,
    file: StaticString = #file,
    line: UInt = #line
  ) {
    let list = List<Int>(from: 0 ..< writers)

    XCTAssertEqual(list.read(), Array(0 ..< writers),
      "Unexpected list contents at start",
      file: file, line: line)

    DispatchQueue.concurrentPerform(iterations: readers + writers) { id in
      if id < writers {
        for _ in 0 ..< iterations {
          list.sink(id)
        }
      } else {
        for _ in 0 ..< iterations {
          let elements = list.readUnique(expectedCount: writers)
          precondition(elements.count <= writers)
        }
      }
    }

    let contents = list.read()
    print(
      contents
        .map { value -> String in
          if let value = value { return "\(value)" }
          return "nil"
        }
        .joined(separator: ", "))
    let values = Set(contents.compactMap { $0 })
    XCTAssertEqual(values, Set(0 ..< writers),
      "Unexpected list contents at end",
      file: file, line: line)
  }

  func test_sink_01_00() { checkSink(writers: 1, readers: 0, iterations: iterations) }
  func test_sink_02_00() { checkSink(writers: 2, readers: 0, iterations: iterations) }
  func test_sink_04_00() { checkSink(writers: 4, readers: 0, iterations: iterations) }
  func test_sink_08_00() { checkSink(writers: 8, readers: 0, iterations: iterations) }

  func test_sink_01_01() { checkSink(writers: 1, readers: 1, iterations: iterations) }
  func test_sink_02_01() { checkSink(writers: 2, readers: 1, iterations: iterations) }
  func test_sink_04_01() { checkSink(writers: 4, readers: 1, iterations: iterations) }
  func test_sink_08_01() { checkSink(writers: 8, readers: 1, iterations: iterations) }

  func test_sink_01_02() { checkSink(writers: 1, readers: 2, iterations: iterations) }
  func test_sink_02_02() { checkSink(writers: 2, readers: 2, iterations: iterations) }
  func test_sink_04_02() { checkSink(writers: 4, readers: 2, iterations: iterations) }
  func test_sink_08_02() { checkSink(writers: 8, readers: 2, iterations: iterations) }

  func test_sink_01_04() { checkSink(writers: 1, readers: 4, iterations: iterations) }
  func test_sink_02_04() { checkSink(writers: 2, readers: 4, iterations: iterations) }
  func test_sink_04_04() { checkSink(writers: 4, readers: 4, iterations: iterations) }
  func test_sink_08_04() { checkSink(writers: 8, readers: 4, iterations: iterations) }

  func test_sink_01_08() { checkSink(writers: 1, readers: 8, iterations: iterations) }
  func test_sink_02_08() { checkSink(writers: 2, readers: 8, iterations: iterations) }
  func test_sink_04_08() { checkSink(writers: 4, readers: 8, iterations: iterations) }
  func test_sink_08_08() { checkSink(writers: 8, readers: 8, iterations: iterations) }

#if MANUAL_TEST_DISCOVERY
  public static var allTests = [
    ("test_sink_01_00", test_sink_01_00),
    ("test_sink_02_00", test_sink_02_00),
    ("test_sink_04_00", test_sink_04_00),
    ("test_sink_08_00", test_sink_08_00),
    ("test_sink_01_01", test_sink_01_01),
    ("test_sink_02_01", test_sink_02_01),
    ("test_sink_04_01", test_sink_04_01),
    ("test_sink_08_01", test_sink_08_01),
    ("test_sink_01_02", test_sink_01_02),
    ("test_sink_02_02", test_sink_02_02),
    ("test_sink_04_02", test_sink_04_02),
    ("test_sink_08_02", test_sink_08_02),
    ("test_sink_01_04", test_sink_01_04),
    ("test_sink_02_04", test_sink_02_04),
    ("test_sink_04_04", test_sink_04_04),
    ("test_sink_08_04", test_sink_08_04),
    ("test_sink_01_08", test_sink_01_08),
    ("test_sink_02_08", test_sink_02_08),
    ("test_sink_04_08", test_sink_04_08),
    ("test_sink_08_08", test_sink_08_08),
  ]
#endif
}