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 { | |
33 public interface BlockingOperation { | |
34 void run() throws InterruptedException; | |
35 } | |
36 | |
31 /** | 37 /** |
32 * Helper function to make sure a thread is joined without getting interrupted . This should be | 38 * Helper function to make sure a blocking operation is executed to completion without getting |
33 * used in cases where |thread| is doing some critical work, e.g. cleanup, tha t must complete | 39 * interrupted. This should be used in cases where the operation is waiting fo r some critical |
34 * before returning. The thread interruption flag is set if an interrupt occur s during join(). | 40 * work, e.g. cleanup, that must complete before returning. If the thread is i nterrupted during |
41 * the blocking operation, this function will re-run the operation until compl etion, and only then | |
42 * re-interrupt the thread. | |
35 */ | 43 */ |
36 public static void joinUninterruptibly(Thread thread) { | 44 public static void executeUninterruptibly(BlockingOperation operation) { |
hbos
2015/09/28 08:43:02
super-nit: Considering this can legitimately be us
magjed_webrtc
2015/09/28 09:05:36
Done.
| |
37 boolean wasInterrupted = false; | 45 boolean wasInterrupted = false; |
38 while (true) { | 46 while (true) { |
39 try { | 47 try { |
40 thread.join(); | 48 operation.run(); |
41 break; | 49 break; |
42 } catch (InterruptedException e) { | 50 } catch (InterruptedException e) { |
43 // Someone is asking us to return early at our convenience. We can't can cel this join(), | 51 // 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. | 52 // but we should preserve the information and pass it along. |
45 wasInterrupted = true; | 53 wasInterrupted = true; |
46 } | 54 } |
47 } | 55 } |
48 // Pass interruption information along. | 56 // Pass interruption information along. |
49 if (wasInterrupted) { | 57 if (wasInterrupted) { |
50 Thread.currentThread().interrupt(); | 58 Thread.currentThread().interrupt(); |
51 } | 59 } |
52 } | 60 } |
61 | |
62 public static void joinUninterruptibly(final Thread thread) { | |
63 executeUninterruptibly(new BlockingOperation() { | |
64 @Override | |
65 public void run() throws InterruptedException { | |
66 thread.join(); | |
67 } | |
68 }); | |
69 } | |
70 | |
71 public static void awaitUninterruptibly(final CountDownLatch latch) { | |
72 executeUninterruptibly(new BlockingOperation() { | |
73 @Override | |
74 public void run() throws InterruptedException { | |
75 latch.await(); | |
76 } | |
77 }); | |
78 } | |
53 } | 79 } |
OLD | NEW |