Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Yang Gao | 5fc9029 | 2015-02-20 09:46:22 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Jorge Canizales | 35f003b | 2015-07-17 21:14:36 -0700 | [diff] [blame] | 34 | #import "GRXConcurrentWriteable.h" |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 35 | |
Jorge Canizales | 3936ed7 | 2015-06-21 14:43:56 -0700 | [diff] [blame] | 36 | #import <RxLibrary/GRXWriteable.h> |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 37 | |
Jorge Canizales | 35f003b | 2015-07-17 21:14:36 -0700 | [diff] [blame] | 38 | @interface GRXConcurrentWriteable () |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 39 | // This is atomic so that cancellation can nillify it from any thread. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 40 | @property(atomic, strong) id<GRXWriteable> writeable; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 41 | @end |
| 42 | |
Jorge Canizales | 35f003b | 2015-07-17 21:14:36 -0700 | [diff] [blame] | 43 | @implementation GRXConcurrentWriteable { |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 44 | dispatch_queue_t _writeableQueue; |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 45 | // This ensures that writesFinishedWithError: is only sent once to the writeable. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 46 | dispatch_once_t _alreadyFinished; |
| 47 | } |
| 48 | |
| 49 | - (instancetype)init { |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 50 | return [self initWithWriteable:nil]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // Designated initializer |
Muxi Yan | 895f3d8 | 2017-04-05 13:12:30 -0700 | [diff] [blame] | 54 | - (instancetype)initWithWriteable:(id<GRXWriteable>)writeable |
| 55 | dispatchQueue:(dispatch_queue_t)queue { |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 56 | if (self = [super init]) { |
Muxi Yan | 895f3d8 | 2017-04-05 13:12:30 -0700 | [diff] [blame] | 57 | _writeableQueue = queue; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 58 | _writeable = writeable; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 59 | } |
| 60 | return self; |
| 61 | } |
| 62 | |
Muxi Yan | 895f3d8 | 2017-04-05 13:12:30 -0700 | [diff] [blame] | 63 | - (instancetype)initWithWriteable:(id<GRXWriteable>)writeable { |
| 64 | return [self initWithWriteable:writeable |
| 65 | dispatchQueue:dispatch_get_main_queue()]; |
| 66 | } |
| 67 | |
Jorge Canizales | 4c6f778 | 2015-07-17 23:13:36 -0700 | [diff] [blame] | 68 | - (void)enqueueValue:(id)value completionHandler:(void (^)())handler { |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 69 | dispatch_async(_writeableQueue, ^{ |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 70 | // We're racing a possible cancellation performed by another thread. To turn all already- |
| 71 | // enqueued messages into noops, cancellation nillifies the writeable property. If we get it |
| 72 | // before it's nil, we won the race. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 73 | id<GRXWriteable> writeable = self.writeable; |
| 74 | if (writeable) { |
Jorge Canizales | 4c6f778 | 2015-07-17 23:13:36 -0700 | [diff] [blame] | 75 | [writeable writeValue:value]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 76 | handler(); |
| 77 | } |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | - (void)enqueueSuccessfulCompletion { |
| 82 | dispatch_async(_writeableQueue, ^{ |
| 83 | dispatch_once(&_alreadyFinished, ^{ |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 84 | // Cancellation is now impossible. None of the other three blocks can run concurrently with |
| 85 | // this one. |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 86 | [self.writeable writesFinishedWithError:nil]; |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 87 | // Skip any possible message to the wrapped writeable enqueued after this one. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 88 | self.writeable = nil; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 89 | }); |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | - (void)cancelWithError:(NSError *)error { |
| 94 | NSAssert(error, @"For a successful completion, use enqueueSuccessfulCompletion."); |
| 95 | dispatch_once(&_alreadyFinished, ^{ |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 96 | // Skip any of the still-enqueued messages to the wrapped writeable. We use the atomic setter to |
| 97 | // nillify writeable because we might be running concurrently with the blocks in |
| 98 | // _writeableQueue, and assignment with ARC isn't atomic. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 99 | id<GRXWriteable> writeable = self.writeable; |
| 100 | self.writeable = nil; |
| 101 | |
| 102 | dispatch_async(_writeableQueue, ^{ |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 103 | [writeable writesFinishedWithError:error]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 104 | }); |
| 105 | }); |
| 106 | } |
| 107 | |
| 108 | - (void)cancelSilently { |
| 109 | dispatch_once(&_alreadyFinished, ^{ |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 110 | // Skip any of the still-enqueued messages to the wrapped writeable. We use the atomic setter to |
| 111 | // nillify writeable because we might be running concurrently with the blocks in |
| 112 | // _writeableQueue, and assignment with ARC isn't atomic. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 113 | self.writeable = nil; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 114 | }); |
| 115 | } |
| 116 | @end |