Tervel  1.0.0
A collection of wait-free containers and algorithms.
tbb_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 TBB_MAP_API_H_
28 #define TBB_MAP_API_H_
29 
30 
31 #include <tbb/concurrent_hash_map.h>
32 using namespace tbb;
33 
34 
35 template<class Key, class Value>
36 class TestClass {
37  private:
38  struct MyHashCompare {
39  static size_t hash(const Key &k) {
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  static bool equal(const Key &key1, const Key &key2) {
50  bool res = memcmp(&key1, &key2, sizeof(Key)) == 0;
51  return res;
52  }
53  };
54  typename tbb::concurrent_hash_map<Key, Value, MyHashCompare> * container;
55 
56  public:
57  TestClass(size_t num_threads, size_t capacity) {
58  container = new tbb::concurrent_hash_map<Key, Value, MyHashCompare>
59  (capacity);
60  }
61 
62  std::string toString() {
63  return "TBB Map";
64  }
65 
66  void attach_thread() {}
67  void detach_thread() {}
68 
69  bool find(Key key, Value &value) {
70  typename tbb::concurrent_hash_map<Key, Value, MyHashCompare>::const_accessor a;
71 
72  if (container->find(a, key)) {
73  value = a->second;
74  return true;
75  } else {
76  return false;
77  }
78  };
79 
80  bool insert(Key key, Value value) {
81  return container->insert( std::make_pair(key, value) );
82  };
83 
84  bool update(Key key, Value &value_expected, Value value_new) {
85  typename tbb::concurrent_hash_map<Key, Value, MyHashCompare>::accessor a;
86 
87  if (container->find(a, key)) {
88  if (a->second == value_expected) {
89  a->second = value_new;
90  return true;
91  } else {
92  value_expected = a->second;
93  return false;
94  }
95  }
96  return false;
97  };
98 
99  bool remove(Key key) {
100  return container->erase(key);
101  };
102 
103  size_t size() {
104  return container->size();
105  };
106 };
107 
108 #endif //
void detach_thread()
Definition: tbb_map.h:67
bool insert(Key key, Value value)
Definition: tbb_map.h:80
static bool equal(const Key &key1, const Key &key2)
Definition: tbb_map.h:49
void attach_thread()
Definition: tbb_map.h:66
Definition: tbb_map.h:38
bool update(Key key, Value &value_expected, Value value_new)
Definition: tbb_map.h:84
uint64_t Value
Definition: testObject.h:59
size_t size()
Definition: tbb_map.h:103
bool find(Key key, Value &value)
Definition: tbb_map.h:69
Definition: blank_api.h:31
TestClass(size_t num_threads, size_t capacity)
Definition: tbb_map.h:57
std::string toString()
Definition: tbb_map.h:62
tbb::concurrent_hash_map< Key, Value, MyHashCompare > * container
Definition: tbb_map.h:54
static size_t hash(const Key &k)
Definition: tbb_map.h:39