From ca37b511fa1e542c936402c0023455fd9a444e76 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Fri, 15 May 2009 08:22:07 +0000 Subject: [PATCH] Factor out some common code, fix more typoes, and add a memory fence that might be needed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@71853 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/ManagedStatic.h | 48 ++++++++++------------------ 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/include/llvm/Support/ManagedStatic.h b/include/llvm/Support/ManagedStatic.h index c1ff0fef935..a504be31d8a 100644 --- a/include/llvm/Support/ManagedStatic.h +++ b/include/llvm/Support/ManagedStatic.h @@ -51,51 +51,35 @@ public: /// template class ManagedStatic : public ManagedStaticBase { +private: + void checkInit() { + sys::cas_flag OldFlag = sys::CompareAndSwap(&InitFlag, 1, 0); + if (OldFlag == 0) { + LazyInit(); + sys::MemoryFence(); + InitFlag = 2; + } else if (OldFlag == 1) { + while (InitFlag == 1) ; + sys::MemoryFence(); + } + } public: // Accessors. C &operator*() { - sys::cas_flag OldFlag = sys::CompareAndSwap(&InitFlag, 1, 0); - if (OldFlag == 0) { - LazyInit(); - sys::MemoryFence(); - InitFlag = 2; - } else if (OldFlag == 1) - while (OldFlag == 1) ; - + checkInit(); return *static_cast(Ptr); } C *operator->() { - sys::cas_flag OldFlag = sys::CompareAndSwap(&InitFlag, 1, 0); - if (OldFlag == 0) { - LazyInit(); - sys::MemoryFence(); - InitFlag = 2; - } else if (OldFlag == 1) - while (OldFlag == 1) ; - + checkInit(); return static_cast(Ptr); } const C &operator*() const { - sys::cas_flag OldFlag = sys::CompareAndSwap(&InitFlag, 1, 0); - if (OldFlag == 0) { - LazyInit(); - sys::MemoryFence(); - InitFlag = 2; - } else if (OldFlag == 1) - while (InitFlag == 1) ; - + checkInit(); return *static_cast(Ptr); } const C *operator->() const { - sys::cas_flag OldFlag = sys::CompareAndSwap(&InitFlag, 1, 0); - if (OldFlag == 0) { - LazyInit(); - sys::MemoryFence(); - InitFlag = 2; - } else if (OldFlag == 1) - while (OldFlag == 1) ; - + checkInit(); return static_cast(Ptr); }