| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | package org.webrtc; |
| 12 |
| 13 | import android.os.Handler; |
| 14 | import android.os.Looper; |
| 15 | import android.os.SystemClock; |
| 16 |
| 17 | import androidx.annotation.Nullable; |
| 18 |
| 19 | import org.telegram.messenger.BuildVars; |
| 20 |
| 21 | import java.util.concurrent.Callable; |
| 22 | import java.util.concurrent.CountDownLatch; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 |
| 25 | public class ThreadUtils { |
| 26 | /** |
| 27 | * Utility class to be used for checking that a method is called on the correct thread. |
| 28 | */ |
| 29 | public static class ThreadChecker { |
| 30 | @Nullable private Thread thread = Thread.currentThread(); |
| 31 |
| 32 | public void checkIsOnValidThread() { |
| 33 | if (thread == null) { |
| 34 | thread = Thread.currentThread(); |
| 35 | } |
| 36 | if (Thread.currentThread() != thread) { |
| 37 | throw new IllegalStateException("Wrong thread"); |
| 38 | } |
| 39 | } |
| 40 |
| 41 | public void detachThread() { |
| 42 | thread = null; |
| 43 | } |
| 44 | } |
| 45 |
| 46 | /** |
| 47 | * Throws exception if called from other than main thread. |
| 48 | */ |
| 49 | public static void checkIsOnMainThread() { |
| 50 | if (BuildVars.DEBUG_PRIVATE_VERSION && Thread.currentThread() != Looper.getMainLooper().getThread()) { |
| 51 | throw new IllegalStateException("Not on main thread!"); |
| 52 | } |
| 53 | } |
| 54 |
| 55 | /** |
| 56 | * Utility interface to be used with executeUninterruptibly() to wait for blocking operations |
| 57 | * to complete without getting interrupted.. |
| 58 | */ |
| 59 | public interface BlockingOperation { void run() throws InterruptedException; } |
| 60 |
| 61 | /** |
| 62 | * Utility method to make sure a blocking operation is executed to completion without getting |
| 63 | * interrupted. This should be used in cases where the operation is waiting for some critical |
| 64 | * work, e.g. cleanup, that must complete before returning. If the thread is interrupted during |
| 65 | * the blocking operation, this function will re-run the operation until completion, and only then |
| 66 | * re-interrupt the thread. |
| 67 | */ |
| 68 | public static void executeUninterruptibly(BlockingOperation operation) { |
| 69 | boolean wasInterrupted = false; |
| 70 | while (true) { |
| 71 | try { |
| 72 | operation.run(); |
| 73 | break; |
| 74 | } catch (InterruptedException e) { |
| 75 | // Someone is asking us to return early at our convenience. We can't cancel this operation, |
| 76 | // but we should preserve the information and pass it along. |
| 77 | wasInterrupted = true; |
| 78 | } |
| 79 | } |
| 80 | // Pass interruption information along. |
| 81 | if (wasInterrupted) { |
| 82 | Thread.currentThread().interrupt(); |
| 83 | } |
| 84 | } |
| 85 |
| 86 | public static boolean joinUninterruptibly(final Thread thread, long timeoutMs) { |
| 87 | final long startTimeMs = SystemClock.elapsedRealtime(); |
| 88 | long timeRemainingMs = timeoutMs; |
| 89 | boolean wasInterrupted = false; |
| 90 | while (timeRemainingMs > 0) { |
| 91 | try { |
| 92 | thread.join(timeRemainingMs); |
| 93 | break; |
| 94 | } catch (InterruptedException e) { |
| 95 | // Someone is asking us to return early at our convenience. We can't cancel this operation, |
| 96 | // but we should preserve the information and pass it along. |
| 97 | wasInterrupted = true; |
| 98 | final long elapsedTimeMs = SystemClock.elapsedRealtime() - startTimeMs; |
| 99 | timeRemainingMs = timeoutMs - elapsedTimeMs; |
| 100 | } |
| 101 | } |
| 102 | // Pass interruption information along. |
| 103 | if (wasInterrupted) { |
| 104 | Thread.currentThread().interrupt(); |
| 105 | } |
| 106 | return !thread.isAlive(); |
| 107 | } |
| 108 |
| 109 | public static void joinUninterruptibly(final Thread thread) { |
| 110 | executeUninterruptibly(new BlockingOperation() { |
| 111 | @Override |
| 112 | public void run() throws InterruptedException { |
| 113 | thread.join(); |
| 114 | } |
| 115 | }); |
| 116 | } |
| 117 |
| 118 | public static void awaitUninterruptibly(final CountDownLatch latch) { |
| 119 | executeUninterruptibly(new BlockingOperation() { |
| 120 | @Override |
| 121 | public void run() throws InterruptedException { |
| 122 | latch.await(); |
| 123 | } |
| 124 | }); |
| 125 | } |
| 126 |
| 127 | public static boolean awaitUninterruptibly(CountDownLatch barrier, long timeoutMs) { |
| 128 | final long startTimeMs = SystemClock.elapsedRealtime(); |
| 129 | long timeRemainingMs = timeoutMs; |
| 130 | boolean wasInterrupted = false; |
| 131 | boolean result = false; |
| 132 | do { |
| 133 | try { |
| 134 | result = barrier.await(timeRemainingMs, TimeUnit.MILLISECONDS); |
| 135 | break; |
| 136 | } catch (InterruptedException e) { |
| 137 | // Someone is asking us to return early at our convenience. We can't cancel this operation, |
| 138 | // but we should preserve the information and pass it along. |
| 139 | wasInterrupted = true; |
| 140 | final long elapsedTimeMs = SystemClock.elapsedRealtime() - startTimeMs; |
| 141 | timeRemainingMs = timeoutMs - elapsedTimeMs; |
| 142 | } |
| 143 | } while (timeRemainingMs > 0); |
| 144 | // Pass interruption information along. |
| 145 | if (wasInterrupted) { |
| 146 | Thread.currentThread().interrupt(); |
| 147 | } |
| 148 | return result; |
| 149 | } |
| 150 |
| 151 | /** |
| 152 | * Post |callable| to |handler| and wait for the result. |
| 153 | */ |
| 154 | public static <V> V invokeAtFrontUninterruptibly( |
| 155 | final Handler handler, final Callable<V> callable) { |
| 156 | if (handler.getLooper().getThread() == Thread.currentThread()) { |
| 157 | try { |
| 158 | return callable.call(); |
| 159 | } catch (Exception e) { |
| 160 | throw new RuntimeException(e); |
| 161 | } |
| 162 | } |
| 163 | // Place-holder classes that are assignable inside nested class. |
| 164 | class CaughtException { |
| 165 | Exception e; |
| 166 | } |
| 167 | class Result { |
| 168 | public V value; |
| 169 | } |
| 170 | final Result result = new Result(); |
| 171 | final CaughtException caughtException = new CaughtException(); |
| 172 | final CountDownLatch barrier = new CountDownLatch(1); |
| 173 | handler.post(new Runnable() { |
| 174 | @Override |
| 175 | public void run() { |
| 176 | try { |
| 177 | result.value = callable.call(); |
| 178 | } catch (Exception e) { |
| 179 | caughtException.e = e; |
| 180 | } |
| 181 | barrier.countDown(); |
| 182 | } |
| 183 | }); |
| 184 | awaitUninterruptibly(barrier); |
| 185 | // Re-throw any runtime exception caught inside the other thread. Since this is an invoke, add |
| 186 | // stack trace for the waiting thread as well. |
| 187 | if (caughtException.e != null) { |
| 188 | final RuntimeException runtimeException = new RuntimeException(caughtException.e); |
| 189 | runtimeException.setStackTrace( |
| 190 | concatStackTraces(caughtException.e.getStackTrace(), runtimeException.getStackTrace())); |
| 191 | throw runtimeException; |
| 192 | } |
| 193 | return result.value; |
| 194 | } |
| 195 |
| 196 | /** |
| 197 | * Post |runner| to |handler|, at the front, and wait for completion. |
| 198 | */ |
| 199 | public static void invokeAtFrontUninterruptibly(final Handler handler, final Runnable runner) { |
| 200 | invokeAtFrontUninterruptibly(handler, new Callable<Void>() { |
| 201 | @Override |
| 202 | public Void call() { |
| 203 | runner.run(); |
| 204 | return null; |
| 205 | } |
| 206 | }); |
| 207 | } |
| 208 |
| 209 | static StackTraceElement[] concatStackTraces( |
| 210 | StackTraceElement[] inner, StackTraceElement[] outer) { |
| 211 | final StackTraceElement[] combined = new StackTraceElement[inner.length + outer.length]; |
| 212 | System.arraycopy(inner, 0, combined, 0, inner.length); |
| 213 | System.arraycopy(outer, 0, combined, inner.length, outer.length); |
| 214 | return combined; |
| 215 | } |
| 216 | } |
| 217 |