Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4a2fbefd authored by Jakub Tyszkowski's avatar Jakub Tyszkowski
Browse files

Csip: Improve multithreaded access to state machines

Add the missing synchronizations to prevent race conditions.

Bug: 372092997
Test: atest GoogleBluetoothInstrumentationTests
Flag: Exempt; no functional change
Change-Id: Ia19743a17cbcb8bf19d4afa72797629887e3898e
parent b991ba76
Loading
Loading
Loading
Loading
+10 −3
Original line number Original line Diff line number Diff line
@@ -51,6 +51,7 @@ import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.ServiceFactory;
import com.android.bluetooth.btservice.ServiceFactory;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.le_audio.LeAudioService;
import com.android.bluetooth.le_audio.LeAudioService;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;


import java.util.ArrayList;
import java.util.ArrayList;
@@ -85,6 +86,7 @@ public class CsipSetCoordinatorService extends ProfileService {


    @VisibleForTesting CsipSetCoordinatorNativeInterface mCsipSetCoordinatorNativeInterface;
    @VisibleForTesting CsipSetCoordinatorNativeInterface mCsipSetCoordinatorNativeInterface;


    @GuardedBy("mStateMachines")
    private final Map<BluetoothDevice, CsipSetCoordinatorStateMachine> mStateMachines =
    private final Map<BluetoothDevice, CsipSetCoordinatorStateMachine> mStateMachines =
            new HashMap<>();
            new HashMap<>();


@@ -143,8 +145,11 @@ public class CsipSetCoordinatorService extends ProfileService {
        // Get LE Audio service (can be null)
        // Get LE Audio service (can be null)
        mLeAudioService = mServiceFactory.getLeAudioService();
        mLeAudioService = mServiceFactory.getLeAudioService();


        // Start handler thread for state machines
        synchronized (mStateMachines) {
            mStateMachines.clear();
            mStateMachines.clear();
        }

        // Start handler thread for state machines
        mStateMachinesThread = new HandlerThread("CsipSetCoordinatorService.StateMachines");
        mStateMachinesThread = new HandlerThread("CsipSetCoordinatorService.StateMachines");
        mStateMachinesThread.start();
        mStateMachinesThread.start();


@@ -1181,9 +1186,11 @@ public class CsipSetCoordinatorService extends ProfileService {
    @Override
    @Override
    public void dump(StringBuilder sb) {
    public void dump(StringBuilder sb) {
        super.dump(sb);
        super.dump(sb);
        synchronized (mStateMachines) {
            for (CsipSetCoordinatorStateMachine sm : mStateMachines.values()) {
            for (CsipSetCoordinatorStateMachine sm : mStateMachines.values()) {
                sm.dump(sb);
                sm.dump(sb);
            }
            }
        }
        ProfileService.println(sb, "mFoundSetMemberToGroupId: ");
        ProfileService.println(sb, "mFoundSetMemberToGroupId: ");
        for (Map.Entry<BluetoothDevice, Integer> entry : mFoundSetMemberToGroupId.entrySet()) {
        for (Map.Entry<BluetoothDevice, Integer> entry : mFoundSetMemberToGroupId.entrySet()) {
            ProfileService.println(
            ProfileService.println(