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