music: synchronize userlibrary value reads

Use synchronized when reading and writing values in UserLibrary.

Convention for all shared objects.
This commit is contained in:
Alexander Capehart 2023-06-07 19:40:31 -06:00
parent 07eefda67a
commit 8d97e86c8d
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
3 changed files with 23 additions and 13 deletions

View file

@ -4,6 +4,7 @@
#### What's Improved #### What's Improved
- Tags formatted as `artistssort` or `albumartistssort` are now recognized by Auxio - Tags formatted as `artistssort` or `albumartistssort` are now recognized by Auxio
- Reduced visual loading time
#### What's Fixed #### What's Fixed
- Disc number is no longer mis-aligned when no subtitle is present - Disc number is no longer mis-aligned when no subtitle is present

View file

@ -429,7 +429,6 @@ constructor(
// Successfully loaded the library, now save the cache and read playlist information // Successfully loaded the library, now save the cache and read playlist information
// in parallel. // in parallel.
logD("Discovered ${rawSongs.size} songs, starting finalization") logD("Discovered ${rawSongs.size} songs, starting finalization")
// TODO: Indicate playlist state in loading process?
emitLoading(IndexingProgress.Indeterminate) emitLoading(IndexingProgress.Indeterminate)
logD("Starting UserLibrary query") logD("Starting UserLibrary query")
if (cache == null || cache.invalidated) { if (cache == null || cache.invalidated) {

View file

@ -161,7 +161,6 @@ private class UserLibraryImpl(
override fun findPlaylist(name: String) = playlistMap.values.find { it.name.raw == name } override fun findPlaylist(name: String) = playlistMap.values.find { it.name.raw == name }
override suspend fun createPlaylist(name: String, songs: List<Song>) { override suspend fun createPlaylist(name: String, songs: List<Song>) {
// TODO: Use synchronized with value access too
val playlistImpl = PlaylistImpl.from(name, songs, musicSettings) val playlistImpl = PlaylistImpl.from(name, songs, musicSettings)
synchronized(this) { playlistMap[playlistImpl.uid] = playlistImpl } synchronized(this) { playlistMap[playlistImpl.uid] = playlistImpl }
val rawPlaylist = val rawPlaylist =
@ -181,8 +180,11 @@ private class UserLibraryImpl(
override suspend fun renamePlaylist(playlist: Playlist, name: String) { override suspend fun renamePlaylist(playlist: Playlist, name: String) {
val playlistImpl = val playlistImpl =
requireNotNull(playlistMap[playlist.uid]) { "Cannot rename invalid playlist" } synchronized(this) {
synchronized(this) { playlistMap[playlist.uid] = playlistImpl.edit(name, musicSettings) } requireNotNull(playlistMap[playlist.uid]) { "Cannot rename invalid playlist" }.also {
playlistMap[it.uid] = it.edit(name, musicSettings)
}
}
try { try {
playlistDao.replacePlaylistInfo(PlaylistInfo(playlist.uid, name)) playlistDao.replacePlaylistInfo(PlaylistInfo(playlist.uid, name))
logD("Successfully renamed $playlist to $name") logD("Successfully renamed $playlist to $name")
@ -195,9 +197,11 @@ private class UserLibraryImpl(
} }
override suspend fun deletePlaylist(playlist: Playlist) { override suspend fun deletePlaylist(playlist: Playlist) {
val playlistImpl = val playlistImpl = synchronized(this) {
requireNotNull(playlistMap[playlist.uid]) { "Cannot remove invalid playlist" } requireNotNull(playlistMap[playlist.uid]) { "Cannot remove invalid playlist" }.also {
synchronized(this) { playlistMap.remove(playlistImpl.uid) } playlistMap.remove(it.uid)
}
}
try { try {
playlistDao.deletePlaylist(playlist.uid) playlistDao.deletePlaylist(playlist.uid)
logD("Successfully deleted $playlist") logD("Successfully deleted $playlist")
@ -210,9 +214,12 @@ private class UserLibraryImpl(
} }
override suspend fun addToPlaylist(playlist: Playlist, songs: List<Song>) { override suspend fun addToPlaylist(playlist: Playlist, songs: List<Song>) {
val playlistImpl = val playlistImpl = synchronized(this) {
requireNotNull(playlistMap[playlist.uid]) { "Cannot add to invalid playlist" } requireNotNull(playlistMap[playlist.uid]) { "Cannot add to invalid playlist" }.also {
synchronized(this) { playlistMap[playlist.uid] = playlistImpl.edit { addAll(songs) } } playlistMap[it.uid] = it.edit { addAll(songs) }
}
}
try { try {
playlistDao.insertPlaylistSongs(playlist.uid, songs.map { PlaylistSong(it.uid) }) playlistDao.insertPlaylistSongs(playlist.uid, songs.map { PlaylistSong(it.uid) })
logD("Successfully added ${songs.size} songs to $playlist") logD("Successfully added ${songs.size} songs to $playlist")
@ -225,9 +232,12 @@ private class UserLibraryImpl(
} }
override suspend fun rewritePlaylist(playlist: Playlist, songs: List<Song>) { override suspend fun rewritePlaylist(playlist: Playlist, songs: List<Song>) {
val playlistImpl = val playlistImpl = synchronized(this) {
requireNotNull(playlistMap[playlist.uid]) { "Cannot rewrite invalid playlist" } requireNotNull(playlistMap[playlist.uid]) { "Cannot rewrite invalid playlist" }.also {
synchronized(this) { playlistMap[playlist.uid] = playlistImpl.edit(songs) } playlistMap[it.uid] = it.edit(songs)
}
}
try { try {
playlistDao.replacePlaylistSongs(playlist.uid, songs.map { PlaylistSong(it.uid) }) playlistDao.replacePlaylistSongs(playlist.uid, songs.map { PlaylistSong(it.uid) })
logD("Successfully rewrote $playlist with ${songs.size} songs") logD("Successfully rewrote $playlist with ${songs.size} songs")