LLVM  9.0.0svn
ManagedStatic.cpp
Go to the documentation of this file.
1 //===-- ManagedStatic.cpp - Static Global wrapper -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the ManagedStatic class and llvm_shutdown().
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/Config/config.h"
15 #include "llvm/Support/Mutex.h"
17 #include "llvm/Support/Threading.h"
18 #include <cassert>
19 using namespace llvm;
20 
21 static const ManagedStaticBase *StaticList = nullptr;
22 static sys::Mutex *ManagedStaticMutex = nullptr;
24 
25 static void initializeMutex() {
26  ManagedStaticMutex = new sys::Mutex();
27 }
28 
31  return ManagedStaticMutex;
32 }
33 
35  void (*Deleter)(void*)) const {
36  assert(Creator);
37  if (llvm_is_multithreaded()) {
39 
40  if (!Ptr.load(std::memory_order_relaxed)) {
41  void *Tmp = Creator();
42 
43  Ptr.store(Tmp, std::memory_order_release);
44  DeleterFn = Deleter;
45 
46  // Add to list of managed statics.
47  Next = StaticList;
48  StaticList = this;
49  }
50  } else {
51  assert(!Ptr && !DeleterFn && !Next &&
52  "Partially initialized ManagedStatic!?");
53  Ptr = Creator();
54  DeleterFn = Deleter;
55 
56  // Add to list of managed statics.
57  Next = StaticList;
58  StaticList = this;
59  }
60 }
61 
63  assert(DeleterFn && "ManagedStatic not initialized correctly!");
64  assert(StaticList == this &&
65  "Not destroyed in reverse order of construction?");
66  // Unlink from list.
67  StaticList = Next;
68  Next = nullptr;
69 
70  // Destroy memory.
71  DeleterFn(Ptr);
72 
73  // Cleanup.
74  Ptr = nullptr;
75  DeleterFn = nullptr;
76 }
77 
78 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
81 
82  while (StaticList)
83  StaticList->destroy();
84 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const ManagedStaticBase * Next
Definition: ManagedStatic.h:42
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
static llvm::once_flag mutex_init_flag
static void initializeMutex()
void(* DeleterFn)(void *)
Definition: ManagedStatic.h:41
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:102
Instances of this class acquire a given Mutex Lock when constructed and hold that lock until destruct...
Definition: MutexGuard.h:26
bool llvm_is_multithreaded()
Returns true if LLVM is compiled with support for multi-threading, and false otherwise.
Definition: Threading.cpp:30
std::once_flag once_flag
Definition: Threading.h:72
std::atomic< void * > Ptr
Definition: ManagedStatic.h:40
ManagedStaticBase - Common base class for ManagedStatic instances.
Definition: ManagedStatic.h:36
static const ManagedStaticBase * StaticList
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void RegisterManagedStatic(void *(*creator)(), void(*deleter)(void *)) const