X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fnormalize_cache.c;h=3ae9124488de675cb4736fd3b632a7484d80dc67;hb=64dccf5757a22cedd3c21ca834e3e02f39dd0504;hp=e67eedb72da94e7cff85824da124e03ebe190892;hpb=ba5951a80fdd0da1d28c318852b69a1178cd6bfa;p=pazpar2-moved-to-github.git diff --git a/src/normalize_cache.c b/src/normalize_cache.c index e67eedb..3ae9124 100644 --- a/src/normalize_cache.c +++ b/src/normalize_cache.c @@ -1,5 +1,5 @@ /* This file is part of Pazpar2. - Copyright (C) 2006-2010 Index Data + Copyright (C) Index Data Pazpar2 is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -20,12 +20,14 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include +#include #include #if HAVE_CONFIG_H #include #endif +#include "ppmutex.h" #include "normalize_cache.h" #include "pazpar2_config.h" @@ -39,6 +41,7 @@ struct cached_item { struct normalize_cache_s { struct cached_item *items; NMEM nmem; + YAZ_MUTEX mutex; }; normalize_cache_t normalize_cache_create(void) @@ -47,6 +50,8 @@ normalize_cache_t normalize_cache_create(void) normalize_cache_t nc = nmem_malloc(nmem, sizeof(*nc)); nc->nmem = nmem; nc->items = 0; + nc->mutex = 0; + pazpar2_mutex_create(&nc->mutex, "normalize_cache"); return nc; } @@ -55,20 +60,27 @@ normalize_record_t normalize_cache_get(normalize_cache_t nc, const char *spec) { normalize_record_t nt; - struct cached_item *ci = nc->items; - for (; ci; ci = ci->next) - if (!strcmp(spec, ci->spec)) - return ci->nt; + struct cached_item *ci; - nt = normalize_record_create(service, spec); - if (nt) + yaz_mutex_enter(nc->mutex); + for (ci = nc->items; ci; ci = ci->next) + if (!strcmp(spec, ci->spec)) + break; + if (ci) + nt = ci->nt; + else { - ci = nmem_malloc(nc->nmem, sizeof(*ci)); - ci->next = nc->items; - nc->items = ci; - ci->nt = nt; - ci->spec = nmem_strdup(nc->nmem, spec); + nt = normalize_record_create(service, spec); + if (nt) + { + ci = nmem_malloc(nc->nmem, sizeof(*ci)); + ci->next = nc->items; + nc->items = ci; + ci->nt = nt; + ci->spec = nmem_strdup(nc->nmem, spec); + } } + yaz_mutex_leave(nc->mutex); return nt; } @@ -79,6 +91,7 @@ void normalize_cache_destroy(normalize_cache_t nc) struct cached_item *ci = nc->items; for (; ci; ci = ci->next) normalize_record_destroy(ci->nt); + yaz_mutex_destroy(&nc->mutex); nmem_destroy(nc->nmem); } }