aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objective-c/RxLibrary/GRXImmediateSingleWriter.m
diff options
context:
space:
mode:
authorGravatar Muxi Yan <muxi@users.noreply.github.com>2019-01-10 09:43:54 -0800
committerGravatar GitHub <noreply@github.com>2019-01-10 09:43:54 -0800
commit412c44992b187e9d749bb040b78fd74d32513d61 (patch)
tree4c2eaf030d389e00eaa31b256b1c6479dc62d54f /src/objective-c/RxLibrary/GRXImmediateSingleWriter.m
parent12bca9fa665d49df7b3c11e00126e94f03278ae6 (diff)
parent121e04bc1e44bb684d464be6788f1c0a065b1116 (diff)
Merge pull request #17578 from muxi/grpccall-safety
Make gRPC ObjC thread safety right
Diffstat (limited to 'src/objective-c/RxLibrary/GRXImmediateSingleWriter.m')
-rw-r--r--src/objective-c/RxLibrary/GRXImmediateSingleWriter.m30
1 files changed, 15 insertions, 15 deletions
diff --git a/src/objective-c/RxLibrary/GRXImmediateSingleWriter.m b/src/objective-c/RxLibrary/GRXImmediateSingleWriter.m
index 3126ae4bd1..079c11b94f 100644
--- a/src/objective-c/RxLibrary/GRXImmediateSingleWriter.m
+++ b/src/objective-c/RxLibrary/GRXImmediateSingleWriter.m
@@ -20,7 +20,6 @@
@implementation GRXImmediateSingleWriter {
id _value;
- id<GRXWriteable> _writeable;
}
@synthesize state = _state;
@@ -38,17 +37,16 @@
}
- (void)startWithWriteable:(id<GRXWriteable>)writeable {
- _state = GRXWriterStateStarted;
- _writeable = writeable;
- [writeable writeValue:_value];
- [self finish];
-}
-
-- (void)finish {
- _state = GRXWriterStateFinished;
- _value = nil;
- id<GRXWriteable> writeable = _writeable;
- _writeable = nil;
+ id copiedValue = nil;
+ @synchronized(self) {
+ if (_state != GRXWriterStateNotStarted) {
+ return;
+ }
+ copiedValue = _value;
+ _value = nil;
+ _state = GRXWriterStateFinished;
+ }
+ [writeable writeValue:copiedValue];
[writeable writesFinishedWithError:nil];
}
@@ -65,9 +63,11 @@
// the original \a map function returns a new Writer of another type. So we
// need to override this function here.
- (GRXWriter *)map:(id (^)(id))map {
- // Since _value is available when creating the object, we can simply
- // apply the map and store the output.
- _value = map(_value);
+ @synchronized(self) {
+ // Since _value is available when creating the object, we can simply
+ // apply the map and store the output.
+ _value = map(_value);
+ }
return self;
}