File: UnboundedBufferStateMachine.swift

package info (click to toggle)
swiftlang 6.1.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,791,644 kB
  • sloc: cpp: 9,901,738; ansic: 2,201,433; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (258 lines) | stat: -rw-r--r-- 8,607 bytes parent folder | download | duplicates (2)
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2022 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
//
//===----------------------------------------------------------------------===//

import DequeModule

struct UnboundedBufferStateMachine<Base: AsyncSequence> {
  typealias Element = Base.Element
  typealias SuspendedConsumer = UnsafeContinuation<Result<Element, Error>?, Never>

  enum Policy {
    case unlimited
    case bufferingNewest(Int)
    case bufferingOldest(Int)
  }

  // We are using UnsafeTransfer here since we have to get the elements from the task
  // into the consumer task. This is a transfer but we cannot prove this to the compiler at this point
  // since next is not marked as transferring the return value.
  fileprivate enum State {
    case initial(base: Base)
    case buffering(
      task: Task<Void, Never>,
      buffer: Deque<Result<UnsafeTransfer<Element>, Error>>,
      suspendedConsumer: SuspendedConsumer?
    )
    case modifying
    case finished(buffer: Deque<Result<UnsafeTransfer<Element>, Error>>)
  }

  private var state: State
  private let policy: Policy

  init(base: Base, policy: Policy) {
    self.state = .initial(base: base)
    self.policy = policy
  }

  var task: Task<Void, Never>? {
    switch self.state {
      case .buffering(let task, _, _):
        return task
      default:
        return nil
    }
  }

  mutating func taskStarted(task: Task<Void, Never>) {
    switch self.state {
      case .initial:
        self.state = .buffering(task: task, buffer: [], suspendedConsumer: nil)

      case .buffering:
        preconditionFailure("Invalid state.")

      case .modifying:
        preconditionFailure("Invalid state.")

      case .finished:
        preconditionFailure("Invalid state.")
    }
  }

  enum ElementProducedAction {
    case none
    case resumeConsumer(
      continuation: SuspendedConsumer,
      result: Result<Element, Error>
    )
  }

  mutating func elementProduced(element: Element) -> ElementProducedAction {
    switch self.state {
      case .initial:
        preconditionFailure("Invalid state. The task should already by started.")

      case .buffering(let task, var buffer, .none):
        // we are either idle or the buffer is already in use (no awaiting consumer)
        // we have to apply the policy when stacking the new element
        self.state = .modifying
        switch self.policy {
          case .unlimited:
            buffer.append(.success(.init(element)))
          case .bufferingNewest(let limit):
            if buffer.count >= limit {
              _ = buffer.popFirst()
            }
            buffer.append(.success(.init(element)))
          case .bufferingOldest(let limit):
            if buffer.count < limit {
              buffer.append(.success(.init(element)))
            }
        }
        self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
        return .none

      case .buffering(let task, let buffer, .some(let suspendedConsumer)):
        // we have an awaiting consumer, we can resume it with the element
        precondition(buffer.isEmpty, "Invalid state. The buffer should be empty.")
        self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
        return .resumeConsumer(
          continuation: suspendedConsumer,
          result: .success(element)
        )

      case .modifying:
        preconditionFailure("Invalid state.")

      case .finished:
        return .none
    }
  }

  enum FinishAction {
    case none
    case resumeConsumer(continuation: SuspendedConsumer?)
  }

  mutating func finish(error: Error?) -> FinishAction {
    switch self.state {
      case .initial:
        preconditionFailure("Invalid state. The task should already by started.")
        
      case .buffering(_, var buffer, .none):
        // we are either idle or the buffer is already in use (no awaiting consumer)
        // if we have an error we stack it in the buffer so it can be consumed later
        if let error {
          buffer.append(.failure(error))
        }
        self.state = .finished(buffer: buffer)
        return .none

      case .buffering(_, let buffer, let suspendedConsumer):
        // we have an awaiting consumer, we can resume it with nil or the error
        precondition(buffer.isEmpty, "Invalid state. The buffer should be empty.")
        self.state = .finished(buffer: [])
        return .resumeConsumer(continuation: suspendedConsumer)

      case .modifying:
        preconditionFailure("Invalid state.")

      case .finished:
        return .none
    }
  }

  enum NextAction {
    case startTask(base: Base)
    case suspend
    case returnResult(Result<Element, Error>?)
  }

  mutating func next() -> NextAction {
    switch self.state {
      case .initial(let base):
        return .startTask(base: base)
        
      case .buffering(_, let buffer, let suspendedConsumer) where buffer.isEmpty:
        // we are idle, we have to suspend the consumer
        precondition(suspendedConsumer == nil, "Invalid states. There is already a suspended consumer.")
        return .suspend

      case .buffering(let task, var buffer, let suspendedConsumer):
        // the buffer is already in use, we can unstack a value and directly resume the consumer
        precondition(suspendedConsumer == nil, "Invalid states. There is already a suspended consumer.")
        self.state = .modifying
        let result = buffer.popFirst()!
        self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
        return .returnResult(result.map { $0.wrapped })

      case .modifying:
        preconditionFailure("Invalid state.")

      case .finished(let buffer) where buffer.isEmpty:
        return .returnResult(nil)

      case .finished(var buffer):
        self.state = .modifying
        let result = buffer.popFirst()!
        self.state = .finished(buffer: buffer)
        return .returnResult(result.map { $0.wrapped })
    }
  }

  enum NextSuspendedAction {
    case none
    case resumeConsumer(Result<Element, Error>?)
  }

  mutating func nextSuspended(continuation: SuspendedConsumer) -> NextSuspendedAction {
    switch self.state {
      case .initial:
        preconditionFailure("Invalid state. The task should already by started.")

      case .buffering(let task, let buffer, let suspendedConsumer) where buffer.isEmpty:
        // we are idle, we confirm the suspension of the consumer
        precondition(suspendedConsumer == nil, "Invalid states. There is already a suspended consumer.")
        self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: continuation)
        return .none

      case .buffering(let task, var buffer, let suspendedConsumer):
        // the buffer is already in use, we can unstack a value and directly resume the consumer
        precondition(suspendedConsumer == nil, "Invalid states. There is already a suspended consumer.")
        self.state = .modifying
        let result = buffer.popFirst()!
        self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
        return .resumeConsumer(result.map { $0.wrapped })

      case .modifying:
        preconditionFailure("Invalid state.")

      case .finished(let buffer) where buffer.isEmpty:
        return .resumeConsumer(nil)

      case .finished(var buffer):
        self.state = .modifying
        let result = buffer.popFirst()!
        self.state = .finished(buffer: buffer)
        return .resumeConsumer(result.map { $0.wrapped })
    }
  }

  enum InterruptedAction {
    case none
    case resumeConsumer(task: Task<Void, Never>, continuation: SuspendedConsumer?)
  }

  mutating func interrupted() -> InterruptedAction {
    switch self.state {
      case .initial:
        state = .finished(buffer: [])
        return .none
        
      case .buffering(let task, _, let suspendedConsumer):
        self.state = .finished(buffer: [])
        return .resumeConsumer(task: task, continuation: suspendedConsumer)

      case .modifying:
        preconditionFailure("Invalid state.")

      case .finished:
        self.state = .finished(buffer: [])
        return .none
    }
  }
}

extension UnboundedBufferStateMachine: Sendable where Base: Sendable { }
extension UnboundedBufferStateMachine.State: Sendable where Base: Sendable { }