base.h (10815B) - Raw
1 /* 2 * Copyright (c) 2008-2012 Apple Inc. All rights reserved. 3 * 4 * @APPLE_APACHE_LICENSE_HEADER_START@ 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 * @APPLE_APACHE_LICENSE_HEADER_END@ 19 */ 20 21 #ifndef __DISPATCH_BASE__ 22 #define __DISPATCH_BASE__ 23 24 #ifndef __DISPATCH_INDIRECT__ 25 #error "Please #include <dispatch/dispatch.h> instead of this file directly." 26 #endif 27 28 #ifndef __has_builtin 29 #define __has_builtin(x) 0 30 #endif 31 #ifndef __has_include 32 #define __has_include(x) 0 33 #endif 34 #ifndef __has_feature 35 #define __has_feature(x) 0 36 #endif 37 #ifndef __has_attribute 38 #define __has_attribute(x) 0 39 #endif 40 #ifndef __has_extension 41 #define __has_extension(x) 0 42 #endif 43 44 #if __GNUC__ 45 #define DISPATCH_NORETURN __attribute__((__noreturn__)) 46 #define DISPATCH_NOTHROW __attribute__((__nothrow__)) 47 #define DISPATCH_NONNULL1 __attribute__((__nonnull__(1))) 48 #define DISPATCH_NONNULL2 __attribute__((__nonnull__(2))) 49 #define DISPATCH_NONNULL3 __attribute__((__nonnull__(3))) 50 #define DISPATCH_NONNULL4 __attribute__((__nonnull__(4))) 51 #define DISPATCH_NONNULL5 __attribute__((__nonnull__(5))) 52 #define DISPATCH_NONNULL6 __attribute__((__nonnull__(6))) 53 #define DISPATCH_NONNULL7 __attribute__((__nonnull__(7))) 54 #if __clang__ && __clang_major__ < 3 55 // rdar://problem/6857843 56 #define DISPATCH_NONNULL_ALL 57 #else 58 #define DISPATCH_NONNULL_ALL __attribute__((__nonnull__)) 59 #endif 60 #define DISPATCH_SENTINEL __attribute__((__sentinel__)) 61 #define DISPATCH_PURE __attribute__((__pure__)) 62 #define DISPATCH_CONST __attribute__((__const__)) 63 #define DISPATCH_WARN_RESULT __attribute__((__warn_unused_result__)) 64 #define DISPATCH_MALLOC __attribute__((__malloc__)) 65 #define DISPATCH_ALWAYS_INLINE __attribute__((__always_inline__)) 66 #define DISPATCH_UNAVAILABLE __attribute__((__unavailable__)) 67 #define DISPATCH_UNAVAILABLE_MSG(msg) __attribute__((__unavailable__(msg))) 68 #elif defined(_MSC_VER) 69 #define DISPATCH_NORETURN __declspec(noreturn) 70 #define DISPATCH_NOTHROW __declspec(nothrow) 71 #define DISPATCH_NONNULL1 72 #define DISPATCH_NONNULL2 73 #define DISPATCH_NONNULL3 74 #define DISPATCH_NONNULL4 75 #define DISPATCH_NONNULL5 76 #define DISPATCH_NONNULL6 77 #define DISPATCH_NONNULL7 78 #define DISPATCH_NONNULL_ALL 79 #define DISPATCH_SENTINEL 80 #define DISPATCH_PURE 81 #define DISPATCH_CONST 82 #if (_MSC_VER >= 1700) 83 #define DISPATCH_WARN_RESULT _Check_return_ 84 #else 85 #define DISPATCH_WARN_RESULT 86 #endif 87 #define DISPATCH_MALLOC 88 #define DISPATCH_ALWAYS_INLINE __forceinline 89 #define DISPATCH_UNAVAILABLE 90 #define DISPATCH_UNAVAILABLE_MSG(msg) 91 #else 92 /*! @parseOnly */ 93 #define DISPATCH_NORETURN 94 /*! @parseOnly */ 95 #define DISPATCH_NOTHROW 96 /*! @parseOnly */ 97 #define DISPATCH_NONNULL1 98 /*! @parseOnly */ 99 #define DISPATCH_NONNULL2 100 /*! @parseOnly */ 101 #define DISPATCH_NONNULL3 102 /*! @parseOnly */ 103 #define DISPATCH_NONNULL4 104 /*! @parseOnly */ 105 #define DISPATCH_NONNULL5 106 /*! @parseOnly */ 107 #define DISPATCH_NONNULL6 108 /*! @parseOnly */ 109 #define DISPATCH_NONNULL7 110 /*! @parseOnly */ 111 #define DISPATCH_NONNULL_ALL 112 /*! @parseOnly */ 113 #define DISPATCH_SENTINEL 114 /*! @parseOnly */ 115 #define DISPATCH_PURE 116 /*! @parseOnly */ 117 #define DISPATCH_CONST 118 /*! @parseOnly */ 119 #define DISPATCH_WARN_RESULT 120 /*! @parseOnly */ 121 #define DISPATCH_MALLOC 122 /*! @parseOnly */ 123 #define DISPATCH_ALWAYS_INLINE 124 /*! @parseOnly */ 125 #define DISPATCH_UNAVAILABLE 126 /*! @parseOnly */ 127 #define DISPATCH_UNAVAILABLE_MSG(msg) 128 #endif 129 130 #if defined(__cplusplus) 131 # if __cplusplus >= 201703L 132 # define DISPATCH_FALLTHROUGH [[fallthrough]] 133 # elif __cplusplus >= 201103L 134 # if defined(__clang__) 135 # define DISPATCH_FALLTHROUGH [[clang::fallthrough]] 136 # elif defined(__GNUC__) && __GNUC__ >= 7 137 # define DISPATCH_FALLTHROUGH [[gnu::fallthrough]] 138 # else 139 # define DISPATCH_FALLTHROUGH 140 # endif 141 # else 142 # define DISPATCH_FALLTHROUGH 143 # endif 144 #elif defined(__GNUC__) && __GNUC__ >= 7 145 # define DISPATCH_FALLTHROUGH __attribute__((__fallthrough__)) 146 #elif defined(__clang__) 147 # if __has_attribute(fallthrough) && __clang_major__ >= 5 148 # define DISPATCH_FALLTHROUGH __attribute__((__fallthrough__)) 149 # else 150 # define DISPATCH_FALLTHROUGH 151 # endif 152 #else 153 # define DISPATCH_FALLTHROUGH 154 #endif 155 156 157 #define DISPATCH_LINUX_UNAVAILABLE() 158 159 #ifdef __FreeBSD__ 160 #define DISPATCH_FREEBSD_UNAVAILABLE() \ 161 DISPATCH_UNAVAILABLE_MSG( \ 162 "This interface is unavailable on FreeBSD systems") 163 #else 164 #define DISPATCH_FREEBSD_UNAVAILABLE() 165 #endif 166 167 #ifndef DISPATCH_ALIAS_V2 168 #if TARGET_OS_MAC 169 #define DISPATCH_ALIAS_V2(sym) __asm__("_" #sym "$V2") 170 #else 171 #define DISPATCH_ALIAS_V2(sym) 172 #endif 173 #endif 174 175 #if defined(_WIN32) 176 #if defined(__cplusplus) 177 #define DISPATCH_EXPORT extern "C" __declspec(dllimport) 178 #else 179 #define DISPATCH_EXPORT extern __declspec(dllimport) 180 #endif 181 #elif __GNUC__ 182 #define DISPATCH_EXPORT extern __attribute__((visibility("default"))) 183 #else 184 #define DISPATCH_EXPORT extern 185 #endif 186 187 #if __GNUC__ 188 #define DISPATCH_INLINE static __inline__ 189 #else 190 #define DISPATCH_INLINE static inline 191 #endif 192 193 #if __GNUC__ 194 #define DISPATCH_EXPECT(x, v) __builtin_expect((x), (v)) 195 #define dispatch_compiler_barrier() __asm__ __volatile__("" ::: "memory") 196 #else 197 #define DISPATCH_EXPECT(x, v) (x) 198 #define dispatch_compiler_barrier() do { } while (0) 199 #endif 200 201 #if __has_attribute(not_tail_called) 202 #define DISPATCH_NOT_TAIL_CALLED __attribute__((__not_tail_called__)) 203 #else 204 #define DISPATCH_NOT_TAIL_CALLED 205 #endif 206 207 #if __has_builtin(__builtin_assume) 208 #define DISPATCH_COMPILER_CAN_ASSUME(expr) __builtin_assume(expr) 209 #else 210 #define DISPATCH_COMPILER_CAN_ASSUME(expr) ((void)(expr)) 211 #endif 212 213 #if __has_attribute(noescape) 214 #define DISPATCH_NOESCAPE __attribute__((__noescape__)) 215 #else 216 #define DISPATCH_NOESCAPE 217 #endif 218 219 #if __has_attribute(cold) 220 #define DISPATCH_COLD __attribute__((__cold__)) 221 #else 222 #define DISPATCH_COLD 223 #endif 224 225 #if __has_feature(assume_nonnull) 226 #define DISPATCH_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin") 227 #define DISPATCH_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end") 228 #else 229 #define DISPATCH_ASSUME_NONNULL_BEGIN 230 #define DISPATCH_ASSUME_NONNULL_END 231 #endif 232 233 #if __has_feature(bounds_attributes) 234 #define DISPATCH_ASSUME_ABI_SINGLE_BEGIN _Pragma("clang abi_ptr_attr set(single)") 235 #define DISPATCH_ASSUME_ABI_SINGLE_END _Pragma("clang abi_ptr_attr set(unsafe_indexable)") 236 #define DISPATCH_UNSAFE_INDEXABLE __attribute__((__unsafe_indexable__)) 237 #define DISPATCH_COUNTED_BY(X) __attribute__((__counted_by__(X))) 238 #define DISPATCH_SIZED_BY(X) __attribute__((__sized_by__(X))) 239 #else 240 #define DISPATCH_ASSUME_ABI_SINGLE_BEGIN 241 #define DISPATCH_ASSUME_ABI_SINGLE_END 242 #define DISPATCH_UNSAFE_INDEXABLE 243 #define DISPATCH_COUNTED_BY(X) 244 #define DISPATCH_SIZED_BY(X) 245 #endif 246 247 #define DISPATCH_OSX_SUPPORTS_AT_LEAST(macos, ios, tvos, watchos, bridgeos, visionos) \ 248 ( (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= macos) \ 249 || (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= ios) \ 250 || (defined(__TV_OS_VERSION_MIN_REQUIRED) && __TV_OS_VERSION_MIN_REQUIRED >= tvos) \ 251 || (defined(__WATCH_OS_VERSION_MIN_REQUIRED) && __WATCH_OS_VERSION_MIN_REQUIRED >= watchos) \ 252 || (defined(__BRIDGE_OS_VERSION_MIN_REQUIRED) && __BRIDGE_OS_VERSION_MIN_REQUIRED >= bridgeos) \ 253 || (defined(__VISION_OS_VERSION_MIN_REQUIRED) && __VISION_OS_VERSION_MIN_REQUIRED >= visionos) \ 254 ) 255 256 #if !__has_feature(nullability) 257 #ifndef _Nullable 258 #define _Nullable 259 #endif 260 #ifndef _Nonnull 261 #define _Nonnull 262 #endif 263 #ifndef _Null_unspecified 264 #define _Null_unspecified 265 #endif 266 #endif 267 268 #ifndef DISPATCH_RETURNS_RETAINED_BLOCK 269 #if __has_attribute(ns_returns_retained) 270 #define DISPATCH_RETURNS_RETAINED_BLOCK __attribute__((__ns_returns_retained__)) 271 #else 272 #define DISPATCH_RETURNS_RETAINED_BLOCK 273 #endif 274 #endif 275 276 #if __has_attribute(enum_extensibility) 277 #define __DISPATCH_ENUM_ATTR __attribute__((__enum_extensibility__(open))) 278 #define __DISPATCH_ENUM_ATTR_CLOSED __attribute__((__enum_extensibility__(closed))) 279 #else 280 #define __DISPATCH_ENUM_ATTR 281 #define __DISPATCH_ENUM_ATTR_CLOSED 282 #endif // __has_attribute(enum_extensibility) 283 284 #if __has_attribute(flag_enum) 285 #define __DISPATCH_OPTIONS_ATTR __attribute__((__flag_enum__)) 286 #else 287 #define __DISPATCH_OPTIONS_ATTR 288 #endif // __has_attribute(flag_enum) 289 290 291 #if __has_feature(objc_fixed_enum) || __has_extension(cxx_strong_enums) || \ 292 __has_extension(cxx_fixed_enum) || defined(_WIN32) 293 #define DISPATCH_ENUM(name, type, ...) \ 294 typedef enum : type { __VA_ARGS__ } __DISPATCH_ENUM_ATTR name##_t 295 #define DISPATCH_OPTIONS(name, type, ...) \ 296 typedef enum : type { __VA_ARGS__ } __DISPATCH_OPTIONS_ATTR __DISPATCH_ENUM_ATTR name##_t 297 #else 298 #define DISPATCH_ENUM(name, type, ...) \ 299 enum { __VA_ARGS__ } __DISPATCH_ENUM_ATTR; typedef type name##_t 300 #define DISPATCH_OPTIONS(name, type, ...) \ 301 enum { __VA_ARGS__ } __DISPATCH_OPTIONS_ATTR __DISPATCH_ENUM_ATTR; typedef type name##_t 302 #endif // __has_feature(objc_fixed_enum) ... 303 304 305 306 #if __has_feature(enumerator_attributes) 307 #define DISPATCH_ENUM_API_AVAILABLE(...) API_AVAILABLE(__VA_ARGS__) 308 #define DISPATCH_ENUM_API_DEPRECATED(...) API_DEPRECATED(__VA_ARGS__) 309 #define DISPATCH_ENUM_API_DEPRECATED_WITH_REPLACEMENT(...) \ 310 API_DEPRECATED_WITH_REPLACEMENT(__VA_ARGS__) 311 #else 312 #define DISPATCH_ENUM_API_AVAILABLE(...) 313 #define DISPATCH_ENUM_API_DEPRECATED(...) 314 #define DISPATCH_ENUM_API_DEPRECATED_WITH_REPLACEMENT(...) 315 #endif 316 317 #ifdef __swift__ 318 #define DISPATCH_SWIFT3_OVERLAY 1 319 #else // __swift__ 320 #define DISPATCH_SWIFT3_OVERLAY 0 321 #endif // __swift__ 322 323 #if __has_feature(attribute_availability_swift) 324 #define DISPATCH_SWIFT_UNAVAILABLE(_msg) \ 325 __attribute__((__availability__(swift, unavailable, message=_msg))) 326 #else 327 #define DISPATCH_SWIFT_UNAVAILABLE(_msg) 328 #endif 329 330 #if DISPATCH_SWIFT3_OVERLAY 331 #define DISPATCH_SWIFT3_UNAVAILABLE(_msg) DISPATCH_SWIFT_UNAVAILABLE(_msg) 332 #else 333 #define DISPATCH_SWIFT3_UNAVAILABLE(_msg) 334 #endif 335 336 #if __has_attribute(swift_private) 337 #define DISPATCH_REFINED_FOR_SWIFT __attribute__((__swift_private__)) 338 #else 339 #define DISPATCH_REFINED_FOR_SWIFT 340 #endif 341 342 #if __has_attribute(swift_name) 343 #define DISPATCH_SWIFT_NAME(_name) __attribute__((__swift_name__(#_name))) 344 #else 345 #define DISPATCH_SWIFT_NAME(_name) 346 #endif 347 348 #ifndef __cplusplus 349 #define DISPATCH_TRANSPARENT_UNION __attribute__((__transparent_union__)) 350 #else 351 #define DISPATCH_TRANSPARENT_UNION 352 #endif 353 354 DISPATCH_ASSUME_ABI_SINGLE_BEGIN 355 356 DISPATCH_SWIFT_UNAVAILABLE("Unavailable in Swift") 357 typedef void (*dispatch_function_t)(void *_Nullable); 358 359 DISPATCH_ASSUME_ABI_SINGLE_END 360 361 #endif