Summary: One standard way to schedule work is by starting a thread. We treat this by - Treating invocations of `start` on a receiver with the `Runnable _` attribute as scheduling that runnable for parallel execution in the background (as opposed to on the UI thread). - If `start` is used on an object of a subclass of `Thread` everything already works thanks to the `get_exp_attributes` function which will implicitly ascribe to an expression the attribute `Runnable _` if the expression points to an object with a known `run` method. This will even take care of some degree of dynamic dispatch, yay! - If `start` is used on a `Thread` object which has been created with a constructor call provided with a `Runnable` argument, we have to appropriately model that constructor call, which is what is done in `do_call`. Reviewed By: jvillard Differential Revision: D18726676 fbshipit-source-id: 0bd83c28emaster
parent
aef34d8384
commit
7a538c5004
@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import android.os.Binder;
|
||||||
|
import android.os.RemoteException;
|
||||||
|
|
||||||
|
class ThreadScheduling {
|
||||||
|
static Binder binder;
|
||||||
|
|
||||||
|
private static void doTransact() {
|
||||||
|
try {
|
||||||
|
binder.transact(0, null, null, 0);
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object monitorA;
|
||||||
|
|
||||||
|
public void scheduleBlockingCallOnContendedLockBad() {
|
||||||
|
Thread t =
|
||||||
|
new Thread(
|
||||||
|
new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
synchronized (monitorA) {
|
||||||
|
doTransact();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
t.start();
|
||||||
|
|
||||||
|
Executors.runOnUiThread(
|
||||||
|
new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
synchronized (monitorA) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Object monitorB, monitorC;
|
||||||
|
|
||||||
|
public void scheduleDeadlockBad() {
|
||||||
|
Thread t =
|
||||||
|
new Thread(
|
||||||
|
new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
synchronized (monitorB) {
|
||||||
|
synchronized (monitorC) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
t.start();
|
||||||
|
|
||||||
|
Executors.runOnUiThread(
|
||||||
|
new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
synchronized (monitorC) {
|
||||||
|
synchronized (monitorB) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Object monitorD;
|
||||||
|
|
||||||
|
class BadThread extends Thread {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
synchronized (monitorD) {
|
||||||
|
doTransact();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void scheduleBlockingCallOnContendedLockViaInheritanceBad() {
|
||||||
|
Thread t = new BadThread();
|
||||||
|
t.start();
|
||||||
|
|
||||||
|
Executors.runOnUiThread(
|
||||||
|
new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
synchronized (monitorD) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue