| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.util; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.AtomicLong; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 19 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 20 |
|
| 21 |
/** |
| 22 |
* Utility class to help with backpressure-related operations such as request aggregation. |
| 23 |
*/ |
| 24 |
public final class BackpressureHelper { |
| 25 |
/** Utility class. */ |
| 26 |
private BackpressureHelper() { |
| 27 |
throw new IllegalStateException("No instances!"); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Adds two long values and caps the sum at {@link Long#MAX_VALUE}. |
| 32 |
* @param a the first value |
| 33 |
* @param b the second value |
| 34 |
* @return the sum capped at {@link Long#MAX_VALUE} |
| 35 |
*/ |
| 36 |
public static long addCap(long a, long b) { |
| 37 |
long u = a + b; |
| 38 |
if (u < 0L) { |
| 39 |
return Long.MAX_VALUE; |
| 40 |
} |
| 41 |
return u; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Multiplies two long values and caps the product at {@link Long#MAX_VALUE}. |
| 46 |
* @param a the first value |
| 47 |
* @param b the second value |
| 48 |
* @return the product capped at {@link Long#MAX_VALUE} |
| 49 |
*/ |
| 50 |
public static long multiplyCap(long a, long b) { |
| 51 |
long u = a * b; |
| 52 |
if (((a | b) >>> 31) != 0) { |
| 53 |
if (u / a != b) { |
| 54 |
return Long.MAX_VALUE; |
| 55 |
} |
| 56 |
} |
| 57 |
return u; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Atomically adds the positive value n to the requested value in the {@link AtomicLong} and |
| 62 |
* caps the result at {@link Long#MAX_VALUE} and returns the previous value. |
| 63 |
* @param requested the {@code AtomicLong} holding the current requested value |
| 64 |
* @param n the value to add, must be positive (not verified) |
| 65 |
* @return the original value before the add |
| 66 |
*/ |
| 67 |
public static long add(@NonNull AtomicLong requested, long n) { |
| 68 |
for (;;) { |
| 69 |
long r = requested.get(); |
| 70 |
if (r == Long.MAX_VALUE) { |
| 71 |
return Long.MAX_VALUE; |
| 72 |
} |
| 73 |
long u = addCap(r, n); |
| 74 |
if (requested.compareAndSet(r, u)) { |
| 75 |
return r; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Atomically adds the positive value n to the requested value in the {@link AtomicLong} and |
| 82 |
* caps the result at {@link Long#MAX_VALUE} and returns the previous value and |
| 83 |
* considers {@link Long#MIN_VALUE} as a cancel indication (no addition then). |
| 84 |
* @param requested the {@code AtomicLong} holding the current requested value |
| 85 |
* @param n the value to add, must be positive (not verified) |
| 86 |
* @return the original value before the add |
| 87 |
*/ |
| 88 |
public static long addCancel(@NonNull AtomicLong requested, long n) { |
| 89 |
for (;;) { |
| 90 |
long r = requested.get(); |
| 91 |
if (r == Long.MIN_VALUE) { |
| 92 |
return Long.MIN_VALUE; |
| 93 |
} |
| 94 |
if (r == Long.MAX_VALUE) { |
| 95 |
return Long.MAX_VALUE; |
| 96 |
} |
| 97 |
long u = addCap(r, n); |
| 98 |
if (requested.compareAndSet(r, u)) { |
| 99 |
return r; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Atomically subtract the given number (positive, not validated) from the target field unless it contains {@link Long#MAX_VALUE}. |
| 106 |
* @param requested the target field holding the current requested amount |
| 107 |
* @param n the produced element count, positive (not validated) |
| 108 |
* @return the new amount |
| 109 |
*/ |
| 110 |
public static long produced(@NonNull AtomicLong requested, long n) { |
| 111 |
for (;;) { |
| 112 |
long current = requested.get(); |
| 113 |
if (current == Long.MAX_VALUE) { |
| 114 |
return Long.MAX_VALUE; |
| 115 |
} |
| 116 |
long update = current - n; |
| 117 |
if (update < 0L) { |
| 118 |
RxJavaPlugins.onError(new IllegalStateException("More produced than requested: " + update)); |
| 119 |
update = 0L; |
| 120 |
} |
| 121 |
if (requested.compareAndSet(current, update)) { |
| 122 |
return update; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Atomically subtract the given number (positive, not validated) from the target field if |
| 129 |
* it doesn't contain {@link Long#MIN_VALUE} (indicating some cancelled state) or {@link Long#MAX_VALUE} (unbounded mode). |
| 130 |
* @param requested the target field holding the current requested amount |
| 131 |
* @param n the produced element count, positive (not validated) |
| 132 |
* @return the new amount |
| 133 |
*/ |
| 134 |
public static long producedCancel(@NonNull AtomicLong requested, long n) { |
| 135 |
for (;;) { |
| 136 |
long current = requested.get(); |
| 137 |
if (current == Long.MIN_VALUE) { |
| 138 |
return Long.MIN_VALUE; |
| 139 |
} |
| 140 |
if (current == Long.MAX_VALUE) { |
| 141 |
return Long.MAX_VALUE; |
| 142 |
} |
| 143 |
long update = current - n; |
| 144 |
if (update < 0L) { |
| 145 |
RxJavaPlugins.onError(new IllegalStateException("More produced than requested: " + update)); |
| 146 |
update = 0L; |
| 147 |
} |
| 148 |
if (requested.compareAndSet(current, update)) { |
| 149 |
return update; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |