LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/Atomic.h"
17 #include <cassert>
18 using namespace llvm;
19 
20 static const ManagedStaticBase *StaticList = 0;
21 
23  void (*Deleter)(void*)) const {
24  if (llvm_is_multithreaded()) {
26 
27  if (Ptr == 0) {
28  void* tmp = Creator ? Creator() : 0;
29 
30  TsanHappensBefore(this);
32 
33  // This write is racy against the first read in the ManagedStatic
34  // accessors. The race is benign because it does a second read after a
35  // memory fence, at which point it isn't possible to get a partial value.
37  Ptr = tmp;
39  DeleterFn = Deleter;
40 
41  // Add to list of managed statics.
42  Next = StaticList;
43  StaticList = this;
44  }
45 
47  } else {
48  assert(Ptr == 0 && DeleterFn == 0 && Next == 0 &&
49  "Partially initialized ManagedStatic!?");
50  Ptr = Creator ? Creator() : 0;
51  DeleterFn = Deleter;
52 
53  // Add to list of managed statics.
54  Next = StaticList;
55  StaticList = this;
56  }
57 }
58 
60  assert(DeleterFn && "ManagedStatic not initialized correctly!");
61  assert(StaticList == this &&
62  "Not destroyed in reverse order of construction?");
63  // Unlink from list.
64  StaticList = Next;
65  Next = 0;
66 
67  // Destroy memory.
68  DeleterFn(Ptr);
69 
70  // Cleanup.
71  Ptr = 0;
72  DeleterFn = 0;
73 }
74 
75 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
77  while (StaticList)
79 
81 }
void llvm_acquire_global_lock()
Definition: Threading.cpp:58
const ManagedStaticBase * Next
Definition: ManagedStatic.h:45
void llvm_release_global_lock()
Definition: Threading.cpp:62
#define TsanHappensBefore(cv)
Definition: Valgrind.h:52
void(* DeleterFn)(void *)
Definition: ManagedStatic.h:44
void llvm_shutdown()
llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
bool llvm_is_multithreaded()
Definition: Threading.cpp:54
void MemoryFence()
Definition: Atomic.cpp:28
void RegisterManagedStatic(void *(*creator)(), void(*deleter)(void *)) const
ManagedStaticBase - Common base class for ManagedStatic instances.
Definition: ManagedStatic.h:39
static const ManagedStaticBase * StaticList
#define TsanIgnoreWritesEnd()
Definition: Valgrind.h:64
#define TsanIgnoreWritesBegin()
Definition: Valgrind.h:60
void llvm_stop_multithreaded()
Definition: Threading.cpp:41