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
31 * Defines GPR_ABSEIL_SYNC to use synchronization features from Abseil
33 #ifndef GPR_ABSEIL_SYNC
34 #if defined(__APPLE__)
35 // This is disabled on Apple platforms because macos/grpc_basictests_c_cpp
36 // fails with this. https://github.com/grpc/grpc/issues/23661
38 #define GPR_ABSEIL_SYNC 1
40 #endif // GPR_ABSEIL_SYNC
42 /* Get windows.h included everywhere (we need it) */
43 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
44 #ifndef WIN32_LEAN_AND_MEAN
45 #define GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
46 #define WIN32_LEAN_AND_MEAN
47 #endif /* WIN32_LEAN_AND_MEAN */
50 #define GRPC_NOMINMX_WAS_NOT_DEFINED
58 "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
59 #else /* !defined(_WIN32_WINNT) */
60 #if (_WIN32_WINNT < 0x0600)
62 "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
63 #endif /* _WIN32_WINNT < 0x0600 */
64 #endif /* defined(_WIN32_WINNT) */
66 #ifdef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
67 #undef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
68 #undef WIN32_LEAN_AND_MEAN
69 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
71 #ifdef GRPC_NOMINMAX_WAS_NOT_DEFINED
72 #undef GRPC_NOMINMAX_WAS_NOT_DEFINED
74 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
75 #endif /* defined(_WIN64) || defined(WIN64) || defined(_WIN32) || \
78 /* Override this file with one for your platform if you need to redefine
81 #if !defined(GPR_NO_AUTODETECT_PLATFORM)
82 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
83 #if defined(_WIN64) || defined(WIN64)
88 #define GPR_PLATFORM_STRING "windows"
90 #define GPR_WINDOWS_SUBPROCESS 1
91 #define GPR_WINDOWS_ENV
93 #define GPR_GETPID_IN_UNISTD_H 1
94 #define GPR_MSYS_TMPFILE
96 #define GPR_POSIX_STRING
97 #define GPR_POSIX_TIME
99 #define GPR_GETPID_IN_PROCESS_H 1
100 #define GPR_WINDOWS_TMPFILE
101 #define GPR_WINDOWS_LOG
102 #define GPR_WINDOWS_CRASH_HANDLER 1
103 #define GPR_WINDOWS_STRING
104 #define GPR_WINDOWS_TIME
107 #define GPR_GCC_ATOMIC 1
109 #define GPR_WINDOWS_ATOMIC 1
111 #define GPR_STDCPP_TLS 1
112 #elif defined(ANDROID) || defined(__ANDROID__)
113 #define GPR_PLATFORM_STRING "android"
114 #define GPR_ANDROID 1
115 // TODO(apolcyn): re-evaluate support for c-ares
116 // on android after upgrading our c-ares dependency.
117 // See https://github.com/grpc/grpc/issues/18038.
120 #define GPR_ARCH_64 1
122 #define GPR_ARCH_32 1
124 #define GPR_CPU_POSIX 1
125 #define GPR_GCC_SYNC 1
126 #define GPR_STDCPP_TLS 1
127 #define GPR_POSIX_ENV 1
128 #define GPR_POSIX_TMPFILE 1
129 #define GPR_ANDROID_LOG 1
130 #define GPR_POSIX_STRING 1
131 #define GPR_POSIX_SUBPROCESS 1
132 #define GPR_POSIX_SYNC 1
133 #define GPR_POSIX_TIME 1
134 #define GPR_HAS_PTHREAD_H 1
135 #define GPR_GETPID_IN_UNISTD_H 1
136 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
137 #elif defined(__linux__)
138 #define GPR_PLATFORM_STRING "linux"
142 #ifndef _DEFAULT_SOURCE
143 #define _DEFAULT_SOURCE
148 #include <features.h>
149 #define GPR_CPU_LINUX 1
150 #define GPR_GCC_ATOMIC 1
151 #define GPR_STDCPP_TLS 1
153 #define GPR_LINUX_LOG
154 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
155 #define GPR_LINUX_ENV 1
156 #define GPR_POSIX_TMPFILE 1
157 #define GPR_POSIX_STRING 1
158 #define GPR_POSIX_SUBPROCESS 1
159 #define GPR_POSIX_SYNC 1
160 #define GPR_POSIX_TIME 1
161 #define GPR_HAS_PTHREAD_H 1
162 #define GPR_GETPID_IN_UNISTD_H 1
164 #define GPR_ARCH_64 1
166 #define GPR_ARCH_32 1
169 #define GPR_POSIX_CRASH_HANDLER 1
170 #define GPR_LINUX_PTHREAD_NAME 1
171 #include <linux/version.h>
172 #else /* musl libc */
173 #define GPR_MUSL_LIBC_COMPAT 1
175 #elif defined(__ASYLO__)
176 #define GPR_ARCH_64 1
177 #define GPR_CPU_POSIX 1
178 #define GPR_GCC_TLS 1
179 #define GPR_PLATFORM_STRING "asylo"
180 #define GPR_GCC_SYNC 1
181 #define GPR_POSIX_SYNC 1
182 #define GPR_POSIX_STRING 1
183 #define GPR_POSIX_LOG 1
184 #define GPR_POSIX_TIME 1
185 #define GPR_POSIX_ENV 1
187 #define GRPC_POSIX_SOCKET 1
188 #define GRPC_POSIX_SOCKETADDR
189 #define GRPC_POSIX_SOCKETUTILS 1
190 #define GRPC_TIMER_USE_GENERIC 1
191 #define GRPC_POSIX_NO_SPECIAL_WAKEUP_FD 1
192 #define GRPC_POSIX_WAKEUP_FD 1
194 #define GPR_NO_AUTODETECT_PLATFORM 1
195 #elif defined(__APPLE__)
196 #include <Availability.h>
197 #include <TargetConditionals.h>
202 #define GPR_PLATFORM_STRING "ios"
203 #define GPR_CPU_IPHONE 1
204 #define GPR_STDCPP_TLS 1
205 #define GRPC_CFSTREAM 1
206 /* the c-ares resolver isn't safe to enable on iOS */
208 #else /* TARGET_OS_IPHONE */
209 #define GPR_PLATFORM_STRING "osx"
210 #define GPR_CPU_POSIX 1
211 #define GPR_STDCPP_TLS 1
212 #define GPR_POSIX_CRASH_HANDLER 1
215 #define GPR_GCC_ATOMIC 1
216 #define GPR_POSIX_LOG 1
217 #define GPR_POSIX_ENV 1
218 #define GPR_POSIX_TMPFILE 1
219 #define GPR_POSIX_STRING 1
220 #define GPR_POSIX_SUBPROCESS 1
221 #define GPR_POSIX_SYNC 1
222 #define GPR_POSIX_TIME 1
223 #define GPR_HAS_PTHREAD_H 1
224 #define GPR_GETPID_IN_UNISTD_H 1
225 #ifndef GRPC_CFSTREAM
226 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
229 #define GPR_ARCH_64 1
231 #define GPR_ARCH_32 1
233 #elif defined(__FreeBSD__)
234 #define GPR_PLATFORM_STRING "freebsd"
238 #define GPR_FREEBSD 1
239 #define GPR_CPU_POSIX 1
240 #define GPR_GCC_ATOMIC 1
241 #define GPR_GCC_TLS 1
242 #define GPR_POSIX_LOG 1
243 #define GPR_POSIX_ENV 1
244 #define GPR_POSIX_TMPFILE 1
245 #define GPR_POSIX_STRING 1
246 #define GPR_POSIX_SUBPROCESS 1
247 #define GPR_POSIX_SYNC 1
248 #define GPR_POSIX_TIME 1
249 #define GPR_HAS_PTHREAD_H 1
250 #define GPR_GETPID_IN_UNISTD_H 1
251 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
253 #define GPR_ARCH_64 1
255 #define GPR_ARCH_32 1
257 #elif defined(__OpenBSD__)
258 #define GPR_PLATFORM_STRING "openbsd"
262 #define GPR_OPENBSD 1
263 #define GPR_CPU_POSIX 1
264 #define GPR_GCC_ATOMIC 1
265 #define GPR_GCC_TLS 1
266 #define GPR_POSIX_LOG 1
267 #define GPR_POSIX_ENV 1
268 #define GPR_POSIX_TMPFILE 1
269 #define GPR_POSIX_STRING 1
270 #define GPR_POSIX_SUBPROCESS 1
271 #define GPR_POSIX_SYNC 1
272 #define GPR_POSIX_TIME 1
273 #define GPR_HAS_PTHREAD_H 1
274 #define GPR_GETPID_IN_UNISTD_H 1
275 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
277 #define GPR_ARCH_64 1
279 #define GPR_ARCH_32 1
281 #elif defined(__sun) && defined(__SVR4)
282 #define GPR_PLATFORM_STRING "solaris"
283 #define GPR_SOLARIS 1
284 #define GPR_CPU_POSIX 1
285 #define GPR_GCC_ATOMIC 1
286 #define GPR_GCC_TLS 1
287 #define GPR_POSIX_LOG 1
288 #define GPR_POSIX_ENV 1
289 #define GPR_POSIX_TMPFILE 1
290 #define GPR_POSIX_STRING 1
291 #define GPR_POSIX_SUBPROCESS 1
292 #define GPR_POSIX_SYNC 1
293 #define GPR_POSIX_TIME 1
294 #define GPR_HAS_PTHREAD_H 1
295 #define GPR_GETPID_IN_UNISTD_H 1
297 #define GPR_ARCH_64 1
299 #define GPR_ARCH_32 1
302 #define GPR_PLATFORM_STRING "aix"
307 #define GPR_CPU_POSIX 1
308 #define GPR_GCC_ATOMIC 1
309 #define GPR_GCC_TLS 1
310 #define GPR_POSIX_LOG 1
311 #define GPR_POSIX_ENV 1
312 #define GPR_POSIX_TMPFILE 1
313 #define GPR_POSIX_STRING 1
314 #define GPR_POSIX_SUBPROCESS 1
315 #define GPR_POSIX_SYNC 1
316 #define GPR_POSIX_TIME 1
317 #define GPR_HAS_PTHREAD_H 1
318 #define GPR_GETPID_IN_UNISTD_H 1
320 #define GPR_ARCH_64 1
322 #define GPR_ARCH_32 1
324 #elif defined(__native_client__)
325 #define GPR_PLATFORM_STRING "nacl"
329 #ifndef _DEFAULT_SOURCE
330 #define _DEFAULT_SOURCE
336 #define GPR_CPU_POSIX 1
337 #define GPR_GCC_ATOMIC 1
338 #define GPR_GCC_TLS 1
339 #define GPR_POSIX_LOG 1
340 #define GPR_POSIX_ENV 1
341 #define GPR_POSIX_TMPFILE 1
342 #define GPR_POSIX_STRING 1
343 #define GPR_POSIX_SUBPROCESS 1
344 #define GPR_POSIX_SYNC 1
345 #define GPR_POSIX_TIME 1
346 #define GPR_HAS_PTHREAD_H 1
347 #define GPR_GETPID_IN_UNISTD_H 1
349 #define GPR_ARCH_64 1
351 #define GPR_ARCH_32 1
353 #elif defined(__Fuchsia__)
354 #define GPR_FUCHSIA 1
355 #define GPR_ARCH_64 1
356 #define GPR_PLATFORM_STRING "fuchsia"
357 #include <features.h>
358 // Specifying musl libc affects wrap_memcpy.c. It causes memmove() to be
360 #define GPR_MUSL_LIBC_COMPAT 1
361 #define GPR_CPU_POSIX 1
362 #define GPR_GCC_ATOMIC 1
363 #define GPR_PTHREAD_TLS 1
364 #define GPR_POSIX_LOG 1
365 #define GPR_POSIX_SYNC 1
366 #define GPR_POSIX_ENV 1
367 #define GPR_POSIX_TMPFILE 1
368 #define GPR_POSIX_SUBPROCESS 1
369 #define GPR_POSIX_SYNC 1
370 #define GPR_POSIX_STRING 1
371 #define GPR_POSIX_TIME 1
372 #define GPR_HAS_PTHREAD_H 1
373 #define GPR_GETPID_IN_UNISTD_H 1
375 #error "Could not auto-detect platform"
377 #endif /* GPR_NO_AUTODETECT_PLATFORM */
379 #if defined(GPR_BACKWARDS_COMPATIBILITY_MODE)
381 * For backward compatibility mode, reset _FORTIFY_SOURCE to prevent
382 * a library from having non-standard symbols such as __asprintf_chk.
383 * This helps non-glibc systems such as alpine using musl to find symbols.
385 #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
386 #undef _FORTIFY_SOURCE
387 #define _FORTIFY_SOURCE 0
391 #if defined(__has_include)
392 #if __has_include(<atomic>)
393 #define GRPC_HAS_CXX11_ATOMIC
394 #endif /* __has_include(<atomic>) */
395 #endif /* defined(__has_include) */
397 #ifndef GPR_PLATFORM_STRING
398 #warning "GPR_PLATFORM_STRING not auto-detected"
399 #define GPR_PLATFORM_STRING "unknown"
403 #undef GPR_FORBID_UNREACHABLE_CODE
404 #define GPR_FORBID_UNREACHABLE_CODE 1
409 typedef __int8 int8_t;
410 typedef __int16 int16_t;
411 typedef __int32 int32_t;
412 typedef __int64 int64_t;
413 typedef unsigned __int8 uint8_t;
414 typedef unsigned __int16 uint16_t;
415 typedef unsigned __int32 uint32_t;
416 typedef unsigned __int64 uint64_t;
419 #endif /* _MSC_VER < 1700 */
422 #endif /* _MSC_VER */
424 /* Type of cycle clock implementation */
426 /* Disable cycle clock by default.
427 TODO(soheil): enable when we support fallback for unstable cycle clocks.
428 #if defined(__i386__)
429 #define GPR_CYCLE_COUNTER_RDTSC_32 1
430 #elif defined(__x86_64__) || defined(__amd64__)
431 #define GPR_CYCLE_COUNTER_RDTSC_64 1
433 #define GPR_CYCLE_COUNTER_FALLBACK 1
436 #define GPR_CYCLE_COUNTER_FALLBACK 1
438 #define GPR_CYCLE_COUNTER_FALLBACK 1
439 #endif /* GPR_LINUX */
441 /* Cache line alignment */
442 #ifndef GPR_CACHELINE_SIZE_LOG
443 #if defined(__i386__) || defined(__x86_64__)
444 #define GPR_CACHELINE_SIZE_LOG 6
446 #ifndef GPR_CACHELINE_SIZE_LOG
447 /* A reasonable default guess. Note that overestimates tend to waste more
448 space, while underestimates tend to waste more time. */
449 #define GPR_CACHELINE_SIZE_LOG 6
450 #endif /* GPR_CACHELINE_SIZE_LOG */
451 #endif /* GPR_CACHELINE_SIZE_LOG */
453 #define GPR_CACHELINE_SIZE (1 << GPR_CACHELINE_SIZE_LOG)
455 /* scrub GCC_ATOMIC if it's not available on this compiler */
456 #if defined(GPR_GCC_ATOMIC) && !defined(__ATOMIC_RELAXED)
457 #undef GPR_GCC_ATOMIC
458 #define GPR_GCC_SYNC 1
461 /* Validate platform combinations */
462 #if defined(GPR_GCC_ATOMIC) + defined(GPR_GCC_SYNC) + \
463 defined(GPR_WINDOWS_ATOMIC) != \
465 #error Must define exactly one of GPR_GCC_ATOMIC, GPR_GCC_SYNC, GPR_WINDOWS_ATOMIC
468 #if defined(GPR_ARCH_32) + defined(GPR_ARCH_64) != 1
469 #error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
472 #if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WINDOWS) + \
473 defined(GPR_CPU_IPHONE) + defined(GPR_CPU_CUSTOM) != \
475 #error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WINDOWS, GPR_CPU_IPHONE, GPR_CPU_CUSTOM
478 #if defined(GPR_MSVC_TLS) + defined(GPR_GCC_TLS) + defined(GPR_PTHREAD_TLS) + \
479 defined(GPR_STDCPP_TLS) + defined(GPR_CUSTOM_TLS) != \
481 #error Must define exactly one of GPR_MSVC_TLS, GPR_GCC_TLS, GPR_PTHREAD_TLS, GPR_STDCPP_TLS, GPR_CUSTOM_TLS
484 /* maximum alignment needed for any type on this platform, rounded up to a
486 #define GPR_MAX_ALIGNMENT 16
492 #ifndef GRPC_IF_NAMETOINDEX
493 #define GRPC_IF_NAMETOINDEX 1
496 #ifndef GRPC_MUST_USE_RESULT
497 #if defined(__GNUC__) && !defined(__MINGW32__)
498 #define GRPC_MUST_USE_RESULT __attribute__((warn_unused_result))
499 #define GPR_ALIGN_STRUCT(n) __attribute__((aligned(n)))
501 #define GRPC_MUST_USE_RESULT
502 #define GPR_ALIGN_STRUCT(n)
507 #if defined(__GNUC__) && !defined(__MINGW32__)
508 #define GRPC_UNUSED __attribute__((unused))
514 #ifndef GPR_PRINT_FORMAT_CHECK
516 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS) \
517 __attribute__((format(printf, FORMAT_STR, ARGS)))
519 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS)
521 #endif /* GPR_PRINT_FORMAT_CHECK */
523 #if GPR_FORBID_UNREACHABLE_CODE
524 #define GPR_UNREACHABLE_CODE(STATEMENT)
526 #define GPR_UNREACHABLE_CODE(STATEMENT) \
528 gpr_log(GPR_ERROR, "Should never reach here."); \
532 #endif /* GPR_FORBID_UNREACHABLE_CODE */
539 #define GRPCAPI GPRAPI
543 #define CENSUSAPI GRPCAPI
546 #ifndef GPR_HAS_ATTRIBUTE
547 #ifdef __has_attribute
548 #define GPR_HAS_ATTRIBUTE(a) __has_attribute(a)
550 #define GPR_HAS_ATTRIBUTE(a) 0
552 #endif /* GPR_HAS_ATTRIBUTE */
554 #ifndef GPR_HAS_FEATURE
556 #define GPR_HAS_FEATURE(a) __has_feature(a)
558 #define GPR_HAS_FEATURE(a) 0
560 #endif /* GPR_HAS_FEATURE */
562 #ifndef GPR_ATTRIBUTE_NOINLINE
563 #if GPR_HAS_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__))
564 #define GPR_ATTRIBUTE_NOINLINE __attribute__((noinline))
565 #define GPR_HAS_ATTRIBUTE_NOINLINE 1
567 #define GPR_ATTRIBUTE_NOINLINE
569 #endif /* GPR_ATTRIBUTE_NOINLINE */
571 #ifndef GPR_ATTRIBUTE_WEAK
572 /* Attribute weak is broken on LLVM/windows:
573 * https://bugs.llvm.org/show_bug.cgi?id=37598 */
574 #if (GPR_HAS_ATTRIBUTE(weak) || (defined(__GNUC__) && !defined(__clang__))) && \
575 !(defined(__llvm__) && defined(_WIN32))
576 #define GPR_ATTRIBUTE_WEAK __attribute__((weak))
577 #define GPR_HAS_ATTRIBUTE_WEAK 1
579 #define GPR_ATTRIBUTE_WEAK
581 #endif /* GPR_ATTRIBUTE_WEAK */
583 #ifndef GPR_ATTRIBUTE_NO_TSAN /* (1) */
584 #if GPR_HAS_FEATURE(thread_sanitizer)
585 #define GPR_ATTRIBUTE_NO_TSAN __attribute__((no_sanitize("thread")))
586 #endif /* GPR_HAS_FEATURE */
587 #ifndef GPR_ATTRIBUTE_NO_TSAN /* (2) */
588 #define GPR_ATTRIBUTE_NO_TSAN
589 #endif /* GPR_ATTRIBUTE_NO_TSAN (2) */
590 #endif /* GPR_ATTRIBUTE_NO_TSAN (1) */
592 /* GRPC_TSAN_ENABLED will be defined, when compiled with thread sanitizer. */
593 #ifndef GRPC_TSAN_SUPPRESSED
594 #if defined(__SANITIZE_THREAD__)
595 #define GRPC_TSAN_ENABLED
596 #elif GPR_HAS_FEATURE(thread_sanitizer)
597 #define GRPC_TSAN_ENABLED
601 /* GRPC_ASAN_ENABLED will be defined, when compiled with address sanitizer. */
602 #ifndef GRPC_ASAN_SUPPRESSED
603 #if defined(__SANITIZE_ADDRESS__)
604 #define GRPC_ASAN_ENABLED
605 #elif GPR_HAS_FEATURE(address_sanitizer)
606 #define GRPC_ASAN_ENABLED
610 /* GRPC_ALLOW_EXCEPTIONS should be 0 or 1 if exceptions are allowed or not */
611 #ifndef GRPC_ALLOW_EXCEPTIONS
613 #if defined(_MSC_VER) && defined(_CPPUNWIND)
614 #define GRPC_ALLOW_EXCEPTIONS 1
615 #elif defined(__EXCEPTIONS)
616 #define GRPC_ALLOW_EXCEPTIONS 1
618 #define GRPC_ALLOW_EXCEPTIONS 0
620 #else /* GPR_WINDOWS */
622 #define GRPC_ALLOW_EXCEPTIONS 1
623 #else /* __EXCEPTIONS */
624 #define GRPC_ALLOW_EXCEPTIONS 0
625 #endif /* __EXCEPTIONS */
626 #endif /* __GPR_WINDOWS */
627 #endif /* GRPC_ALLOW_EXCEPTIONS */
629 /* Use GPR_LIKELY only in cases where you are sure that a certain outcome is the
630 * most likely. Ideally, also collect performance numbers to justify the claim.
633 #define GPR_LIKELY(x) __builtin_expect((x), 1)
634 #define GPR_UNLIKELY(x) __builtin_expect((x), 0)
636 #define GPR_LIKELY(x) (x)
637 #define GPR_UNLIKELY(x) (x)
638 #endif /* __GNUC__ */
640 #ifndef __STDC_FORMAT_MACROS
641 #define __STDC_FORMAT_MACROS
644 #endif /* GRPC_IMPL_CODEGEN_PORT_PLATFORM_H */