|
| 1 | +// |
| 2 | +// Mutex.swift |
| 3 | +// swift-mutex |
| 4 | +// |
| 5 | +// Created by Simon Whitty on 07/09/2024. |
| 6 | +// Copyright 2024 Simon Whitty |
| 7 | +// |
| 8 | +// Distributed under the permissive MIT license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/swift-mutex |
| 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 | +#if compiler(>=6) |
| 33 | + |
| 34 | +#if canImport(Darwin) |
| 35 | +// Backports the Synchronization.Mutex API for earlier Darwin platforms |
| 36 | + |
| 37 | +@usableFromInline |
| 38 | +struct Mutex<Value>: @unchecked Sendable { |
| 39 | + let storage: Storage |
| 40 | + |
| 41 | + @usableFromInline |
| 42 | + init(_ initialValue: consuming sending Value) { |
| 43 | + self.storage = Storage(initialValue) |
| 44 | + } |
| 45 | + |
| 46 | + @usableFromInline |
| 47 | + borrowing func withLock<Result, E: Error>( |
| 48 | + _ body: (inout sending Value) throws(E) -> sending Result |
| 49 | + ) throws(E) -> sending Result { |
| 50 | + storage.lock() |
| 51 | + defer { storage.unlock() } |
| 52 | + return try body(&storage.value) |
| 53 | + } |
| 54 | + |
| 55 | + @usableFromInline |
| 56 | + borrowing func withLockIfAvailable<Result, E>( |
| 57 | + _ body: (inout sending Value) throws(E) -> sending Result |
| 58 | + ) throws(E) -> sending Result? where E: Error { |
| 59 | + guard storage.tryLock() else { return nil } |
| 60 | + defer { storage.unlock() } |
| 61 | + return try body(&storage.value) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +import struct os.os_unfair_lock_t |
| 66 | +import struct os.os_unfair_lock |
| 67 | +import func os.os_unfair_lock_lock |
| 68 | +import func os.os_unfair_lock_unlock |
| 69 | +import func os.os_unfair_lock_trylock |
| 70 | + |
| 71 | +extension Mutex { |
| 72 | + |
| 73 | + final class Storage { |
| 74 | + private let _lock: os_unfair_lock_t |
| 75 | + |
| 76 | + var value: Value |
| 77 | + |
| 78 | + init(_ initialValue: Value) { |
| 79 | + self._lock = .allocate(capacity: 1) |
| 80 | + self._lock.initialize(to: os_unfair_lock()) |
| 81 | + self.value = initialValue |
| 82 | + } |
| 83 | + |
| 84 | + func lock() { |
| 85 | + os_unfair_lock_lock(_lock) |
| 86 | + } |
| 87 | + |
| 88 | + func unlock() { |
| 89 | + os_unfair_lock_unlock(_lock) |
| 90 | + } |
| 91 | + |
| 92 | + func tryLock() -> Bool { |
| 93 | + os_unfair_lock_trylock(_lock) |
| 94 | + } |
| 95 | + |
| 96 | + deinit { |
| 97 | + self._lock.deinitialize(count: 1) |
| 98 | + self._lock.deallocate() |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#elseif canImport(Synchronization) |
| 104 | + |
| 105 | +import Synchronization |
| 106 | + |
| 107 | +typealias Mutex = Synchronization.Mutex |
| 108 | + |
| 109 | +#endif |
| 110 | +#endif |
0 commit comments