3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #ifndef GRPC_IMPL_CODEGEN_PORT_PLATFORM_H
20 #define GRPC_IMPL_CODEGEN_PORT_PLATFORM_H
23 * Define GPR_BACKWARDS_COMPATIBILITY_MODE to try harder to be ABI
24 * compatible with older platforms (currently only on Linux)
26 * - some libc calls to be gotten via dlsym
27 * - some syscalls to be made directly
30 /* Get windows.h included everywhere (we need it) */
31 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
32 #ifndef WIN32_LEAN_AND_MEAN
33 #define GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
34 #define WIN32_LEAN_AND_MEAN
35 #endif /* WIN32_LEAN_AND_MEAN */
38 #define GRPC_NOMINMX_WAS_NOT_DEFINED
44 "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
45 #else /* !defined(_WIN32_WINNT) */
46 #if (_WIN32_WINNT < 0x0600)
48 "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
49 #endif /* _WIN32_WINNT < 0x0600 */
50 #endif /* defined(_WIN32_WINNT) */
54 #ifdef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
55 #undef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
56 #undef WIN32_LEAN_AND_MEAN
57 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
59 #ifdef GRPC_NOMINMAX_WAS_NOT_DEFINED
60 #undef GRPC_NOMINMAX_WAS_NOT_DEFINED
62 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
63 #endif /* defined(_WIN64) || defined(WIN64) || defined(_WIN32) || \
66 /* Override this file with one for your platform if you need to redefine
69 #if !defined(GPR_NO_AUTODETECT_PLATFORM)
70 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
71 #if defined(_WIN64) || defined(WIN64)
76 #define GPR_PLATFORM_STRING "windows"
78 #define GPR_WINDOWS_SUBPROCESS 1
79 #define GPR_WINDOWS_ENV
81 #define GPR_GETPID_IN_UNISTD_H 1
82 #define GPR_MSYS_TMPFILE
84 #define GPR_POSIX_STRING
85 #define GPR_POSIX_TIME
87 #define GPR_GETPID_IN_PROCESS_H 1
88 #define GPR_WINDOWS_TMPFILE
89 #define GPR_WINDOWS_LOG
90 #define GPR_WINDOWS_CRASH_HANDLER 1
91 #define GPR_WINDOWS_STRING
92 #define GPR_WINDOWS_TIME
95 #define GPR_GCC_ATOMIC 1
98 #define GPR_WINDOWS_ATOMIC 1
99 #define GPR_MSVC_TLS 1
101 #elif defined(GPR_MANYLINUX1)
102 // TODO(atash): manylinux1 is just another __linux__ but with ancient
103 // libraries; it should be integrated with the `__linux__` definitions below.
104 #define GPR_PLATFORM_STRING "manylinux"
105 #define GPR_POSIX_CRASH_HANDLER 1
106 #define GPR_CPU_POSIX 1
107 #define GPR_GCC_ATOMIC 1
108 #define GPR_GCC_TLS 1
110 #define GPR_LINUX_LOG 1
111 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
112 #define GPR_LINUX_ENV 1
113 #define GPR_POSIX_TMPFILE 1
114 #define GPR_POSIX_STRING 1
115 #define GPR_POSIX_SUBPROCESS 1
116 #define GPR_POSIX_SYNC 1
117 #define GPR_POSIX_TIME 1
118 #define GPR_GETPID_IN_UNISTD_H 1
120 #define GPR_ARCH_64 1
122 #define GPR_ARCH_32 1
124 #elif defined(ANDROID) || defined(__ANDROID__)
125 #define GPR_PLATFORM_STRING "android"
126 #define GPR_ANDROID 1
128 #define GPR_ARCH_64 1
130 #define GPR_ARCH_32 1
132 #define GPR_CPU_POSIX 1
133 #define GPR_GCC_SYNC 1
134 #define GPR_GCC_TLS 1
135 #define GPR_POSIX_ENV 1
136 #define GPR_POSIX_TMPFILE 1
137 #define GPR_ANDROID_LOG 1
138 #define GPR_POSIX_STRING 1
139 #define GPR_POSIX_SUBPROCESS 1
140 #define GPR_POSIX_SYNC 1
141 #define GPR_POSIX_TIME 1
142 #define GPR_GETPID_IN_UNISTD_H 1
143 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
144 #elif defined(__linux__)
145 #define GPR_PLATFORM_STRING "linux"
149 #ifndef _DEFAULT_SOURCE
150 #define _DEFAULT_SOURCE
155 #include <features.h>
156 #define GPR_CPU_LINUX 1
157 #define GPR_GCC_ATOMIC 1
158 #define GPR_GCC_TLS 1
160 #define GPR_LINUX_LOG
161 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
162 #define GPR_LINUX_ENV 1
163 #define GPR_POSIX_TMPFILE 1
164 #define GPR_POSIX_STRING 1
165 #define GPR_POSIX_SUBPROCESS 1
166 #define GPR_POSIX_SYNC 1
167 #define GPR_POSIX_TIME 1
168 #define GPR_GETPID_IN_UNISTD_H 1
170 #define GPR_ARCH_64 1
172 #define GPR_ARCH_32 1
175 #define GPR_POSIX_CRASH_HANDLER 1
176 #define GPR_LINUX_PTHREAD_NAME 1
177 #include <linux/version.h>
178 #else /* musl libc */
179 #define GPR_MUSL_LIBC_COMPAT 1
181 #elif defined(__APPLE__)
182 #include <Availability.h>
183 #include <TargetConditionals.h>
188 #define GPR_PLATFORM_STRING "ios"
189 #define GPR_CPU_IPHONE 1
190 #define GPR_PTHREAD_TLS 1
191 #else /* TARGET_OS_IPHONE */
192 #define GPR_PLATFORM_STRING "osx"
193 #ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
194 #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_7
195 #define GPR_CPU_IPHONE 1
196 #define GPR_PTHREAD_TLS 1
197 #else /* __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_7 */
198 #define GPR_CPU_POSIX 1
199 /* TODO(vjpai): there is a reported issue in bazel build for Mac where __thread
200 in a header is currently not working (bazelbuild/bazel#4341). Remove
201 the following conditional and use GPR_GCC_TLS when that is fixed */
202 #ifndef GRPC_BAZEL_BUILD
203 #define GPR_GCC_TLS 1
204 #else /* GRPC_BAZEL_BUILD */
205 #define GPR_PTHREAD_TLS 1
206 #endif /* GRPC_BAZEL_BUILD */
207 #define GPR_APPLE_PTHREAD_NAME 1
209 #else /* __MAC_OS_X_VERSION_MIN_REQUIRED */
210 #define GPR_CPU_POSIX 1
211 /* TODO(vjpai): Remove the following conditional and use only GPR_GCC_TLS
212 when bazelbuild/bazel#4341 is fixed */
213 #ifndef GRPC_BAZEL_BUILD
214 #define GPR_GCC_TLS 1
215 #else /* GRPC_BAZEL_BUILD */
216 #define GPR_PTHREAD_TLS 1
217 #endif /* GRPC_BAZEL_BUILD */
219 #define GPR_POSIX_CRASH_HANDLER 1
222 #define GPR_GCC_ATOMIC 1
223 #define GPR_POSIX_LOG 1
224 #define GPR_POSIX_ENV 1
225 #define GPR_POSIX_TMPFILE 1
226 #define GPR_POSIX_STRING 1
227 #define GPR_POSIX_SUBPROCESS 1
228 #define GPR_POSIX_SYNC 1
229 #define GPR_POSIX_TIME 1
230 #define GPR_GETPID_IN_UNISTD_H 1
231 /* TODO(mxyan): Remove when CFStream becomes default */
232 #ifndef GRPC_CFSTREAM
233 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
236 #define GPR_ARCH_64 1
238 #define GPR_ARCH_32 1
240 #elif defined(__FreeBSD__)
241 #define GPR_PLATFORM_STRING "freebsd"
245 #define GPR_FREEBSD 1
246 #define GPR_CPU_POSIX 1
247 #define GPR_GCC_ATOMIC 1
248 #define GPR_GCC_TLS 1
249 #define GPR_POSIX_LOG 1
250 #define GPR_POSIX_ENV 1
251 #define GPR_POSIX_TMPFILE 1
252 #define GPR_POSIX_STRING 1
253 #define GPR_POSIX_SUBPROCESS 1
254 #define GPR_POSIX_SYNC 1
255 #define GPR_POSIX_TIME 1
256 #define GPR_GETPID_IN_UNISTD_H 1
257 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
259 #define GPR_ARCH_64 1
261 #define GPR_ARCH_32 1
263 #elif defined(__OpenBSD__)
264 #define GPR_PLATFORM_STRING "openbsd"
268 #define GPR_OPENBSD 1
269 #define GPR_CPU_POSIX 1
270 #define GPR_GCC_ATOMIC 1
271 #define GPR_GCC_TLS 1
272 #define GPR_POSIX_LOG 1
273 #define GPR_POSIX_ENV 1
274 #define GPR_POSIX_TMPFILE 1
275 #define GPR_POSIX_STRING 1
276 #define GPR_POSIX_SUBPROCESS 1
277 #define GPR_POSIX_SYNC 1
278 #define GPR_POSIX_TIME 1
279 #define GPR_GETPID_IN_UNISTD_H 1
280 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
282 #define GPR_ARCH_64 1
284 #define GPR_ARCH_32 1
286 #elif defined(__sun) && defined(__SVR4)
287 #define GPR_PLATFORM_STRING "solaris"
288 #define GPR_SOLARIS 1
289 #define GPR_CPU_POSIX 1
290 #define GPR_GCC_ATOMIC 1
291 #define GPR_GCC_TLS 1
292 #define GPR_POSIX_LOG 1
293 #define GPR_POSIX_ENV 1
294 #define GPR_POSIX_TMPFILE 1
295 #define GPR_POSIX_STRING 1
296 #define GPR_POSIX_SUBPROCESS 1
297 #define GPR_POSIX_SYNC 1
298 #define GPR_POSIX_TIME 1
299 #define GPR_GETPID_IN_UNISTD_H 1
301 #define GPR_ARCH_64 1
303 #define GPR_ARCH_32 1
306 #define GPR_PLATFORM_STRING "aix"
311 #define GPR_CPU_POSIX 1
312 #define GPR_GCC_ATOMIC 1
313 #define GPR_GCC_TLS 1
314 #define GPR_POSIX_LOG 1
315 #define GPR_POSIX_ENV 1
316 #define GPR_POSIX_TMPFILE 1
317 #define GPR_POSIX_STRING 1
318 #define GPR_POSIX_SUBPROCESS 1
319 #define GPR_POSIX_SYNC 1
320 #define GPR_POSIX_TIME 1
321 #define GPR_GETPID_IN_UNISTD_H 1
323 #define GPR_ARCH_64 1
325 #define GPR_ARCH_32 1
327 #elif defined(__native_client__)
328 #define GPR_PLATFORM_STRING "nacl"
332 #ifndef _DEFAULT_SOURCE
333 #define _DEFAULT_SOURCE
339 #define GPR_CPU_POSIX 1
340 #define GPR_GCC_ATOMIC 1
341 #define GPR_GCC_TLS 1
342 #define GPR_POSIX_LOG 1
343 #define GPR_POSIX_ENV 1
344 #define GPR_POSIX_TMPFILE 1
345 #define GPR_POSIX_STRING 1
346 #define GPR_POSIX_SUBPROCESS 1
347 #define GPR_POSIX_SYNC 1
348 #define GPR_POSIX_TIME 1
349 #define GPR_GETPID_IN_UNISTD_H 1
351 #define GPR_ARCH_64 1
353 #define GPR_ARCH_32 1
356 #error "Could not auto-detect platform"
358 #endif /* GPR_NO_AUTODETECT_PLATFORM */
361 * There are platforms for which TLS should not be used even though the
362 * compiler makes it seem like it's supported (Android NDK < r12b for example).
363 * This is primarily because of linker problems and toolchain misconfiguration:
364 * TLS isn't supported until NDK r12b per
365 * https://developer.android.com/ndk/downloads/revision_history.html
366 * TLS also does not work with Android NDK if GCC is being used as the compiler
368 * Since NDK r16, `__NDK_MAJOR__` and `__NDK_MINOR__` are defined in
369 * <android/ndk-version.h>. For NDK < r16, users should define these macros,
370 * e.g. `-D__NDK_MAJOR__=11 -D__NKD_MINOR__=0` for NDK r11. */
371 #if defined(__ANDROID__) && defined(GPR_GCC_TLS)
372 #if __has_include(<android/ndk-version.h>)
373 #include <android/ndk-version.h>
374 #endif /* __has_include(<android/ndk-version.h>) */
375 #if (defined(__clang__) && defined(__NDK_MAJOR__) && defined(__NDK_MINOR__) && \
376 ((__NDK_MAJOR__ < 12) || \
377 ((__NDK_MAJOR__ == 12) && (__NDK_MINOR__ < 1)))) || \
378 (defined(__GNUC__) && !defined(__clang__))
380 #define GPR_PTHREAD_TLS 1
382 #endif /*defined(__ANDROID__) && defined(GPR_GCC_TLS) */
384 #if defined(__has_include)
385 #if __has_include(<atomic>)
386 #define GRPC_HAS_CXX11_ATOMIC
387 #endif /* __has_include(<atomic>) */
388 #endif /* defined(__has_include) */
390 #ifndef GPR_PLATFORM_STRING
391 #warning "GPR_PLATFORM_STRING not auto-detected"
392 #define GPR_PLATFORM_STRING "unknown"
396 #undef GPR_FORBID_UNREACHABLE_CODE
397 #define GPR_FORBID_UNREACHABLE_CODE 1
402 typedef __int8 int8_t;
403 typedef __int16 int16_t;
404 typedef __int32 int32_t;
405 typedef __int64 int64_t;
406 typedef unsigned __int8 uint8_t;
407 typedef unsigned __int16 uint16_t;
408 typedef unsigned __int32 uint32_t;
409 typedef unsigned __int64 uint64_t;
412 #endif /* _MSC_VER < 1700 */
415 #endif /* _MSC_VER */
417 /* Cache line alignment */
418 #ifndef GPR_CACHELINE_SIZE_LOG
419 #if defined(__i386__) || defined(__x86_64__)
420 #define GPR_CACHELINE_SIZE_LOG 6
422 #ifndef GPR_CACHELINE_SIZE_LOG
423 /* A reasonable default guess. Note that overestimates tend to waste more
424 space, while underestimates tend to waste more time. */
425 #define GPR_CACHELINE_SIZE_LOG 6
426 #endif /* GPR_CACHELINE_SIZE_LOG */
427 #endif /* GPR_CACHELINE_SIZE_LOG */
429 #define GPR_CACHELINE_SIZE (1 << GPR_CACHELINE_SIZE_LOG)
431 /* scrub GCC_ATOMIC if it's not available on this compiler */
432 #if defined(GPR_GCC_ATOMIC) && !defined(__ATOMIC_RELAXED)
433 #undef GPR_GCC_ATOMIC
434 #define GPR_GCC_SYNC 1
437 /* Validate platform combinations */
438 #if defined(GPR_GCC_ATOMIC) + defined(GPR_GCC_SYNC) + \
439 defined(GPR_WINDOWS_ATOMIC) != \
441 #error Must define exactly one of GPR_GCC_ATOMIC, GPR_GCC_SYNC, GPR_WINDOWS_ATOMIC
444 #if defined(GPR_ARCH_32) + defined(GPR_ARCH_64) != 1
445 #error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
448 #if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WINDOWS) + \
449 defined(GPR_CPU_IPHONE) + defined(GPR_CPU_CUSTOM) != \
451 #error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WINDOWS, GPR_CPU_IPHONE, GPR_CPU_CUSTOM
454 #if defined(GPR_MSVC_TLS) + defined(GPR_GCC_TLS) + defined(GPR_PTHREAD_TLS) + \
455 defined(GPR_CUSTOM_TLS) != \
457 #error Must define exactly one of GPR_MSVC_TLS, GPR_GCC_TLS, GPR_PTHREAD_TLS, GPR_CUSTOM_TLS
460 /* maximum alignment needed for any type on this platform, rounded up to a
462 #define GPR_MAX_ALIGNMENT 16
468 #ifndef GRPC_MUST_USE_RESULT
469 #if defined(__GNUC__) && !defined(__MINGW32__)
470 #define GRPC_MUST_USE_RESULT __attribute__((warn_unused_result))
471 #define GPR_ALIGN_STRUCT(n) __attribute__((aligned(n)))
473 #define GRPC_MUST_USE_RESULT
474 #define GPR_ALIGN_STRUCT(n)
479 #if defined(__GNUC__) && !defined(__MINGW32__)
480 #define GRPC_UNUSED __attribute__((unused))
486 #ifndef GPR_PRINT_FORMAT_CHECK
488 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS) \
489 __attribute__((format(printf, FORMAT_STR, ARGS)))
491 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS)
493 #endif /* GPR_PRINT_FORMAT_CHECK */
495 #if GPR_FORBID_UNREACHABLE_CODE
496 #define GPR_UNREACHABLE_CODE(STATEMENT)
498 #define GPR_UNREACHABLE_CODE(STATEMENT) \
500 gpr_log(GPR_ERROR, "Should never reach here."); \
504 #endif /* GPR_FORBID_UNREACHABLE_CODE */
511 #define GRPCAPI GPRAPI
515 #define CENSUSAPI GRPCAPI
518 #ifndef GPR_ATTRIBUTE_NO_TSAN /* (1) */
519 #if defined(__has_feature)
520 #if __has_feature(thread_sanitizer)
521 #define GPR_ATTRIBUTE_NO_TSAN __attribute__((no_sanitize("thread")))
522 #endif /* __has_feature(thread_sanitizer) */
523 #endif /* defined(__has_feature) */
524 #ifndef GPR_ATTRIBUTE_NO_TSAN /* (2) */
525 #define GPR_ATTRIBUTE_NO_TSAN
526 #endif /* GPR_ATTRIBUTE_NO_TSAN (2) */
527 #endif /* GPR_ATTRIBUTE_NO_TSAN (1) */
529 /* GRPC_TSAN_ENABLED will be defined, when compiled with thread sanitizer. */
530 #if defined(__SANITIZE_THREAD__)
531 #define GRPC_TSAN_ENABLED
532 #elif defined(__has_feature)
533 #if __has_feature(thread_sanitizer)
534 #define GRPC_TSAN_ENABLED
538 /* GRPC_ALLOW_EXCEPTIONS should be 0 or 1 if exceptions are allowed or not */
539 #ifndef GRPC_ALLOW_EXCEPTIONS
540 /* If not already set, set to 1 on Windows (style guide standard) but to
541 * 0 on non-Windows platforms unless the compiler defines __EXCEPTIONS */
543 #define GRPC_ALLOW_EXCEPTIONS 1
544 #else /* GPR_WINDOWS */
546 #define GRPC_ALLOW_EXCEPTIONS 1
547 #else /* __EXCEPTIONS */
548 #define GRPC_ALLOW_EXCEPTIONS 0
549 #endif /* __EXCEPTIONS */
550 #endif /* __GPR_WINDOWS */
551 #endif /* GRPC_ALLOW_EXCEPTIONS */
553 /* Use GPR_LIKELY only in cases where you are sure that a certain outcome is the
554 * most likely. Ideally, also collect performance numbers to justify the claim.
557 #define GPR_LIKELY(x) __builtin_expect((x), 1)
558 #define GPR_UNLIKELY(x) __builtin_expect((x), 0)
560 #define GPR_LIKELY(x) (x)
561 #define GPR_UNLIKELY(x) (x)
562 #endif /* __GNUC__ */
564 #ifndef __STDC_FORMAT_MACROS
565 #define __STDC_FORMAT_MACROS
568 #endif /* GRPC_IMPL_CODEGEN_PORT_PLATFORM_H */