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

Commit dcb26bbf authored by Fyodor Kupolov's avatar Fyodor Kupolov
Browse files

Avoid locking in getCurrentUser when there is no user switch

Added an optimization to avoid AM lock when userId is not going to
change soon due to mTargetUserId.

Test: manual
Bug: 38143512
Change-Id: Ie358abdb74836ed0dbf782a6ab00362f3685353d
parent e16b83bb
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -123,12 +123,13 @@ final class UserController {
    private final Injector mInjector;
    private final Handler mHandler;

    // Holds the current foreground user's id
    // Holds the current foreground user's id. Use mLock when updating
    @GuardedBy("mLock")
    private int mCurrentUserId = UserHandle.USER_SYSTEM;
    // Holds the target user's id during a user switch
    private volatile int mCurrentUserId = UserHandle.USER_SYSTEM;
    // Holds the target user's id during a user switch. The value of mCurrentUserId will be updated
    // once target user goes into the foreground. Use mLock when updating
    @GuardedBy("mLock")
    private int mTargetUserId = UserHandle.USER_NULL;
    private volatile int mTargetUserId = UserHandle.USER_NULL;

    /**
     * Which users have been started, so are allowed to run code.
@@ -1505,6 +1506,11 @@ final class UserController {
            Slog.w(TAG, msg);
            throw new SecurityException(msg);
        }

        // Optimization - if there is no pending user switch, return current id
        if (mTargetUserId == UserHandle.USER_NULL) {
            return getUserInfo(mCurrentUserId);
        }
        synchronized (mLock) {
            return getCurrentUserLocked();
        }