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