#include "llvm/PassInfo.h"
#include "llvm/PassRegistry.h"
#include "llvm/Support/Atomic.h"
+#include "llvm/Support/Threading.h"
+#include <functional>
namespace llvm {
class TargetMachine;
-#define CALL_ONCE_INITIALIZATION(function) \
- static volatile sys::cas_flag initialized = 0; \
- sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
- if (old_val == 0) { \
- function(Registry); \
- sys::MemoryFence(); \
- TsanIgnoreWritesBegin(); \
- TsanHappensBefore(&initialized); \
- initialized = 2; \
- TsanIgnoreWritesEnd(); \
- } else { \
- sys::cas_flag tmp = initialized; \
- sys::MemoryFence(); \
- while (tmp != 2) { \
- tmp = initialized; \
- sys::MemoryFence(); \
- } \
- } \
- TsanHappensAfter(&initialized);
-
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
static void *initialize##passName##PassOnce(PassRegistry &Registry) { \
PassInfo *PI = new PassInfo( \
Registry.registerPass(*PI, true); \
return PI; \
} \
+ LLVM_DEFINE_ONCE_FLAG(Initialize##passName##PassFlag); \
void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
- CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
+ llvm::call_once(Initialize##passName##PassFlag, \
+ initialize##passName##PassOnce, std::ref(Registry)); \
}
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
Registry.registerPass(*PI, true); \
return PI; \
} \
+ LLVM_DEFINE_ONCE_FLAG(Initialize##passName##PassFlag); \
void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
- CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
+ llvm::call_once(Initialize##passName##PassFlag, \
+ initialize##passName##PassOnce, std::ref(Registry)); \
}
#define INITIALIZE_PASS_WITH_OPTIONS(PassName, Arg, Name, Cfg, Analysis) \
Registry.registerAnalysisGroup(&agName::ID, 0, *AI, false, true); \
return AI; \
} \
+ LLVM_DEFINE_ONCE_FLAG(Initialize##agName##AnalysisGroupFlag); \
void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
- CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
+ llvm::call_once(Initialize##agName##AnalysisGroupFlag, \
+ initialize##agName##AnalysisGroupOnce, \
+ std::ref(Registry)); \
}
#define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
true); \
return AI; \
} \
+ LLVM_DEFINE_ONCE_FLAG(Initialize##passName##PassFlag); \
void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
- CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
+ llvm::call_once(Initialize##passName##PassFlag, \
+ initialize##passName##PassOnce, std::ref(Registry)); \
}
#define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
Registry.registerAnalysisGroup(&agName::ID, &passName::ID, *AI, def, true); \
return AI; \
} \
+ LLVM_DEFINE_ONCE_FLAG(Initialize##passName##PassFlag); \
void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
- CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
+ llvm::call_once(Initialize##passName##PassFlag, \
+ initialize##passName##PassOnce, std::ref(Registry)); \
}
//===---------------------------------------------------------------------------
#define LLVM_SUPPORT_THREADING_H
#include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX
+#include "llvm/Support/Compiler.h"
#include <ciso646> // So we can check the C++ standard lib macros.
+#include <functional>
// We use std::call_once on all Unix platforms except for NetBSD with
// libstdc++. That platform has a bug they are working to fix, and they'll
///
/// \param flag Flag used for tracking whether or not this has run.
/// \param UserFn Function to call once.
- void call_once(once_flag &flag, void (*UserFn)(void));
+ template <typename Function, typename... Args>
+ void call_once(once_flag &flag, Function &&F, Args &&... ArgList) {
+#if LLVM_THREADING_USE_STD_CALL_ONCE
+ std::call_once(flag, std::forward<Function>(F),
+ std::forward<Args>(ArgList)...);
+#else
+ // For other platforms we use a generic (if brittle) version based on our
+ // atomics.
+ sys::cas_flag old_val = sys::CompareAndSwap(&flag, Wait, Uninitialized);
+ if (old_val == Uninitialized) {
+ std::forward<Function>(F)(std::forward<Args>(ArgList)...);
+ sys::MemoryFence();
+ TsanIgnoreWritesBegin();
+ TsanHappensBefore(&flag);
+ flag = Done;
+ TsanIgnoreWritesEnd();
+ } else {
+ // Wait until any thread doing the call has finished.
+ sys::cas_flag tmp = flag;
+ sys::MemoryFence();
+ while (tmp != Done) {
+ tmp = flag;
+ sys::MemoryFence();
+ }
+ }
+ TsanHappensAfter(&flag);
+#endif
+ }
}
#endif