-
-
Notifications
You must be signed in to change notification settings - Fork 22k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Thread: Re-add <new>
include for std::hardware_destructive_interference_size
#101004
Thread: Re-add <new>
include for std::hardware_destructive_interference_size
#101004
Conversation
@@ -31,6 +31,8 @@ | |||
#ifndef THREAD_SAFE_H | |||
#define THREAD_SAFE_H | |||
|
|||
#include "core/os/mutex.h" // IWYU pragma: keep // Used in macro. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It indeed worked for clangd
, so much so that it's part of their guidelines:
https://clangd.llvm.org/guides/include-cleaner#scenarios-and-solutions
10c28f2
to
a2b5460
Compare
core/os/thread.h
Outdated
#if defined(__cpp_lib_hardware_interference_size) && !defined(ANDROID_ENABLED) | ||
#include <new> // For CACHE_LINE_BYTES. | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming that adding the #if
conditions matching where it's used will prevent tools like iwyu from flagging this as unused when run on platforms which don't fulfill that condition (seems to be GCC specific, possibly supported on Clang, but thus not MSVC most likely).
If not, we should add a pragma comment to prevent its removal, but then I wouldn't trust the judgement of those tools.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok this is what broke my fix, to test __cpp_lib_hardware_interference_size
we first need to include <new>
.
So pragma comment it will be.
I went maybe a tiny bit heavy handed on tweaking code style / diff --git a/core/os/thread.h b/core/os/thread.h
index 066c4befa7..1c442b41f6 100644
--- a/core/os/thread.h
+++ b/core/os/thread.h
@@ -42,6 +42,8 @@
#include "core/templates/safe_refcount.h"
#include "core/typedefs.h"
+#include <new>
+
#ifdef MINGW_ENABLED
#define MINGW_STDTHREAD_REDUNDANCY_WARNING
#include "thirdparty/mingw-std-threads/mingw.thread.h"
diff --git a/core/os/thread_safe.h b/core/os/thread_safe.h
index 316b0c9687..042a0b7d98 100644
--- a/core/os/thread_safe.h
+++ b/core/os/thread_safe.h
@@ -31,6 +31,8 @@
#ifndef THREAD_SAFE_H
#define THREAD_SAFE_H
+#include "core/os/mutex.h"
+
#define _THREAD_SAFE_CLASS_ mutable Mutex _thread_safe_;
#define _THREAD_SAFE_METHOD_ MutexLock _thread_safe_method_(_thread_safe_);
#define _THREAD_SAFE_LOCK_ _thread_safe_.lock(); This PR should be equivalent to that diff if I didn't mess up. |
Taking back as draft for a bit, the latest iteration of my PR doesn't seem to fix the crash anymore... Seems like some of the "harmless" refactoring I've done may have caused harm? Or the problem is deeper than I thought. |
…rence_size` Somehow it would still build fine, but would crash when compiled with GCC 12.2 on Debian 12. Also re-add wrongly removed Mutex include from `thread_safe.h`, where it's used in macros. Add IWYU pragma comments to prevent it from mistakenly flagging those as unused.
a2b5460
to
f2d4dac
Compare
OK I fixed the issue, the include of Which hints to why everything compiled fine despite the removal of #if defined(__cpp_lib_hardware_interference_size) && !defined(ANDROID_ENABLED) // This would be OK with NDK >= 26.
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winterference-size"
#endif
static constexpr size_t CACHE_LINE_BYTES = std::hardware_destructive_interference_size;
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
#else
// At a negligible memory cost, we use a conservatively high value.
static constexpr size_t CACHE_LINE_BYTES = 128;
#endif Either it doesn't work properly, or we're mixing both branches depending on whether I made this quick test to confirm (on diff --git a/core/os/thread.h b/core/os/thread.h
index 066c4be..6d1ac06 100644
--- a/core/os/thread.h
+++ b/core/os/thread.h
@@ -90,11 +90,13 @@ public:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winterference-size"
#endif
+ #warning Good branch.
static constexpr size_t CACHE_LINE_BYTES = std::hardware_destructive_interference_size;
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
#else
+ #warning Bad branch.
// At a negligible memory cost, we use a conservatively high value.
static constexpr size_t CACHE_LINE_BYTES = 128;
#endif
Here are the full build logs: There's indeed a few "Bad branches." in there:
Including |
I think the only way is build with Github actions, and then test with a benchmark server with a gpu, but it might to be too expensive. |
Somehow it would still build fine, but would crash when compiled with GCC 12.2 on Debian 12.
Also re-add wrongly removed Mutex include from
thread_safe.h
, where it's used in macros.Add IWYU pragma comments to prevent it from mistakenly flagging those as unused.
CC @RandomShaper @hpvb
I still don't know why it would compile OK without
<new>
, but then crash, but I've confirmed that this patch fixes the crash with Debian 12's GCC 12.2.0.