Loading...
Searching...
No Matches
os.hpp
1#pragma once
2
3#include <cstdlib>
4#include <cstdio>
5#include <string>
6#include <thread>
7#include <new>
8
9#define TF_OS_LINUX 0
10#define TF_OS_DRAGONFLY 0
11#define TF_OS_FREEBSD 0
12#define TF_OS_NETBSD 0
13#define TF_OS_OPENBSD 0
14#define TF_OS_DARWIN 0
15#define TF_OS_WINDOWS 0
16#define TF_OS_CNK 0
17#define TF_OS_HURD 0
18#define TF_OS_SOLARIS 0
19#define TF_OS_UNIX 0
20
21#ifdef _WIN32
22#undef TF_OS_WINDOWS
23#define TF_OS_WINDOWS 1
24#endif
25
26#ifdef __CYGWIN__
27#undef TF_OS_WINDOWS
28#define TF_OS_WINDOWS 1
29#endif
30
31#if (defined __APPLE__ && defined __MACH__)
32#undef TF_OS_DARWIN
33#define TF_OS_DARWIN 1
34#endif
35
36// in some ppc64 linux installations, only the second condition is met
37#if (defined __linux)
38#undef TF_OS_LINUX
39#define TF_OS_LINUX 1
40#elif (defined __linux__)
41#undef TF_OS_LINUX
42#define TF_OS_LINUX 1
43#else
44#endif
45
46#if (defined __DragonFly__)
47#undef TF_OS_DRAGONFLY
48#define TF_OS_DRAGONFLY 1
49#endif
50
51#if (defined __FreeBSD__)
52#undef TF_OS_FREEBSD
53#define TF_OS_FREEBSD 1
54#endif
55
56#if (defined __NetBSD__)
57#undef TF_OS_NETBSD
58#define TF_OS_NETBSD 1
59#endif
60
61#if (defined __OpenBSD__)
62#undef TF_OS_OPENBSD
63#define TF_OS_OPENBSD 1
64#endif
65
66#if (defined __bgq__)
67#undef TF_OS_CNK
68#define TF_OS_CNK 1
69#endif
70
71#if (defined __GNU__)
72#undef TF_OS_HURD
73#define TF_OS_HURD 1
74#endif
75
76#if (defined __sun)
77#undef TF_OS_SOLARIS
78#define TF_OS_SOLARIS 1
79#endif
80
81#if (1 != \
82 TF_OS_LINUX + TF_OS_DRAGONFLY + TF_OS_FREEBSD + TF_OS_NETBSD + \
83 TF_OS_OPENBSD + TF_OS_DARWIN + TF_OS_WINDOWS + TF_OS_HURD + \
84 TF_OS_SOLARIS)
85#define TF_OS_UNKNOWN 1
86#endif
87
88#if TF_OS_LINUX || TF_OS_DRAGONFLY || TF_OS_FREEBSD || TF_OS_NETBSD || \
89 TF_OS_OPENBSD || TF_OS_DARWIN || TF_OS_HURD || TF_OS_SOLARIS
90#undef TF_OS_UNIX
91#define TF_OS_UNIX 1
92#endif
93
94//-----------------------------------------------------------------------------
95// Cache line alignment
96//-----------------------------------------------------------------------------
97#if defined(__i386__) || defined(__x86_64__)
98 #define TF_CACHELINE_SIZE 64
99#elif defined(__powerpc64__)
100 // This is the L1 D-cache line size of our Power7 machines.
101 // Need to check if this is appropriate for other PowerPC64 systems.
102 #define TF_CACHELINE_SIZE 128
103#elif defined(__arm__)
104 // Cache line sizes for ARM: These values are not strictly correct since
105 // cache line sizes depend on implementations, not architectures.
106 // There are even implementations with cache line sizes configurable
107 // at boot time.
108 #if defined(__ARM_ARCH_5T__)
109 #define TF_CACHELINE_SIZE 32
110 #elif defined(__ARM_ARCH_7A__)
111 #define TF_CACHELINE_SIZE 64
112 #endif
113#endif
114
115#ifndef TF_CACHELINE_SIZE
116// A reasonable default guess. Note that overestimates tend to waste more
117// space, while underestimates tend to waste more time.
118 #define TF_CACHELINE_SIZE 64
119#endif
120
121
122
123namespace tf {
124
147template <typename T>
149 public:
153 alignas (TF_CACHELINE_SIZE) T data;
154
160 T& get() { return data; }
161
167 const T& get() const { return data; }
168};
169
183inline std::string get_env(const std::string& str) {
184#ifdef _MSC_VER
185 char *ptr = nullptr;
186 size_t len = 0;
187
188 if(_dupenv_s(&ptr, &len, str.c_str()) == 0 && ptr != nullptr) {
189 std::string res(ptr, len);
190 std::free(ptr);
191 return res;
192 }
193 return "";
194
195#else
196 auto ptr = std::getenv(str.c_str());
197 return ptr ? ptr : "";
198#endif
199}
200
213inline bool has_env(const std::string& str) {
214#ifdef _MSC_VER
215 char *ptr = nullptr;
216 size_t len = 0;
217
218 if(_dupenv_s(&ptr, &len, str.c_str()) == 0 && ptr != nullptr) {
219 std::string res(ptr, len);
220 std::free(ptr);
221 return true;
222 }
223 return false;
224
225#else
226 auto ptr = std::getenv(str.c_str());
227 return ptr ? true : false;
228#endif
229}
230
231
232} // end of namespace tf -----------------------------------------------------
233
234
235
236
237
238
239
240
241
class to ensure cacheline-aligned storage for an object.
Definition os.hpp:148
T & get()
accesses the underlying object
Definition os.hpp:160
const T & get() const
accesses the underlying object as a constant reference
Definition os.hpp:167
T data
The stored object, aligned to twice the cacheline size.
Definition os.hpp:153
taskflow namespace
Definition small_vector.hpp:20
std::string get_env(const std::string &str)
retrieves the value of an environment variable
Definition os.hpp:183
bool has_env(const std::string &str)
checks whether an environment variable is defined
Definition os.hpp:213