|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +// Copied and modified from: |
| 14 | +// https://github.com/ReactiveCocoa/ReactiveSwift/blob/master/Sources/Atomic.swift 🙏 |
| 15 | +// |
| 16 | +// Copyright (c) 2012 - 2016, GitHub, Inc. All rights reserved. |
| 17 | +// |
| 18 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 19 | +// this software and associated documentation files (the "Software"), to deal in |
| 20 | +// the Software without restriction, including without limitation the rights to |
| 21 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 22 | +// the Software, and to permit persons to whom the Software is furnished to do so, |
| 23 | +// subject to the following conditions: |
| 24 | +// |
| 25 | +// The above copyright notice and this permission notice shall be included in all |
| 26 | +// copies or substantial portions of the Software. |
| 27 | +// |
| 28 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 29 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 30 | +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 31 | +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 32 | +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 33 | +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 34 | + |
| 35 | +import Foundation |
| 36 | +#if os(macOS) |
| 37 | +import MachO |
| 38 | +#endif |
| 39 | + |
| 40 | +/// `Lock` exposes `os_unfair_lock` on supported platforms, with `pthread_mutex_t` |
| 41 | +/// as the fallback (or for recursive locks). |
| 42 | +public class Lock { |
| 43 | + |
| 44 | + #if os(macOS) |
| 45 | + @available(macOS 10.12, *) |
| 46 | + final class UnfairLock: Lock { |
| 47 | + private let _lock: os_unfair_lock_t |
| 48 | + |
| 49 | + override init() { |
| 50 | + _lock = .allocate(capacity: 1) |
| 51 | + _lock.initialize(to: os_unfair_lock()) |
| 52 | + super.init() |
| 53 | + } |
| 54 | + |
| 55 | + override func lock() { |
| 56 | + os_unfair_lock_lock(_lock) |
| 57 | + } |
| 58 | + |
| 59 | + override func unlock() { |
| 60 | + os_unfair_lock_unlock(_lock) |
| 61 | + } |
| 62 | + |
| 63 | + override func `try`() -> Bool { |
| 64 | + return os_unfair_lock_trylock(_lock) |
| 65 | + } |
| 66 | + |
| 67 | + deinit { |
| 68 | + _lock.deinitialize(count: 1) |
| 69 | + _lock.deallocate() |
| 70 | + } |
| 71 | + } |
| 72 | + #endif |
| 73 | + |
| 74 | + final class PthreadLock: Lock { |
| 75 | + private let _lock: UnsafeMutablePointer<pthread_mutex_t> |
| 76 | + |
| 77 | + init(recursive: Bool = false) { |
| 78 | + _lock = .allocate(capacity: 1) |
| 79 | + _lock.initialize(to: pthread_mutex_t()) |
| 80 | + |
| 81 | + let attr = UnsafeMutablePointer<pthread_mutexattr_t>.allocate(capacity: 1) |
| 82 | + attr.initialize(to: pthread_mutexattr_t()) |
| 83 | + pthread_mutexattr_init(attr) |
| 84 | + |
| 85 | + defer { |
| 86 | + pthread_mutexattr_destroy(attr) |
| 87 | + attr.deinitialize(count: 1) |
| 88 | + attr.deallocate() |
| 89 | + } |
| 90 | + |
| 91 | + pthread_mutexattr_settype( |
| 92 | + attr, Int32(recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_ERRORCHECK)) |
| 93 | + |
| 94 | + let status = pthread_mutex_init(_lock, attr) |
| 95 | + assert(status == 0, "Unexpected pthread mutex error code: \(status)") |
| 96 | + |
| 97 | + super.init() |
| 98 | + } |
| 99 | + |
| 100 | + override func lock() { |
| 101 | + let status = pthread_mutex_lock(_lock) |
| 102 | + assert(status == 0, "Unexpected pthread mutex error code: \(status)") |
| 103 | + } |
| 104 | + |
| 105 | + override func unlock() { |
| 106 | + let status = pthread_mutex_unlock(_lock) |
| 107 | + assert(status == 0, "Unexpected pthread mutex error code: \(status)") |
| 108 | + } |
| 109 | + |
| 110 | + override func `try`() -> Bool { |
| 111 | + let status = pthread_mutex_trylock(_lock) |
| 112 | + switch status { |
| 113 | + case 0: |
| 114 | + return true |
| 115 | + case EBUSY, EAGAIN, EDEADLK: |
| 116 | + return false |
| 117 | + default: |
| 118 | + assertionFailure("Unexpected pthread mutex error code: \(status)") |
| 119 | + return false |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + deinit { |
| 124 | + let status = pthread_mutex_destroy(_lock) |
| 125 | + assert(status == 0, "Unexpected pthread mutex error code: \(status)") |
| 126 | + |
| 127 | + _lock.deinitialize(count: 1) |
| 128 | + _lock.deallocate() |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /// Return an instance of a `Lock`, according to API availability |
| 133 | + /// (`os_unfair_lock_t` or `pthread_mutex_t` based). |
| 134 | + /// |
| 135 | + /// - returns: a `Lock` instance |
| 136 | + public static func make() -> Self { |
| 137 | + #if os(macOS) |
| 138 | + if #available(*, macOS 10.12) { |
| 139 | + return UnfairLock() as! Self |
| 140 | + } |
| 141 | + #endif |
| 142 | + |
| 143 | + return PthreadLock() as! Self |
| 144 | + } |
| 145 | + |
| 146 | + private init() {} |
| 147 | + |
| 148 | + public func lock() { fatalError() } |
| 149 | + public func unlock() { fatalError() } |
| 150 | + public func `try`() -> Bool { fatalError() } |
| 151 | +} |
0 commit comments