Tervel  1.0.0
A collection of wait-free containers and algorithms.
util.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 #ifndef __TERVEL_UTIL_UTIL_H_
26 #define __TERVEL_UTIL_UTIL_H_
27 
28 #include <chrono>
29 #include <thread>
30 #include <cmath>
31 
32 namespace tervel {
33 namespace util {
34 
35 //Memory Protection Macros:
36 
37 // #define TERVEL_MEM_NO_FREE
38 // #define TERVEL_MEM_NO_WATCH
39 // caused both RC and HP versions to be defined
40 #ifdef TERVEL_MEM_NO_FREE
41  #define TERVEL_MEM_HP_NO_FREE
42  #define TERVEL_MEM_RC_NO_FREE
43 #endif
44 #ifdef TERVEL_MEM_NO_WATCH
45  #define TERVEL_MEM_HP_NO_WATCH
46  #define TERVEL_MEM_RC_NO_WATCH
47 #endif
48 
49 // #define TERVEL_MEM_HP_NO_FREE
50  // causes safeDelete to return without freeing the object
51  // its destructor is NOT called.
52 
53 // #define TERVEL_MEM_HP_NO_WATCH
54 /* causes watch functions to return success
55  * causes is_watch to return false
56  * causes unwatch to return immediately
57 */
58 #ifdef TERVEL_MEM_HP_NO_WATCH
59  #ifndef TERVEL_MEM_HP_NO_FREE
60  #define TERVEL_MEM_HP_NO_FREE
61  #endif
62 #endif
63 
64 // #define TERVEL_MEM_RC_NO_FREE
65 // -causes new objects to be allocated from the allocator
66 
67 // #define TERVEL_MEM_RC_NO_WATCH
68 /* causes watch functions to return success
69  * causes is_watch to return false
70  * causes unwatch to return immediately
71 */
72 
73 // #define TERVEL_MEM_RC_MAX_NODES
74  // if the number of nodes in the pool > then this value then the excess is sent to the shared pool
75  // excess is determined by TERVEL_MEM_RC_MIN_NODES
76 #ifndef TERVEL_MEM_RC_MAX_NODES
77  #define TERVEL_MEM_RC_MAX_NODES 25
78 #endif
79 #ifndef TERVEL_MEM_RC_MIN_NODES
80  #define TERVEL_MEM_RC_MIN_NODES 5
81 #endif
82 
83 
84 
85 // TERVEL Progress Assurance MACROS:
86 
87 // #define TERVEL_PROG_ASSUR_DELAY
88 // sets the delay between calling the check for announcement function
89 #ifndef TERVEL_PROG_ASSUR_DELAY
90  #define TERVEL_PROG_ASSUR_DELAY 100
91 #endif
92 
93 // #define TERVEL_PROG_ASSUR_NO_CHECK
94 // sets the delay to -1, which makes it never reach 0 and never triggers the call
95 // if this is set then set TERVEL_PROG_NO_ANNOUNCE
96 #ifdef TERVEL_PROG_ASSUR_NO_CHECK
97  #ifdef TERVEL_PROG_ASSUR_DELAY
98  #undef TERVEL_PROG_ASSUR_DELAY
99  #endif
100  #define TERVEL_PROG_ASSUR_DELAY -1
101  #define TERVEL_PROG_NO_ANNOUNCE
102 #endif
103 
104 
105 // #define TERVEL_PROG_ASSUR_ALWAYS_CHECK
106  // sets the delay to 0, which makes it always trigger
107 #ifdef TERVEL_PROG_ASSUR_ALWAYS_CHECK
108  #ifdef TERVEL_PROG_ASSUR_DELAY
109  #undef TERVEL_PROG_ASSUR_DELAY
110  #endif
111  #define TERVEL_PROG_ASSUR_DELAY 0
112 #endif
113 
114 #ifdef TERVEL_PROG_ASSUR_NO_CHECK
115  #error TERVEL_PROG_ASSUR_NO_CHECK and TERVEL_PROG_ASSUR_ALWAYS_CHECK can not both be set.
116 #endif
117 
118 // #define TERVEL_PROG_ASSUR_LIMIT
119  // sets the delay before making an announcement
120 #ifndef TERVEL_PROG_ASSUR_LIMIT
121  #define TERVEL_PROG_ASSUR_LIMIT 1000
122 #endif
123 
124 // #define TERVEL_PROG_ASSUR_NO_ANNOUNCE
125  // disables the making of an announcement
126  // sets TERVEL_PROG_ASSUR_LIMIT = -1
127 
128 #ifdef TERVEL_PROG_ASSUR_NO_ANNOUNCE
129  #ifdef TERVEL_PROG_ASSUR_LIMIT
130  #undef TERVEL_PROG_ASSUR_LIMIT
131  #endif
132  #define TERVEL_PROG_ASSUR_LIMIT -1
133 #endif
134 
135 // #define TERVEL_PROG_ASSUR_ALWAYS_ANNOUNCE
136  // always use progress assurance
137  // sets TERVEL_PROG_ASSUR_LIMIT = 0
138 #ifdef TERVEL_PROG_ASSUR_ALWAYS_ANNOUNCE
139  #ifdef TERVEL_PROG_ASSUR_LIMIT
140  #undef TERVEL_PROG_ASSUR_LIMIT
141  #endif
142  #define TERVEL_PROG_ASSUR_LIMIT 0
143 #endif
144 
145 
155 inline bool isValid(void * value) {
156  uintptr_t temp = reinterpret_cast<uintptr_t>(value);
157  return !(temp & 3);
158 }
159 
160 
161 #ifndef TERVEL_DEF_BACKOFF_TIME_NS
162  #define TERVEL_DEF_BACKOFF_TIME_NS 10000
163 #endif
164 
172 inline void backoff(int duration = TERVEL_DEF_BACKOFF_TIME_NS) {
173  std::this_thread::sleep_for(std::chrono::nanoseconds(duration));
174 }
175 
186 inline int round_to_next_power_of_two(uint64_t value) {
187  double val = std::log2(value);
188  int int_val = static_cast<int>(val);
189  if (int_val < val) {
190  int_val++;
191  }
192  return int_val;
193 };
194 
195 } // namespace util
196 } // namespace tervel
197 
198 
199 // A macro to disallow the copy constructor and operator= functions. This
200 // should be used in the `private` declarations for a class. Use unless you have
201 // a good reason for a class to be copy-able.
202 // see:
203 // http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Copy_Constructors
204 // http://stackoverflow.com/questions/20026445
205 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
206  TypeName(const TypeName&) = delete; \
207  void operator=(const TypeName&) = delete
208 
209 #endif // __TERVEL_UTIL_UTIL_H_
bool isValid(void *value)
Returns whether or not the passed value is has one of the reserved bits set to 1. ...
Definition: util.h:155
TODO(steven):
Definition: mcas.h:36
#define TERVEL_DEF_BACKOFF_TIME_NS
Definition: util.h:162
int round_to_next_power_of_two(uint64_t value)
Returns the next power of two.
Definition: wf_hash_map_no_delete.h:44
void backoff(int duration=TERVEL_DEF_BACKOFF_TIME_NS)
Sets the amount of time in nano-seconds for a thread to backoff before re-retrying.
Definition: util.h:172