Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/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/Mutex.h"
16
#include "llvm/Support/MutexGuard.h"
17
#include "llvm/Support/Threading.h"
18
#include <cassert>
19
using namespace llvm;
20
21
static const ManagedStaticBase *StaticList = nullptr;
22
static sys::Mutex *ManagedStaticMutex = nullptr;
23
static llvm::once_flag mutex_init_flag;
24
25
139k
static void initializeMutex() {
26
139k
  ManagedStaticMutex = new sys::Mutex();
27
139k
}
28
29
1.39M
static sys::Mutex* getManagedStaticMutex() {
30
1.39M
  llvm::call_once(mutex_init_flag, initializeMutex);
31
1.39M
  return ManagedStaticMutex;
32
1.39M
}
33
34
void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
35
1.26M
                                              void (*Deleter)(void*)) const {
36
1.26M
  assert(Creator);
37
1.26M
  if (llvm_is_multithreaded()) {
38
1.26M
    MutexGuard Lock(*getManagedStaticMutex());
39
1.26M
40
1.26M
    if (!Ptr.load(std::memory_order_relaxed)) {
41
1.26M
      void *Tmp = Creator();
42
1.26M
43
1.26M
      Ptr.store(Tmp, std::memory_order_release);
44
1.26M
      DeleterFn = Deleter;
45
1.26M
46
1.26M
      // Add to list of managed statics.
47
1.26M
      Next = StaticList;
48
1.26M
      StaticList = this;
49
1.26M
    }
50
1.26M
  } else {
51
0
    assert(!Ptr && !DeleterFn && !Next &&
52
0
           "Partially initialized ManagedStatic!?");
53
0
    Ptr = Creator();
54
0
    DeleterFn = Deleter;
55
0
56
0
    // Add to list of managed statics.
57
0
    Next = StaticList;
58
0
    StaticList = this;
59
0
  }
60
1.26M
}
61
62
1.18M
void ManagedStaticBase::destroy() const {
63
1.18M
  assert(DeleterFn && "ManagedStatic not initialized correctly!");
64
1.18M
  assert(StaticList == this &&
65
1.18M
         "Not destroyed in reverse order of construction?");
66
1.18M
  // Unlink from list.
67
1.18M
  StaticList = Next;
68
1.18M
  Next = nullptr;
69
1.18M
70
1.18M
  // Destroy memory.
71
1.18M
  DeleterFn(Ptr);
72
1.18M
73
1.18M
  // Cleanup.
74
1.18M
  Ptr = nullptr;
75
1.18M
  DeleterFn = nullptr;
76
1.18M
}
77
78
/// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
79
127k
void llvm::llvm_shutdown() {
80
127k
  MutexGuard Lock(*getManagedStaticMutex());
81
127k
82
1.31M
  while (StaticList)
83
1.18M
    StaticList->destroy();
84
127k
}