[lld] r234733 - [ARM] Use std::call_once for thread safe initialization

Denis Protivensky dprotivensky at accesssoftek.com
Mon Apr 13 03:30:47 PDT 2015


I think we should introduce initialization method which will be called before parallel_for_each (that calls applyRelocation) to calculate all the needed values once without any
threading issues. As a positive side effect, it will also allow us to use const modifier for getters in TargetLayout. Thoughts?

- Denis.

On 04/13/2015 01:18 PM, Denis Protivensky wrote:

Author: denis-protivensky
Date: Mon Apr 13 05:18:21 2015
New Revision: 234733

URL: http://llvm.org/viewvc/llvm-project?rev=234733&view=rev
Log:
[ARM] Use std::call_once for thread safe initialization

Thanks to r234628 & r234631

Modified:
    lld/trunk/lib/ReaderWriter/ELF/ARM/ARMTargetHandler.h

Modified: lld/trunk/lib/ReaderWriter/ELF/ARM/ARMTargetHandler.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/ARM/ARMTargetHandler.h?rev=234733&r1=234732&r2=234733&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/ARM/ARMTargetHandler.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/ARM/ARMTargetHandler.h Mon Apr 13 05:18:21 2015
@@ -14,7 +14,6 @@
 #include "ARMRelocationHandler.h"
 #include "ELFReader.h"
 #include "TargetLayout.h"
-#include "llvm/ADT/Optional.h"

 namespace lld {
 namespace elf {
@@ -25,24 +24,24 @@ public:
   ARMTargetLayout(ARMLinkingContext &ctx) : TargetLayout<ELFT>(ctx) {}

   uint64_t getGOTSymAddr() {
-    if (!_gotSymAddr.hasValue()) {
-      AtomLayout *gotAtom = this->findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
-      _gotSymAddr = gotAtom ? gotAtom->_virtualAddr : 0;
-    }
-    return *_gotSymAddr;
+    std::call_once(_gotSymOnce, [this]() {
+      if (AtomLayout *gotAtom = this->findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_"))
+        _gotSymAddr = gotAtom->_virtualAddr;
+    });
+    return _gotSymAddr;
   }

   uint64_t getTPOffset() {
-    if (_tpOff.hasValue())
-      return *_tpOff;
-
-    for (const auto &phdr : *this->_programHeader) {
-      if (phdr->p_type == llvm::ELF::PT_TLS) {
-        _tpOff = llvm::RoundUpToAlignment(TCB_SIZE, phdr->p_align);
-        return *_tpOff;
+    std::call_once(_tpOffOnce, [this]() {
+      for (const auto &phdr : *this->_programHeader) {
+        if (phdr->p_type == llvm::ELF::PT_TLS) {
+          _tpOff = llvm::RoundUpToAlignment(TCB_SIZE, phdr->p_align);
+          break;
+        }
       }
-    }
-    llvm_unreachable("TLS segment not found");
+      assert(_tpOff != 0 && "TLS segment not found");
+    });
+    return _tpOff;
   }

   bool target1Rel() const { return this->_ctx.armTarget1Rel(); }
@@ -52,11 +51,10 @@ private:
   enum { TCB_SIZE = 0x8 };

 private:
-  // Cached value of the GOT origin symbol address.
-  llvm::Optional<uint64_t> _gotSymAddr;
-
-  // Cached value of the TLS offset from the $tp pointer.
-  llvm::Optional<uint64_t> _tpOff;
+  uint64_t _gotSymAddr = 0;
+  uint64_t _tpOff = 0;
+  std::once_flag _gotSymOnce;
+  std::once_flag _tpOffOnce;
 };

 class ARMTargetHandler final : public TargetHandler {


_______________________________________________
llvm-commits mailing list
llvm-commits at cs.uiuc.edu<mailto:llvm-commits at cs.uiuc.edu>
http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150413/d8572a62/attachment.html>


More information about the llvm-commits mailing list