summaryrefslogtreecommitdiff
path: root/core/java/android/util/ArraySet.java
diff options
context:
space:
mode:
authorJeff Sharkey <jsharkey@android.com>2016-02-27 21:10:34 -0700
committerJeff Sharkey <jsharkey@android.com>2016-02-27 21:15:01 -0700
commit3d1cb6a2b6882a9b702fc97aa50b2d5779956492 (patch)
tree1c2b760b33aa8667172e2f3f42034385fb718cf4 /core/java/android/util/ArraySet.java
parentb15a71913eaea4d2e7063437b3d1a8db5d98c221 (diff)
Utility to detect lock inversions in system.
This change adds a new LockGuard utility class that can be used to detect lock inversions across the system server. For example, if a thread is trying to acquire the ActivityManager lock while holding the PackageManager lock, it will yell. This class requires no prior knowledge of locks or their ordering; it derives all of this data at runtime. However, this means the overhead is substantial and it should not be enabled by default. Adds overrides to ArrayMap and ArraySet to use identityHashCode() instead of the hashCode() provided by the object. Bug: 27336728 Change-Id: I26c31bc99fe8d61ff13c3455aaeddd5517e44433
Diffstat (limited to 'core/java/android/util/ArraySet.java')
-rw-r--r--core/java/android/util/ArraySet.java16
1 files changed, 11 insertions, 5 deletions
diff --git a/core/java/android/util/ArraySet.java b/core/java/android/util/ArraySet.java
index b7a3c42e8f24..9e9314fba4c4 100644
--- a/core/java/android/util/ArraySet.java
+++ b/core/java/android/util/ArraySet.java
@@ -69,6 +69,7 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
static Object[] mTwiceBaseCache;
static int mTwiceBaseCacheSize;
+ final boolean mIdentityHashCode;
int[] mHashes;
Object[] mArray;
int mSize;
@@ -222,15 +223,19 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
* will grow once items are added to it.
*/
public ArraySet() {
- mHashes = EmptyArray.INT;
- mArray = EmptyArray.OBJECT;
- mSize = 0;
+ this(0, false);
}
/**
* Create a new ArraySet with a given initial capacity.
*/
public ArraySet(int capacity) {
+ this(capacity, false);
+ }
+
+ /** {@hide} */
+ public ArraySet(int capacity, boolean identityHashCode) {
+ mIdentityHashCode = identityHashCode;
if (capacity == 0) {
mHashes = EmptyArray.INT;
mArray = EmptyArray.OBJECT;
@@ -306,7 +311,8 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
* @return Returns the index of the value if it exists, else a negative integer.
*/
public int indexOf(Object key) {
- return key == null ? indexOfNull() : indexOf(key, key.hashCode());
+ return key == null ? indexOfNull()
+ : indexOf(key, mIdentityHashCode ? System.identityHashCode(key) : key.hashCode());
}
/**
@@ -343,7 +349,7 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
hash = 0;
index = indexOfNull();
} else {
- hash = value.hashCode();
+ hash = mIdentityHashCode ? System.identityHashCode(value) : value.hashCode();
index = indexOf(value, hash);
}
if (index >= 0) {