Tervel  1.0.0
A collection of wait-free containers and algorithms.
lock_boost_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_BOOST_H_
28 #define API_LOCK_BOOST_H_
29 
30 #include <boost/thread.hpp>
31 #include <boost/unordered_map.hpp>
32 
33 template<class Key, class Value>
34 class TestClass {
35 
36  template<class T>
37  struct s_hash : public std::unary_function<T, std::size_t> {
38  std::size_t operator()(T const &k) const{
39  size_t hash_v = 0;
40  char * temp = (char *)(&k);
41  for (int i = 0; i < sizeof(Key); i++) {
42  hash_v += temp[i];
43  }
44  hash_v = hash_v + 1;
45  return hash_v;
46  }
47  };
48 
49  typedef struct s_hash<Key> c_hash;
50 
51  template <class T> struct equal_to : std::binary_function <T,T,bool> {
52  bool operator() (const T& x, const T& y) const
53  {
54  bool res = memcmp(&x, &y, sizeof(Key)) == 0;
55  return res;
56  }
57  };
58  typedef struct equal_to<Key> c_equals;
59 
60  typedef typename boost::unordered_map<Key, Value, c_hash, c_equals, std::allocator<std::pair<Key, Value> > > hash_t;
61 
62  public:
63  TestClass(size_t num_threads, size_t capacity) {
64  test_container = new hash_t(capacity);
65  // v_mutex.unlock();
66  }
67 
68  std::string toString() {
69  return "Locked Boost 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  return res.second;
92  }
93 
94  bool update(Key key, Value &value_expected, Value value_new) {
95  boost::mutex::scoped_lock lock(v_mutex);
96  bool res = false;
97  typename hash_t::iterator iter = test_container->find(key);
98  if(iter != test_container->end()) {
99  if (iter->second == value_expected) {
100  iter->second = value_new;
101  res = true;
102  } else {
103  value_expected = iter->second;
104  }
105 
106  }
107  return res;
108  }
109 
110  bool remove(Key key) {
111  boost::mutex::scoped_lock lock(v_mutex);
112  int res= test_container->erase(key);
113 
114  return res;
115  }
116 
117  size_t size() {
118  return test_container->size();
119  }
120 
121  private:
122  boost::mutex v_mutex;
123  hash_t *test_container;
124 
125 };
126 
127 #endif //API_LOCK_BOOST_H_
struct equal_to< Value > c_equals
Definition: lock_boost_map.h:58
void detach_thread()
Definition: lock_boost_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_boost_map.h:88
boost::unordered_map< Key, Value, c_hash, c_equals, std::allocator< std::pair< Key, Value > > > hash_t
Definition: lock_boost_map.h:60
Definition: lock_boost_map.h:51
cds::container::MichaelHashMap< cds::gc::HP, Key2Value_list, map_traits > hash_t
Definition: cds_michael_map.h:70
void attach_thread()
Definition: lock_boost_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_boost_map.h:94
uint64_t Value
Definition: testObject.h:59
size_t size()
Definition: lock_boost_map.h:117
bool find(Key key, Value &value)
Definition: lock_boost_map.h:76
Definition: blank_api.h:31
TestClass(size_t num_threads, size_t capacity)
Definition: lock_boost_map.h:63
boost::mutex v_mutex
Definition: lock_boost_map.h:122
std::size_t operator()(T const &k) const
Definition: lock_boost_map.h:38
std::string toString()
Definition: lock_boost_map.h:68