diff options
author | Muxi Yan <mxyan@google.com> | 2018-12-21 13:39:21 -0800 |
---|---|---|
committer | Muxi Yan <mxyan@google.com> | 2019-01-07 10:27:20 -0800 |
commit | 5e10a3b037bcd20ab17428ccb765ba9464eb3644 (patch) | |
tree | 187a4a277055a8d969cabb77d605477599895731 /src/objective-c/RxLibrary/GRXImmediateSingleWriter.m | |
parent | 4ff3543465e0a8d26a5bdf934f1dc09d0703970b (diff) |
Make gRPC ObjC thread safety right
Diffstat (limited to 'src/objective-c/RxLibrary/GRXImmediateSingleWriter.m')
-rw-r--r-- | src/objective-c/RxLibrary/GRXImmediateSingleWriter.m | 30 |
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..eadad6c3b6 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; } |