Adding a unit test for HHWheelTimer exercising the default timeout functionality.
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2015 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 #ifndef FOLLY_PORTABILITY_H_
18 #define FOLLY_PORTABILITY_H_
19
20 #include <cstddef>
21
22 #ifndef FOLLY_NO_CONFIG
23 #include <folly/folly-config.h>
24 #endif
25
26 #ifdef FOLLY_PLATFORM_CONFIG
27 #include FOLLY_PLATFORM_CONFIG
28 #endif
29
30 #if FOLLY_HAVE_FEATURES_H
31 #include <features.h>
32 #endif
33
34 #include <folly/CPortability.h>
35
36 #ifdef __APPLE__
37 # include <malloc/malloc.h>
38 #endif
39
40 #if FOLLY_HAVE_SCHED_H
41  #include <sched.h>
42  #ifndef FOLLY_HAVE_PTHREAD_YIELD
43   #define pthread_yield sched_yield
44  #endif
45 #endif
46
47 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
48 // MAP_ANON on OSX/BSD.
49 #if defined(__APPLE__) || defined(__FreeBSD__)
50   #include <sys/mman.h>
51   #ifndef MAP_ANONYMOUS
52     #ifdef MAP_ANON
53       #define MAP_ANONYMOUS MAP_ANON
54     #endif
55   #endif
56 #endif
57
58 // compiler specific attribute translation
59 // msvc should come first, so if clang is in msvc mode it gets the right defines
60
61 #if defined(__clang__) || defined(__GNUC__)
62 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
63 #elif defined(_MSC_VER)
64 # define FOLLY_ALIGNED(size) __declspec(align(size))
65 #else
66 # error Cannot define FOLLY_ALIGNED on this platform
67 #endif
68 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
69
70 // NOTE: this will only do checking in msvc with versions that support /analyze
71 #if _MSC_VER
72 # ifdef _USE_ATTRIBUTES_FOR_SAL
73 #    undef _USE_ATTRIBUTES_FOR_SAL
74 # endif
75 /* nolint */
76 # define _USE_ATTRIBUTES_FOR_SAL 1
77 # include <sal.h>
78 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
79 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
80 #else
81 # define FOLLY_PRINTF_FORMAT /**/
82 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
83   __attribute__((__format__(__printf__, format_param, dots_param)))
84 #endif
85
86 // deprecated
87 #if defined(__clang__) || defined(__GNUC__)
88 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
89 #elif defined(_MSC_VER)
90 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
91 #else
92 # define FOLLY_DEPRECATED(msg)
93 #endif
94
95 // noreturn
96 #if defined(_MSC_VER)
97 # define FOLLY_NORETURN __declspec(noreturn)
98 #elif defined(__clang__) || defined(__GNUC__)
99 # define FOLLY_NORETURN __attribute__((__noreturn__))
100 #else
101 # define FOLLY_NORETURN
102 #endif
103
104 // noinline
105 #ifdef _MSC_VER
106 # define FOLLY_NOINLINE __declspec(noinline)
107 #elif defined(__clang__) || defined(__GNUC__)
108 # define FOLLY_NOINLINE __attribute__((__noinline__))
109 #else
110 # define FOLLY_NOINLINE
111 #endif
112
113 // always inline
114 #ifdef _MSC_VER
115 # define FOLLY_ALWAYS_INLINE __forceinline
116 #elif defined(__clang__) || defined(__GNUC__)
117 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
118 #else
119 # define FOLLY_ALWAYS_INLINE
120 #endif
121
122 // detection for 64 bit
123 #if defined(__x86_64__) || defined(_M_X64)
124 # define FOLLY_X64 1
125 #else
126 # define FOLLY_X64 0
127 #endif
128
129 #if defined(__aarch64__)
130 # define FOLLY_A64 1
131 #else
132 # define FOLLY_A64 0
133 #endif
134
135 // packing is very ugly in msvc
136 #ifdef _MSC_VER
137 # define FOLLY_PACK_ATTR /**/
138 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
139 # define FOLLY_PACK_POP __pragma(pack(pop))
140 #elif defined(__clang__) || defined(__GNUC__)
141 # define FOLLY_PACK_ATTR __attribute__((__packed__))
142 # define FOLLY_PACK_PUSH /**/
143 # define FOLLY_PACK_POP /**/
144 #else
145 # define FOLLY_PACK_ATTR /**/
146 # define FOLLY_PACK_PUSH /**/
147 # define FOLLY_PACK_POP /**/
148 #endif
149
150 // portable version check
151 #ifndef __GNUC_PREREQ
152 # if defined __GNUC__ && defined __GNUC_MINOR__
153 /* nolint */
154 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
155                                    ((maj) << 16) + (min))
156 # else
157 /* nolint */
158 #  define __GNUC_PREREQ(maj, min) 0
159 # endif
160 #endif
161
162 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
163 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
164 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
165 // Alias it into std (where it's found in 4.9 and later)
166 namespace std { typedef ::max_align_t max_align_t; }
167 #endif
168
169 /* Define macro wrappers for C++11's "final" and "override" keywords, which
170  * are supported in gcc 4.7 but not gcc 4.6. */
171 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
172 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
173 #  define FOLLY_FINAL final
174 #  define FOLLY_OVERRIDE override
175 # elif defined(_MSC_VER) && _MSC_VER >= 1600
176 #  define FOLLY_FINAL final
177 #  define FOLLY_OVERRIDE override
178 # else
179 #  define FOLLY_FINAL /**/
180 #  define FOLLY_OVERRIDE /**/
181 # endif
182 #endif
183
184 /* Platform specific TLS support
185  * gcc implements __thread
186  * msvc implements __declspec(thread)
187  * the semantics are the same
188  * (but remember __thread has different semantics when using emutls (ex. apple))
189  */
190 #if defined(_MSC_VER)
191 # define FOLLY_TLS __declspec(thread)
192 #elif defined(__GNUC__) || defined(__clang__)
193 # define FOLLY_TLS __thread
194 #else
195 # error cannot define platform specific thread local storage
196 #endif
197
198 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
199 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
200 # if defined(__GLIBC_PREREQ)
201 #  if __GLIBC_PREREQ(2, 10)
202 #   define FOLLY_HAVE_PREADV 1
203 #   define FOLLY_HAVE_PWRITEV 1
204 #  endif
205 # endif
206 #endif
207
208 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
209 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
210 // up in a macro to make forward-declarations easier.
211 #if FOLLY_USE_LIBCPP
212 #include <__config>
213 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
214 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
215 #else
216 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
217 #define FOLLY_NAMESPACE_STD_END       }
218 #endif
219
220 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
221 // versions of these into the global namespace.
222 #if FOLLY_HAVE_CLOCK_GETTIME
223 #include <time.h>
224 #else
225 #include <folly/detail/Clock.h>
226 #endif
227
228 // Provide our own std::__throw_* wrappers for platforms that don't have them
229 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
230 #include <bits/functexcept.h>
231 #else
232 #include <folly/detail/FunctionalExcept.h>
233 #endif
234
235 #if defined(__cplusplus)
236 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
237 // usage of __has_trivial_copy(), so we can't use it as a
238 // least-common-denominator for C++11 implementations that don't support
239 // std::is_trivially_copyable<T>.
240 //
241 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
242 //
243 // As a result, use std::is_trivially_copyable() where it exists, and fall back
244 // to Boost otherwise.
245 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
246 #include <type_traits>
247 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
248   (std::is_trivially_copyable<T>::value)
249 #else
250 #include <boost/type_traits.hpp>
251 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
252   (boost::has_trivial_copy<T>::value &&                  \
253    boost::has_trivial_destructor<T>::value)
254 #endif
255 #endif // __cplusplus
256
257 // MSVC specific defines
258 // mainly for posix compat
259 #ifdef _MSC_VER
260
261 // this definition is in a really silly place with a silly name
262 // and ifdefing it every time we want it is painful
263 #include <basetsd.h>
264 typedef SSIZE_T ssize_t;
265
266 // sprintf semantics are not exactly identical
267 // but current usage is not a problem
268 # define snprintf _snprintf
269
270 // semantics here are identical
271 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
272
273 // compiler specific to compiler specific
274 // nolint
275 # define __PRETTY_FUNCTION__ __FUNCSIG__
276
277 // Hide a GCC specific thing that breaks MSVC if left alone.
278 # define __extension__
279
280 #ifdef _M_IX86_FP
281 # define FOLLY_SSE _M_IX86_FP
282 # define FOLLY_SSE_MINOR 0
283 #endif
284
285 #endif
286
287 #ifndef FOLLY_SSE
288 # if defined(__SSE4_2__)
289 #  define FOLLY_SSE 4
290 #  define FOLLY_SSE_MINOR 2
291 # elif defined(__SSE4_1__)
292 #  define FOLLY_SSE 4
293 #  define FOLLY_SSE_MINOR 1
294 # elif defined(__SSE4__)
295 #  define FOLLY_SSE 4
296 #  define FOLLY_SSE_MINOR 0
297 # elif defined(__SSE3__)
298 #  define FOLLY_SSE 3
299 #  define FOLLY_SSE_MINOR 0
300 # elif defined(__SSE2__)
301 #  define FOLLY_SSE 2
302 #  define FOLLY_SSE_MINOR 0
303 # elif defined(__SSE__)
304 #  define FOLLY_SSE 1
305 #  define FOLLY_SSE_MINOR 0
306 # else
307 #  define FOLLY_SSE 0
308 #  define FOLLY_SSE_MINOR 0
309 # endif
310 #endif
311
312 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
313 namespace FOLLY_GFLAGS_NAMESPACE { }
314 namespace gflags {
315 using namespace FOLLY_GFLAGS_NAMESPACE;
316 }  // namespace gflags
317 #endif
318
319 // for TARGET_OS_IPHONE
320 #ifdef __APPLE__
321 #include <TargetConditionals.h>
322 #endif
323
324 // MacOS doesn't have malloc_usable_size()
325 #if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
326 inline size_t malloc_usable_size(void* ptr) {
327   return malloc_size(ptr);
328 }
329 #endif
330
331 // RTTI may not be enabled for this compilation unit.
332 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
333     (defined(_MSC_VER) && defined(_CPPRTTI))
334 # define FOLLY_HAS_RTTI 1
335 #endif
336
337 #ifdef _MSC_VER
338 # include <intrin.h>
339 #endif
340
341 namespace folly {
342
343 inline void asm_volatile_memory() {
344 #if defined(__clang__) || defined(__GNUC__)
345   asm volatile("" : : : "memory");
346 #elif defined(_MSC_VER)
347   ::_ReadWriteBarrier();
348 #endif
349 }
350
351 inline void asm_volatile_pause() {
352 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
353   ::_mm_pause();
354 #elif defined(__i386__) || FOLLY_X64
355   asm volatile ("pause");
356 #elif FOLLY_A64
357   asm volatile ("wfe");
358 #endif
359 }
360 inline void asm_pause() {
361 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
362   ::_mm_pause();
363 #elif defined(__i386__) || FOLLY_X64
364   asm ("pause");
365 #elif FOLLY_A64
366   asm ("wfe");
367 #endif
368 }
369
370 }
371
372 #endif // FOLLY_PORTABILITY_H_