File size: 8,810 Bytes
d1be154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#pragma once

#include <cstdlib>
#include <cstdio>
#include <string>
#include <thread>

#define TF_OS_LINUX 0
#define TF_OS_DRAGONFLY 0
#define TF_OS_FREEBSD 0
#define TF_OS_NETBSD 0
#define TF_OS_OPENBSD 0
#define TF_OS_DARWIN 0
#define TF_OS_WINDOWS 0
#define TF_OS_CNK 0
#define TF_OS_HURD 0
#define TF_OS_SOLARIS 0
#define TF_OS_UNIX 0

#ifdef _WIN32
#undef TF_OS_WINDOWS
#define TF_OS_WINDOWS 1
#endif

#ifdef __CYGWIN__
#undef TF_OS_WINDOWS
#define TF_OS_WINDOWS 1
#endif

#if (defined __APPLE__ && defined __MACH__)
#undef TF_OS_DARWIN
#define TF_OS_DARWIN 1
#endif

// in some ppc64 linux installations, only the second condition is met
#if (defined __linux)
#undef TF_OS_LINUX
#define TF_OS_LINUX 1
#elif (defined __linux__)
#undef TF_OS_LINUX
#define TF_OS_LINUX 1
#else
#endif

#if (defined __DragonFly__)
#undef TF_OS_DRAGONFLY
#define TF_OS_DRAGONFLY 1
#endif

#if (defined __FreeBSD__)
#undef TF_OS_FREEBSD
#define TF_OS_FREEBSD 1
#endif

#if (defined __NetBSD__)
#undef TF_OS_NETBSD
#define TF_OS_NETBSD 1
#endif

#if (defined __OpenBSD__)
#undef TF_OS_OPENBSD
#define TF_OS_OPENBSD 1
#endif

#if (defined __bgq__)
#undef TF_OS_CNK
#define TF_OS_CNK 1
#endif

#if (defined __GNU__)
#undef TF_OS_HURD
#define TF_OS_HURD 1
#endif

#if (defined __sun)
#undef TF_OS_SOLARIS
#define TF_OS_SOLARIS 1
#endif

#if (1 !=                                                                  \
     TF_OS_LINUX + TF_OS_DRAGONFLY + TF_OS_FREEBSD + TF_OS_NETBSD +        \
     TF_OS_OPENBSD + TF_OS_DARWIN + TF_OS_WINDOWS + TF_OS_HURD +           \
     TF_OS_SOLARIS)
#define TF_OS_UNKNOWN 1
#endif

#if TF_OS_LINUX || TF_OS_DRAGONFLY || TF_OS_FREEBSD || TF_OS_NETBSD ||     \
    TF_OS_OPENBSD || TF_OS_DARWIN || TF_OS_HURD || TF_OS_SOLARIS
#undef TF_OS_UNIX
#define TF_OS_UNIX 1
#endif


//-----------------------------------------------------------------------------
// Cache line alignment
//-----------------------------------------------------------------------------
#if defined(__i386__) || defined(__x86_64__)
  #define TF_CACHELINE_SIZE 64
#elif defined(__powerpc64__)
  // This is the L1 D-cache line size of our Power7 machines.
  // Need to check if this is appropriate for other PowerPC64 systems.
  #define TF_CACHELINE_SIZE 128
#elif defined(__arm__)
  // Cache line sizes for ARM: These values are not strictly correct since
  // cache line sizes depend on implementations, not architectures.
  // There are even implementations with cache line sizes configurable
  // at boot time.
  #if defined(__ARM_ARCH_5T__)
    #define TF_CACHELINE_SIZE 32
  #elif defined(__ARM_ARCH_7A__)
    #define TF_CACHELINE_SIZE 64
  #endif
#endif

#ifndef TF_CACHELINE_SIZE
// A reasonable default guess.  Note that overestimates tend to waste more
// space, while underestimates tend to waste more time.
  #define TF_CACHELINE_SIZE 64
#endif



namespace tf {

/**
 @class CachelineAligned

 @brief class to ensure cacheline-aligned storage for an object.
 
 @tparam T The type of the stored object.
 
 This utility class aligns the stored object `data` to twice the size of a cacheline.
 The alignment improves performance by optimizing data access in cache-sensitive scenarios.
 
 @code{.cpp}
 // create two integers on two separate cachelines to avoid false sharing
 tf::CachelineAligned<int> counter1;
 tf::CachelineAligned<int> counter2;
 
 // two threads access the two counters without false sharing
 std::thread t1([&]{ counter1.get() = 1; });
 std::thread t2([&]{ counter2.get() = 2; });
 t1.join();
 t2.join();
 @endcode
*/
template <typename T>
class CachelineAligned {
  public:
  /**
   * @brief The stored object, aligned to twice the cacheline size.
   */
  alignas (2*TF_CACHELINE_SIZE) T data;

  /**
   * @brief accesses the underlying object
   * 
   * @return a reference to the underlying object.
   */
  T& get() { return data; }
  
  /**
   * @brief accesses the underlying object as a constant reference
   * 
   * @return a constant reference to the underlying object.
   */
  const T& get() const { return data; }
};

/**
 * @brief retrieves the value of an environment variable
 *
 * This function fetches the value of an environment variable by name.
 * If the variable is not found, it returns an empty string.
 *
 * @param str The name of the environment variable to retrieve.
 * @return The value of the environment variable as a string, or an empty string if not found.
 *
 * @attention The implementation differs between Windows and POSIX platforms:
 *  - On Windows, it uses `_dupenv_s` to fetch the value.
 *  - On POSIX, it uses `std::getenv`.
 *
 */
inline std::string get_env(const std::string& str) {
#ifdef _MSC_VER
  char *ptr = nullptr;
  size_t len = 0;

  if(_dupenv_s(&ptr, &len, str.c_str()) == 0 && ptr != nullptr) {
    std::string res(ptr, len);
    std::free(ptr);
    return res;
  }
  return "";

#else
  auto ptr = std::getenv(str.c_str());
  return ptr ? ptr : "";
#endif
}

/**
 * @brief checks whether an environment variable is defined
 *
 * This function determines if a specific environment variable exists in the current environment.
 *
 * @param str The name of the environment variable to check.
 * @return `true` if the environment variable exists, `false` otherwise.
 *
 * @attention The implementation differs between Windows and POSIX platforms:
 *  - On Windows, it uses `_dupenv_s` to check for the variable's presence.
 *  - On POSIX, it uses `std::getenv` to check for the variable's presence.
 *
 */
inline bool has_env(const std::string& str) {
#ifdef _MSC_VER
  char *ptr = nullptr;
  size_t len = 0;

  if(_dupenv_s(&ptr, &len, str.c_str()) == 0 && ptr != nullptr) {
    std::string res(ptr, len);
    std::free(ptr);
    return true;
  }
  return false;

#else
  auto ptr = std::getenv(str.c_str());
  return ptr ? true : false;
#endif
}

/**
 * @fn pause
 * 
 * This function is used in spin-wait loops to hint the CPU that the current 
 * thread is in a busy-wait state. 
 * It helps reduce power consumption and improves performance on hyper-threaded processors 
 * by preventing the CPU from consuming unnecessary cycles while waiting. 
 * It is particularly useful in low-contention scenarios, where the thread 
 * is likely to quickly acquire the lock or condition it's waiting for, 
 * avoiding an expensive context switch. 
 * On modern x86 processors, this instruction can be invoked using @c __builtin_ia32_pause() 
 * in GCC/Clang or @c _mm_pause() in MSVC. 
 * In non-x86 architectures, alternative mechanisms such as yielding the CPU may be used instead.
 * 
 */
inline void pause() {
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
    // x86 and x86_64: Use the PAUSE instruction
  #if defined(_MSC_VER)
    // Microsoft Visual C++
    _mm_pause();
  #elif defined(__GNUC__) || defined(__clang__)
    // GCC and Clang
    __builtin_ia32_pause();
  #else
    asm volatile("pause" ::: "memory");
  #endif

#elif defined(__aarch64__) || defined(__arm__)
    // ARM and AArch64: Use the YIELD instruction
  #if defined(__GNUC__) || defined(__clang__)
    asm volatile("yield" ::: "memory");
  #endif

#else
  // Fallback: Portable yield for unknown architectures
  std::this_thread::yield();
#endif
}

/**
@brief pause CPU for a specified number of iterations
*/
inline void pause(size_t count) {
  while(count-- > 0) pause();
}

/**
 * @brief spins until the given predicate becomes true
 * 
 * @tparam P the type of the predicate function or callable.
 * @param predicate the callable that returns a boolean value, which is checked in the loop.
 * 
 * This function repeatedly checks the provided predicate in a spin-wait loop
 * and uses a backoff strategy to minimize CPU waste during the wait. Initially,
 * it uses the `pause()` instruction for the first 100 iterations to hint to the
 * CPU that the thread is waiting, thus reducing power consumption and avoiding
 * unnecessary cycles. After 100 iterations, it switches to yielding the CPU using
 * `std::this_thread::yield()` to allow other threads to run and improve system
 * responsiveness.
 * 
 * The function operates as follows:
 * 1. For the first 100 iterations, it invokes `pause()` to reduce power consumption
 *    during the spin-wait.
 * 2. After 100 iterations, it uses `std::this_thread::yield()` to relinquish the
 *    CPU, allowing other threads to execute.
 * 
 * @attention This function is useful when you need to wait for a condition to be true, but
 *       want to optimize CPU usage during the wait by using a busy-wait approach.
 * 
 */
template <typename P>
void spin_until(P&& predicate) {
  size_t num_pauses = 0;
  while(!predicate()) {
    (num_pauses++ < 100) ? pause() : std::this_thread::yield();
  }
}



}  // end of namespace tf -----------------------------------------------------