Index: talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java |
diff --git a/talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java b/talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java |
index 0d8968aba9e50c17070e9d629e978edc078cb295..b078cb8b19c2405c9c68d410a46e6ee20c93a01d 100644 |
--- a/talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java |
+++ b/talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java |
@@ -28,9 +28,11 @@ |
package org.webrtc; |
import android.os.Handler; |
+import android.os.SystemClock; |
import java.util.concurrent.Callable; |
import java.util.concurrent.CountDownLatch; |
+import java.util.concurrent.TimeUnit; |
final class ThreadUtils { |
/** |
@@ -104,6 +106,30 @@ final class ThreadUtils { |
}); |
} |
+ public static boolean awaitUninterruptibly(CountDownLatch barrier, long timeoutMs) { |
+ final long startTimeMs = SystemClock.elapsedRealtime(); |
+ long timeRemainingMs = timeoutMs; |
+ boolean wasInterrupted = false; |
+ boolean result = false; |
+ do { |
+ try { |
+ result = barrier.await(timeRemainingMs, TimeUnit.MILLISECONDS); |
+ break; |
+ } catch (InterruptedException e) { |
+ // Someone is asking us to return early at our convenience. We can't cancel this operation, |
+ // but we should preserve the information and pass it along. |
+ wasInterrupted = true; |
+ final long elapsedTimeMs = SystemClock.elapsedRealtime() - startTimeMs; |
+ timeRemainingMs = timeoutMs - elapsedTimeMs; |
+ } |
+ } while (timeRemainingMs > 0); |
+ // Pass interruption information along. |
+ if (wasInterrupted) { |
+ Thread.currentThread().interrupt(); |
+ } |
+ return result; |
+ } |
+ |
/** |
* Post |callable| to |handler| and wait for the result. |
*/ |