-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathAtomic.swift
331 lines (281 loc) · 7.81 KB
/
Atomic.swift
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//
// Atomic.swift
// ReactiveSwift
//
// Created by Justin Spahr-Summers on 2014-06-10.
// Copyright (c) 2014 GitHub. All rights reserved.
//
import Foundation
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import os
import MachO
#endif
/// A simple, generic lock-free finite state machine.
///
/// - warning: `deinitialize` must be called to dispose of the consumed memory.
internal struct UnsafeAtomicState<State: RawRepresentable> where State.RawValue == Int32 {
internal typealias Transition = (expected: State, next: State)
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
private let value: UnsafeMutablePointer<Int32>
/// Create a finite state machine with the specified initial state.
///
/// - parameters:
/// - initial: The desired initial state.
internal init(_ initial: State) {
value = UnsafeMutablePointer<Int32>.allocate(capacity: 1)
value.initialize(to: initial.rawValue)
}
/// Deinitialize the finite state machine.
internal func deinitialize() {
value.deinitialize(count: 1)
value.deallocate()
}
/// Compare the current state with the specified state.
///
/// - parameters:
/// - expected: The expected state.
///
/// - returns: `true` if the current state matches the expected state.
/// `false` otherwise.
internal func `is`(_ expected: State) -> Bool {
return expected.rawValue == value.pointee
}
/// Try to transition from the expected current state to the specified next
/// state.
///
/// - parameters:
/// - expected: The expected state.
/// - next: The state to transition to.
///
/// - returns: `true` if the transition succeeds. `false` otherwise.
internal func tryTransition(from expected: State, to next: State) -> Bool {
return OSAtomicCompareAndSwap32Barrier(expected.rawValue,
next.rawValue,
value)
}
#else
private let value: Atomic<Int32>
/// Create a finite state machine with the specified initial state.
///
/// - parameters:
/// - initial: The desired initial state.
internal init(_ initial: State) {
value = Atomic(initial.rawValue)
}
/// Deinitialize the finite state machine.
internal func deinitialize() {}
/// Compare the current state with the specified state.
///
/// - parameters:
/// - expected: The expected state.
///
/// - returns: `true` if the current state matches the expected state.
/// `false` otherwise.
internal func `is`(_ expected: State) -> Bool {
return value.value == expected.rawValue
}
/// Try to transition from the expected current state to the specified next
/// state.
///
/// - parameters:
/// - expected: The expected state.
///
/// - returns: `true` if the transition succeeds. `false` otherwise.
internal func tryTransition(from expected: State, to next: State) -> Bool {
return value.modify { value in
if value == expected.rawValue {
value = next.rawValue
return true
}
return false
}
}
#endif
}
/// `Lock` exposes `os_unfair_lock` or `OSAllocatedUnfairLock`
/// on supported platforms, with pthread mutex as the
/// fallback.
internal class Lock: LockProtocol {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
internal final class UnfairLock: Lock {
private let _lock: os_unfair_lock_t
override init() {
_lock = .allocate(capacity: 1)
_lock.initialize(to: os_unfair_lock())
super.init()
}
override func lock() {
os_unfair_lock_lock(_lock)
}
override func unlock() {
os_unfair_lock_unlock(_lock)
}
override func `try`() -> Bool {
return os_unfair_lock_trylock(_lock)
}
deinit {
_lock.deinitialize(count: 1)
_lock.deallocate()
}
}
#endif
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
#if swift(>=5.7)
@available(iOS 16.0, *)
@available(macOS 13.0, *)
@available(tvOS 16.0, *)
@available(watchOS 9.0, *)
internal final class AllocatedUnfairLock: Lock {
private let _lock = OSAllocatedUnfairLock()
override init() {
super.init()
}
override func lock() {
_lock.lock()
}
override func unlock() {
_lock.unlock()
}
override func `try`() -> Bool {
_lock.lockIfAvailable()
}
}
#endif
#endif
internal final class PthreadLock: Lock {
private let _lock: UnsafeMutablePointer<pthread_mutex_t>
init(recursive: Bool = false) {
_lock = .allocate(capacity: 1)
_lock.initialize(to: pthread_mutex_t())
let attr = UnsafeMutablePointer<pthread_mutexattr_t>.allocate(capacity: 1)
attr.initialize(to: pthread_mutexattr_t())
pthread_mutexattr_init(attr)
defer {
pthread_mutexattr_destroy(attr)
attr.deinitialize(count: 1)
attr.deallocate()
}
pthread_mutexattr_settype(attr, Int32(recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_ERRORCHECK))
let status = pthread_mutex_init(_lock, attr)
assert(status == 0, "Unexpected pthread mutex error code: \(status)")
super.init()
}
override func lock() {
let status = pthread_mutex_lock(_lock)
assert(status == 0, "Unexpected pthread mutex error code: \(status)")
}
override func unlock() {
let status = pthread_mutex_unlock(_lock)
assert(status == 0, "Unexpected pthread mutex error code: \(status)")
}
override func `try`() -> Bool {
let status = pthread_mutex_trylock(_lock)
switch status {
case 0:
return true
case EBUSY, EAGAIN, EDEADLK:
return false
default:
assertionFailure("Unexpected pthread mutex error code: \(status)")
return false
}
}
deinit {
let status = pthread_mutex_destroy(_lock)
assert(status == 0, "Unexpected pthread mutex error code: \(status)")
_lock.deinitialize(count: 1)
_lock.deallocate()
}
}
static func make() -> Self {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
#if swift(>=5.7)
guard #available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) else {
return UnfairLock() as! Self
}
return AllocatedUnfairLock() as! Self
#else
return UnfairLock() as! Self
#endif
#else
return PthreadLock() as! Self
#endif
}
private init() {}
func lock() { fatalError() }
func unlock() { fatalError() }
func `try`() -> Bool { fatalError() }
}
internal protocol LockProtocol {
static func make() -> Self
func lock()
func unlock()
func `try`() -> Bool
}
internal struct NoLock: LockProtocol {
static func make() -> NoLock { NoLock() }
func lock() {}
func unlock() {}
func `try`() -> Bool { true }
}
/// An atomic variable.
public final class Atomic<Value> {
private let lock: Lock
private var _value: Value
/// Atomically get or set the value of the variable.
public var value: Value {
get {
return withValue { $0 }
}
set(newValue) {
swap(newValue)
}
}
/// Initialize the variable with the given initial value.
///
/// - parameters:
/// - value: Initial value for `self`.
public init(_ value: Value) {
_value = value
lock = Lock.make()
}
/// Atomically modifies the variable.
///
/// - parameters:
/// - action: A closure that takes the current value.
///
/// - returns: The result of the action.
@discardableResult
public func modify<Result>(_ action: (inout Value) throws -> Result) rethrows -> Result {
lock.lock()
defer { lock.unlock() }
return try action(&_value)
}
/// Atomically perform an arbitrary action using the current value of the
/// variable.
///
/// - parameters:
/// - action: A closure that takes the current value.
///
/// - returns: The result of the action.
@discardableResult
public func withValue<Result>(_ action: (Value) throws -> Result) rethrows -> Result {
lock.lock()
defer { lock.unlock() }
return try action(_value)
}
/// Atomically replace the contents of the variable.
///
/// - parameters:
/// - newValue: A new value for the variable.
///
/// - returns: The old value.
@discardableResult
public func swap(_ newValue: Value) -> Value {
return modify { (value: inout Value) in
let oldValue = value
value = newValue
return oldValue
}
}
}