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

Commit 976bb4d5 authored by Avichal Rakesh's avatar Avichal Rakesh
Browse files

camera: make waiting for state more accurate

The current implementation of Camera3Device doesn't account for spurious
thread wakeups when waiting for a specific device state in
`waitUntilStateThenRelock`. This is usually not an issue but in the
worst case can double or triple the time a thread waits for the device
to reach the desired state.

This CL reduces the timeout every time the thread wakes up to ensure
that a thread waits only as long as specified by the passed parameter.

Bug: 223736973
Test: Existing camera CTS tests pass
Change-Id: Ie25c9c8e48f7cd369a089803774d62f6ba566c88
parent ce01b72e
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -1549,13 +1549,22 @@ status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
    }

    bool stateSeen = false;
    nsecs_t startTime = systemTime();
    do {
        if (active == (mStatus == STATUS_ACTIVE)) {
            // Desired state is current
            break;
        }

        res = mStatusChanged.waitRelative(mLock, timeout);
        nsecs_t timeElapsed = systemTime() - startTime;
        nsecs_t timeToWait = timeout - timeElapsed;
        if (timeToWait <= 0) {
            // Thread woke up spuriously but has timed out since.
            // Force out of loop with TIMED_OUT result.
            res = TIMED_OUT;
            break;
        }
        res = mStatusChanged.waitRelative(mLock, timeToWait);
        if (res != OK) break;

        // This is impossible, but if not, could result in subtle deadlocks and invalid state