update: fix multiple deletes from *vec iterators
{song,dir}vec_for_each each failed to gracefully handle deleted files when iterating through. While we were thread-safe, we were not safe within the calling thread. If a callback we passed caused sv->nr to shring, our index would still increment; causing files to stay in the database. A way to test this is to remove 10 or so contiguous songs from a >10 song directory.
This commit is contained in:
parent
9aeacdef56
commit
d52437d43f
@ -98,16 +98,20 @@ int dirvec_for_each(const struct dirvec *dv,
|
|||||||
int (*fn)(struct directory *, void *), void *arg)
|
int (*fn)(struct directory *, void *), void *arg)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
size_t prev_nr;
|
||||||
|
|
||||||
pthread_mutex_lock(&nr_lock);
|
pthread_mutex_lock(&nr_lock);
|
||||||
for (i = 0; i < dv->nr; ++i) {
|
for (i = 0; i < dv->nr; ) {
|
||||||
struct directory *dir = dv->base[i];
|
struct directory *dir = dv->base[i];
|
||||||
|
|
||||||
assert(dir);
|
assert(dir);
|
||||||
|
prev_nr = dv->nr;
|
||||||
pthread_mutex_unlock(&nr_lock);
|
pthread_mutex_unlock(&nr_lock);
|
||||||
if (fn(dir, arg) < 0)
|
if (fn(dir, arg) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
pthread_mutex_lock(&nr_lock); /* dv->nr may change in fn() */
|
pthread_mutex_lock(&nr_lock); /* dv->nr may change in fn() */
|
||||||
|
if (prev_nr == dv->nr)
|
||||||
|
++i;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&nr_lock);
|
pthread_mutex_unlock(&nr_lock);
|
||||||
|
|
||||||
|
@ -96,18 +96,22 @@ songvec_for_each(const struct songvec *sv,
|
|||||||
int (*fn)(struct song *, void *), void *arg)
|
int (*fn)(struct song *, void *), void *arg)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
size_t prev_nr;
|
||||||
|
|
||||||
pthread_mutex_lock(&nr_lock);
|
pthread_mutex_lock(&nr_lock);
|
||||||
for (i = 0; i < sv->nr; ++i) {
|
for (i = 0; i < sv->nr; ) {
|
||||||
struct song *song = sv->base[i];
|
struct song *song = sv->base[i];
|
||||||
|
|
||||||
assert(song);
|
assert(song);
|
||||||
assert(*song->url);
|
assert(*song->url);
|
||||||
|
|
||||||
|
prev_nr = sv->nr;
|
||||||
pthread_mutex_unlock(&nr_lock); /* fn() may block */
|
pthread_mutex_unlock(&nr_lock); /* fn() may block */
|
||||||
if (fn(song, arg) < 0)
|
if (fn(song, arg) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
pthread_mutex_lock(&nr_lock); /* sv->nr may change in fn() */
|
pthread_mutex_lock(&nr_lock); /* sv->nr may change in fn() */
|
||||||
|
if (prev_nr == sv->nr)
|
||||||
|
++i;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&nr_lock);
|
pthread_mutex_unlock(&nr_lock);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user