songvec: avoid holding nr_lock during free(3)

We only need to lock sv->nr changes to prevent traversals ( why
it's called "nr_lock").  free(3) is a "slow" function on my
system; so we can avoid unnecessarily holding a lock long for
longer than needed.
This commit is contained in:
Eric Wong 2008-10-13 09:55:07 +02:00 committed by Max Kellermann
parent 3035a3303d
commit 313405edb6

View File

@ -53,10 +53,12 @@ songvec_delete(struct songvec *sv, const struct song *del)
for (i = sv->nr; --i >= 0; ) {
if (sv->base[i] != del)
continue;
/* we _don't_ call freeSong() here */
/* we _don't_ call song_free() here */
if (!--sv->nr) {
pthread_mutex_unlock(&nr_lock);
free(sv->base);
sv->base = NULL;
return i;
} else {
memmove(&sv->base[i], &sv->base[i + 1],
(sv->nr - i + 1) * sizeof(struct song *));
@ -82,12 +84,12 @@ songvec_add(struct songvec *sv, struct song *add)
void songvec_destroy(struct songvec *sv)
{
pthread_mutex_lock(&nr_lock);
sv->nr = 0;
pthread_mutex_unlock(&nr_lock);
if (sv->base) {
free(sv->base);
sv->base = NULL;
}
sv->nr = 0;
pthread_mutex_unlock(&nr_lock);
}
int