blob: b8fb2841e5256fdf5b08573b8b4368aecfba490f [file] [log] [blame]
Chris Lattner9daff492006-09-28 00:31:55 +00001//===-- ManagedStatic.cpp - Static Global wrapper -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner9daff492006-09-28 00:31:55 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ManagedStatic class and llvm_shutdown().
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/ManagedStatic.h"
Owen Andersonb9a4a572009-05-20 00:39:20 +000015#include "llvm/Config/config.h"
Benjamin Kramer17388a62014-03-03 18:02:34 +000016#include "llvm/Support/Atomic.h"
Zachary Turnerd119fa02014-06-21 00:24:51 +000017#include "llvm/Support/Mutex.h"
18#include "llvm/Support/MutexGuard.h"
Chris Lattner9daff492006-09-28 00:31:55 +000019#include <cassert>
20using namespace llvm;
21
Craig Topperc10719f2014-04-07 04:17:22 +000022static const ManagedStaticBase *StaticList = nullptr;
Chris Lattner9daff492006-09-28 00:31:55 +000023
Zachary Turnerd119fa02014-06-21 00:24:51 +000024static sys::Mutex& getManagedStaticMutex() {
25 // We need to use a function local static here, since this can get called
26 // during a static constructor and we need to guarantee that it's initialized
27 // correctly.
28 static sys::Mutex ManagedStaticMutex;
29 return ManagedStaticMutex;
Zachary Turner6ad24442014-06-19 16:17:42 +000030}
31
Owen Andersonb9a4a572009-05-20 00:39:20 +000032void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
Chris Lattner9daff492006-09-28 00:31:55 +000033 void (*Deleter)(void*)) const {
David Blaikie5b015932014-04-17 20:30:35 +000034 assert(Creator);
Owen Anderson4cb4b612009-06-16 17:33:51 +000035 if (llvm_is_multithreaded()) {
Zachary Turnerd119fa02014-06-21 00:24:51 +000036 MutexGuard Lock(getManagedStaticMutex());
Owen Andersonb9a4a572009-05-20 00:39:20 +000037
Craig Topper8d399f82014-04-09 04:20:00 +000038 if (!Ptr) {
David Blaikie5b015932014-04-17 20:30:35 +000039 void* tmp = Creator();
Owen Andersonb9a4a572009-05-20 00:39:20 +000040
Nick Lewyckyfe856112011-11-14 20:50:16 +000041 TsanHappensBefore(this);
Benjamin Kramer17388a62014-03-03 18:02:34 +000042 sys::MemoryFence();
Nick Lewyckyfe856112011-11-14 20:50:16 +000043
44 // This write is racy against the first read in the ManagedStatic
45 // accessors. The race is benign because it does a second read after a
46 // memory fence, at which point it isn't possible to get a partial value.
47 TsanIgnoreWritesBegin();
Owen Andersonb9a4a572009-05-20 00:39:20 +000048 Ptr = tmp;
Nick Lewyckyfe856112011-11-14 20:50:16 +000049 TsanIgnoreWritesEnd();
Owen Andersonb9a4a572009-05-20 00:39:20 +000050 DeleterFn = Deleter;
51
52 // Add to list of managed statics.
53 Next = StaticList;
54 StaticList = this;
55 }
Owen Andersonb9a4a572009-05-20 00:39:20 +000056 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +000057 assert(!Ptr && !DeleterFn && !Next &&
Bill Wendling09f17a82009-05-30 01:09:53 +000058 "Partially initialized ManagedStatic!?");
David Blaikie5b015932014-04-17 20:30:35 +000059 Ptr = Creator();
Owen Andersonb9a4a572009-05-20 00:39:20 +000060 DeleterFn = Deleter;
Chris Lattner9daff492006-09-28 00:31:55 +000061
Owen Andersonb9a4a572009-05-20 00:39:20 +000062 // Add to list of managed statics.
63 Next = StaticList;
64 StaticList = this;
65 }
Chris Lattner9daff492006-09-28 00:31:55 +000066}
67
68void ManagedStaticBase::destroy() const {
Chris Lattner4510c992007-02-20 06:18:57 +000069 assert(DeleterFn && "ManagedStatic not initialized correctly!");
Chris Lattner9daff492006-09-28 00:31:55 +000070 assert(StaticList == this &&
71 "Not destroyed in reverse order of construction?");
72 // Unlink from list.
73 StaticList = Next;
Craig Topperc10719f2014-04-07 04:17:22 +000074 Next = nullptr;
Chris Lattner9daff492006-09-28 00:31:55 +000075
76 // Destroy memory.
77 DeleterFn(Ptr);
78
79 // Cleanup.
Craig Topperc10719f2014-04-07 04:17:22 +000080 Ptr = nullptr;
81 DeleterFn = nullptr;
Chris Lattner9daff492006-09-28 00:31:55 +000082}
83
84/// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
Chris Lattneradf409532006-09-29 18:43:14 +000085void llvm::llvm_shutdown() {
Zachary Turnerd119fa02014-06-21 00:24:51 +000086 MutexGuard Lock(getManagedStaticMutex());
Zachary Turner6ad24442014-06-19 16:17:42 +000087
Chris Lattner9daff492006-09-28 00:31:55 +000088 while (StaticList)
89 StaticList->destroy();
Chris Lattner9daff492006-09-28 00:31:55 +000090}