Summary: Holding a master lock and then acquiring two other locks inside can generate a false positive as shown. Reviewed By: mityal Differential Revision: D17710076 fbshipit-source-id: 5bc910ba2master
parent
f57bb9be0a
commit
fb77efea6a
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
class MasterLock {
|
||||
Object a, b;
|
||||
|
||||
// classic deadlock between 2 methods
|
||||
// 1st method has a and wants b
|
||||
void oneWayBad() {
|
||||
synchronized (a) {
|
||||
synchronized (b) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2nd method has b and wants a
|
||||
void theOtherWayBad() {
|
||||
synchronized (b) {
|
||||
synchronized (a) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Object master, x, y;
|
||||
|
||||
// both methods hold the master lock so cannot interleave
|
||||
// and thus cannot deadlock
|
||||
void FP_oneWayOk() {
|
||||
synchronized (master) {
|
||||
synchronized (x) {
|
||||
synchronized (y) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FP_theOtherWayOk() {
|
||||
synchronized (master) {
|
||||
synchronized (y) {
|
||||
synchronized (x) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue