blob: 6a1c2a545a8d87f2a59ffed59c0542aa30caf32b [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"
Chris Lattner9daff492006-09-28 00:31:55 +000017#include <cassert>
18using namespace llvm;
19
Craig Topperc10719f2014-04-07 04:17:22 +000020static const ManagedStaticBase *StaticList = nullptr;
Chris Lattner9daff492006-09-28 00:31:55 +000021
Owen Andersonb9a4a572009-05-20 00:39:20 +000022void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
Chris Lattner9daff492006-09-28 00:31:55 +000023 void (*Deleter)(void*)) const {
David Blaikie5b015932014-04-17 20:30:35 +000024 assert(Creator);
Owen Anderson4cb4b612009-06-16 17:33:51 +000025 if (llvm_is_multithreaded()) {
26 llvm_acquire_global_lock();
Owen Andersonb9a4a572009-05-20 00:39:20 +000027
Craig Topper8d399f82014-04-09 04:20:00 +000028 if (!Ptr) {
David Blaikie5b015932014-04-17 20:30:35 +000029 void* tmp = Creator();
Owen Andersonb9a4a572009-05-20 00:39:20 +000030
Nick Lewyckyfe856112011-11-14 20:50:16 +000031 TsanHappensBefore(this);
Benjamin Kramer17388a62014-03-03 18:02:34 +000032 sys::MemoryFence();
Nick Lewyckyfe856112011-11-14 20:50:16 +000033
34 // This write is racy against the first read in the ManagedStatic
35 // accessors. The race is benign because it does a second read after a
36 // memory fence, at which point it isn't possible to get a partial value.
37 TsanIgnoreWritesBegin();
Owen Andersonb9a4a572009-05-20 00:39:20 +000038 Ptr = tmp;
Nick Lewyckyfe856112011-11-14 20:50:16 +000039 TsanIgnoreWritesEnd();
Owen Andersonb9a4a572009-05-20 00:39:20 +000040 DeleterFn = Deleter;
41
42 // Add to list of managed statics.
43 Next = StaticList;
44 StaticList = this;
45 }
46
Owen Anderson4cb4b612009-06-16 17:33:51 +000047 llvm_release_global_lock();
Owen Andersonb9a4a572009-05-20 00:39:20 +000048 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +000049 assert(!Ptr && !DeleterFn && !Next &&
Bill Wendling09f17a82009-05-30 01:09:53 +000050 "Partially initialized ManagedStatic!?");
David Blaikie5b015932014-04-17 20:30:35 +000051 Ptr = Creator();
Owen Andersonb9a4a572009-05-20 00:39:20 +000052 DeleterFn = Deleter;
Chris Lattner9daff492006-09-28 00:31:55 +000053
Owen Andersonb9a4a572009-05-20 00:39:20 +000054 // Add to list of managed statics.
55 Next = StaticList;
56 StaticList = this;
57 }
Chris Lattner9daff492006-09-28 00:31:55 +000058}
59
60void ManagedStaticBase::destroy() const {
Chris Lattner4510c992007-02-20 06:18:57 +000061 assert(DeleterFn && "ManagedStatic not initialized correctly!");
Chris Lattner9daff492006-09-28 00:31:55 +000062 assert(StaticList == this &&
63 "Not destroyed in reverse order of construction?");
64 // Unlink from list.
65 StaticList = Next;
Craig Topperc10719f2014-04-07 04:17:22 +000066 Next = nullptr;
Chris Lattner9daff492006-09-28 00:31:55 +000067
68 // Destroy memory.
69 DeleterFn(Ptr);
70
71 // Cleanup.
Craig Topperc10719f2014-04-07 04:17:22 +000072 Ptr = nullptr;
73 DeleterFn = nullptr;
Chris Lattner9daff492006-09-28 00:31:55 +000074}
75
76/// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
Chris Lattneradf409532006-09-29 18:43:14 +000077void llvm::llvm_shutdown() {
Chris Lattner9daff492006-09-28 00:31:55 +000078 while (StaticList)
79 StaticList->destroy();
Owen Andersonb9a4a572009-05-20 00:39:20 +000080
Owen Anderson4cb4b612009-06-16 17:33:51 +000081 if (llvm_is_multithreaded()) llvm_stop_multithreaded();
Chris Lattner9daff492006-09-28 00:31:55 +000082}