|
| 1 | +/** |
| 2 | + * Copyright 2015 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package rx.internal.operators; |
| 17 | + |
| 18 | +import java.util.concurrent.atomic.AtomicLong; |
| 19 | +import java.util.concurrent.atomic.AtomicLongFieldUpdater; |
| 20 | + |
| 21 | +/** |
| 22 | + * Utility functions for use with backpressure. |
| 23 | + * |
| 24 | + */ |
| 25 | +final class BackpressureUtils { |
| 26 | + |
| 27 | + /** |
| 28 | + * Adds {@code n} to {@code requested} field and returns the value prior to |
| 29 | + * addition once the addition is successful (uses CAS semantics). If |
| 30 | + * overflows then sets {@code requested} field to {@code Long.MAX_VALUE}. |
| 31 | + * |
| 32 | + * @param requested |
| 33 | + * atomic field updater for a request count |
| 34 | + * @param object |
| 35 | + * contains the field updated by the updater |
| 36 | + * @param n |
| 37 | + * the number of requests to add to the requested count |
| 38 | + * @return requested value just prior to successful addition |
| 39 | + */ |
| 40 | + static <T> long getAndAddRequest(AtomicLongFieldUpdater<T> requested, T object, long n) { |
| 41 | + // add n to field but check for overflow |
| 42 | + while (true) { |
| 43 | + long current = requested.get(object); |
| 44 | + long next = current + n; |
| 45 | + // check for overflow |
| 46 | + if (next < 0) |
| 47 | + next = Long.MAX_VALUE; |
| 48 | + if (requested.compareAndSet(object, current, next)) |
| 49 | + return current; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Adds {@code n} to {@code requested} and returns the value prior to addition once the |
| 55 | + * addition is successful (uses CAS semantics). If overflows then sets |
| 56 | + * {@code requested} field to {@code Long.MAX_VALUE}. |
| 57 | + * |
| 58 | + * @param requested |
| 59 | + * atomic field updater for a request count |
| 60 | + * @param object |
| 61 | + * contains the field updated by the updater |
| 62 | + * @param n |
| 63 | + * the number of requests to add to the requested count |
| 64 | + * @return requested value just prior to successful addition |
| 65 | + */ |
| 66 | + static <T> long getAndAddRequest(AtomicLong requested, long n) { |
| 67 | + // add n to field but check for overflow |
| 68 | + while (true) { |
| 69 | + long current = requested.get(); |
| 70 | + long next = current + n; |
| 71 | + // check for overflow |
| 72 | + if (next < 0) |
| 73 | + next = Long.MAX_VALUE; |
| 74 | + if (requested.compareAndSet(current, next)) |
| 75 | + return current; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments