LLVM  4.0.0
ManagedStatic.cpp
Go to the documentation of this file.
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 
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Mutex.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 LLVM_DEFINE_ONCE_FLAG(mutex_init_flag);
25 
26 static void initializeMutex() {
28 }
29 
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  llvm::call_once(mutex_init_flag, initializeMutex);
35  return ManagedStaticMutex;
36 }
37 
39  void (*Deleter)(void*)) const {
40  assert(Creator);
41  if (llvm_is_multithreaded()) {
43 
44  if (!Ptr.load(std::memory_order_relaxed)) {
45  void *Tmp = Creator();
46 
47  Ptr.store(Tmp, std::memory_order_release);
48  DeleterFn = Deleter;
49 
50  // Add to list of managed statics.
51  Next = StaticList;
52  StaticList = this;
53  }
54  } else {
55  assert(!Ptr && !DeleterFn && !Next &&
56  "Partially initialized ManagedStatic!?");
57  Ptr = Creator();
58  DeleterFn = Deleter;
59 
60  // Add to list of managed statics.
61  Next = StaticList;
62  StaticList = this;
63  }
64 }
65 
67  assert(DeleterFn && "ManagedStatic not initialized correctly!");
68  assert(StaticList == this &&
69  "Not destroyed in reverse order of construction?");
70  // Unlink from list.
71  StaticList = Next;
72  Next = nullptr;
73 
74  // Destroy memory.
75  DeleterFn(Ptr);
76 
77  // Cleanup.
78  Ptr = nullptr;
79  DeleterFn = nullptr;
80 }
81 
82 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
85 
86  while (StaticList)
88 }
const ManagedStaticBase * Next
Definition: ManagedStatic.h:46
static sys::Mutex Lock
static sys::Mutex * getManagedStaticMutex()
static sys::Mutex * ManagedStaticMutex
SmartMutex< false > Mutex
Mutex - A standard, always enforced mutex.
Definition: Mutex.h:138
void call_once(once_flag &flag, Function &&F, Args &&...ArgList)
Execute the function specified as a parameter once.
Definition: Threading.h:91
void(* DeleterFn)(void *)
Definition: ManagedStatic.h:45
static void initializeMutex()
void llvm_shutdown()
llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
Instances of this class acquire a given Mutex Lock when constructed and hold that lock until destruct...
Definition: MutexGuard.h:27
bool llvm_is_multithreaded()
Returns true if LLVM is compiled with support for multi-threading, and false otherwise.
Definition: Threading.cpp:25
std::atomic< void * > Ptr
Definition: ManagedStatic.h:44
LLVM_DEFINE_ONCE_FLAG(mutex_init_flag)
void RegisterManagedStatic(void *(*creator)(), void(*deleter)(void *)) const
ManagedStaticBase - Common base class for ManagedStatic instances.
Definition: ManagedStatic.h:40
static const ManagedStaticBase * StaticList
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())