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