Tervel  1.0.0
A collection of wait-free containers and algorithms.
wf_mcas_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_MCAS_API_H_
28 #define WF_MCAS_API_H_
29 #include <string>
30 #include <functional>
31 
32 #include <tervel/util/info.h>
33 #include <tervel/util/tervel.h>
38 
39 class TestClass {
40  public:
41  TestClass(size_t num_threads) {
42  tervel_obj = new tervel::Tervel(num_threads);
43  attach_thread();
44  }
45 
46  std::string toString() {
47  return "WF MCAS";
48  };
49 
50  void attach_thread() {
51  tervel::ThreadContext* thread_context __attribute__((unused));
52  thread_context = new tervel::ThreadContext(tervel_obj);
53  };
54 
55  void detach_thread() {};
56 
57  void * calc_next_value(void * value) {
58  uintptr_t temp = reinterpret_cast<uintptr_t>(value);
59  temp = (temp + 0x16) & (~3);
60  return reinterpret_cast<void *>(temp);
61  }
62 
63  bool mcas(int len, std::function<int()> posFunc, std::atomic<void *> *address) {
64 
67 
68  for (int i = 0; i < len; i++) {
69  bool success;
70  do {
71  int var = posFunc();
72  std::atomic<void *> *pos = &(address[var]);
73  void * expected_value = tervel::algorithms::wf::mcas::read<void *>(pos);
74  void * new_value = calc_next_value(expected_value);
75  success = mcas->add_cas_triple(pos, expected_value, new_value);
76  }while(!success);
77  }
78 
79  bool res = mcas->execute();
80  mcas->safe_delete();
81  return res;
82  }
83 
84 
85  private:
87 };
88 
89 #endif // WF_MCAS_API_H_
void detach_thread()
Definition: wf_mcas_api.h:55
Thread local information.
Definition: thread_context.h:62
TestClass(size_t num_threads)
Definition: wf_mcas_api.h:41
void * calc_next_value(void *value)
Definition: wf_mcas_api.h:57
void attach_thread()
Definition: blank_api.h:40
Definition: mcas_helper.h:42
bool mcas(int len, std::function< int()> posFunc, std::atomic< void * > *address)
Definition: wf_mcas_api.h:63
Contains shared information that should be accessible by all threads.
Definition: tervel.h:39
Definition: blank_api.h:31
tervel::Tervel * tervel_obj
Definition: wf_hashmap_api.h:98
std::string toString()
Definition: wf_mcas_api.h:46