6f5cf6bcb1a8f08594ba4e6afa066d4227f71e20
[oota-llvm.git] / lib / Support / ManagedStatic.cpp
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/Atomic.h"
17 #include "llvm/Support/MutexGuard.h"
18 #include <cassert>
19 using namespace llvm;
20
21 static const ManagedStaticBase *StaticList = nullptr;
22
23 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
24                                               void (*Deleter)(void*)) const {
25   assert(Creator);
26   if (llvm_is_multithreaded()) {
27     llvm::MutexGuard Lock(llvm::llvm_get_global_lock());
28
29     if (!Ptr) {
30       void* tmp = Creator();
31
32       TsanHappensBefore(this);
33       sys::MemoryFence();
34
35       // This write is racy against the first read in the ManagedStatic
36       // accessors. The race is benign because it does a second read after a
37       // memory fence, at which point it isn't possible to get a partial value.
38       TsanIgnoreWritesBegin();
39       Ptr = tmp;
40       TsanIgnoreWritesEnd();
41       DeleterFn = Deleter;
42       
43       // Add to list of managed statics.
44       Next = StaticList;
45       StaticList = this;
46     }
47   } else {
48     assert(!Ptr && !DeleterFn && !Next &&
49            "Partially initialized ManagedStatic!?");
50     Ptr = Creator();
51     DeleterFn = Deleter;
52   
53     // Add to list of managed statics.
54     Next = StaticList;
55     StaticList = this;
56   }
57 }
58
59 void ManagedStaticBase::destroy() const {
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 = nullptr;
66
67   // Destroy memory.
68   DeleterFn(Ptr);
69   
70   // Cleanup.
71   Ptr = nullptr;
72   DeleterFn = nullptr;
73 }
74
75 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
76 void llvm::llvm_shutdown() {
77   while (StaticList)
78     StaticList->destroy();
79
80   if (llvm_is_multithreaded()) llvm_stop_multithreaded();
81 }