Add readWithPriority to PriorityMPMCQueue
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <cstddef>
20
21 #include <folly/portability/Config.h>
22
23 #include <folly/CPortability.h>
24
25 // Unaligned loads and stores
26 namespace folly {
27 #if FOLLY_HAVE_UNALIGNED_ACCESS
28 constexpr bool kHasUnalignedAccess = true;
29 #else
30 constexpr bool kHasUnalignedAccess = false;
31 #endif
32 } // namespace folly
33
34 // compiler specific attribute translation
35 // msvc should come first, so if clang is in msvc mode it gets the right defines
36
37 #if defined(__clang__) || defined(__GNUC__)
38 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
39 #elif defined(_MSC_VER)
40 # define FOLLY_ALIGNED(size) __declspec(align(size))
41 #else
42 # error Cannot define FOLLY_ALIGNED on this platform
43 #endif
44
45 // NOTE: this will only do checking in msvc with versions that support /analyze
46 #if _MSC_VER
47 # ifdef _USE_ATTRIBUTES_FOR_SAL
48 #    undef _USE_ATTRIBUTES_FOR_SAL
49 # endif
50 /* nolint */
51 # define _USE_ATTRIBUTES_FOR_SAL 1
52 # include <sal.h> // @manual
53 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
54 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
55 #else
56 # define FOLLY_PRINTF_FORMAT /**/
57 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
58   __attribute__((__format__(__printf__, format_param, dots_param)))
59 #endif
60
61 // deprecated
62 #if defined(__clang__) || defined(__GNUC__)
63 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
64 #elif defined(_MSC_VER)
65 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
66 #else
67 # define FOLLY_DEPRECATED(msg)
68 #endif
69
70 // warn unused result
71 #if defined(__has_cpp_attribute)
72 #if __has_cpp_attribute(nodiscard)
73 #define FOLLY_NODISCARD [[nodiscard]]
74 #endif
75 #endif
76 #if !defined FOLLY_NODISCARD
77 #if defined(_MSC_VER) && (_MSC_VER >= 1700)
78 #define FOLLY_NODISCARD _Check_return_
79 #elif defined(__clang__) || defined(__GNUC__)
80 #define FOLLY_NODISCARD __attribute__((__warn_unused_result__))
81 #else
82 #define FOLLY_NODISCARD
83 #endif
84 #endif
85
86 // target
87 #ifdef _MSC_VER
88 # define FOLLY_TARGET_ATTRIBUTE(target)
89 #else
90 # define FOLLY_TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
91 #endif
92
93 // detection for 64 bit
94 #if defined(__x86_64__) || defined(_M_X64)
95 # define FOLLY_X64 1
96 #else
97 # define FOLLY_X64 0
98 #endif
99
100 #if defined(__arm__)
101 #define FOLLY_ARM 1
102 #else
103 #define FOLLY_ARM 0
104 #endif
105
106 #if defined(__aarch64__)
107 # define FOLLY_AARCH64 1
108 #else
109 # define FOLLY_AARCH64 0
110 #endif
111
112 #if defined (__powerpc64__)
113 # define FOLLY_PPC64 1
114 #else
115 # define FOLLY_PPC64 0
116 #endif
117
118 namespace folly {
119 constexpr bool kIsArchArm = FOLLY_ARM == 1;
120 constexpr bool kIsArchAmd64 = FOLLY_X64 == 1;
121 constexpr bool kIsArchAArch64 = FOLLY_AARCH64 == 1;
122 constexpr bool kIsArchPPC64 = FOLLY_PPC64 == 1;
123 } // namespace folly
124
125 namespace folly {
126
127 #if FOLLY_SANITIZE_ADDRESS
128 constexpr bool kIsSanitizeAddress = true;
129 #else
130 constexpr bool kIsSanitizeAddress = false;
131 #endif
132
133 #if FOLLY_SANITIZE_THREAD
134 constexpr bool kIsSanitizeThread = true;
135 #else
136 constexpr bool kIsSanitizeThread = false;
137 #endif
138
139 #if FOLLY_SANITIZE
140 constexpr bool kIsSanitize = true;
141 #else
142 constexpr bool kIsSanitize = false;
143 #endif
144 } // namespace folly
145
146 // packing is very ugly in msvc
147 #ifdef _MSC_VER
148 # define FOLLY_PACK_ATTR /**/
149 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
150 # define FOLLY_PACK_POP __pragma(pack(pop))
151 #elif defined(__clang__) || defined(__GNUC__)
152 # define FOLLY_PACK_ATTR __attribute__((__packed__))
153 # define FOLLY_PACK_PUSH /**/
154 # define FOLLY_PACK_POP /**/
155 #else
156 # define FOLLY_PACK_ATTR /**/
157 # define FOLLY_PACK_PUSH /**/
158 # define FOLLY_PACK_POP /**/
159 #endif
160
161 // Generalize warning push/pop.
162 #if defined(_MSC_VER)
163 # define FOLLY_PUSH_WARNING __pragma(warning(push))
164 # define FOLLY_POP_WARNING __pragma(warning(pop))
165 // Disable the GCC warnings.
166 # define FOLLY_GCC_DISABLE_WARNING(warningName)
167 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
168 #elif defined(__clang__) || defined(__GNUC__)
169 # define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
170 # define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
171 # define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) #warningName
172 # define FOLLY_GCC_DISABLE_WARNING(warningName) \
173   _Pragma(                                      \
174   FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored warningName))
175 // Disable the MSVC warnings.
176 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
177 #else
178 # define FOLLY_PUSH_WARNING
179 # define FOLLY_POP_WARNING
180 # define FOLLY_GCC_DISABLE_WARNING(warningName)
181 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
182 #endif
183
184 #ifdef FOLLY_HAVE_SHADOW_LOCAL_WARNINGS
185 #define FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS        \
186   FOLLY_GCC_DISABLE_WARNING("-Wshadow-compatible-local") \
187   FOLLY_GCC_DISABLE_WARNING("-Wshadow-local")
188 #else
189 #define FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS /* empty */
190 #endif
191
192 /* Platform specific TLS support
193  * gcc implements __thread
194  * msvc implements __declspec(thread)
195  * the semantics are the same
196  * (but remember __thread has different semantics when using emutls (ex. apple))
197  */
198 #if defined(_MSC_VER)
199 # define FOLLY_TLS __declspec(thread)
200 #elif defined(__GNUC__) || defined(__clang__)
201 # define FOLLY_TLS __thread
202 #else
203 # error cannot define platform specific thread local storage
204 #endif
205
206 #if FOLLY_MOBILE
207 #undef FOLLY_TLS
208 #endif
209
210 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
211 // the 'std' namespace; the latter uses inline namespaces. Wrap this decision
212 // up in a macro to make forward-declarations easier.
213 #if FOLLY_USE_LIBCPP
214 #include <__config> // @manual
215 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
216 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
217 #else
218 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
219 #define FOLLY_NAMESPACE_STD_END       }
220 #endif
221
222 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
223 // some types, e.g. std::list.
224 #if _GLIBCXX_USE_CXX11_ABI
225 #define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN \
226   inline _GLIBCXX_BEGIN_NAMESPACE_CXX11
227 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END   _GLIBCXX_END_NAMESPACE_CXX11
228 #else
229 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
230 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
231 #endif
232
233 // MSVC specific defines
234 // mainly for posix compat
235 #ifdef _MSC_VER
236 #include <folly/portability/SysTypes.h>
237
238 // compiler specific to compiler specific
239 // nolint
240 # define __PRETTY_FUNCTION__ __FUNCSIG__
241
242 // Hide a GCC specific thing that breaks MSVC if left alone.
243 # define __extension__
244
245 // We have compiler support for the newest of the new, but
246 // MSVC doesn't tell us that.
247 #define __SSE4_2__ 1
248
249 #endif
250
251 // Debug
252 namespace folly {
253 #ifdef NDEBUG
254 constexpr auto kIsDebug = false;
255 #else
256 constexpr auto kIsDebug = true;
257 #endif
258 } // namespace folly
259
260 // Endianness
261 namespace folly {
262 #ifdef _MSC_VER
263 // It's MSVC, so we just have to guess ... and allow an override
264 #ifdef FOLLY_ENDIAN_BE
265 constexpr auto kIsLittleEndian = false;
266 #else
267 constexpr auto kIsLittleEndian = true;
268 #endif
269 #else
270 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
271 #endif
272 constexpr auto kIsBigEndian = !kIsLittleEndian;
273 } // namespace folly
274
275 #ifndef FOLLY_SSE
276 # if defined(__SSE4_2__)
277 #  define FOLLY_SSE 4
278 #  define FOLLY_SSE_MINOR 2
279 # elif defined(__SSE4_1__)
280 #  define FOLLY_SSE 4
281 #  define FOLLY_SSE_MINOR 1
282 # elif defined(__SSE4__)
283 #  define FOLLY_SSE 4
284 #  define FOLLY_SSE_MINOR 0
285 # elif defined(__SSE3__)
286 #  define FOLLY_SSE 3
287 #  define FOLLY_SSE_MINOR 0
288 # elif defined(__SSE2__)
289 #  define FOLLY_SSE 2
290 #  define FOLLY_SSE_MINOR 0
291 # elif defined(__SSE__)
292 #  define FOLLY_SSE 1
293 #  define FOLLY_SSE_MINOR 0
294 # else
295 #  define FOLLY_SSE 0
296 #  define FOLLY_SSE_MINOR 0
297 # endif
298 #endif
299
300 #define FOLLY_SSE_PREREQ(major, minor) \
301   (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
302
303 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
304 namespace FOLLY_GFLAGS_NAMESPACE { }
305 namespace gflags {
306 using namespace FOLLY_GFLAGS_NAMESPACE;
307 } // namespace gflags
308 #endif
309
310 // for TARGET_OS_IPHONE
311 #ifdef __APPLE__
312 #include <TargetConditionals.h> // @manual
313 #endif
314
315 // RTTI may not be enabled for this compilation unit.
316 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
317     (defined(_MSC_VER) && defined(_CPPRTTI))
318 # define FOLLY_HAS_RTTI 1
319 #endif
320
321 #if defined(__APPLE__) || defined(_MSC_VER)
322 #define FOLLY_STATIC_CTOR_PRIORITY_MAX
323 #else
324 // 101 is the highest priority allowed by the init_priority attribute.
325 // This priority is already used by JEMalloc and other memory allocators so
326 // we will take the next one.
327 #define FOLLY_STATIC_CTOR_PRIORITY_MAX __attribute__((__init_priority__(102)))
328 #endif
329
330 namespace folly {
331
332 #if __OBJC__
333 constexpr auto kIsObjC = true;
334 #else
335 constexpr auto kIsObjC = false;
336 #endif
337
338 #if FOLLY_MOBILE
339 constexpr auto kIsMobile = true;
340 #else
341 constexpr auto kIsMobile = false;
342 #endif
343
344 #if defined(__linux__) && !FOLLY_MOBILE
345 constexpr auto kIsLinux = true;
346 #else
347 constexpr auto kIsLinux = false;
348 #endif
349
350 #if defined(_WIN32)
351 constexpr auto kIsWindows = true;
352 constexpr auto kMscVer = _MSC_VER;
353 #else
354 constexpr auto kIsWindows = false;
355 constexpr auto kMscVer = 0;
356 #endif
357 } // namespace folly
358
359 // Define FOLLY_USE_CPP14_CONSTEXPR to be true if the compiler's C++14
360 // constexpr support is "good enough".
361 #ifndef FOLLY_USE_CPP14_CONSTEXPR
362 #if defined(__clang__)
363 #define FOLLY_USE_CPP14_CONSTEXPR __cplusplus >= 201300L
364 #elif defined(__GNUC__)
365 #define FOLLY_USE_CPP14_CONSTEXPR __cplusplus >= 201304L
366 #else
367 #define FOLLY_USE_CPP14_CONSTEXPR 0 // MSVC?
368 #endif
369 #endif
370
371 #if FOLLY_USE_CPP14_CONSTEXPR
372 #define FOLLY_CPP14_CONSTEXPR constexpr
373 #else
374 #define FOLLY_CPP14_CONSTEXPR inline
375 #endif
376
377 #if __cpp_coroutines >= 201703L || (_MSC_VER && _RESUMABLE_FUNCTIONS_SUPPORTED)
378 #define FOLLY_HAS_COROUTINES 1
379 #endif
380
381 // MSVC 2017.5
382 #if __cpp_noexcept_function_type >= 201510 || _MSC_FULL_VER >= 191225816
383 #define FOLLY_HAVE_NOEXCEPT_FUNCTION_TYPE 1
384 #endif