GRXConcurrentWriteable.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #import "GRXConcurrentWriteable.h"
  19. #import <RxLibrary/GRXWriteable.h>
  20. @interface GRXConcurrentWriteable ()
  21. // This is atomic so that cancellation can nillify it from any thread.
  22. @property(atomic, strong) id<GRXWriteable> writeable;
  23. @end
  24. @implementation GRXConcurrentWriteable {
  25. dispatch_queue_t _writeableQueue;
  26. // This ivar ensures that writesFinishedWithError: is only sent once to the writeable. Protected
  27. // by _writeableQueue.
  28. BOOL _alreadyFinished;
  29. // This ivar ensures that a cancelWithError: call prevents further values to be sent to
  30. // self.writeable. It must support manipulation outside of _writeableQueue and thus needs to be
  31. // protected by self lock.
  32. BOOL _cancelled;
  33. }
  34. - (instancetype)init {
  35. return [self initWithWriteable:nil];
  36. }
  37. // Designated initializer
  38. - (instancetype)initWithWriteable:(id<GRXWriteable>)writeable
  39. dispatchQueue:(dispatch_queue_t)queue {
  40. if (self = [super init]) {
  41. _writeableQueue = queue;
  42. _writeable = writeable;
  43. _alreadyFinished = NO;
  44. _cancelled = NO;
  45. }
  46. return self;
  47. }
  48. - (instancetype)initWithWriteable:(id<GRXWriteable>)writeable {
  49. return [self initWithWriteable:writeable dispatchQueue:dispatch_get_main_queue()];
  50. }
  51. - (void)enqueueValue:(id)value completionHandler:(void (^)(void))handler {
  52. dispatch_async(_writeableQueue, ^{
  53. if (self->_alreadyFinished) {
  54. return;
  55. }
  56. @synchronized(self) {
  57. if (self->_cancelled) {
  58. return;
  59. }
  60. }
  61. [self.writeable writeValue:value];
  62. handler();
  63. });
  64. }
  65. - (void)enqueueSuccessfulCompletion {
  66. dispatch_async(_writeableQueue, ^{
  67. if (self->_alreadyFinished) {
  68. return;
  69. }
  70. @synchronized(self) {
  71. if (self->_cancelled) {
  72. return;
  73. }
  74. }
  75. [self.writeable writesFinishedWithError:nil];
  76. // Skip any possible message to the wrapped writeable enqueued after this one.
  77. self->_alreadyFinished = YES;
  78. self.writeable = nil;
  79. });
  80. }
  81. - (void)cancelWithError:(NSError *)error {
  82. NSAssert(error != nil, @"For a successful completion, use enqueueSuccessfulCompletion.");
  83. @synchronized(self) {
  84. self->_cancelled = YES;
  85. }
  86. dispatch_async(_writeableQueue, ^{
  87. if (self->_alreadyFinished) {
  88. // a cancel or a successful completion is already issued
  89. return;
  90. }
  91. [self.writeable writesFinishedWithError:error];
  92. // Skip any possible message to the wrapped writeable enqueued after this one.
  93. self->_alreadyFinished = YES;
  94. self.writeable = nil;
  95. });
  96. }
  97. - (void)cancelSilently {
  98. dispatch_async(_writeableQueue, ^{
  99. if (self->_alreadyFinished) {
  100. return;
  101. }
  102. self.writeable = nil;
  103. });
  104. }
  105. @end