|
| 1 | +// |
| 2 | +// AllocatedLock.swift |
| 3 | +// AllocatedLock |
| 4 | +// |
| 5 | +// Created by Simon Whitty on 10/04/2023. |
| 6 | +// Copyright 2023 Simon Whitty |
| 7 | +// |
| 8 | +// Distributed under the permissive MIT license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/AllocatedLock |
| 12 | +// |
| 13 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | +// of this software and associated documentation files (the "Software"), to deal |
| 15 | +// in the Software without restriction, including without limitation the rights |
| 16 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | +// copies of the Software, and to permit persons to whom the Software is |
| 18 | +// furnished to do so, subject to the following conditions: |
| 19 | +// |
| 20 | +// The above copyright notice and this permission notice shall be included in all |
| 21 | +// copies or substantial portions of the Software. |
| 22 | +// |
| 23 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 | +// SOFTWARE. |
| 30 | +// |
| 31 | + |
| 32 | +// Backports the Swift interface around os_unfair_lock_t available in recent Darwin platforms |
| 33 | +// |
| 34 | +@available(iOS, deprecated: 16.0, message: "use OSAllocatedUnfairLock directly") |
| 35 | +@available(tvOS, deprecated: 16.0, message: "use OSAllocatedUnfairLock directly") |
| 36 | +@available(watchOS, deprecated: 9, message: "use OSAllocatedUnfairLock directly") |
| 37 | +@available(macOS, deprecated: 13.0, message: "use OSAllocatedUnfairLock directly") |
| 38 | +public struct AllocatedLock<State> { |
| 39 | + |
| 40 | + private let storage: Storage |
| 41 | + |
| 42 | + public init(initialState: State) { |
| 43 | + self.storage = Storage(initialState: initialState) |
| 44 | + } |
| 45 | + |
| 46 | + public func withLock<R>(_ body: @Sendable (inout State) throws -> R) rethrows -> R where R: Sendable { |
| 47 | + storage.lock() |
| 48 | + defer { storage.unlock() } |
| 49 | + return try body(&storage.state) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +public extension AllocatedLock where State == Void { |
| 54 | + |
| 55 | + init() { |
| 56 | + self.storage = Storage(initialState: ()) |
| 57 | + } |
| 58 | + |
| 59 | + func lock() { |
| 60 | + storage.lock() |
| 61 | + } |
| 62 | + |
| 63 | + func unlock() { |
| 64 | + storage.unlock() |
| 65 | + } |
| 66 | + |
| 67 | + func withLock<R>(_ body: @Sendable () throws -> R) rethrows -> R where R: Sendable { |
| 68 | + storage.lock() |
| 69 | + defer { storage.unlock() } |
| 70 | + return try body() |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +#if canImport(Darwin) |
| 75 | +@_implementationOnly import os |
| 76 | + |
| 77 | +private extension AllocatedLock { |
| 78 | + final class Storage { |
| 79 | + private let _lock: os_unfair_lock_t |
| 80 | + var state: State |
| 81 | + |
| 82 | + init(initialState: State) { |
| 83 | + self._lock = .allocate(capacity: 1) |
| 84 | + self._lock.initialize(to: os_unfair_lock()) |
| 85 | + self.state = initialState |
| 86 | + } |
| 87 | + |
| 88 | + func lock() { |
| 89 | + os_unfair_lock_lock(_lock) |
| 90 | + } |
| 91 | + |
| 92 | + func unlock() { |
| 93 | + os_unfair_lock_unlock(_lock) |
| 94 | + } |
| 95 | + |
| 96 | + deinit { |
| 97 | + self._lock.deinitialize(count: 1) |
| 98 | + self._lock.deallocate() |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#elseif canImport(Glibc) |
| 104 | +@_implementationOnly import Glibc |
| 105 | + |
| 106 | +private extension AllocatedLock { |
| 107 | + final class Storage { |
| 108 | + private let _lock: UnsafeMutablePointer<pthread_mutex_t> |
| 109 | + |
| 110 | + var state: State |
| 111 | + |
| 112 | + init(initialState: State) { |
| 113 | + var attr = pthread_mutexattr_t() |
| 114 | + pthread_mutexattr_init(&attr) |
| 115 | + self._lock = .allocate(capacity: 1) |
| 116 | + let err = pthread_mutex_init(self._lock, &attr) |
| 117 | + precondition(err == 0, "pthread_mutex_init error: \(err)") |
| 118 | + self.state = initialState |
| 119 | + } |
| 120 | + |
| 121 | + func lock() { |
| 122 | + let err = pthread_mutex_lock(_lock) |
| 123 | + precondition(err == 0, "pthread_mutex_lock error: \(err)") |
| 124 | + } |
| 125 | + |
| 126 | + func unlock() { |
| 127 | + let err = pthread_mutex_unlock(_lock) |
| 128 | + precondition(err == 0, "pthread_mutex_unlock error: \(err)") |
| 129 | + } |
| 130 | + |
| 131 | + deinit { |
| 132 | + let err = pthread_mutex_destroy(self._lock) |
| 133 | + precondition(err == 0, "pthread_mutex_destroy error: \(err)") |
| 134 | + self._lock.deallocate() |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | +#endif |
0 commit comments