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, |
11 * this list of conditions and the following disclaimer in the documentation | 11 * this list of conditions and the following disclaimer in the documentation |
12 * and/or other materials provided with the distribution. | 12 * and/or other materials provided with the distribution. |
13 * 3. The name of the author may not be used to endorse or promote products | 13 * 3. The name of the author may not be used to endorse or promote products |
14 * derived from this software without specific prior written permission. | 14 * derived from this software without specific prior written permission. |
15 * | 15 * |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
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 public class ThreadUtils { | 30 import java.util.concurrent.CountDownLatch; |
| 31 |
| 32 final class ThreadUtils { |
31 /** | 33 /** |
32 * Helper function to make sure a thread is joined without getting interrupted
. This should be | 34 * Utility interface to be used with executeUninterruptibly() to wait for bloc
king operations |
33 * used in cases where |thread| is doing some critical work, e.g. cleanup, tha
t must complete | 35 * to complete without getting interrupted.. |
34 * before returning. The thread interruption flag is set if an interrupt occur
s during join(). | |
35 */ | 36 */ |
36 public static void joinUninterruptibly(Thread thread) { | 37 public interface BlockingOperation { |
| 38 void run() throws InterruptedException; |
| 39 } |
| 40 |
| 41 /** |
| 42 * Utility method to make sure a blocking operation is executed to completion
without getting |
| 43 * interrupted. This should be used in cases where the operation is waiting fo
r some critical |
| 44 * work, e.g. cleanup, that must complete before returning. If the thread is i
nterrupted during |
| 45 * the blocking operation, this function will re-run the operation until compl
etion, and only then |
| 46 * re-interrupt the thread. |
| 47 */ |
| 48 public static void executeUninterruptibly(BlockingOperation operation) { |
37 boolean wasInterrupted = false; | 49 boolean wasInterrupted = false; |
38 while (true) { | 50 while (true) { |
39 try { | 51 try { |
40 thread.join(); | 52 operation.run(); |
41 break; | 53 break; |
42 } catch (InterruptedException e) { | 54 } catch (InterruptedException e) { |
43 // Someone is asking us to return early at our convenience. We can't can
cel this join(), | 55 // Someone is asking us to return early at our convenience. We can't can
cel this operation, |
44 // but we should preserve the information and pass it along. | 56 // but we should preserve the information and pass it along. |
45 wasInterrupted = true; | 57 wasInterrupted = true; |
46 } | 58 } |
47 } | 59 } |
48 // Pass interruption information along. | 60 // Pass interruption information along. |
49 if (wasInterrupted) { | 61 if (wasInterrupted) { |
50 Thread.currentThread().interrupt(); | 62 Thread.currentThread().interrupt(); |
51 } | 63 } |
52 } | 64 } |
| 65 |
| 66 public static void joinUninterruptibly(final Thread thread) { |
| 67 executeUninterruptibly(new BlockingOperation() { |
| 68 @Override |
| 69 public void run() throws InterruptedException { |
| 70 thread.join(); |
| 71 } |
| 72 }); |
| 73 } |
| 74 |
| 75 public static void awaitUninterruptibly(final CountDownLatch latch) { |
| 76 executeUninterruptibly(new BlockingOperation() { |
| 77 @Override |
| 78 public void run() throws InterruptedException { |
| 79 latch.await(); |
| 80 } |
| 81 }); |
| 82 } |
53 } | 83 } |
OLD | NEW |