Merge "Use ReentrantReadWriteLock in ExpandableBinaryDictionary."

main
Keisuke Kuroynagi 2013-07-03 11:39:17 +00:00 committed by Android (Google) Code Review
commit 55de0bd981
1 changed files with 18 additions and 15 deletions

View File

@ -35,7 +35,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
/** /**
* Abstract base class for an expandable dictionary that can be created and updated dynamically * Abstract base class for an expandable dictionary that can be created and updated dynamically
@ -151,14 +151,14 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
@Override @Override
public void close() { public void close() {
// Ensure that no other threads are accessing the local binary dictionary. // Ensure that no other threads are accessing the local binary dictionary.
mLocalDictionaryController.lock(); mLocalDictionaryController.writeLock().lock();
try { try {
if (mBinaryDictionary != null) { if (mBinaryDictionary != null) {
mBinaryDictionary.close(); mBinaryDictionary.close();
mBinaryDictionary = null; mBinaryDictionary = null;
} }
} finally { } finally {
mLocalDictionaryController.unlock(); mLocalDictionaryController.writeLock().unlock();
} }
} }
@ -205,14 +205,14 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
final String prevWord, final ProximityInfo proximityInfo, final String prevWord, final ProximityInfo proximityInfo,
final boolean blockOffensiveWords) { final boolean blockOffensiveWords) {
asyncReloadDictionaryIfRequired(); asyncReloadDictionaryIfRequired();
if (mLocalDictionaryController.tryLock()) { if (mLocalDictionaryController.readLock().tryLock()) {
try { try {
if (mBinaryDictionary != null) { if (mBinaryDictionary != null) {
return mBinaryDictionary.getSuggestions(composer, prevWord, proximityInfo, return mBinaryDictionary.getSuggestions(composer, prevWord, proximityInfo,
blockOffensiveWords); blockOffensiveWords);
} }
} finally { } finally {
mLocalDictionaryController.unlock(); mLocalDictionaryController.readLock().unlock();
} }
} }
return null; return null;
@ -225,11 +225,11 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
} }
protected boolean isValidWordInner(final String word) { protected boolean isValidWordInner(final String word) {
if (mLocalDictionaryController.tryLock()) { if (mLocalDictionaryController.readLock().tryLock()) {
try { try {
return isValidWordLocked(word); return isValidWordLocked(word);
} finally { } finally {
mLocalDictionaryController.unlock(); mLocalDictionaryController.readLock().unlock();
} }
} }
return false; return false;
@ -246,11 +246,11 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
} }
protected boolean isValidBigramInner(final String word1, final String word2) { protected boolean isValidBigramInner(final String word1, final String word2) {
if (mLocalDictionaryController.tryLock()) { if (mLocalDictionaryController.readLock().tryLock()) {
try { try {
return isValidBigramLocked(word1, word2); return isValidBigramLocked(word1, word2);
} finally { } finally {
mLocalDictionaryController.unlock(); mLocalDictionaryController.readLock().unlock();
} }
} }
return false; return false;
@ -293,9 +293,12 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
// Ensure all threads accessing the current dictionary have finished before swapping in // Ensure all threads accessing the current dictionary have finished before swapping in
// the new one. // the new one.
final BinaryDictionary oldBinaryDictionary = mBinaryDictionary; final BinaryDictionary oldBinaryDictionary = mBinaryDictionary;
mLocalDictionaryController.lock(); mLocalDictionaryController.writeLock().lock();
try {
mBinaryDictionary = newBinaryDictionary; mBinaryDictionary = newBinaryDictionary;
mLocalDictionaryController.unlock(); } finally {
mLocalDictionaryController.writeLock().unlock();
}
oldBinaryDictionary.close(); oldBinaryDictionary.close();
} else { } else {
mBinaryDictionary = newBinaryDictionary; mBinaryDictionary = newBinaryDictionary;
@ -390,7 +393,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
private final void syncReloadDictionaryInternal() { private final void syncReloadDictionaryInternal() {
// Ensure that only one thread attempts to read or write to the shared binary dictionary // Ensure that only one thread attempts to read or write to the shared binary dictionary
// file at the same time. // file at the same time.
mSharedDictionaryController.lock(); mSharedDictionaryController.writeLock().lock();
try { try {
final long time = SystemClock.uptimeMillis(); final long time = SystemClock.uptimeMillis();
final boolean dictionaryFileExists = dictionaryFileExists(); final boolean dictionaryFileExists = dictionaryFileExists();
@ -424,7 +427,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
} }
mLocalDictionaryController.mLastUpdateTime = time; mLocalDictionaryController.mLastUpdateTime = time;
} finally { } finally {
mSharedDictionaryController.unlock(); mSharedDictionaryController.writeLock().unlock();
} }
} }
@ -449,7 +452,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
* dictionary is out of date. Can be shared across multiple dictionary instances that access the * dictionary is out of date. Can be shared across multiple dictionary instances that access the
* same filename. * same filename.
*/ */
private static class DictionaryController extends ReentrantLock { private static class DictionaryController extends ReentrantReadWriteLock {
private volatile long mLastUpdateTime = 0; private volatile long mLastUpdateTime = 0;
private volatile long mLastUpdateRequestTime = 0; private volatile long mLastUpdateRequestTime = 0;