LLVM  8.0.0svn
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;
25 
26 static void initializeMutex() {
27  ManagedStaticMutex = new sys::Mutex();
28 }
29 
32  return ManagedStaticMutex;
33 }
34 
36  void (*Deleter)(void*)) const {
37  assert(Creator);
38  if (llvm_is_multithreaded()) {
40 
41  if (!Ptr.load(std::memory_order_relaxed)) {
42  void *Tmp = Creator();
43 
44  Ptr.store(Tmp, std::memory_order_release);
45  DeleterFn = Deleter;
46 
47  // Add to list of managed statics.
48  Next = StaticList;
49  StaticList = this;
50  }
51  } else {
52  assert(!Ptr && !DeleterFn && !Next &&
53  "Partially initialized ManagedStatic!?");
54  Ptr = Creator();
55  DeleterFn = Deleter;
56 
57  // Add to list of managed statics.
58  Next = StaticList;
59  StaticList = this;
60  }
61 }
62 
64  assert(DeleterFn && "ManagedStatic not initialized correctly!");
65  assert(StaticList == this &&
66  "Not destroyed in reverse order of construction?");
67  // Unlink from list.
68  StaticList = Next;
69  Next = nullptr;
70 
71  // Destroy memory.
72  DeleterFn(Ptr);
73 
74  // Cleanup.
75  Ptr = nullptr;
76  DeleterFn = nullptr;
77 }
78 
79 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
82 
83  while (StaticList)
84  StaticList->destroy();
85 }
This class represents lattice values for constants.
Definition: AllocatorList.h:24
const ManagedStaticBase * Next
Definition: ManagedStatic.h:43
static sys::Mutex Lock
static sys::Mutex * getManagedStaticMutex()
static sys::Mutex * ManagedStaticMutex
SmartMutex< false > Mutex
Mutex - A standard, always enforced mutex.
Definition: Mutex.h:139
static llvm::once_flag mutex_init_flag
static void initializeMutex()
void(* DeleterFn)(void *)
Definition: ManagedStatic.h:42
void llvm_shutdown()
llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
void call_once(once_flag &flag, Function &&F, Args &&... ArgList)
Execute the function specified as a parameter once.
Definition: Threading.h:100
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:31
std::once_flag once_flag
Definition: Threading.h:70
std::atomic< void * > Ptr
Definition: ManagedStatic.h:41
ManagedStaticBase - Common base class for ManagedStatic instances.
Definition: ManagedStatic.h:37
static const ManagedStaticBase * StaticList
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void RegisterManagedStatic(void *(*creator)(), void(*deleter)(void *)) const