Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: base/android/java/src/org/chromium/base/ThreadUtils.java

Issue 2810373002: InstalledAppProviderImpl: Assert that code is run off the UI thread. (Closed)
Patch Set: Use real assert instead of exception (apparently this works now). Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/public/android/java/src/org/chromium/content/browser/installedapp/InstalledAppProviderImpl.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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.base; 5 package org.chromium.base;
6 6
7 import android.os.Handler; 7 import android.os.Handler;
8 import android.os.Looper; 8 import android.os.Looper;
9 import android.os.Process; 9 import android.os.Process;
10 10
11 import org.chromium.base.annotations.CalledByNative; 11 import org.chromium.base.annotations.CalledByNative;
12 12
13 import java.util.concurrent.Callable; 13 import java.util.concurrent.Callable;
14 import java.util.concurrent.ExecutionException; 14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.FutureTask; 15 import java.util.concurrent.FutureTask;
16 16
17 /** 17 /**
18 * Helper methods to deal with threading related tasks. 18 * Helper methods to deal with threading related tasks.
19 */ 19 */
20 public class ThreadUtils { 20 public class ThreadUtils {
21 21
22 private static final Object sLock = new Object(); 22 private static final Object sLock = new Object();
23 23
24 private static boolean sWillOverride; 24 private static boolean sWillOverride;
25 25
26 private static Handler sUiThreadHandler; 26 private static Handler sUiThreadHandler;
27 27
28 private static boolean sThreadAssertsEnabled = true;
agrieve 2017/05/05 15:00:48 nit: Generally better to have things default-initi
Matt Giuca 2017/05/08 01:18:10 Done.
29
28 public static void setWillOverrideUiThread() { 30 public static void setWillOverrideUiThread() {
29 synchronized (sLock) { 31 synchronized (sLock) {
30 sWillOverride = true; 32 sWillOverride = true;
31 } 33 }
32 } 34 }
33 35
34 @VisibleForTesting 36 @VisibleForTesting
35 public static void setUiThread(Looper looper) { 37 public static void setUiThread(Looper looper) {
36 synchronized (sLock) { 38 synchronized (sLock) {
37 if (looper == null) { 39 if (looper == null) {
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 * 186 *
185 * @param task The Runnable to run 187 * @param task The Runnable to run
186 * @param delayMillis The delay in milliseconds until the Runnable will be r un 188 * @param delayMillis The delay in milliseconds until the Runnable will be r un
187 */ 189 */
188 @VisibleForTesting 190 @VisibleForTesting
189 public static void postOnUiThreadDelayed(Runnable task, long delayMillis) { 191 public static void postOnUiThreadDelayed(Runnable task, long delayMillis) {
190 getUiThreadHandler().postDelayed(task, delayMillis); 192 getUiThreadHandler().postDelayed(task, delayMillis);
191 } 193 }
192 194
193 /** 195 /**
194 * Asserts that the current thread is running on the main thread. 196 * Asserts that the current thread is the main (UI) thread.
197 *
198 * Can be disabled by setThreadAssertsEnabled(false).
195 */ 199 */
196 public static void assertOnUiThread() { 200 public static void assertOnUiThread() {
197 if (BuildConfig.DCHECK_IS_ON && !runningOnUiThread()) { 201 if (!sThreadAssertsEnabled) {
198 throw new IllegalStateException("Must be called on the Ui thread."); 202 return;
199 } 203 }
204
205 assert runningOnUiThread() : "Must be called on the UI thread.";
200 } 206 }
201 207
202 /** 208 /**
209 * Asserts that the current thread is NOT the main (UI) thread.
210 *
211 * Can be disabled by setThreadAssertsEnabled(false).
212 */
213 public static void assertOnBackgroundThread() {
214 if (!sThreadAssertsEnabled) {
215 return;
216 }
217
218 assert !runningOnUiThread() : "Must be called on a thread other than UI. ";
219 }
220
221 /**
222 * Disables thread asserts.
223 *
224 * Can be used by tests where code that normally runs multi-threaded is goin g to run
225 * single-threaded for the test (otherwise asserts that are valid in product ion would fail in
226 * those tests).
227 */
228 @VisibleForTesting
229 public static void setThreadAssertsEnabled(boolean enabled) {
Ted C 2017/05/05 14:09:39 I would add "ForTesting" to the end of the name to
agrieve 2017/05/05 15:00:49 Along the same lines, probably would drop "Visible
Matt Giuca 2017/05/08 01:18:10 I'm confused about both of these suggestions... yo
agrieve 2017/05/08 14:18:15 Consistency is certainly not our strong suite. To
230 sThreadAssertsEnabled = enabled;
231 }
232
233 /**
203 * @return true iff the current thread is the main (UI) thread. 234 * @return true iff the current thread is the main (UI) thread.
204 */ 235 */
205 public static boolean runningOnUiThread() { 236 public static boolean runningOnUiThread() {
206 return getUiThreadHandler().getLooper() == Looper.myLooper(); 237 return getUiThreadHandler().getLooper() == Looper.myLooper();
207 } 238 }
208 239
209 public static Looper getUiThreadLooper() { 240 public static Looper getUiThreadLooper() {
210 return getUiThreadHandler().getLooper(); 241 return getUiThreadHandler().getLooper();
211 } 242 }
212 243
213 /** 244 /**
214 * Set thread priority to audio. 245 * Set thread priority to audio.
215 */ 246 */
216 @CalledByNative 247 @CalledByNative
217 public static void setThreadPriorityAudio(int tid) { 248 public static void setThreadPriorityAudio(int tid) {
218 Process.setThreadPriority(tid, Process.THREAD_PRIORITY_AUDIO); 249 Process.setThreadPriority(tid, Process.THREAD_PRIORITY_AUDIO);
219 } 250 }
220 251
221 /** 252 /**
222 * Checks whether Thread priority is THREAD_PRIORITY_AUDIO or not. 253 * Checks whether Thread priority is THREAD_PRIORITY_AUDIO or not.
223 * @param tid Thread id. 254 * @param tid Thread id.
224 * @return true for THREAD_PRIORITY_AUDIO and false otherwise. 255 * @return true for THREAD_PRIORITY_AUDIO and false otherwise.
225 */ 256 */
226 @CalledByNative 257 @CalledByNative
227 private static boolean isThreadPriorityAudio(int tid) { 258 private static boolean isThreadPriorityAudio(int tid) {
228 return Process.getThreadPriority(tid) == Process.THREAD_PRIORITY_AUDIO; 259 return Process.getThreadPriority(tid) == Process.THREAD_PRIORITY_AUDIO;
229 } 260 }
230 } 261 }
OLDNEW
« no previous file with comments | « no previous file | content/public/android/java/src/org/chromium/content/browser/installedapp/InstalledAppProviderImpl.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698