Merge "Better fix for race condition in SoundTriggerHw2Enforcer" into sc-dev am: 3f08df9fe9

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/13393560

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Id17339acbf4421466acc3f15781b70aab5f24261
This commit is contained in:
Ytai Ben-tsvi
2021-01-27 17:24:56 +00:00
committed by Automerger Merge Worker

View File

@@ -42,7 +42,7 @@ public class SoundTriggerHw2Enforcer implements ISoundTriggerHw2 {
static final String TAG = "SoundTriggerHw2Enforcer"; static final String TAG = "SoundTriggerHw2Enforcer";
final ISoundTriggerHw2 mUnderlying; final ISoundTriggerHw2 mUnderlying;
final Map<Integer, Boolean> mModelStates = new HashMap<>(); Map<Integer, Boolean> mModelStates = new HashMap<>();
public SoundTriggerHw2Enforcer( public SoundTriggerHw2Enforcer(
ISoundTriggerHw2 underlying) { ISoundTriggerHw2 underlying) {
@@ -62,12 +62,12 @@ public class SoundTriggerHw2Enforcer implements ISoundTriggerHw2 {
public int loadSoundModel(ISoundTriggerHw.SoundModel soundModel, Callback callback, public int loadSoundModel(ISoundTriggerHw.SoundModel soundModel, Callback callback,
int cookie) { int cookie) {
try { try {
synchronized (mModelStates) {
int handle = mUnderlying.loadSoundModel(soundModel, new CallbackEnforcer(callback), int handle = mUnderlying.loadSoundModel(soundModel, new CallbackEnforcer(callback),
cookie); cookie);
synchronized (mModelStates) {
mModelStates.put(handle, false); mModelStates.put(handle, false);
return handle;
} }
return handle;
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw handleException(e); throw handleException(e);
} }
@@ -77,13 +77,13 @@ public class SoundTriggerHw2Enforcer implements ISoundTriggerHw2 {
public int loadPhraseSoundModel(ISoundTriggerHw.PhraseSoundModel soundModel, Callback callback, public int loadPhraseSoundModel(ISoundTriggerHw.PhraseSoundModel soundModel, Callback callback,
int cookie) { int cookie) {
try { try {
synchronized (mModelStates) {
int handle = mUnderlying.loadPhraseSoundModel(soundModel, int handle = mUnderlying.loadPhraseSoundModel(soundModel,
new CallbackEnforcer(callback), new CallbackEnforcer(callback),
cookie); cookie);
synchronized (mModelStates) {
mModelStates.put(handle, false); mModelStates.put(handle, false);
return handle;
} }
return handle;
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw handleException(e); throw handleException(e);
} }
@@ -92,8 +92,8 @@ public class SoundTriggerHw2Enforcer implements ISoundTriggerHw2 {
@Override @Override
public void unloadSoundModel(int modelHandle) { public void unloadSoundModel(int modelHandle) {
try { try {
synchronized (mModelStates) {
mUnderlying.unloadSoundModel(modelHandle); mUnderlying.unloadSoundModel(modelHandle);
synchronized (mModelStates) {
mModelStates.remove(modelHandle); mModelStates.remove(modelHandle);
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {
@@ -104,8 +104,8 @@ public class SoundTriggerHw2Enforcer implements ISoundTriggerHw2 {
@Override @Override
public void stopRecognition(int modelHandle) { public void stopRecognition(int modelHandle) {
try { try {
synchronized (mModelStates) {
mUnderlying.stopRecognition(modelHandle); mUnderlying.stopRecognition(modelHandle);
synchronized (mModelStates) {
mModelStates.replace(modelHandle, false); mModelStates.replace(modelHandle, false);
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {
@@ -116,8 +116,8 @@ public class SoundTriggerHw2Enforcer implements ISoundTriggerHw2 {
@Override @Override
public void stopAllRecognitions() { public void stopAllRecognitions() {
try { try {
synchronized (mModelStates) {
mUnderlying.stopAllRecognitions(); mUnderlying.stopAllRecognitions();
synchronized (mModelStates) {
for (Map.Entry<Integer, Boolean> entry : mModelStates.entrySet()) { for (Map.Entry<Integer, Boolean> entry : mModelStates.entrySet()) {
entry.setValue(false); entry.setValue(false);
} }
@@ -130,12 +130,14 @@ public class SoundTriggerHw2Enforcer implements ISoundTriggerHw2 {
@Override @Override
public void startRecognition(int modelHandle, RecognitionConfig config, Callback callback, public void startRecognition(int modelHandle, RecognitionConfig config, Callback callback,
int cookie) { int cookie) {
try { // It is possible that an event will be sent before the HAL returns from the
// startRecognition call, thus it is important to set the state to active before the call.
synchronized (mModelStates) { synchronized (mModelStates) {
mUnderlying.startRecognition(modelHandle, config, new CallbackEnforcer(callback),
cookie);
mModelStates.replace(modelHandle, true); mModelStates.replace(modelHandle, true);
} }
try {
mUnderlying.startRecognition(modelHandle, config, new CallbackEnforcer(callback),
cookie);
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw handleException(e); throw handleException(e);
} }