diff --git a/Sources/Logging/Locks.swift b/Sources/Logging/Locks.swift index 86f4f1b..5c4d32d 100644 --- a/Sources/Logging/Locks.swift +++ b/Sources/Logging/Locks.swift @@ -38,7 +38,8 @@ import Glibc /// /// 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. +/// one used by NIO. On Windows, the lock is based on the substantially similar +/// `SRWLOCK` type. internal final class Lock { #if os(Windows) fileprivate let mutex: UnsafeMutablePointer = @@ -53,8 +54,12 @@ internal final class Lock { #if os(Windows) InitializeSRWLock(self.mutex) #else - let err = pthread_mutex_init(self.mutex, nil) - precondition(err == 0) + var attr = pthread_mutexattr_t() + pthread_mutexattr_init(&attr) + pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK)) + + let err = pthread_mutex_init(self.mutex, &attr) + precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") #endif } @@ -63,7 +68,7 @@ internal final class Lock { // SRWLOCK does not need to be free'd #else let err = pthread_mutex_destroy(self.mutex) - precondition(err == 0) + precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") #endif self.mutex.deallocate() } @@ -77,7 +82,7 @@ internal final class Lock { AcquireSRWLockExclusive(self.mutex) #else let err = pthread_mutex_lock(self.mutex) - precondition(err == 0) + precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") #endif } @@ -90,7 +95,7 @@ internal final class Lock { ReleaseSRWLockExclusive(self.mutex) #else let err = pthread_mutex_unlock(self.mutex) - precondition(err == 0) + precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") #endif } } @@ -120,11 +125,12 @@ extension Lock { } } -/// A threading lock based on `libpthread` instead of `libdispatch`. +/// A reader/writer threading lock based on `libpthread` instead of `libdispatch`. /// -/// This object provides a lock on top of a single `pthread_mutex_t`. This kind +/// This object provides a lock on top of a single `pthread_rwlock_t`. This kind /// of lock is safe to use with `libpthread`-based threading models, such as the -/// one used by NIO. +/// one used by NIO. On Windows, the lock is based on the substantially similar +/// `SRWLOCK` type. internal final class ReadWriteLock { #if os(Windows) fileprivate let rwlock: UnsafeMutablePointer = @@ -141,7 +147,7 @@ internal final class ReadWriteLock { InitializeSRWLock(self.rwlock) #else let err = pthread_rwlock_init(self.rwlock, nil) - precondition(err == 0) + precondition(err == 0, "\(#function) failed in pthread_rwlock with error \(err)") #endif } @@ -150,43 +156,44 @@ internal final class ReadWriteLock { // SRWLOCK does not need to be free'd #else let err = pthread_rwlock_destroy(self.rwlock) - precondition(err == 0) + precondition(err == 0, "\(#function) failed in pthread_rwlock with error \(err)") #endif self.rwlock.deallocate() } /// Acquire a reader lock. /// - /// Whenever possible, consider using `withLock` instead of this method and - /// `unlock`, to simplify lock handling. + /// Whenever possible, consider using `withReaderLock` instead of this + /// method and `unlock`, to simplify lock handling. public func lockRead() { #if os(Windows) AcquireSRWLockShared(self.rwlock) self.shared = true #else let err = pthread_rwlock_rdlock(self.rwlock) - precondition(err == 0) + precondition(err == 0, "\(#function) failed in pthread_rwlock with error \(err)") #endif } /// Acquire a writer lock. /// - /// Whenever possible, consider using `withLock` instead of this method and - /// `unlock`, to simplify lock handling. + /// Whenever possible, consider using `withWriterLock` instead of this + /// method and `unlock`, to simplify lock handling. public func lockWrite() { #if os(Windows) AcquireSRWLockExclusive(self.rwlock) - self.shared = true + self.shared = false #else let err = pthread_rwlock_wrlock(self.rwlock) - precondition(err == 0) + precondition(err == 0, "\(#function) failed in pthread_rwlock with error \(err)") #endif } /// Release the lock. /// - /// Whenever possible, consider using `withLock` instead of this method and - /// `lock`, to simplify lock handling. + /// Whenever possible, consider using `withReaderLock` and `withWriterLock` + /// instead of this method and `lockRead` and `lockWrite`, to simplify lock + /// handling. public func unlock() { #if os(Windows) if self.shared { @@ -196,7 +203,7 @@ internal final class ReadWriteLock { } #else let err = pthread_rwlock_unlock(self.rwlock) - precondition(err == 0) + precondition(err == 0, "\(#function) failed in pthread_rwlock with error \(err)") #endif } } @@ -204,11 +211,11 @@ internal final class ReadWriteLock { 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. + /// This convenience method should be preferred to `lockRead` 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. + /// - Parameter body: The block to execute while holding the reader lock. /// - Returns: The value returned by the block. @inlinable internal func withReaderLock(_ body: () throws -> T) rethrows -> T { @@ -221,11 +228,11 @@ extension ReadWriteLock { /// 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. + /// This convenience method should be preferred to `lockWrite` 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. + /// - Parameter body: The block to execute while holding the writer lock. /// - Returns: The value returned by the block. @inlinable internal func withWriterLock(_ body: () throws -> T) rethrows -> T {