fix folly::FunctionScheduler.cancelFunctionAndWait() hanging issue
[folly.git] / folly / CPortability.h
1 /*
2  * Copyright 2017 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 /* These definitions are in a separate file so that they
20  * may be included from C- as well as C++-based projects. */
21
22 /**
23  * Portable version check.
24  */
25 #ifndef __GNUC_PREREQ
26 # if defined __GNUC__ && defined __GNUC_MINOR__
27 /* nolint */
28 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
29                                    ((maj) << 16) + (min))
30 # else
31 /* nolint */
32 #  define __GNUC_PREREQ(maj, min) 0
33 # endif
34 #endif
35
36 /* Define a convenience macro to test when address sanitizer is being used
37  * across the different compilers (e.g. clang, gcc) */
38 #if defined(__clang__)
39 # if __has_feature(address_sanitizer)
40 #  define FOLLY_SANITIZE_ADDRESS 1
41 # endif
42 #elif defined (__GNUC__) && \
43       (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ >= 5)) && \
44       __SANITIZE_ADDRESS__
45 # define FOLLY_SANITIZE_ADDRESS 1
46 #endif
47
48 /* Define attribute wrapper for function attribute used to disable
49  * address sanitizer instrumentation. Unfortunately, this attribute
50  * has issues when inlining is used, so disable that as well. */
51 #ifdef FOLLY_SANITIZE_ADDRESS
52 # if defined(__clang__)
53 #  if __has_attribute(__no_sanitize__)
54 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
55       __attribute__((__no_sanitize__("address"), __noinline__))
56 #  elif __has_attribute(__no_address_safety_analysis__)
57 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
58       __attribute__((__no_address_safety_analysis__, __noinline__))
59 #  elif __has_attribute(__no_sanitize_address__)
60 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
61       __attribute__((__no_sanitize_address__, __noinline__))
62 #  endif
63 # elif defined(__GNUC__)
64 #  define FOLLY_DISABLE_ADDRESS_SANITIZER \
65      __attribute__((__no_address_safety_analysis__, __noinline__))
66 # endif
67 #endif
68 #ifndef FOLLY_DISABLE_ADDRESS_SANITIZER
69 # define FOLLY_DISABLE_ADDRESS_SANITIZER
70 #endif
71
72 /* Define a convenience macro to test when thread sanitizer is being used
73  * across the different compilers (e.g. clang, gcc) */
74 #if defined(__clang__)
75 # if __has_feature(thread_sanitizer)
76 #  define FOLLY_SANITIZE_THREAD 1
77 # endif
78 #elif defined(__GNUC__) && __SANITIZE_THREAD__
79 # define FOLLY_SANITIZE_THREAD 1
80 #endif
81
82 /**
83  * ASAN/MSAN/TSAN define pre-processor symbols:
84  * ADDRESS_SANITIZER/MEMORY_SANITIZER/THREAD_SANITIZER.
85  *
86  * UBSAN doesn't define anything and makes it hard to
87  * conditionally compile.
88  *
89  * The build system should define UNDEFINED_SANITIZER=1 when UBSAN is
90  * used as folly whitelists some functions.
91  */
92 #if UNDEFINED_SANITIZER
93 #define FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER(...) \
94   __attribute__((no_sanitize(__VA_ARGS__)))
95 #else
96 #define FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER(...)
97 #endif // UNDEFINED_SANITIZER
98
99 /**
100  * Macro for marking functions as having public visibility.
101  */
102 #if defined(__GNUC__)
103 # if __GNUC_PREREQ(4, 9)
104 #  define FOLLY_EXPORT [[gnu::visibility("default")]]
105 # else
106 #  define FOLLY_EXPORT __attribute__((__visibility__("default")))
107 # endif
108 #else
109 # define FOLLY_EXPORT
110 #endif
111
112 // noinline
113 #ifdef _MSC_VER
114 # define FOLLY_NOINLINE __declspec(noinline)
115 #elif defined(__clang__) || defined(__GNUC__)
116 # define FOLLY_NOINLINE __attribute__((__noinline__))
117 #else
118 # define FOLLY_NOINLINE
119 #endif
120
121 // always inline
122 #ifdef _MSC_VER
123 # define FOLLY_ALWAYS_INLINE __forceinline
124 #elif defined(__clang__) || defined(__GNUC__)
125 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
126 #else
127 # define FOLLY_ALWAYS_INLINE inline
128 #endif
129
130 // attribute hidden
131 #if _MSC_VER
132 #define FOLLY_ATTR_VISIBILITY_HIDDEN
133 #elif defined(__clang__) || defined(__GNUC__)
134 #define FOLLY_ATTR_VISIBILITY_HIDDEN __attribute__((__visibility__("hidden")))
135 #else
136 #define FOLLY_ATTR_VISIBILITY_HIDDEN
137 #endif