27 # ifndef TPL_PROTECTED_CACHE_H 28 # define TPL_PROTECTED_CACHE_H 30 # include <tpl_cache.H> 31 # include <useMutex.H> 35 template <
class Key,
class Data>
36 class Protected_Cache :
protected Cache<Key, Data>
40 pthread_mutex_t mutex;
44 typedef typename Cache<Key, Data>::Cache_Entry Cache_Entry;
46 Protected_Cache(
size_t (*hash_fct)(
const Key&),
const size_t & size)
47 : Cache<Key,Data>(hash_fct, size)
52 virtual ~Protected_Cache()
57 Cache_Entry * insert(
const Key& key,
const Data& data)
59 CRITICAL_SECTION(mutex);
61 return Cache<Key,Data>::insert(key, data);
64 Cache_Entry * search(
const Key & key)
66 CRITICAL_SECTION(mutex);
68 return Cache<Key,Data>::search(key);
71 Cache_Entry * insert_and_lock(
const Key& key,
const Data& data)
73 CRITICAL_SECTION(mutex);
74 Cache_Entry * cache_entry = Cache<Key,Data>::insert(key, data);
75 this->lock_entry(cache_entry);
80 Cache_Entry * search_and_lock(
const Key & key)
82 CRITICAL_SECTION(mutex);
83 Cache_Entry * cache_entry = Cache<Key,Data>::search(key);
84 this->lock_entry(cache_entry);
89 Cache_Entry * search_next_and_lock(Cache_Entry * cache_entry)
91 CRITICAL_SECTION(mutex);
93 return Cache<Key,Data>::search_next(cache_entry);
96 void unlock_entry(Cache_Entry * cache_entry)
98 CRITICAL_SECTION(mutex);
99 Cache<Key,Data>::unlock_entry(cache_entry);
102 void remove(Cache_Entry * cache_entry)
104 CRITICAL_SECTION(mutex);
105 Cache<Key,Data>::remove(cache_entry);
108 void expand(
const size_t & plus_size)
110 CRITICAL_SECTION(mutex);
112 Cache<Key,Data>::expand(plus_size);
115 const size_t & get_size()
const 117 CRITICAL_SECTION(mutex);
119 return Cache<Key,Data>::get_size();
122 const size_t & get_num_items()
const 124 CRITICAL_SECTION(mutex);
126 return Cache<Key,Data>::get_num_items();
131 # endif // TPL_PROTECTED_CACHE_H