greenlet_compiler_compat.hpp 4.2 KB

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