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
					Eric Wong
				
			
				
					committed by
					
						 Max Kellermann
						Max Kellermann
					
				
			
			
				
	
			
			
			 Max Kellermann
						Max Kellermann
					
				
			
						parent
						
							3035a3303d
						
					
				
				
					commit
					313405edb6
				
			| @@ -53,10 +53,12 @@ songvec_delete(struct songvec *sv, const struct song *del) | |||||||
| 	for (i = sv->nr; --i >= 0; ) { | 	for (i = sv->nr; --i >= 0; ) { | ||||||
| 		if (sv->base[i] != del) | 		if (sv->base[i] != del) | ||||||
| 			continue; | 			continue; | ||||||
| 		/* we _don't_ call freeSong() here */ | 		/* we _don't_ call song_free() here */ | ||||||
| 		if (!--sv->nr) { | 		if (!--sv->nr) { | ||||||
|  | 			pthread_mutex_unlock(&nr_lock); | ||||||
| 			free(sv->base); | 			free(sv->base); | ||||||
| 			sv->base = NULL; | 			sv->base = NULL; | ||||||
|  | 			return i; | ||||||
| 		} else { | 		} else { | ||||||
| 			memmove(&sv->base[i], &sv->base[i + 1], | 			memmove(&sv->base[i], &sv->base[i + 1], | ||||||
| 				(sv->nr - i + 1) * sizeof(struct song *)); | 				(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) | void songvec_destroy(struct songvec *sv) | ||||||
| { | { | ||||||
| 	pthread_mutex_lock(&nr_lock); | 	pthread_mutex_lock(&nr_lock); | ||||||
|  | 	sv->nr = 0; | ||||||
|  | 	pthread_mutex_unlock(&nr_lock); | ||||||
| 	if (sv->base) { | 	if (sv->base) { | ||||||
| 		free(sv->base); | 		free(sv->base); | ||||||
| 		sv->base = NULL; | 		sv->base = NULL; | ||||||
| 	} | 	} | ||||||
| 	sv->nr = 0; |  | ||||||
| 	pthread_mutex_unlock(&nr_lock); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| int | int | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user