Tervel  1.0.0
A collection of wait-free containers and algorithms.
cds_michael_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_CDS_SPLIT_MAP_H_
28 #define API_CDS_SPLIT_MAP_H_
29 #define USE_CDS 1
30 
31 #include <cds/init.h>
32 #include <cds/gc/hp.h>
33 #include <cds/opt/hash.h>
34 #include <cds/container/michael_kvlist_hp.h>
35 #include <cds/container/michael_map.h>
36 
37 template<class Key, class Value>
38 class TestClass {
39  template <class T>
40  struct less_s : std::binary_function <T,T,bool> {
41  bool operator() (const T& x, const T& y) const {
42  return (memcmp(&x, &y, sizeof(T)) < 0);
43  }
44  };
45 
46 
47  // List traits based on std::less predicate
48  struct list_traits: public cds::container::michael_list::type_traits {
49  typedef struct less_s<Key> less;
50  };
51 
52  // Ordered list
53  typedef typename cds::container::MichaelKVList< cds::gc::HP, Key, Value, list_traits> Key2Value_list;
54  // Map traits
55  struct map_traits: public cds::container::michael_map::type_traits {
56  struct hash {
57  size_t operator()(const Key &k) const {
58  size_t hash_v = 0;
59  char *temp = (char *)(&k);
60  for (int i = 0; i < sizeof(Key); i++) {
61  hash_v += temp[i];
62  }
63  hash_v = hash_v + 1;
64  return hash_v;
65  }
66  };
67  };
68 
69  typedef typename cds::container::MichaelHashMap<cds::gc::HP, Key2Value_list,
70  map_traits > hash_t ;
71 
72  struct functor
73  {
75  bool res;
76  void operator ()(std::pair<Key, Value> pair) {
77  curr_value = pair.second;
78  res = true;
79  }
80 
81  bool getValue(Value &val) {
82  if (res) {
83  val = curr_value;
84  }
85  return res;
86  }
87  };
88 
89  struct ufunctor {
92  bool res;
93 
94  void operator ()( bool bNew, std::pair<Key const, Value >& item) {
95  if (bNew) {
96  res = false;
97  } else {
98  std::atomic<Value> * address = (std::atomic<Value> *)&(item.second);
99  res = address->compare_exchange_strong(value_expected, value_new);
100  }
101  }
102 
103  bool getValue(Value &val) {
104  if (!res) {
105  val = value_expected;
106  }
107  return res;
108  }
109  };
110 
111  public:
112  TestClass(size_t num_threads, size_t capacity) {
113  test_container = new hash_t(capacity, test_michael_map);
114  }
115 
116  std::string toString() {
117  const int t = test_michael_map;
118  return "CDS MICHAEL Map(" + std::to_string(t) + ")";
119  }
120 
121  void attach_thread() {
122  }
123 
124  void detach_thread() {}
125 
126  bool find(Key key, Value &value) {
127  struct functor ef;
128  if (test_container->find(key, cds::ref(ef))) {
129  return ef.getValue(value);
130  } else {
131  return false;
132  }
133  }
134 
135  bool insert(Key key, Value value) {
136  return test_container->insert(key, value);
137  }
138 
139  bool update(Key key, Value &value_expected, Value value_new) {
140  struct ufunctor uf;
141  uf.value_expected = value_expected;
142  uf.value_new = value_new;
143  uf.res = false;
144 
145  test_container->ensure(key, cds::ref(uf));
146  return uf.getValue(value_expected);
147  }
148 
149  bool remove(Key key) {
150  return test_container->erase(key);
151  }
152 
153  size_t size() {
154  return test_container->size();
155  }
156 
157  private:
158  hash_t *test_container;
159 };
160 
161 #endif //API_CDS_SPLIT_MAP_H_
void detach_thread()
Definition: cds_michael_map.h:124
Value value_new
Definition: cds_michael_map.h:90
bool insert(Key key, Value value)
Definition: cds_michael_map.h:135
Definition: cds_michael_map.h:40
size_t operator()(const Key &k) const
Definition: cds_michael_map.h:57
Value curr_value
Definition: cds_michael_map.h:74
bool getValue(Value &val)
Definition: cds_michael_map.h:103
void operator()(bool bNew, std::pair< Key const, Value > &item)
Definition: cds_michael_map.h:94
bool operator()(const T &x, const T &y) const
Definition: cds_michael_map.h:41
Definition: cds_michael_map.h:89
cds::container::MichaelHashMap< cds::gc::HP, Key2Value_list, map_traits > hash_t
Definition: cds_michael_map.h:70
Definition: cds_michael_map.h:48
void attach_thread()
Definition: cds_michael_map.h:121
hash_t * test_container
Definition: cds_michael_map.h:158
Definition: cds_michael_map.h:72
Value value_expected
Definition: cds_michael_map.h:91
Definition: cds_michael_map.h:56
bool update(Key key, Value &value_expected, Value value_new)
Definition: cds_michael_map.h:139
uint64_t Value
Definition: testObject.h:59
size_t size()
Definition: cds_michael_map.h:153
bool res
Definition: cds_michael_map.h:75
bool find(Key key, Value &value)
Definition: cds_michael_map.h:126
void operator()(std::pair< Key, Value > pair)
Definition: cds_michael_map.h:76
cds::container::MichaelKVList< cds::gc::HP, Key, Value, list_traits > Key2Value_list
Definition: cds_michael_map.h:53
bool res
Definition: cds_michael_map.h:92
Definition: blank_api.h:31
TestClass(size_t num_threads, size_t capacity)
Definition: cds_michael_map.h:112
Definition: cds_michael_map.h:55
std::string toString()
Definition: cds_michael_map.h:116
bool getValue(Value &val)
Definition: cds_michael_map.h:81