#include "debug.h"
#include "utils/linked_list.h"
+#include "threading/rwlock.h"
#define MAX_INCLUSION_LEVEL 10
* contents of loaded files
*/
linked_list_t *files;
+
+ /**
+ * lock to safely access the settings
+ */
+ rwlock_t *lock;
};
/**
}
/**
- * find a section by a given key
+ * Find a section by a given key (thread-safe).
*/
-static section_t *find_section(section_t *section, char *key, va_list args)
+static section_t *find_section(private_settings_t *this, section_t *section,
+ char *key, va_list args)
{
char buf[128], keybuf[512];
+ section_t *found;
if (snprintf(keybuf, sizeof(keybuf), "%s", key) >= sizeof(keybuf))
{
return NULL;
}
- return find_section_buffered(section, keybuf, keybuf, args, buf, sizeof(buf));
+ this->lock->read_lock(this->lock);
+ found = find_section_buffered(section, keybuf, keybuf, args, buf,
+ sizeof(buf));
+ this->lock->unlock(this->lock);
+ return found;
}
/**
}
/**
- * Find the string value for a key
+ * Find the string value for a key (thread-safe).
*/
-static char *find_value(section_t *section, char *key, va_list args)
+static char *find_value(private_settings_t *this, section_t *section,
+ char *key, va_list args)
{
- char buf[128], keybuf[512];
+ char buf[128], keybuf[512], *value;
if (snprintf(keybuf, sizeof(keybuf), "%s", key) >= sizeof(keybuf))
{
return NULL;
}
- return find_value_buffered(section, keybuf, keybuf, args, buf, sizeof(buf));
+ this->lock->read_lock(this->lock);
+ value = find_value_buffered(section, keybuf, keybuf, args, buf,
+ sizeof(buf));
+ this->lock->unlock(this->lock);
+ return value;
}
METHOD(settings_t, get_str, char*,
va_list args;
va_start(args, def);
- value = find_value(this->top, key, args);
+ value = find_value(this, this->top, key, args);
va_end(args);
if (value)
{
va_list args;
va_start(args, def);
- value = find_value(this->top, key, args);
+ value = find_value(this, this->top, key, args);
va_end(args);
if (value)
{
va_list args;
va_start(args, def);
- value = find_value(this->top, key, args);
+ value = find_value(this, this->top, key, args);
va_end(args);
if (value)
{
va_list args;
va_start(args, def);
- value = find_value(this->top, key, args);
+ value = find_value(this, this->top, key, args);
va_end(args);
if (value)
{
va_list args;
va_start(args, def);
- value = find_value(this->top, key, args);
+ value = find_value(this, this->top, key, args);
va_end(args);
if (value)
{
va_list args;
va_start(args, key);
- section = find_section(this->top, key, args);
+ section = find_section(this, this->top, key, args);
va_end(args);
if (!section)
{
return enumerator_create_empty();
}
+ this->lock->read_lock(this->lock);
return enumerator_create_filter(
- section->sections->create_enumerator(section->sections),
- (void*)section_filter, NULL, NULL);
+ section->sections->create_enumerator(section->sections),
+ (void*)section_filter, this->lock, (void*)this->lock->unlock);
}
/**
va_list args;
va_start(args, key);
- section = find_section(this->top, key, args);
+ section = find_section(this, this->top, key, args);
va_end(args);
if (!section)
{
return enumerator_create_empty();
}
+ this->lock->read_lock(this->lock);
return enumerator_create_filter(
section->kv->create_enumerator(section->kv),
- (void*)kv_filter, NULL, NULL);
+ (void*)kv_filter, this->lock, (void*)this->lock->unlock);
}
/**
return FALSE;
}
+ this->lock->write_lock(this->lock);
/* extend parent section */
section_extend(parent, section);
/* move contents of loaded files to main store */
{
this->files->insert_last(this->files, text);
}
+ this->lock->unlock(this->lock);
section_destroy(section);
files->destroy(files);
{
section_destroy(this->top);
this->files->destroy_function(this->files, (void*)free);
+ this->lock->destroy(this->lock);
free(this);
}
},
.top = section_create(NULL),
.files = linked_list_create(),
+ .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
);
if (file == NULL)