Skip to content

Commit

Permalink
Merge branch 'master' into cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
ktoso authored Feb 27, 2020
2 parents a26240c + 1b5f8a5 commit 80de74d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ DerivedData

.SourceKitten/
docs/

.*.sw?
61 changes: 59 additions & 2 deletions Sources/Logging/Locks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import Darwin
#elseif os(Windows)
import WinSDK
#else
import Glibc
#endif
Expand All @@ -38,17 +40,31 @@ import Glibc
/// 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<pthread_mutex_t> = UnsafeMutablePointer.allocate(capacity: 1)
#if os(Windows)
fileprivate let mutex: UnsafeMutablePointer<SRWLOCK> =
UnsafeMutablePointer.allocate(capacity: 1)
#else
fileprivate let mutex: UnsafeMutablePointer<pthread_mutex_t> =
UnsafeMutablePointer.allocate(capacity: 1)
#endif

/// Create a new lock.
public init() {
#if os(Windows)
InitializeSRWLock(self.mutex)
#else
let err = pthread_mutex_init(self.mutex, nil)
precondition(err == 0)
#endif
}

deinit {
#if os(Windows)
// SRWLOCK does not need to be free'd
#else
let err = pthread_mutex_destroy(self.mutex)
precondition(err == 0)
#endif
self.mutex.deallocate()
}

Expand All @@ -57,17 +73,25 @@ internal final class Lock {
/// Whenever possible, consider using `withLock` instead of this method and
/// `unlock`, to simplify lock handling.
public func lock() {
#if os(Windows)
AcquireSRWLockExclusive(self.mutex)
#else
let err = pthread_mutex_lock(self.mutex)
precondition(err == 0)
#endif
}

/// Release the lock.
///
/// Whenever possible, consider using `withLock` instead of this method and
/// `lock`, to simplify lock handling.
public func unlock() {
#if os(Windows)
ReleaseSRWLockExclusive(self.mutex)
#else
let err = pthread_mutex_unlock(self.mutex)
precondition(err == 0)
#endif
}
}

Expand Down Expand Up @@ -102,17 +126,32 @@ extension Lock {
/// 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<pthread_rwlock_t> = UnsafeMutablePointer.allocate(capacity: 1)
#if os(Windows)
fileprivate let rwlock: UnsafeMutablePointer<SRWLOCK> =
UnsafeMutablePointer.allocate(capacity: 1)
fileprivate var shared: Bool = true
#else
fileprivate let rwlock: UnsafeMutablePointer<pthread_rwlock_t> =
UnsafeMutablePointer.allocate(capacity: 1)
#endif

/// Create a new lock.
public init() {
#if os(Windows)
InitializeSRWLock(self.rwlock)
#else
let err = pthread_rwlock_init(self.rwlock, nil)
precondition(err == 0)
#endif
}

deinit {
#if os(Windows)
// SRWLOCK does not need to be free'd
#else
let err = pthread_rwlock_destroy(self.rwlock)
precondition(err == 0)
#endif
self.rwlock.deallocate()
}

Expand All @@ -121,26 +160,44 @@ internal final class ReadWriteLock {
/// Whenever possible, consider using `withLock` 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)
#endif
}

/// Acquire a writer lock.
///
/// Whenever possible, consider using `withLock` instead of this method and
/// `unlock`, to simplify lock handling.
public func lockWrite() {
#if os(Windows)
AcquireSRWLockExclusive(self.rwlock)
self.shared = true
#else
let err = pthread_rwlock_wrlock(self.rwlock)
precondition(err == 0)
#endif
}

/// Release the lock.
///
/// Whenever possible, consider using `withLock` instead of this method and
/// `lock`, to simplify lock handling.
public func unlock() {
#if os(Windows)
if self.shared {
ReleaseSRWLockShared(self.rwlock)
} else {
ReleaseSRWLockExclusive(self.rwlock)
}
#else
let err = pthread_rwlock_unlock(self.rwlock)
precondition(err == 0)
#endif
}
}

Expand Down
13 changes: 13 additions & 0 deletions Sources/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import Darwin
#elseif os(Windows)
import MSVCRT
#else
import Glibc
#endif
Expand Down Expand Up @@ -528,9 +530,17 @@ internal struct StdioOutputStream: TextOutputStream {

internal func write(_ string: String) {
string.withCString { ptr in
#if os(Windows)
_lock_file(self.file)
#else
flockfile(self.file)
#endif
defer {
#if os(Windows)
_unlock_file(self.file)
#else
funlockfile(self.file)
#endif
}
_ = fputs(ptr, self.file)
if case .always = self.flushMode {
Expand Down Expand Up @@ -559,6 +569,9 @@ internal struct StdioOutputStream: TextOutputStream {
#if os(macOS) || os(tvOS) || os(iOS) || os(watchOS)
let systemStderr = Darwin.stderr
let systemStdout = Darwin.stdout
#elseif os(Windows)
let systemStderr = MSVCRT.stderr
let systemStdout = MSVCRT.stdout
#else
let systemStderr = Glibc.stderr!
let systemStdout = Glibc.stdout!
Expand Down

0 comments on commit 80de74d

Please sign in to comment.