1 //===-- ManagedStatic.cpp - Static Global wrapper -------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the ManagedStatic class and llvm_shutdown().
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/ManagedStatic.h"
14 #include "llvm/Config/config.h"
15 #include "llvm/Support/Threading.h"
20 static const ManagedStaticBase
*StaticList
= nullptr;
22 static std::recursive_mutex
*getManagedStaticMutex() {
23 static std::recursive_mutex m
;
27 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator
)(),
28 void (*Deleter
)(void*)) const {
30 if (llvm_is_multithreaded()) {
31 std::lock_guard
<std::recursive_mutex
> Lock(*getManagedStaticMutex());
33 if (!Ptr
.load(std::memory_order_relaxed
)) {
34 void *Tmp
= Creator();
36 Ptr
.store(Tmp
, std::memory_order_release
);
39 // Add to list of managed statics.
44 assert(!Ptr
&& !DeleterFn
&& !Next
&&
45 "Partially initialized ManagedStatic!?");
49 // Add to list of managed statics.
55 void ManagedStaticBase::destroy() const {
56 assert(DeleterFn
&& "ManagedStatic not initialized correctly!");
57 assert(StaticList
== this &&
58 "Not destroyed in reverse order of construction?");
71 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
72 /// IMPORTANT: it's only safe to call llvm_shutdown() in single thread,
73 /// without any other threads executing LLVM APIs.
74 /// llvm_shutdown() should be the last use of LLVM APIs.
75 void llvm::llvm_shutdown() {
77 StaticList
->destroy();