PackageManagerService分析(Android 10)->Pms的两把锁

/** * Keep track of all those APKs everywhere. * <p> * Internally there are two important locks: * <ul> * <li>{@link #mPackages} is used to guard all in-memory parsed package details * and other related state. It is a fine-grained lock that should only be held * momentarily, as it's one of the most contended locks in the system. * <li>{@link #mInstallLock} is used to guard all {@code installd} access, whose * operations typically involve heavy lifting of application data on disk. Since * {@code installd} is single-threaded, and it's operations can often be slow, * this lock should never be acquired while already holding {@link #mPackages}. * Conversely, it's safe to acquire {@link #mPackages} momentarily while already * holding {@link #mInstallLock}. * </ul> * Many internal methods rely on the caller to hold the appropriate locks, and * this contract is expressed through method name suffixes: * <ul> * <li>fooLI(): the caller must hold {@link #mInstallLock} * <li>fooLIF(): the caller must hold {@link #mInstallLock} and the package * being modified must be frozen * <li>fooLPr(): the caller must hold {@link #mPackages} for reading * <li>fooLPw(): the caller must hold {@link #mPackages} for writing * </ul> * <p> * Because this class is very central to the platform's security; please run all * CTS and unit tests whenever making modifications: * * <pre> * $ runtest -c android.content.pm.PackageManagerTests frameworks-core * $ cts-tradefed run commandAndExit cts -m CtsAppSecurityHostTestCases * </pre> */

注释里面说的比较清楚Pms有两把锁,一把叫mPackages,是fine-grained lock(细粒度的锁)。 另外一把是mInstallLock,它是一把重量级的锁。 另外还有四个方法实例表示该方法持有什么样的锁,分别是:java

  • fooLI() LI结尾表示调用者必须持有mInstallLock这把锁
  • fooLIF() LIF结尾调用者必须持有mInstallLock 这把锁,而且必须冻结(冻结表示禁止启动)要修改的软件包。
  • fooLPr() LPr结尾表示必须持有mPackages才能够调用,而且该方法只读mPackages锁保护的数据。
  • fooLPw() LPw结尾表示必须持有mPackages才能够调用,而且该方法会修改mPackages锁保护的数据。

另外因为mPackages为细粒度的锁, mInstallLock为重量级锁,因此不该该在持有 mPackages锁的状况下再去获取mInstallLock,不然mPackages也会变成重量级。android

上述都不是重点, 重点是Pms到底如何使用这两把锁, 如今由我来给你们说明。express

PackageManagerService主要有两项工做:
1 应用安装卸载: mInstallLock 主要保证这个流程是串行执行。而这个过程会修改mPackages锁保护的数据,mPackages保证安装卸载过程当中查询数据正确。
2 应用信息查询:mPackages 也是保证查询数据正确,这个过程不会修改mPackages保护的数据。

app

相关文章
相关标签/搜索