Aleph-w  1.9
General library for algorithms and data structures
protected_cache.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 # ifndef TPL_PROTECTED_CACHE_H
28 # define TPL_PROTECTED_CACHE_H
29 
30 # include <tpl_cache.H>
31 # include <useMutex.H>
32 
33 # include <pthread.h>
34 
35  template <class Key, class Data>
36 class Protected_Cache : protected Cache<Key, Data>
37 {
38 private:
39 
40  pthread_mutex_t mutex;
41 
42 public:
43 
44  typedef typename Cache<Key, Data>::Cache_Entry Cache_Entry;
45 
46  Protected_Cache(size_t (*hash_fct)(const Key&), const size_t & size)
47  : Cache<Key,Data>(hash_fct, size)
48  {
49  init_mutex(mutex);
50  }
51 
52  virtual ~Protected_Cache()
53  {
54  destroy_mutex(mutex);
55  }
56 
57  Cache_Entry * insert(const Key& key, const Data& data)
58  {
59  CRITICAL_SECTION(mutex);
60 
61  return Cache<Key,Data>::insert(key, data);
62  }
63 
64  Cache_Entry * search(const Key & key)
65  {
66  CRITICAL_SECTION(mutex);
67 
68  return Cache<Key,Data>::search(key);
69  }
70 
71  Cache_Entry * insert_and_lock(const Key& key, const Data& data)
72  {
73  CRITICAL_SECTION(mutex);
74  Cache_Entry * cache_entry = Cache<Key,Data>::insert(key, data);
75  this->lock_entry(cache_entry);
76 
77  return cache_entry;
78  }
79 
80  Cache_Entry * search_and_lock(const Key & key)
81  {
82  CRITICAL_SECTION(mutex);
83  Cache_Entry * cache_entry = Cache<Key,Data>::search(key);
84  this->lock_entry(cache_entry);
85 
86  return cache_entry;
87  }
88 
89  Cache_Entry * search_next_and_lock(Cache_Entry * cache_entry)
90  {
91  CRITICAL_SECTION(mutex);
92 
93  return Cache<Key,Data>::search_next(cache_entry);
94  }
95 
96  void unlock_entry(Cache_Entry * cache_entry)
97  {
98  CRITICAL_SECTION(mutex);
99  Cache<Key,Data>::unlock_entry(cache_entry);
100  }
101 
102  void remove(Cache_Entry * cache_entry)
103  {
104  CRITICAL_SECTION(mutex);
105  Cache<Key,Data>::remove(cache_entry);
106  }
107 
108  void expand(const size_t & plus_size)
109  {
110  CRITICAL_SECTION(mutex);
111 
112  Cache<Key,Data>::expand(plus_size);
113  }
114 
115  const size_t & get_size() const
116  {
117  CRITICAL_SECTION(mutex);
118 
119  return Cache<Key,Data>::get_size();
120  }
121 
122  const size_t & get_num_items() const
123  {
124  CRITICAL_SECTION(mutex);
125 
126  return Cache<Key,Data>::get_num_items();
127  }
128 };
129 
130 
131 # endif // TPL_PROTECTED_CACHE_H
132 
133 

Leandro Rabindranath León