|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftServiceLifecycle open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the SwiftServiceLifecycle project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftServiceLifecycle project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | +// |
| 17 | +// This source file is part of the Swift Async Algorithms open source project |
| 18 | +// |
| 19 | +// Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 20 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 21 | +// |
| 22 | +// See https://swift.org/LICENSE.txt for license information |
| 23 | +// |
| 24 | +//===----------------------------------------------------------------------===// |
| 25 | + |
| 26 | +#if canImport(Darwin) |
| 27 | +@_implementationOnly import Darwin |
| 28 | +#elseif canImport(Glibc) |
| 29 | +@_implementationOnly import Glibc |
| 30 | +#elseif canImport(WinSDK) |
| 31 | +@_implementationOnly import WinSDK |
| 32 | +#endif |
| 33 | + |
| 34 | +internal struct Lock { |
| 35 | +#if canImport(Darwin) |
| 36 | + typealias Primitive = os_unfair_lock |
| 37 | +#elseif canImport(Glibc) |
| 38 | + typealias Primitive = pthread_mutex_t |
| 39 | +#elseif canImport(WinSDK) |
| 40 | + typealias Primitive = SRWLOCK |
| 41 | +#endif |
| 42 | + |
| 43 | + typealias PlatformLock = UnsafeMutablePointer<Primitive> |
| 44 | + let platformLock: PlatformLock |
| 45 | + |
| 46 | + private init(_ platformLock: PlatformLock) { |
| 47 | + self.platformLock = platformLock |
| 48 | + } |
| 49 | + |
| 50 | + fileprivate static func initialize(_ platformLock: PlatformLock) { |
| 51 | +#if canImport(Darwin) |
| 52 | + platformLock.initialize(to: os_unfair_lock()) |
| 53 | +#elseif canImport(Glibc) |
| 54 | + let result = pthread_mutex_init(platformLock, nil) |
| 55 | + precondition(result == 0, "pthread_mutex_init failed") |
| 56 | +#elseif canImport(WinSDK) |
| 57 | + InitializeSRWLock(platformLock) |
| 58 | +#endif |
| 59 | + } |
| 60 | + |
| 61 | + fileprivate static func deinitialize(_ platformLock: PlatformLock) { |
| 62 | +#if canImport(Glibc) |
| 63 | + let result = pthread_mutex_destroy(platformLock) |
| 64 | + precondition(result == 0, "pthread_mutex_destroy failed") |
| 65 | +#endif |
| 66 | + platformLock.deinitialize(count: 1) |
| 67 | + } |
| 68 | + |
| 69 | + fileprivate static func lock(_ platformLock: PlatformLock) { |
| 70 | +#if canImport(Darwin) |
| 71 | + os_unfair_lock_lock(platformLock) |
| 72 | +#elseif canImport(Glibc) |
| 73 | + pthread_mutex_lock(platformLock) |
| 74 | +#elseif canImport(WinSDK) |
| 75 | + AcquireSRWLockExclusive(platformLock) |
| 76 | +#endif |
| 77 | + } |
| 78 | + |
| 79 | + fileprivate static func unlock(_ platformLock: PlatformLock) { |
| 80 | +#if canImport(Darwin) |
| 81 | + os_unfair_lock_unlock(platformLock) |
| 82 | +#elseif canImport(Glibc) |
| 83 | + let result = pthread_mutex_unlock(platformLock) |
| 84 | + precondition(result == 0, "pthread_mutex_unlock failed") |
| 85 | +#elseif canImport(WinSDK) |
| 86 | + ReleaseSRWLockExclusive(platformLock) |
| 87 | +#endif |
| 88 | + } |
| 89 | + |
| 90 | + static func allocate() -> Lock { |
| 91 | + let platformLock = PlatformLock.allocate(capacity: 1) |
| 92 | + initialize(platformLock) |
| 93 | + return Lock(platformLock) |
| 94 | + } |
| 95 | + |
| 96 | + func deinitialize() { |
| 97 | + Lock.deinitialize(platformLock) |
| 98 | + } |
| 99 | + |
| 100 | + func lock() { |
| 101 | + Lock.lock(platformLock) |
| 102 | + } |
| 103 | + |
| 104 | + func unlock() { |
| 105 | + Lock.unlock(platformLock) |
| 106 | + } |
| 107 | + |
| 108 | + /// Acquire the lock for the duration of the given block. |
| 109 | + /// |
| 110 | + /// This convenience method should be preferred to `lock` and `unlock` in |
| 111 | + /// most situations, as it ensures that the lock will be released regardless |
| 112 | + /// of how `body` exits. |
| 113 | + /// |
| 114 | + /// - Parameter body: The block to execute while holding the lock. |
| 115 | + /// - Returns: The value returned by the block. |
| 116 | + func withLock<T>(_ body: () throws -> T) rethrows -> T { |
| 117 | + self.lock() |
| 118 | + defer { |
| 119 | + self.unlock() |
| 120 | + } |
| 121 | + return try body() |
| 122 | + } |
| 123 | + |
| 124 | + // specialise Void return (for performance) |
| 125 | + func withLockVoid(_ body: () throws -> Void) rethrows -> Void { |
| 126 | + try self.withLock(body) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +struct ManagedCriticalState<State> { |
| 131 | + private final class LockedBuffer: ManagedBuffer<State, Lock.Primitive> { |
| 132 | + deinit { |
| 133 | + withUnsafeMutablePointerToElements { Lock.deinitialize($0) } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private let buffer: ManagedBuffer<State, Lock.Primitive> |
| 138 | + |
| 139 | + init(_ initial: State) { |
| 140 | + buffer = LockedBuffer.create(minimumCapacity: 1) { buffer in |
| 141 | + buffer.withUnsafeMutablePointerToElements { Lock.initialize($0) } |
| 142 | + return initial |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + func withCriticalRegion<R>(_ critical: (inout State) throws -> R) rethrows -> R { |
| 147 | + try buffer.withUnsafeMutablePointers { header, lock in |
| 148 | + Lock.lock(lock) |
| 149 | + defer { Lock.unlock(lock) } |
| 150 | + return try critical(&header.pointee) |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +extension ManagedCriticalState: @unchecked Sendable where State: Sendable { } |
0 commit comments