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
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021 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 Swift project authors
*/
import Foundation
#if os(Windows)
import WinSDK
#endif
/// A wrapper type that ensures a synchronous access to a value.
///
/// To guarantee safe concurrent access to a value wrap it in a `Synchronized` type.
/// ```swift
/// let index = Synchronized([String: String]())
///
/// // Mutate the value
/// index.sync { $0["key"] = "value" }
/// // Access the value
/// let value = index.sync { $0["key"] }
/// ```
public class Synchronized<Value> {
/// A value that requires synchronized access.
private var value: Value
#if os(macOS) || os(iOS)
/// A lock type appropriate for the current platform.
/// > Note: To avoid access race reports we manage the memory manually.
var lock: UnsafeMutablePointer<os_unfair_lock>
#elseif os(Linux) || os(Android)
/// A lock type appropriate for the current platform.
var lock: UnsafeMutablePointer<pthread_mutex_t>
#elseif os(Windows)
var lock: UnsafeMutablePointer<SRWLOCK>
#else
#error("Unsupported platform")
#endif
/// Creates a new synchronization over the given value.
/// - Parameter value: A value that requires synchronous access.
public init(_ value: Value) {
self.value = value
#if os(macOS) || os(iOS)
lock = UnsafeMutablePointer<os_unfair_lock>.allocate(capacity: 1)
lock.initialize(to: os_unfair_lock())
#elseif os(Linux) || os(Android)
lock = UnsafeMutablePointer<pthread_mutex_t>.allocate(capacity: 1)
lock.initialize(to: pthread_mutex_t())
pthread_mutex_init(lock, nil)
#elseif os(Windows)
lock = UnsafeMutablePointer<SRWLOCK>.allocate(capacity: 1)
InitializeSRWLock(lock)
#else
#error("Unsupported platform")
#endif
}
deinit {
// Release the lock's memory.
lock.deallocate()
}
/// Performs a given block of code while synchronizing over the type's stored value.
/// - Parameter block: A throwing block of work that optionally returns a value.
/// - Returns: Returns the returned value of `block`, if any.
@discardableResult
public func sync<Result>(_ block: (inout Value) throws -> Result) rethrows -> Result {
#if os(macOS) || os(iOS)
os_unfair_lock_lock(lock)
defer { os_unfair_lock_unlock(lock) }
#elseif os(Linux) || os(Android)
pthread_mutex_lock(lock)
defer { pthread_mutex_unlock(lock) }
#elseif os(Windows)
AcquireSRWLockExclusive(lock)
defer { ReleaseSRWLockExclusive(lock) }
#else
#error("Unsupported platform")
#endif
return try block(&value)
}
}
/// A platform-appropriate locking mechanism.
/// - Note: Prefer ``Synchronized`` if you need to synchronize access
/// to a given value instead of a generic lock.
/// ```swift
/// let lock = Lock()
///
/// lock.sync { myVar = 1 }
/// let result = lock.sync { return index["key"] }
/// ```
typealias Lock = Synchronized<Void>
public extension Lock {
/// Creates a new lock.
convenience init() {
self.init(())
}
@discardableResult
func sync<Result>(_ block: () throws -> Result) rethrows -> Result {
#if os(macOS) || os(iOS)
os_unfair_lock_lock(lock)
defer { os_unfair_lock_unlock(lock) }
#elseif os(Linux) || os(Android)
pthread_mutex_lock(lock)
defer { pthread_mutex_unlock(lock) }
#elseif os(Windows)
AcquireSRWLockExclusive(lock)
defer { ReleaseSRWLockExclusive(lock) }
#else
#error("Unsupported platform")
#endif
return try block()
}
}
|