LCOV - code coverage report
Current view: top level - lib/Support - ManagedStatic.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 27 31 87.1 %
Date: 2017-09-14 15:23:50 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- ManagedStatic.cpp - Static Global wrapper -------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file implements the ManagedStatic class and llvm_shutdown().
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/Support/ManagedStatic.h"
      15             : #include "llvm/Config/config.h"
      16             : #include "llvm/Support/Mutex.h"
      17             : #include "llvm/Support/MutexGuard.h"
      18             : #include "llvm/Support/Threading.h"
      19             : #include <cassert>
      20             : using namespace llvm;
      21             : 
      22             : static const ManagedStaticBase *StaticList = nullptr;
      23             : static sys::Mutex *ManagedStaticMutex = nullptr;
      24             : static llvm::once_flag mutex_init_flag;
      25             : 
      26      121345 : static void initializeMutex() {
      27      242690 :   ManagedStaticMutex = new sys::Mutex();
      28      121345 : }
      29             : 
      30             : static sys::Mutex* getManagedStaticMutex() {
      31             :   // We need to use a function local static here, since this can get called
      32             :   // during a static constructor and we need to guarantee that it's initialized
      33             :   // correctly.
      34     1052906 :   llvm::call_once(mutex_init_flag, initializeMutex);
      35     1052906 :   return ManagedStaticMutex;
      36             : }
      37             : 
      38      990540 : void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
      39             :                                               void (*Deleter)(void*)) const {
      40             :   assert(Creator);
      41      990540 :   if (llvm_is_multithreaded()) {
      42     2971620 :     MutexGuard Lock(*getManagedStaticMutex());
      43             : 
      44     1981080 :     if (!Ptr.load(std::memory_order_relaxed)) {
      45      990539 :       void *Tmp = Creator();
      46             : 
      47     1981078 :       Ptr.store(Tmp, std::memory_order_release);
      48      990539 :       DeleterFn = Deleter;
      49             :       
      50             :       // Add to list of managed statics.
      51      990539 :       Next = StaticList;
      52      990539 :       StaticList = this;
      53             :     }
      54             :   } else {
      55             :     assert(!Ptr && !DeleterFn && !Next &&
      56             :            "Partially initialized ManagedStatic!?");
      57           0 :     Ptr = Creator();
      58           0 :     DeleterFn = Deleter;
      59             :   
      60             :     // Add to list of managed statics.
      61           0 :     Next = StaticList;
      62           0 :     StaticList = this;
      63             :   }
      64      990540 : }
      65             : 
      66      662237 : void ManagedStaticBase::destroy() const {
      67             :   assert(DeleterFn && "ManagedStatic not initialized correctly!");
      68             :   assert(StaticList == this &&
      69             :          "Not destroyed in reverse order of construction?");
      70             :   // Unlink from list.
      71      662237 :   StaticList = Next;
      72      662237 :   Next = nullptr;
      73             : 
      74             :   // Destroy memory.
      75     1324474 :   DeleterFn(Ptr);
      76             :   
      77             :   // Cleanup.
      78     1324474 :   Ptr = nullptr;
      79      662237 :   DeleterFn = nullptr;
      80      662237 : }
      81             : 
      82             : /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
      83       62366 : void llvm::llvm_shutdown() {
      84      124732 :   MutexGuard Lock(*getManagedStaticMutex());
      85             : 
      86     1386840 :   while (StaticList)
      87      662237 :     StaticList->destroy();
      88       62366 : }

Generated by: LCOV version 1.13