codekingpro/portable-devtools
114k
1/* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2#ifndef GREENLET_COMPILER_COMPAT_HPP
3#define GREENLET_COMPILER_COMPAT_HPP
4
5/**
6 * Definitions to aid with compatibility with different compilers.
7 *
8 * .. caution:: Use extreme care with noexcept.
9 * Some compilers and runtimes, specifically gcc/libgcc/libstdc++ on
10 * Linux, implement stack unwinding by throwing an uncatchable
11 * exception, one that specifically does not appear to be an active
12 * exception to the rest of the runtime. If this happens while we're in a noexcept function,
13 * we have violated our dynamic exception contract, and so the runtime
14 * will call std::terminate(), which kills the process with the
15 * unhelpful message "terminate called without an active exception".
16 *
17 * This has happened in this scenario: A background thread is running
18 * a greenlet that has made a native call and released the GIL.
19 * Meanwhile, the main thread finishes and starts shutting down the
20 * interpreter. When the background thread is scheduled again and
21 * attempts to obtain the GIL, it notices that the interpreter is
22 * exiting and calls ``pthread_exit()``. This in turn starts to unwind
23 * the stack by throwing that exception. But we had the ``PyCall``
24 * functions annotated as noexcept, so the runtime terminated us.
25 *
26 * #2 0x00007fab26fec2b7 in std::terminate() () from /lib/x86_64-linux-gnu/libstdc++.so.6
27 * #3 0x00007fab26febb3c in __gxx_personality_v0 () from /lib/x86_64-linux-gnu/libstdc++.so.6
28 * #4 0x00007fab26f34de6 in ?? () from /lib/x86_64-linux-gnu/libgcc_s.so.1
29 * #6 0x00007fab276a34c6 in __GI___pthread_unwind at ./nptl/unwind.c:130
30 * #7 0x00007fab2769bd3a in __do_cancel () at ../sysdeps/nptl/pthreadP.h:280
31 * #8 __GI___pthread_exit (value=value@entry=0x0) at ./nptl/pthread_exit.c:36
32 * #9 0x000000000052e567 in PyThread_exit_thread () at ../Python/thread_pthread.h:370
33 * #10 0x00000000004d60b5 in take_gil at ../Python/ceval_gil.h:224
34 * #11 0x00000000004d65f9 in PyEval_RestoreThread at ../Python/ceval.c:467
35 * #12 0x000000000060cce3 in setipaddr at ../Modules/socketmodule.c:1203
36 * #13 0x00000000006101cd in socket_gethostbyname
37 */
38
39#include <cstdint>
40
41# define G_NO_COPIES_OF_CLS(Cls) private: \
42 Cls(const Cls& other) = delete; \
43 Cls& operator=(const Cls& other) = delete
44
45# define G_NO_ASSIGNMENT_OF_CLS(Cls) private: \
46 Cls& operator=(const Cls& other) = delete
47
48# define G_NO_COPY_CONSTRUCTOR_OF_CLS(Cls) private: \
49 Cls(const Cls& other) = delete;
50
51
52// CAUTION: MSVC is stupidly picky:
53//
54// "The compiler ignores, without warning, any __declspec keywords
55// placed after * or & and in front of the variable identifier in a
56// declaration."
57// (https://docs.microsoft.com/en-us/cpp/cpp/declspec?view=msvc-160)
58//
59// So pointer return types must be handled differently (because of the
60// trailing *), or you get inscrutable compiler warnings like "error
61// C2059: syntax error: ''"
62//
63// In C++ 11, there is a standard syntax for attributes, and
64// GCC defines an attribute to use with this: [[gnu:noinline]].
65// In the future, this is expected to become standard.
66
67#if defined(__GNUC__) || defined(__clang__)
68/* We used to check for GCC 4+ or 3.4+, but those compilers are
69 laughably out of date. Just assume they support it. */
70# define GREENLET_NOINLINE(name) __attribute__((noinline)) name
71# define GREENLET_NOINLINE_P(rtype, name) rtype __attribute__((noinline)) name
72# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
73#elif defined(_MSC_VER)
74/* We used to check for && (_MSC_VER >= 1300) but that's also out of date. */
75# define GREENLET_NOINLINE(name) __declspec(noinline) name
76# define GREENLET_NOINLINE_P(rtype, name) __declspec(noinline) rtype name
77# define UNUSED(x) UNUSED_ ## x
78#endif
79
80#if defined(_MSC_VER)
81# define G_NOEXCEPT_WIN32 noexcept
82#else
83# define G_NOEXCEPT_WIN32
84#endif
85
86#if defined(__GNUC__) && defined(__POWERPC__) && defined(__APPLE__)
87// 32-bit PPC/MacOSX. Only known to be tested on unreleased versions
88// of macOS 10.6 using a macports build gcc 14. It appears that
89// running C++ destructors of thread-local variables is broken.
90
91// See https://github.com/python-greenlet/greenlet/pull/419
92# define GREENLET_BROKEN_THREAD_LOCAL_CLEANUP_JUST_LEAK 1
93#else
94# define GREENLET_BROKEN_THREAD_LOCAL_CLEANUP_JUST_LEAK 0
95#endif
96
97
98#endif
99 