|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +//===----------------------------------------------------------------------===// |
| 7 | +// |
| 8 | +// This source file is part of the Swift Metrics API open source project |
| 9 | +// |
| 10 | +// Copyright (c) 2018-2019 Apple Inc. and the Swift Metrics API project authors |
| 11 | +// Licensed under Apache License v2.0 |
| 12 | +// |
| 13 | +// See LICENSE.txt for license information |
| 14 | +// See CONTRIBUTORS.txt for the list of Swift Metrics API project authors |
| 15 | +// |
| 16 | +// SPDX-License-Identifier: Apache-2.0 |
| 17 | +// |
| 18 | +//===----------------------------------------------------------------------===// |
| 19 | + |
| 20 | +//===----------------------------------------------------------------------===// |
| 21 | +// |
| 22 | +// This source file is part of the SwiftNIO open source project |
| 23 | +// |
| 24 | +// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors |
| 25 | +// Licensed under Apache License v2.0 |
| 26 | +// |
| 27 | +// See LICENSE.txt for license information |
| 28 | +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 29 | +// |
| 30 | +// SPDX-License-Identifier: Apache-2.0 |
| 31 | +// |
| 32 | +//===----------------------------------------------------------------------===// |
| 33 | + |
| 34 | +#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) |
| 35 | +import Darwin |
| 36 | +#else |
| 37 | +import Glibc |
| 38 | +#endif |
| 39 | + |
| 40 | +/// A threading lock based on `libpthread` instead of `libdispatch`. |
| 41 | +/// |
| 42 | +/// This object provides a lock on top of a single `pthread_mutex_t`. This kind |
| 43 | +/// of lock is safe to use with `libpthread`-based threading models, such as the |
| 44 | +/// one used by NIO. |
| 45 | +internal final class Lock { |
| 46 | + fileprivate let mutex: UnsafeMutablePointer<pthread_mutex_t> = UnsafeMutablePointer.allocate(capacity: 1) |
| 47 | + |
| 48 | + /// Create a new lock. |
| 49 | + public init() { |
| 50 | + let err = pthread_mutex_init(self.mutex, nil) |
| 51 | + precondition(err == 0, "pthread_mutex_init failed with error \(err)") |
| 52 | + } |
| 53 | + |
| 54 | + deinit { |
| 55 | + let err = pthread_mutex_destroy(self.mutex) |
| 56 | + precondition(err == 0, "pthread_mutex_destroy failed with error \(err)") |
| 57 | + self.mutex.deallocate() |
| 58 | + } |
| 59 | + |
| 60 | + /// Acquire the lock. |
| 61 | + /// |
| 62 | + /// Whenever possible, consider using `withLock` instead of this method and |
| 63 | + /// `unlock`, to simplify lock handling. |
| 64 | + public func lock() { |
| 65 | + let err = pthread_mutex_lock(self.mutex) |
| 66 | + precondition(err == 0, "pthread_mutex_lock failed with error \(err)") |
| 67 | + } |
| 68 | + |
| 69 | + /// Release the lock. |
| 70 | + /// |
| 71 | + /// Whenever possible, consider using `withLock` instead of this method and |
| 72 | + /// `lock`, to simplify lock handling. |
| 73 | + public func unlock() { |
| 74 | + let err = pthread_mutex_unlock(self.mutex) |
| 75 | + precondition(err == 0, "pthread_mutex_unlock failed with error \(err)") |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +extension Lock { |
| 80 | + /// Acquire the lock for the duration of the given block. |
| 81 | + /// |
| 82 | + /// This convenience method should be preferred to `lock` and `unlock` in |
| 83 | + /// most situations, as it ensures that the lock will be released regardless |
| 84 | + /// of how `body` exits. |
| 85 | + /// |
| 86 | + /// - Parameter body: The block to execute while holding the lock. |
| 87 | + /// - Returns: The value returned by the block. |
| 88 | + @inlinable |
| 89 | + internal func withLock<T>(_ body: () throws -> T) rethrows -> T { |
| 90 | + self.lock() |
| 91 | + defer { |
| 92 | + self.unlock() |
| 93 | + } |
| 94 | + return try body() |
| 95 | + } |
| 96 | + |
| 97 | + // specialise Void return (for performance) |
| 98 | + @inlinable |
| 99 | + internal func withLockVoid(_ body: () throws -> Void) rethrows { |
| 100 | + try self.withLock(body) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/// A threading lock based on `libpthread` instead of `libdispatch`. |
| 105 | +/// |
| 106 | +/// This object provides a lock on top of a single `pthread_mutex_t`. This kind |
| 107 | +/// of lock is safe to use with `libpthread`-based threading models, such as the |
| 108 | +/// one used by NIO. |
| 109 | +internal final class ReadWriteLock { |
| 110 | + fileprivate let rwlock: UnsafeMutablePointer<pthread_rwlock_t> = UnsafeMutablePointer.allocate(capacity: 1) |
| 111 | + |
| 112 | + /// Create a new lock. |
| 113 | + public init() { |
| 114 | + let err = pthread_rwlock_init(self.rwlock, nil) |
| 115 | + precondition(err == 0, "pthread_rwlock_init failed with error \(err)") |
| 116 | + } |
| 117 | + |
| 118 | + deinit { |
| 119 | + let err = pthread_rwlock_destroy(self.rwlock) |
| 120 | + precondition(err == 0, "pthread_rwlock_destroy failed with error \(err)") |
| 121 | + self.rwlock.deallocate() |
| 122 | + } |
| 123 | + |
| 124 | + /// Acquire a reader lock. |
| 125 | + /// |
| 126 | + /// Whenever possible, consider using `withLock` instead of this method and |
| 127 | + /// `unlock`, to simplify lock handling. |
| 128 | + public func lockRead() { |
| 129 | + let err = pthread_rwlock_rdlock(self.rwlock) |
| 130 | + precondition(err == 0, "pthread_rwlock_rdlock failed with error \(err)") |
| 131 | + } |
| 132 | + |
| 133 | + /// Acquire a writer lock. |
| 134 | + /// |
| 135 | + /// Whenever possible, consider using `withLock` instead of this method and |
| 136 | + /// `unlock`, to simplify lock handling. |
| 137 | + public func lockWrite() { |
| 138 | + let err = pthread_rwlock_wrlock(self.rwlock) |
| 139 | + precondition(err == 0, "pthread_rwlock_wrlock failed with error \(err)") |
| 140 | + } |
| 141 | + |
| 142 | + /// Release the lock. |
| 143 | + /// |
| 144 | + /// Whenever possible, consider using `withLock` instead of this method and |
| 145 | + /// `lock`, to simplify lock handling. |
| 146 | + public func unlock() { |
| 147 | + let err = pthread_rwlock_unlock(self.rwlock) |
| 148 | + precondition(err == 0, "pthread_rwlock_unlock failed with error \(err)") |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +extension ReadWriteLock { |
| 153 | + /// Acquire the reader lock for the duration of the given block. |
| 154 | + /// |
| 155 | + /// This convenience method should be preferred to `lock` and `unlock` in |
| 156 | + /// most situations, as it ensures that the lock will be released regardless |
| 157 | + /// of how `body` exits. |
| 158 | + /// |
| 159 | + /// - Parameter body: The block to execute while holding the lock. |
| 160 | + /// - Returns: The value returned by the block. |
| 161 | + @inlinable |
| 162 | + internal func withReaderLock<T>(_ body: () throws -> T) rethrows -> T { |
| 163 | + self.lockRead() |
| 164 | + defer { |
| 165 | + self.unlock() |
| 166 | + } |
| 167 | + return try body() |
| 168 | + } |
| 169 | + |
| 170 | + /// Acquire the writer lock for the duration of the given block. |
| 171 | + /// |
| 172 | + /// This convenience method should be preferred to `lock` and `unlock` in |
| 173 | + /// most situations, as it ensures that the lock will be released regardless |
| 174 | + /// of how `body` exits. |
| 175 | + /// |
| 176 | + /// - Parameter body: The block to execute while holding the lock. |
| 177 | + /// - Returns: The value returned by the block. |
| 178 | + @inlinable |
| 179 | + internal func withWriterLock<T>(_ body: () throws -> T) rethrows -> T { |
| 180 | + self.lockWrite() |
| 181 | + defer { |
| 182 | + self.unlock() |
| 183 | + } |
| 184 | + return try body() |
| 185 | + } |
| 186 | + |
| 187 | + // specialise Void return (for performance) |
| 188 | + @inlinable |
| 189 | + internal func withReaderLockVoid(_ body: () throws -> Void) rethrows { |
| 190 | + try self.withReaderLock(body) |
| 191 | + } |
| 192 | + |
| 193 | + // specialise Void return (for performance) |
| 194 | + @inlinable |
| 195 | + internal func withWriterLockVoid(_ body: () throws -> Void) rethrows { |
| 196 | + try self.withWriterLock(body) |
| 197 | + } |
| 198 | +} |
0 commit comments