Introduce database schema locking.

We recently tried fixing some obscure SQLite deadlocking in a51c3d1c,
but the cure was worse than the disease, since any threads just about
to perform a database operation before we close() the database would
encounter an "Cannot perform this operation because the connection
pool has been closed" exception.

Since we don't have control over the deep inner workings of SQLite
which result in the deadlock, the best we can do is to create a
ReadWriteLock where the "readers" are all normal database mutations
or queries, and the "writers" are schema modifications.

This second phase introduces a new ReadWriteLock and begins using
it across all database operations.  This allows us to restore the
original behavior of setFilterVolumeNames() which would apply the
schema change without resorting to closing the entire database.

This new lock is also used to replace mSchemaChanging, since it's
easier to check if the current thread is holding the write lock.

Bug: 152005629, 152802030
Test: atest --test-mapping packages/providers/MediaProvider
Test: atest MediaProviderClientTests:com.android.providers.media.client.LegacyProviderMigrationTest --rerun-until-failure 100
Change-Id: Id0b254e25baa8d960f7a5016e0a719a13b1d2281
3 files changed