OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net.urlconnection; | 5 package org.chromium.net.urlconnection; |
6 | 6 |
7 import java.io.IOException; | 7 import java.io.IOException; |
| 8 import java.io.InterruptedIOException; |
| 9 import java.net.SocketTimeoutException; |
8 import java.util.concurrent.BlockingQueue; | 10 import java.util.concurrent.BlockingQueue; |
9 import java.util.concurrent.Executor; | 11 import java.util.concurrent.Executor; |
10 import java.util.concurrent.LinkedBlockingQueue; | 12 import java.util.concurrent.LinkedBlockingQueue; |
11 import java.util.concurrent.RejectedExecutionException; | 13 import java.util.concurrent.RejectedExecutionException; |
| 14 import java.util.concurrent.TimeUnit; |
12 | 15 |
13 /** | 16 /** |
14 * A MessageLoop class for use in {@link CronetHttpURLConnection}. | 17 * A MessageLoop class for use in {@link CronetHttpURLConnection}. |
15 */ | 18 */ |
16 class MessageLoop implements Executor { | 19 class MessageLoop implements Executor { |
17 private final BlockingQueue<Runnable> mQueue; | 20 private final BlockingQueue<Runnable> mQueue; |
18 | 21 |
19 // Indicates whether this message loop is currently running. | 22 // Indicates whether this message loop is currently running. |
20 private boolean mLoopRunning = false; | 23 private boolean mLoopRunning = false; |
21 | 24 |
(...skipping 13 matching lines...) Expand all Loading... |
35 | 38 |
36 private boolean calledOnValidThread() { | 39 private boolean calledOnValidThread() { |
37 if (mThreadId == INVALID_THREAD_ID) { | 40 if (mThreadId == INVALID_THREAD_ID) { |
38 mThreadId = Thread.currentThread().getId(); | 41 mThreadId = Thread.currentThread().getId(); |
39 return true; | 42 return true; |
40 } | 43 } |
41 return mThreadId == Thread.currentThread().getId(); | 44 return mThreadId == Thread.currentThread().getId(); |
42 } | 45 } |
43 | 46 |
44 /** | 47 /** |
| 48 * Retrieves a task from the queue with the given timeout. |
| 49 * |
| 50 * @param timeout Time to wait, in milliseconds, or 0 for no timeout. |
| 51 * @return A non-{@code null} Runnable from the queue. |
| 52 * @throws InterruptedIOException |
| 53 */ |
| 54 private Runnable take(int timeout) throws InterruptedIOException { |
| 55 Runnable task = null; |
| 56 try { |
| 57 if (timeout <= 0) { |
| 58 task = mQueue.take(); // Blocks if the queue is empty. |
| 59 } else { |
| 60 // poll returns null upon timeout. |
| 61 task = mQueue.poll(timeout, TimeUnit.MILLISECONDS); |
| 62 } |
| 63 } catch (InterruptedException e) { |
| 64 InterruptedIOException exception = new InterruptedIOException(); |
| 65 exception.initCause(e); |
| 66 throw exception; |
| 67 } |
| 68 if (task == null) { |
| 69 // This will terminate the loop. |
| 70 throw new SocketTimeoutException(); |
| 71 } |
| 72 return task; |
| 73 } |
| 74 |
| 75 /** |
45 * Runs the message loop. Be sure to call {@link MessageLoop#quit()} | 76 * Runs the message loop. Be sure to call {@link MessageLoop#quit()} |
46 * to end the loop. If an interruptedException occurs, the loop cannot be | 77 * to end the loop. If an interruptedException occurs, the loop cannot be |
47 * started again (see {@link #mLoopFailed}). | 78 * started again (see {@link #mLoopFailed}). |
48 * @throws IOException | 79 * @throws IOException |
49 */ | 80 */ |
50 public void loop() throws IOException { | 81 public void loop() throws IOException { |
| 82 loop(0); |
| 83 } |
| 84 |
| 85 /** |
| 86 * Runs the message loop. Be sure to call {@link MessageLoop#quit()} |
| 87 * to end the loop. If an interruptedException occurs, the loop cannot be |
| 88 * started again (see {@link #mLoopFailed}). |
| 89 * @param timeout Timeout, in milliseconds, or 0 for no timeout. |
| 90 * @throws IOException |
| 91 */ |
| 92 public void loop(int timeout) throws IOException { |
51 assert calledOnValidThread(); | 93 assert calledOnValidThread(); |
52 if (mLoopFailed) { | 94 if (mLoopFailed) { |
53 throw new IllegalStateException( | 95 throw new IllegalStateException( |
54 "Cannot run loop as an exception has occurred previously."); | 96 "Cannot run loop as an exception has occurred previously."); |
55 } | 97 } |
56 if (mLoopRunning) { | 98 if (mLoopRunning) { |
57 throw new IllegalStateException( | 99 throw new IllegalStateException( |
58 "Cannot run loop when it is already running."); | 100 "Cannot run loop when it is already running."); |
59 } | 101 } |
60 mLoopRunning = true; | 102 mLoopRunning = true; |
61 while (mLoopRunning) { | 103 while (mLoopRunning) { |
62 try { | 104 try { |
63 Runnable task = mQueue.take(); // Blocks if the queue is empty. | 105 take(timeout).run(); |
64 task.run(); | 106 } catch (InterruptedIOException | RuntimeException e) { |
65 } catch (InterruptedException | RuntimeException e) { | |
66 mLoopRunning = false; | 107 mLoopRunning = false; |
67 mLoopFailed = true; | 108 mLoopFailed = true; |
68 if (e instanceof InterruptedException) { | 109 throw e; |
69 throw new IOException(e); | |
70 } else if (e instanceof RuntimeException) { | |
71 throw (RuntimeException) e; | |
72 } | |
73 } | 110 } |
74 } | 111 } |
75 } | 112 } |
76 | 113 |
77 /** | 114 /** |
78 * This causes {@link #loop()} to stop executing messages after the current | 115 * This causes {@link #loop()} to stop executing messages after the current |
79 * message being executed. Should only be called from the currently | 116 * message being executed. Should only be called from the currently |
80 * executing message. | 117 * executing message. |
81 */ | 118 */ |
82 public void quit() { | 119 public void quit() { |
(...skipping 25 matching lines...) Expand all Loading... |
108 return mLoopRunning; | 145 return mLoopRunning; |
109 } | 146 } |
110 | 147 |
111 /** | 148 /** |
112 * Returns whether an exception occurred in {#loop()}. Used in testing. | 149 * Returns whether an exception occurred in {#loop()}. Used in testing. |
113 */ | 150 */ |
114 public boolean hasLoopFailed() { | 151 public boolean hasLoopFailed() { |
115 return mLoopFailed; | 152 return mLoopFailed; |
116 } | 153 } |
117 } | 154 } |
OLD | NEW |