diff options
Diffstat (limited to 'src/lists.c')
-rw-r--r-- | src/lists.c | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/src/lists.c b/src/lists.c index 00e29ba..98ba866 100644 --- a/src/lists.c +++ b/src/lists.c @@ -33,6 +33,7 @@ typedef struct { } entry_t; static entry_t *entry_list; +static pthread_mutex_t entry_list_mutex = PTHREAD_MUTEX_INITIALIZER; static size_t total_entries; static int parse_entry(lexer_state_t *lexer, vstr_t *token, entry_type_t type) @@ -75,7 +76,8 @@ static int parse_entry(lexer_state_t *lexer, vstr_t *token, entry_type_t type) return 0; } -// FIXME: Make this MT-safe (lock the list before adding shit to it) +// NOTE: This function is not MT-safe. It's to be called only once during +// initialization. Use lists_reload to reload the lists on-the-fly. int lists_load(const char *file, size_t depth) { int error = 1, rv; @@ -127,7 +129,33 @@ out: void lists_destroy(void) { - // TODO... + entry_t *entry, *next; + + for (entry = entry_list; entry; entry = next){ + next = entry->list.next; + + free(entry->pattern); + free(entry); + } + + entry_list = NULL; + total_entries = 0; +} + +int lists_reload(const char *file) +{ + int ret; + + pthread_mutex_lock(&entry_list_mutex); + lists_destroy(); + ret = lists_load(file, 0); + if (!ret) + lists_destroy(); + pthread_mutex_unlock(&entry_list_mutex); + + // FIXME: invalidate all cached results + + return ret; } int lists_test(const char *revdns, const char *whois) @@ -135,6 +163,8 @@ int lists_test(const char *revdns, const char *whois) entry_t *entry; int total = 0; + pthread_mutex_lock(&entry_list_mutex); + eli_for(entry, entry_list, list) { // TODO: regexps if (entry->type == ENTRY_REVDNS && @@ -149,5 +179,7 @@ int lists_test(const char *revdns, const char *whois) total += entry->niceness; } + pthread_mutex_unlock(&entry_list_mutex); + return total; } |