Coverage Report

Created: 2020-06-26 05:44

/home/arjun/llvm-project/llvm/lib/Support/ManagedStatic.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ManagedStatic.cpp - Static Global wrapper -------------------------===//
2
//
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
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the ManagedStatic class and llvm_shutdown().
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Support/ManagedStatic.h"
14
#include "llvm/Config/config.h"
15
#include "llvm/Support/Threading.h"
16
#include <cassert>
17
#include <mutex>
18
using namespace llvm;
19
20
static const ManagedStaticBase *StaticList = nullptr;
21
static std::recursive_mutex *ManagedStaticMutex = nullptr;
22
static llvm::once_flag mutex_init_flag;
23
24
2
static void initializeMutex() {
25
2
  ManagedStaticMutex = new std::recursive_mutex();
26
2
}
27
28
8
static std::recursive_mutex *getManagedStaticMutex() {
29
8
  llvm::call_once(mutex_init_flag, initializeMutex);
30
8
  return ManagedStaticMutex;
31
8
}
32
33
void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
34
8
                                              void (*Deleter)(void*)) const {
35
8
  assert(Creator);
36
8
  if (llvm_is_multithreaded()) {
37
8
    std::lock_guard<std::recursive_mutex> Lock(*getManagedStaticMutex());
38
8
39
8
    if (!Ptr.load(std::memory_order_relaxed)) {
40
8
      void *Tmp = Creator();
41
8
42
8
      Ptr.store(Tmp, std::memory_order_release);
43
8
      DeleterFn = Deleter;
44
8
45
8
      // Add to list of managed statics.
46
8
      Next = StaticList;
47
8
      StaticList = this;
48
8
    }
49
8
  } else {
50
0
    assert(!Ptr && !DeleterFn && !Next &&
51
0
           "Partially initialized ManagedStatic!?");
52
0
    Ptr = Creator();
53
0
    DeleterFn = Deleter;
54
0
55
0
    // Add to list of managed statics.
56
0
    Next = StaticList;
57
0
    StaticList = this;
58
0
  }
59
8
}
60
61
0
void ManagedStaticBase::destroy() const {
62
0
  assert(DeleterFn && "ManagedStatic not initialized correctly!");
63
0
  assert(StaticList == this &&
64
0
         "Not destroyed in reverse order of construction?");
65
0
  // Unlink from list.
66
0
  StaticList = Next;
67
0
  Next = nullptr;
68
0
69
0
  // Destroy memory.
70
0
  DeleterFn(Ptr);
71
0
72
0
  // Cleanup.
73
0
  Ptr = nullptr;
74
0
  DeleterFn = nullptr;
75
0
}
76
77
/// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
78
0
void llvm::llvm_shutdown() {
79
0
  std::lock_guard<std::recursive_mutex> Lock(*getManagedStaticMutex());
80
0
81
0
  while (StaticList)
82
0
    StaticList->destroy();
83
0
}