From 50e49800756ea395306595fefc07b93e2824c3a7 Mon Sep 17 00:00:00 2001 From: Ian Partridge Date: Wed, 1 May 2019 11:43:23 +0100 Subject: [PATCH] fix: remove locking As `SyslogLogHandler` is a value type it isn't necessary to lock access to its properties. --- Sources/LoggingSyslog/Locks.swift | 193 ------------------- Sources/LoggingSyslog/SyslogLogHandler.swift | 31 +-- 2 files changed, 5 insertions(+), 219 deletions(-) delete mode 100644 Sources/LoggingSyslog/Locks.swift diff --git a/Sources/LoggingSyslog/Locks.swift b/Sources/LoggingSyslog/Locks.swift deleted file mode 100644 index 6991817..0000000 --- a/Sources/LoggingSyslog/Locks.swift +++ /dev/null @@ -1,193 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift Logging API open source project -// -// Copyright (c) 2018-2019 Apple Inc. and the Swift Logging API project authors -// Licensed under Apache License v2.0 -// -// See LICENSE.txt for license information -// See CONTRIBUTORS.txt for the list of Swift Logging API project authors -// -// SPDX-License-Identifier: Apache-2.0 -// -//===----------------------------------------------------------------------===// - -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftNIO open source project -// -// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors -// Licensed under Apache License v2.0 -// -// See LICENSE.txt for license information -// See CONTRIBUTORS.txt for the list of SwiftNIO project authors -// -// SPDX-License-Identifier: Apache-2.0 -// -//===----------------------------------------------------------------------===// - -#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) -import Darwin -#else -import Glibc -#endif - -/// A threading lock based on `libpthread` instead of `libdispatch`. -/// -/// This object provides a lock on top of a single `pthread_mutex_t`. This kind -/// of lock is safe to use with `libpthread`-based threading models, such as the -/// one used by NIO. -internal final class Lock { - fileprivate let mutex: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: 1) - - /// Create a new lock. - public init() { - let err = pthread_mutex_init(self.mutex, nil) - precondition(err == 0) - } - - deinit { - let err = pthread_mutex_destroy(self.mutex) - precondition(err == 0) - self.mutex.deallocate() - } - - /// Acquire the lock. - /// - /// Whenever possible, consider using `withLock` instead of this method and - /// `unlock`, to simplify lock handling. - public func lock() { - let err = pthread_mutex_lock(self.mutex) - precondition(err == 0) - } - - /// Release the lock. - /// - /// Whenever possible, consider using `withLock` instead of this method and - /// `lock`, to simplify lock handling. - public func unlock() { - let err = pthread_mutex_unlock(self.mutex) - precondition(err == 0) - } -} - -extension Lock { - /// Acquire the lock for the duration of the given block. - /// - /// This convenience method should be preferred to `lock` and `unlock` in - /// most situations, as it ensures that the lock will be released regardless - /// of how `body` exits. - /// - /// - Parameter body: The block to execute while holding the lock. - /// - Returns: The value returned by the block. - @inlinable - internal func withLock(_ body: () throws -> T) rethrows -> T { - self.lock() - defer { - self.unlock() - } - return try body() - } - - // specialise Void return (for performance) - @inlinable - internal func withLockVoid(_ body: () throws -> Void) rethrows { - try self.withLock(body) - } -} - -/// A threading lock based on `libpthread` instead of `libdispatch`. -/// -/// This object provides a lock on top of a single `pthread_mutex_t`. This kind -/// of lock is safe to use with `libpthread`-based threading models, such as the -/// one used by NIO. -internal final class ReadWriteLock { - fileprivate let rwlock: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: 1) - - /// Create a new lock. - public init() { - let err = pthread_rwlock_init(self.rwlock, nil) - precondition(err == 0) - } - - deinit { - let err = pthread_rwlock_destroy(self.rwlock) - precondition(err == 0) - self.rwlock.deallocate() - } - - /// Acquire a reader lock. - /// - /// Whenever possible, consider using `withLock` instead of this method and - /// `unlock`, to simplify lock handling. - public func lockRead() { - let err = pthread_rwlock_rdlock(self.rwlock) - precondition(err == 0) - } - - /// Acquire a writer lock. - /// - /// Whenever possible, consider using `withLock` instead of this method and - /// `unlock`, to simplify lock handling. - public func lockWrite() { - let err = pthread_rwlock_wrlock(self.rwlock) - precondition(err == 0) - } - - /// Release the lock. - /// - /// Whenever possible, consider using `withLock` instead of this method and - /// `lock`, to simplify lock handling. - public func unlock() { - let err = pthread_rwlock_unlock(self.rwlock) - precondition(err == 0) - } -} - -extension ReadWriteLock { - /// Acquire the reader lock for the duration of the given block. - /// - /// This convenience method should be preferred to `lock` and `unlock` in - /// most situations, as it ensures that the lock will be released regardless - /// of how `body` exits. - /// - /// - Parameter body: The block to execute while holding the lock. - /// - Returns: The value returned by the block. - @inlinable - internal func withReaderLock(_ body: () throws -> T) rethrows -> T { - self.lockRead() - defer { - self.unlock() - } - return try body() - } - - /// Acquire the writer lock for the duration of the given block. - /// - /// This convenience method should be preferred to `lock` and `unlock` in - /// most situations, as it ensures that the lock will be released regardless - /// of how `body` exits. - /// - /// - Parameter body: The block to execute while holding the lock. - /// - Returns: The value returned by the block. - @inlinable - internal func withWriterLock(_ body: () throws -> T) rethrows -> T { - self.lockWrite() - defer { - self.unlock() - } - return try body() - } - - // specialise Void return (for performance) - @inlinable - internal func withReaderLockVoid(_ body: () throws -> Void) rethrows { - try self.withReaderLock(body) - } - - // specialise Void return (for performance) - @inlinable - internal func withWriterLockVoid(_ body: () throws -> Void) rethrows { - try self.withWriterLock(body) - } -} diff --git a/Sources/LoggingSyslog/SyslogLogHandler.swift b/Sources/LoggingSyslog/SyslogLogHandler.swift index 848309f..4ba3e17 100644 --- a/Sources/LoggingSyslog/SyslogLogHandler.swift +++ b/Sources/LoggingSyslog/SyslogLogHandler.swift @@ -3,7 +3,6 @@ import Logging /// A `LogHandler` which logs to `syslog(3)`. public struct SyslogLogHandler: LogHandler { - private let lock = Lock() /// Create a `SyslogLogHandler`. public init(label: String) { @@ -12,17 +11,7 @@ public struct SyslogLogHandler: LogHandler { public let label: String - private var _logLevel: Logger.Level = .info - public var logLevel: Logger.Level { - get { - return self.lock.withLock { self._logLevel } - } - set { - self.lock.withLock { - self._logLevel = newValue - } - } - } + public var logLevel: Logger.Level = .info public func log(level: Logger.Level, message: Logger.Message, @@ -39,28 +28,18 @@ public struct SyslogLogHandler: LogHandler { } private var prettyMetadata: String? - private var _metadata = Logger.Metadata() { + public var metadata = Logger.Metadata() { didSet { - self.prettyMetadata = self.prettify(self._metadata) - } - } - public var metadata: Logger.Metadata { - get { - return self.lock.withLock { self._metadata } - } - set { - self.lock.withLock { self._metadata = newValue } + self.prettyMetadata = self.prettify(self.metadata) } } public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? { get { - return self.lock.withLock { self._metadata[metadataKey] } + return self.metadata[metadataKey] } set { - self.lock.withLock { - self._metadata[metadataKey] = newValue - } + self.metadata[metadataKey] = newValue } }