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

Commit 958006fa authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Simplify Context Hub Service transaction locking" into main

parents 6c30713c e80fa4e1
Loading
Loading
Loading
Loading
+162 −65
Original line number Diff line number Diff line
@@ -584,6 +584,34 @@ import java.util.concurrent.atomic.AtomicInteger;
     */
    /* package */
    void onTransactionResponse(int transactionId, boolean success) {
        if (Flags.simplifyServiceTransactionLock()) {
            synchronized (mTransactionLock) {
                TransactionAcceptConditions conditions =
                        transaction -> {
                            if (transaction.getTransactionId() != transactionId) {
                                Log.w(
                                        TAG,
                                        "Unexpected transaction: expected "
                                                + transactionId
                                                + ", received "
                                                + transaction.getTransactionId());
                                return false;
                            }
                            return true;
                        };
                ContextHubServiceTransaction transaction = getTransactionAndHandleNext(conditions);
                if (transaction == null) {
                    Log.w(TAG, "Received unexpected transaction response");
                    return;
                }

                transaction.onTransactionComplete(
                        success
                                ? ContextHubTransaction.RESULT_SUCCESS
                                : ContextHubTransaction.RESULT_FAILED_AT_HUB);
                transaction.setComplete();
            }
        } else {
            TransactionAcceptConditions conditions =
                    transaction -> {
                        if (transaction.getTransactionId() != transactionId) {
@@ -611,6 +639,7 @@ import java.util.concurrent.atomic.AtomicInteger;
                transaction.setComplete();
            }
        }
    }

    /**
     * Handles a message delivery response from a Context Hub.
@@ -620,6 +649,28 @@ import java.util.concurrent.atomic.AtomicInteger;
     */
    /* package */
    void onMessageDeliveryResponse(int messageSequenceNumber, boolean success) {
        if (Flags.simplifyServiceTransactionLock()) {
            ContextHubServiceTransaction transaction = null;
            synchronized (mReliableMessageLock) {
                transaction = mReliableMessageTransactionMap.get(messageSequenceNumber);
                if (transaction == null) {
                    Log.w(
                            TAG,
                            "Could not find reliable message transaction with "
                                    + "message sequence number = "
                                    + messageSequenceNumber);
                    return;
                }

                removeMessageTransaction(transaction);

                completeMessageTransaction(
                        transaction,
                        success
                                ? ContextHubTransaction.RESULT_SUCCESS
                                : ContextHubTransaction.RESULT_FAILED_AT_HUB);
            }
        } else {
            ContextHubServiceTransaction transaction = null;
            synchronized (mReliableMessageLock) {
                transaction = mReliableMessageTransactionMap.get(messageSequenceNumber);
@@ -640,6 +691,8 @@ import java.util.concurrent.atomic.AtomicInteger;
                    success
                            ? ContextHubTransaction.RESULT_SUCCESS
                            : ContextHubTransaction.RESULT_FAILED_AT_HUB);
        }

        mExecutor.execute(() -> processMessageTransactions());
    }

@@ -650,6 +703,22 @@ import java.util.concurrent.atomic.AtomicInteger;
     */
    /* package */
    void onQueryResponse(List<NanoAppState> nanoAppStateList) {
        if (Flags.simplifyServiceTransactionLock()) {
            synchronized (mTransactionLock) {
                TransactionAcceptConditions conditions =
                        transaction ->
                                transaction.getTransactionType()
                                        == ContextHubTransaction.TYPE_QUERY_NANOAPPS;
                ContextHubServiceTransaction transaction = getTransactionAndHandleNext(conditions);
                if (transaction == null) {
                    Log.w(TAG, "Received unexpected query response");
                    return;
                }

                transaction.onQueryResponse(ContextHubTransaction.RESULT_SUCCESS, nanoAppStateList);
                transaction.setComplete();
            }
        } else {
            TransactionAcceptConditions conditions =
                    transaction ->
                            transaction.getTransactionType()
@@ -665,6 +734,7 @@ import java.util.concurrent.atomic.AtomicInteger;
                transaction.setComplete();
            }
        }
    }

    /** Handles a hub reset event by stopping a pending transaction and starting the next. */
    /* package */
@@ -729,9 +799,13 @@ import java.util.concurrent.atomic.AtomicInteger;
        cancelTimeoutFuture();

        ContextHubServiceTransaction transaction = mTransactionQueue.remove();
        if (Flags.simplifyServiceTransactionLock()) {
            transaction.setComplete();
        } else {
            synchronized (transaction) {
                transaction.setComplete();
            }
        }

        if (!mTransactionQueue.isEmpty()) {
            startNextTransaction();
@@ -766,6 +840,17 @@ import java.util.concurrent.atomic.AtomicInteger;
            if (result == ContextHubTransaction.RESULT_SUCCESS) {
                Runnable onTimeoutFunc =
                        () -> {
                            if (Flags.simplifyServiceTransactionLock()) {
                                synchronized (mTransactionLock) {
                                    if (!transaction.isComplete()) {
                                        Log.d(TAG, transaction + " timed out");
                                        transaction.onTransactionComplete(
                                                ContextHubTransaction.RESULT_FAILED_TIMEOUT);
                                        transaction.setComplete();
                                        removeTransactionAndStartNext();
                                    }
                                }
                            } else {
                                synchronized (transaction) {
                                    if (!transaction.isComplete()) {
                                        Log.d(TAG, transaction + " timed out");
@@ -778,6 +863,7 @@ import java.util.concurrent.atomic.AtomicInteger;
                                synchronized (mTransactionLock) {
                                    removeTransactionAndStartNext();
                                }
                            }
                        };

                long timeoutMs = transaction.getTimeout(TimeUnit.MILLISECONDS);
@@ -787,12 +873,18 @@ import java.util.concurrent.atomic.AtomicInteger;
                } catch (Exception e) {
                    Log.e(TAG, "Error when schedule a timer", e);
                }
            } else {
                if (Flags.simplifyServiceTransactionLock()) {
                    transaction.onTransactionComplete(
                            ContextHubServiceUtil.toTransactionResult(result));
                    transaction.setComplete();
                } else {
                    synchronized (transaction) {
                        transaction.onTransactionComplete(
                                ContextHubServiceUtil.toTransactionResult(result));
                        transaction.setComplete();
                    }
                }

                mTransactionQueue.remove();
            }
@@ -874,12 +966,18 @@ import java.util.concurrent.atomic.AtomicInteger;
     * @param transaction The transaction to complete.
     * @param result The result code.
     */
    @GuardedBy("mReliableMessageLock")
    private void completeMessageTransaction(
            ContextHubServiceTransaction transaction, @ContextHubTransaction.Result int result) {
        if (Flags.simplifyServiceTransactionLock()) {
            transaction.onTransactionComplete(result);
            transaction.setComplete();
        } else {
            synchronized (transaction) {
                transaction.onTransactionComplete(result);
                transaction.setComplete();
            }
        }

        Log.d(
                TAG,
@@ -897,7 +995,6 @@ import java.util.concurrent.atomic.AtomicInteger;
     * @param result The result code.
     * @param iter The iterator for the reliable message map - used to remove the message directly.
     */
    @GuardedBy("mReliableMessageLock")
    private void removeAndCompleteMessageTransaction(
            ContextHubServiceTransaction transaction,
            @ContextHubTransaction.Result int result,