You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.0 KiB
90 lines
2.0 KiB
5 years ago
|
/*
|
||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
|
*
|
||
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
|
*/
|
||
|
|
||
|
// Examples where the code checks what thread it's currently running on.
|
||
|
class ThreadSensitivity {
|
||
|
Object monitorA, monitorB;
|
||
|
|
||
|
void FN_conditionalAssertMainThread_Bad(boolean b) {
|
||
|
if (b) {
|
||
|
// this branch asserts on Main thread
|
||
|
OurThreadUtils.assertMainThread();
|
||
|
synchronized (monitorA) {
|
||
|
synchronized (monitorB) {
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
// this branch asserts nothing, so may run in parallel with the other branch
|
||
|
synchronized (monitorB) {
|
||
|
synchronized (monitorA) {
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Object monitorC, monitorD;
|
||
|
|
||
|
// the branches in the following two methods are both on main/UI thread so cannot deadlock
|
||
|
|
||
|
void FP_conditionalIsMainThread_Ok() {
|
||
|
if (OurThreadUtils.isMainThread()) {
|
||
|
synchronized (monitorC) {
|
||
|
synchronized (monitorD) {
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void FP_conditionalIsUiThread_Ok() {
|
||
|
if (OurThreadUtils.isUiThread()) {
|
||
|
synchronized (monitorD) {
|
||
|
synchronized (monitorC) {
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Object monitorE, monitorF;
|
||
|
// identical to the first case above but negated
|
||
|
void conditionalNegatedIsMainThread_Bad() {
|
||
|
if (!OurThreadUtils.isMainThread()) {
|
||
|
synchronized (monitorE) {
|
||
|
synchronized (monitorF) {
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
synchronized (monitorF) {
|
||
|
synchronized (monitorE) {
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Object monitorG, monitorH;
|
||
|
|
||
|
public void FN_confusedAssertBad(boolean b, boolean c) {
|
||
|
if (b) {
|
||
|
OurThreadUtils.assertOnBackgroundThread();
|
||
|
} else {
|
||
|
OurThreadUtils.assertOnUiThread();
|
||
|
}
|
||
|
|
||
|
// not sure if we're on UI or background, should report
|
||
|
if (c) {
|
||
|
synchronized (monitorG) {
|
||
|
synchronized (monitorH) {
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
synchronized (monitorH) {
|
||
|
synchronized (monitorG) {
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|