Tervel  1.0.0
A collection of wait-free containers and algorithms.
lock_stl_map.h
Go to the documentation of this file.
1 /*
2 #The MIT License (MIT)
3 #
4 #Copyright (c) 2015 University of Central Florida's Computer Software Engineering
5 #Scalable & Secure Systems (CSE - S3) Lab
6 #
7 #Permission is hereby granted, free of charge, to any person obtaining a copy
8 #of this software and associated documentation files (the "Software"), to deal
9 #in the Software without restriction, including without limitation the rights
10 #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 #copies of the Software, and to permit persons to whom the Software is
12 #furnished to do so, subject to the following conditions:
13 #
14 #The above copyright notice and this permission notice shall be included in
15 #all copies or substantial portions of the Software.
16 #
17 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 #THE SOFTWARE.
24 #
25 */
26 
27 #ifndef API_LOCK_STL_H_
28 #define API_LOCK_STL_H_
29 
30 #include <boost/thread.hpp>
31 #include <unordered_map>
32 #include <tr1/unordered_map>
33 
34 template<class Key, class Value>
35 class TestClass {
36 
37  template<class T>
38  struct s_hash : public std::unary_function<T, std::size_t> {
39  std::size_t operator()(T const &k) const{
40  size_t hash_v = 0;
41  char * temp = (char *)(&k);
42  for (int i = 0; i < sizeof(Key); i++) {
43  hash_v += temp[i];
44  }
45  hash_v = hash_v + 1;
46  return hash_v;
47  }
48  };
49 
50  typedef struct s_hash<Key> c_hash;
51 
52  template <class T> struct equal_to : std::binary_function <T,T,bool> {
53  bool operator() (const T& x, const T& y) const
54  {
55  bool res = memcmp(&x, &y, sizeof(Key)) == 0;
56  return res;
57  }
58  };
59  typedef struct equal_to<Key> c_equals;
60 
61  typedef typename std::tr1::unordered_map<Key, Value, c_hash, c_equals, std::allocator<std::pair<Key, Value> > > hash_t;
62  public:
63  TestClass(size_t num_threads, size_t capacity) {
64  test_container = new hash_t(capacity);
65 
66  }
67 
68  std::string toString() {
69  return "Locked STL Map";
70  }
71 
72  void attach_thread() {}
73 
74  void detach_thread() {}
75 
76  bool find(Key key, Value &value) {
77  boost::mutex::scoped_lock lock(v_mutex);
78  bool res = false;
79  typename hash_t::iterator iter = test_container->find(key);
80  if(iter != test_container->end()) {
81  value = iter->second;
82  res = true;
83  }
84 
85  return res;
86  }
87 
88  bool insert(Key key, Value value) {
89  boost::mutex::scoped_lock lock(v_mutex);
90  std::pair<typename hash_t::iterator, bool> res = test_container->insert(std::make_pair(key,value));
91 
92  return res.second;
93  }
94 
95  bool update(Key key, Value &value_expected, Value value_new) {
96  boost::mutex::scoped_lock lock(v_mutex);
97  bool res = false;
98  typename hash_t::iterator iter = test_container->find(key);
99  if(iter != test_container->end()) {
100  if (iter->second == value_expected) {
101  iter->second = value_new;
102  res = true;
103  } else {
104  value_expected = iter->second;
105  }
106 
107  }
108 
109  return res;
110  }
111 
112  bool remove(Key key) {
113  boost::mutex::scoped_lock lock(v_mutex);
114  int res= test_container->erase(key);
115 
116  return res;
117  }
118 
119  size_t size() {
120  return test_container->size();
121  }
122 
123  private:
124  boost::mutex v_mutex;
125  hash_t *test_container;
126 
127 };
128 
129 #endif //API_LOCK_STL_H_
struct equal_to< Value > c_equals
Definition: lock_boost_map.h:58
void detach_thread()
Definition: lock_stl_map.h:74
bool operator()(const T &x, const T &y) const
Definition: lock_boost_map.h:52
bool insert(Key key, Value value)
Definition: lock_stl_map.h:88
std::tr1::unordered_map< Key, Value, c_hash, c_equals, std::allocator< std::pair< Key, Value > > > hash_t
Definition: lock_stl_map.h:61
cds::container::MichaelHashMap< cds::gc::HP, Key2Value_list, map_traits > hash_t
Definition: cds_michael_map.h:70
void attach_thread()
Definition: lock_stl_map.h:72
hash_t * test_container
Definition: cds_michael_map.h:158
struct s_hash< Value > c_hash
Definition: lock_boost_map.h:49
bool update(Key key, Value &value_expected, Value value_new)
Definition: lock_stl_map.h:95
uint64_t Value
Definition: testObject.h:59
size_t size()
Definition: lock_stl_map.h:119
bool find(Key key, Value &value)
Definition: lock_stl_map.h:76
Definition: blank_api.h:31
TestClass(size_t num_threads, size_t capacity)
Definition: lock_stl_map.h:63
boost::mutex v_mutex
Definition: lock_boost_map.h:122
std::size_t operator()(T const &k) const
Definition: lock_stl_map.h:39
std::string toString()
Definition: lock_stl_map.h:68