aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2008-10-07 02:57:59 -0700
committerEric Wong <normalperson@yhbt.net>2008-10-07 03:08:32 -0700
commit19c6f1ee92cf6514e885def44f7deb9d225de4dc (patch)
tree4e7d98a99de3212505e996c21edd1822906d9c20 /src
parenta340bf0dee5bf134770f5d5f532efa90cdb08b3c (diff)
downloadmpd-19c6f1ee92cf6514e885def44f7deb9d225de4dc.tar.gz
mpd-19c6f1ee92cf6514e885def44f7deb9d225de4dc.tar.xz
mpd-19c6f1ee92cf6514e885def44f7deb9d225de4dc.zip
directory: serialize song deletes from playlist during update
This makes the update code thread-safe and doesn't penalize the playlist code by complicating it with complicated and error-prone locks (and the associated overhead, not everybody has a thread-implementation as good as NPTL). The update task blocks during the delete; but the update task is a slow task anyways so we can block w/o people caring too much. This was also our only freeSong call site, so remove that function. Note that deleting entire directories is not fully thread-safe, yet; as their traversals are not yet locked.
Diffstat (limited to '')
-rw-r--r--src/directory.c30
-rw-r--r--src/song.c6
-rw-r--r--src/song.h2
3 files changed, 27 insertions, 11 deletions
diff --git a/src/directory.c b/src/directory.c
index a11d01d25..9613bbfbd 100644
--- a/src/directory.c
+++ b/src/directory.c
@@ -63,8 +63,13 @@ static time_t directory_dbModTime;
static pthread_t update_thr;
static const int update_task_id_max = 1 << 15;
+
static int update_task_id;
+static Song *delete;
+
+static struct condition delete_cond;
+
static int addToDirectory(Directory * directory, const char *name);
static void freeDirectory(Directory * directory);
@@ -144,6 +149,16 @@ void reap_update_task(void)
assert(pthread_equal(pthread_self(), main_task));
+ cond_enter(&delete_cond);
+ if (delete) {
+ char tmp[MPD_PATH_MAX];
+ LOG("removing: %s\n", get_song_url(tmp, delete));
+ deleteASongFromPlaylist(delete);
+ delete = NULL;
+ cond_signal(&delete_cond);
+ }
+ cond_leave(&delete_cond);
+
if (progress != UPDATE_PROGRESS_DONE)
return;
if (pthread_join(update_thr, (void **)&ret))
@@ -215,10 +230,19 @@ static void freeDirectory(Directory * directory)
static void delete_song(Directory *dir, Song *del)
{
- char path_max_tmp[MPD_PATH_MAX]; /* wasteful */
- LOG("removing: %s\n", get_song_url(path_max_tmp, del));
+ /* first, prevent traversers in main task from getting this */
songvec_delete(&dir->songs, del);
- freeSong(del); /* FIXME racy */
+
+ /* now take it out of the playlist (in the main_task) */
+ cond_enter(&delete_cond);
+ assert(!delete);
+ delete = del;
+ wakeup_main_task();
+ do { cond_wait(&delete_cond); } while (delete);
+ cond_leave(&delete_cond);
+
+ /* finally, all possible references gone, free it */
+ freeJustSong(del);
}
static void deleteEmptyDirectoriesInDirectory(Directory * directory)
diff --git a/src/song.c b/src/song.c
index f967e3e17..c9301386d 100644
--- a/src/song.c
+++ b/src/song.c
@@ -79,12 +79,6 @@ Song *newSong(const char *url, Directory * parentDir)
return song;
}
-void freeSong(Song * song)
-{
- deleteASongFromPlaylist(song);
- freeJustSong(song);
-}
-
void freeJustSong(Song * song)
{
if (song->tag)
diff --git a/src/song.h b/src/song.h
index d8189d42c..9aa9efa94 100644
--- a/src/song.h
+++ b/src/song.h
@@ -42,8 +42,6 @@ typedef struct _Song {
Song *newSong(const char *url, struct _Directory *parentDir);
-void freeSong(Song *);
-
void freeJustSong(Song *);
ssize_t song_print_info(Song * song, int fd);