Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 81 additions & 17 deletions Sources/Atomic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,33 +104,97 @@ internal struct UnsafeAtomicState<State: RawRepresentable> where State.RawValue
#endif
}

final class PosixThreadMutex: NSLocking {
private var mutex = pthread_mutex_t()
/// `Lock` exposes `os_unfair_lock` on supported platforms, with pthread mutex as the
// fallback.
internal class Lock {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bummer that this uses both #if os and @available :(

@available(iOS 10.0, *)
@available(macOS 10.12, *)
@available(tvOS 10.0, *)
@available(watchOS 3.0, *)
internal final class UnfairLock: Lock {
private var _lock: os_unfair_lock

override init() {
_lock = os_unfair_lock()
super.init()
}

init() {
let result = pthread_mutex_init(&mutex, nil)
precondition(result == 0, "Failed to initialize mutex with error \(result).")
}
override func lock() {
withUnsafeMutablePointer(to: &_lock, os_unfair_lock_lock)
}

deinit {
let result = pthread_mutex_destroy(&mutex)
precondition(result == 0, "Failed to destroy mutex with error \(result).")
override func unlock() {
withUnsafeMutablePointer(to: &_lock, os_unfair_lock_unlock)
}

override func `try`() -> Bool {
return withUnsafeMutablePointer(to: &_lock, os_unfair_lock_trylock)
}
}
#endif

internal final class PthreadLock: Lock {
private var _lock: pthread_mutex_t

override init() {
_lock = pthread_mutex_t()

let status = withUnsafeMutablePointer(to: &_lock) { pthread_mutex_init($0, nil) }
assert(status == 0, "Unexpected pthread mutex error code: \(status)")

super.init()
}

override func lock() {
let status = withUnsafeMutablePointer(to: &_lock, pthread_mutex_lock)
assert(status == 0, "Unexpected pthread mutex error code: \(status)")
}

override func unlock() {
let status = withUnsafeMutablePointer(to: &_lock, pthread_mutex_unlock)
assert(status == 0, "Unexpected pthread mutex error code: \(status)")
}

override func `try`() -> Bool {
let status = withUnsafeMutablePointer(to: &_lock, pthread_mutex_trylock)
switch status {
case 0:
return true
case EBUSY:
return false
default:
assertionFailure("Unexpected pthread mutex error code: \(status)")
return false
}
}

func lock() {
let result = pthread_mutex_lock(&mutex)
precondition(result == 0, "Failed to lock \(self) with error \(result).")
deinit {
let status = withUnsafeMutablePointer(to: &_lock, pthread_mutex_destroy)
assert(status == 0, "Unexpected pthread mutex error code: \(status)")
}
}

func unlock() {
let result = pthread_mutex_unlock(&mutex)
precondition(result == 0, "Failed to unlock \(self) with error \(result).")
static func make() -> Lock {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
if #available(*, iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0) {
return UnfairLock()
}
#endif

return PthreadLock()
}

private init() {}

func lock() { fatalError() }
func unlock() { fatalError() }
func `try`() -> Bool { fatalError() }
}

/// An atomic variable.
public final class Atomic<Value> {
private let lock: PosixThreadMutex
private let lock: Lock
private var _value: Value

/// Atomically get or set the value of the variable.
Expand All @@ -150,7 +214,7 @@ public final class Atomic<Value> {
/// - value: Initial value for `self`.
public init(_ value: Value) {
_value = value
lock = PosixThreadMutex()
lock = Lock.make()
}

/// Atomically modifies the variable.
Expand Down
15 changes: 6 additions & 9 deletions Sources/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public final class Signal<Value, Error: Swift.Error> {
private var state: SignalState<Value, Error>

/// Used to ensure that state updates are serialized.
private let updateLock: NSLock
private let updateLock: Lock

/// Used to ensure that events are serialized during delivery to observers.
private let sendLock: NSLock
private let sendLock: Lock

/// Initialize a Signal that will immediately invoke the given generator,
/// then forward events sent to the given observer.
Expand All @@ -57,10 +57,8 @@ public final class Signal<Value, Error: Swift.Error> {
/// that will act as an event emitter for the signal.
public init(_ generator: (Observer) -> Disposable?) {
state = .alive(AliveState())
updateLock = NSLock()
updateLock.name = "org.reactivecocoa.ReactiveSwift.Signal.updateLock"
sendLock = NSLock()
sendLock.name = "org.reactivecocoa.ReactiveSwift.Signal.sendLock"
updateLock = Lock.make()
sendLock = Lock.make()

let observer = Observer { [weak self] event in
guard let signal = self else {
Expand Down Expand Up @@ -893,7 +891,7 @@ private final class CombineLatestState<Value> {
}

extension Signal {
private func observeWithStates<U>(_ signalState: CombineLatestState<Value>, _ otherState: CombineLatestState<U>, _ lock: NSLock, _ observer: Signal<(), Error>.Observer) -> Disposable? {
private func observeWithStates<U>(_ signalState: CombineLatestState<Value>, _ otherState: CombineLatestState<U>, _ lock: Lock, _ observer: Signal<(), Error>.Observer) -> Disposable? {
return self.observe { event in
switch event {
case let .value(value):
Expand Down Expand Up @@ -944,8 +942,7 @@ extension Signal {
/// and given signal.
public func combineLatest<U>(with other: Signal<U, Error>) -> Signal<(Value, U), Error> {
return Signal<(Value, U), Error> { observer in
let lock = NSLock()
lock.name = "org.reactivecocoa.ReactiveSwift.combineLatestWith"
let lock = Lock.make()

let signalState = CombineLatestState<Value>()
let otherState = CombineLatestState<U>()
Expand Down