OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 10 matching lines...) Expand all Loading... |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 package org.webrtc; | 28 package org.webrtc; |
29 | 29 |
30 import android.os.Handler; | 30 import android.os.Handler; |
| 31 import android.os.SystemClock; |
31 | 32 |
32 import java.util.concurrent.Callable; | 33 import java.util.concurrent.Callable; |
33 import java.util.concurrent.CountDownLatch; | 34 import java.util.concurrent.CountDownLatch; |
| 35 import java.util.concurrent.TimeUnit; |
34 | 36 |
35 final class ThreadUtils { | 37 final class ThreadUtils { |
36 /** | 38 /** |
37 * Utility class to be used for checking that a method is called on the correc
t thread. | 39 * Utility class to be used for checking that a method is called on the correc
t thread. |
38 */ | 40 */ |
39 public static class ThreadChecker { | 41 public static class ThreadChecker { |
40 private Thread thread = Thread.currentThread(); | 42 private Thread thread = Thread.currentThread(); |
41 | 43 |
42 public void checkIsOnValidThread() { | 44 public void checkIsOnValidThread() { |
43 if (thread == null) { | 45 if (thread == null) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 99 |
98 public static void awaitUninterruptibly(final CountDownLatch latch) { | 100 public static void awaitUninterruptibly(final CountDownLatch latch) { |
99 executeUninterruptibly(new BlockingOperation() { | 101 executeUninterruptibly(new BlockingOperation() { |
100 @Override | 102 @Override |
101 public void run() throws InterruptedException { | 103 public void run() throws InterruptedException { |
102 latch.await(); | 104 latch.await(); |
103 } | 105 } |
104 }); | 106 }); |
105 } | 107 } |
106 | 108 |
| 109 public static boolean awaitUninterruptibly(CountDownLatch barrier, long timeou
tMs) { |
| 110 final long startTimeMs = SystemClock.elapsedRealtime(); |
| 111 long timeRemainingMs = timeoutMs; |
| 112 boolean wasInterrupted = false; |
| 113 boolean result = false; |
| 114 do { |
| 115 try { |
| 116 result = barrier.await(timeRemainingMs, TimeUnit.MILLISECONDS); |
| 117 break; |
| 118 } catch (InterruptedException e) { |
| 119 // Someone is asking us to return early at our convenience. We can't can
cel this operation, |
| 120 // but we should preserve the information and pass it along. |
| 121 wasInterrupted = true; |
| 122 final long elapsedTimeMs = SystemClock.elapsedRealtime() - startTimeMs; |
| 123 timeRemainingMs = timeoutMs - elapsedTimeMs; |
| 124 } |
| 125 } while (timeRemainingMs > 0); |
| 126 // Pass interruption information along. |
| 127 if (wasInterrupted) { |
| 128 Thread.currentThread().interrupt(); |
| 129 } |
| 130 return result; |
| 131 } |
| 132 |
107 /** | 133 /** |
108 * Post |callable| to |handler| and wait for the result. | 134 * Post |callable| to |handler| and wait for the result. |
109 */ | 135 */ |
110 public static <V> V invokeUninterruptibly(final Handler handler, final Callabl
e<V> callable) { | 136 public static <V> V invokeUninterruptibly(final Handler handler, final Callabl
e<V> callable) { |
111 class Result { | 137 class Result { |
112 public V value; | 138 public V value; |
113 } | 139 } |
114 final Result result = new Result(); | 140 final Result result = new Result(); |
115 final CountDownLatch barrier = new CountDownLatch(1); | 141 final CountDownLatch barrier = new CountDownLatch(1); |
116 handler.post(new Runnable() { | 142 handler.post(new Runnable() { |
(...skipping 17 matching lines...) Expand all Loading... |
134 final CountDownLatch barrier = new CountDownLatch(1); | 160 final CountDownLatch barrier = new CountDownLatch(1); |
135 handler.post(new Runnable() { | 161 handler.post(new Runnable() { |
136 @Override public void run() { | 162 @Override public void run() { |
137 runner.run(); | 163 runner.run(); |
138 barrier.countDown(); | 164 barrier.countDown(); |
139 } | 165 } |
140 }); | 166 }); |
141 awaitUninterruptibly(barrier); | 167 awaitUninterruptibly(barrier); |
142 } | 168 } |
143 } | 169 } |
OLD | NEW |