Tervel  1.0.0
A collection of wait-free containers and algorithms.
wf_hashmap_api.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 WF_HASHMAP_API_H_
28 #define WF_HASHMAP_API_H_
29 
30 
32 #include <tervel/util/info.h>
34 #include <tervel/util/tervel.h>
37 
38 #ifndef HASHMAP_EXPANSION_RATE
39  #define HASHMAP_EXPANSION_RATE 3
40 #endif
41 
42 template<class Key, class Value>
43 class TestClass {
44  public:
47 
48  TestClass(size_t num_threads, size_t capacity) {
49  tervel_obj = new tervel::Tervel(num_threads);
50  attach_thread();
51  container = new Map(capacity, HASHMAP_EXPANSION_RATE);
52  }
53 
54  std::string toString() {
55  return "WF Hash Map-"+std::to_string(HASHMAP_EXPANSION_RATE);
56  }
57 
58  void attach_thread() {
59  tervel::ThreadContext* thread_context __attribute__((unused));
60  thread_context = new tervel::ThreadContext(tervel_obj);
61  }
62 
63  void detach_thread() {}
64 
65  bool find(Key key, Value &value) {
67  bool res = container->at(key, va);
68  if (res) {
69  value = *(va.value());
70  }
71  return res;
72  }
73 
74  bool insert(Key key, Value value) {
75  bool res = container->insert(key, value);
76  return res;
77  }
78 
79  bool update(Key key, Value &value_expected, Value value_new) {
81  if (container->at(key, va)) {
82  std::atomic<Value> *temp = reinterpret_cast<std::atomic<Value> *>(va.value());
83  return temp->compare_exchange_strong(value_expected, value_new);
84  }
85  return false;
86  }
87 
88  bool remove(Key key) {
89  bool res = container->remove(key);
90  return res;
91  }
92 
93  size_t size() {
94  return container->size();
95  }
96 
97  private:
99  Map *container;
100 };
101 
102 #endif // WF_HASHMAP_API_H_
This class is used to safe guard access to values.
Definition: wf_hash_map.h:159
void detach_thread()
Definition: wf_hashmap_api.h:63
Thread local information.
Definition: thread_context.h:62
bool insert(Key key, Value value)
Definition: wf_hashmap_api.h:74
map_t * container
Definition: cliff_api.h:102
Map * container
Definition: wf_hashmap_api.h:99
tervel::containers::wf::HashMap< Key, Value >::ValueAccessor Accessor
Definition: wf_hashmap_api.h:46
Value * value()
Definition: wf_hash_map.h:173
void attach_thread()
Definition: blank_api.h:40
#define HASHMAP_EXPANSION_RATE
Definition: wf_hashmap_api.h:39
bool update(Key key, Value &value_expected, Value value_new)
Definition: wf_hashmap_api.h:79
uint64_t Value
Definition: testObject.h:59
Contains shared information that should be accessible by all threads.
Definition: tervel.h:39
size_t size()
Definition: wf_hashmap_api.h:93
bool find(Key key, Value &value)
Definition: wf_hashmap_api.h:65
Definition: blank_api.h:31
TestClass(size_t num_threads, size_t capacity)
Definition: wf_hashmap_api.h:48
A wait-free hash map implementation.
Definition: wf_hash_map.h:75
tervel::containers::wf::HashMap< Key, Value > Map
Definition: wf_hashmap_api.h:45
tervel::Tervel * tervel_obj
Definition: wf_hashmap_api.h:98
std::string toString()
Definition: wf_hashmap_api.h:54