Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce exclusivity of access in PropertyBox. #419

Merged
merged 2 commits into from
May 31, 2017
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# master
*Please add new entries at the top.*

1. `MutableProperty` now enforces exclusivity of access. (#419, kudos to @andersio)

In other words, nested modification in `MutableProperty.modify` is now prohibited. Generally speaking, it should have extremely limited impact as in most cases the `MutableProperty` would have been deadlocked already.

1. `promoteError` can now infer the new error type from the context. (#413, kudos to @andersio)

# 2.0.0-alpha.1
Expand Down
20 changes: 15 additions & 5 deletions Sources/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ public final class Property<Value>: PropertyProtocol {
signal = relay

producer = SignalProducer { [box, signal = relay!] observer, lifetime in
box.modify { value in
box.withValue { value in
observer.send(value: value!)
if let d = signal.observe(Signal.Observer(mappingInterruptedToCompleted: observer)) {
lifetime.observeEnded(d.dispose)
Expand Down Expand Up @@ -605,7 +605,7 @@ public final class MutableProperty<Value>: ComposableMutablePropertyProtocol {
/// Setting this to a new value will notify all observers of `signal`, or
/// signals created using `producer`.
public var value: Value {
get { return box.modify { $0 } }
get { return box.value }
set { modify { $0 = newValue } }
}

Expand All @@ -621,7 +621,7 @@ public final class MutableProperty<Value>: ComposableMutablePropertyProtocol {
/// deinitialized.
public var producer: SignalProducer<Value, NoError> {
return SignalProducer { [box, signal] observer, lifetime in
box.modify { value in
box.withValue { value in
observer.send(value: value)
if let d = signal.observe(Signal.Observer(mappingInterruptedToCompleted: observer)) {
lifetime.observeEnded(d.dispose)
Expand Down Expand Up @@ -682,7 +682,7 @@ public final class MutableProperty<Value>: ComposableMutablePropertyProtocol {
/// - returns: the result of the action.
@discardableResult
public func withValue<Result>(_ action: (Value) throws -> Result) rethrows -> Result {
return try box.modify { try action($0) }
return try box.withValue { try action($0) }
}

deinit {
Expand All @@ -697,16 +697,26 @@ public final class MutableProperty<Value>: ComposableMutablePropertyProtocol {
private final class PropertyBox<Value> {
private let lock: Lock.PthreadLock
private var _value: Value
private var isModifying = false

var value: Value { return modify { $0 } }

init(_ value: Value) {
_value = value
lock = Lock.PthreadLock(recursive: true)
}

func withValue<Result>(_ action: (Value) throws -> Result) rethrows -> Result {
lock.lock()
defer { lock.unlock() }
return try action(_value)
}

func modify<Result>(didSet: (Value) -> Void = { _ in }, _ action: (inout Value) throws -> Result) rethrows -> Result {
lock.lock()
defer { didSet(_value); lock.unlock() }
guard !isModifying else { fatalError("Nested modifications violate exclusivity of access.") }
isModifying = true
defer { isModifying = false; didSet(_value); lock.unlock() }
return try action(&_value)
}
}