Slight refactor of checking if a destination exists

This commit is contained in:
Nils O. Selåsdal
2014-04-19 00:36:24 +02:00
parent edde6ec7b6
commit 008004c6bd
+18 -18
View File
@@ -239,24 +239,30 @@ struct UCLogDestination *uc_log_new_file(const char *filename, int log_level, in
return dest;
}
void uc_log_add_destination(struct UCLogDestination *dest)
//Check if a destination already exists (hold g_log_lock when calling this)
static int uc_log_dest_exists_unlocked(struct UCLogDestination *dest)
{
struct UCLogDestination *it;
int exists = 0;
UC_TAILQ_FOREACH_CONTAINER(it, entry, &g_destinations) {
if (dest == it) {
return 1;
}
}
return 0;
}
void uc_log_add_destination(struct UCLogDestination *dest)
{
int exists;
if (dest == NULL)
return;
pthread_mutex_lock(&g_log_lock);
//make sure the destination isn't added twice.
UC_TAILQ_FOREACH_CONTAINER(it, entry, &g_destinations) {
if (dest == it) {
exists = 1;
break;
}
}
exists = uc_log_dest_exists_unlocked(dest);
//add if it isn't already in the list
if (!exists) {
uc_tailq_insert_head(&g_destinations, &dest->entry);
@@ -268,16 +274,10 @@ void uc_log_add_destination(struct UCLogDestination *dest)
//hold g_log_lock while calling this.
static inline void uc_log_remove_dest_unlocked(struct UCLogDestination *dest)
{
struct UCLogDestination *it;
int exists = 0;
int exists;
//check that it's not already removed
UC_TAILQ_FOREACH_CONTAINER(it, entry, &g_destinations) {
if (it == dest) {
exists = 1;
break;
}
}
exists = uc_log_dest_exists_unlocked(dest);
if (exists) {
uc_tailq_remove(&dest->entry);