forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadGlobals.cpp
More file actions
83 lines (68 loc) · 2.78 KB
/
Copy pathThreadGlobals.cpp
File metadata and controls
83 lines (68 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "pch.h"
#include "Public/winget/ThreadGlobals.h"
namespace AppInstaller::ThreadLocalStorage
{
using namespace AppInstaller::Logging;
// Set and return Globals for Current Thread
static ThreadGlobals* SetOrGetThreadGlobals(bool setThreadGlobals, ThreadGlobals* pThreadGlobals = nullptr);
ThreadGlobals::ThreadGlobals(ThreadGlobals& parent, create_sub_thread_globals_t)
{
parent.Initialize();
m_pDiagnosticLogger = parent.m_pDiagnosticLogger;
m_pTelemetryLogger = parent.m_pTelemetryLogger->CreateSubTraceLogger();
// Flip the initialization flag
std::call_once(m_loggerInitOnceFlag, []() {});
}
DiagnosticLogger& ThreadGlobals::GetDiagnosticLogger()
{
return *(m_pDiagnosticLogger);
}
TelemetryTraceLogger& ThreadGlobals::GetTelemetryLogger()
{
return *(m_pTelemetryLogger);
}
std::unique_ptr<PreviousThreadGlobals> ThreadGlobals::SetForCurrentThread()
{
Initialize();
std::unique_ptr<PreviousThreadGlobals> p_prevThreadGlobals = std::make_unique<PreviousThreadGlobals>(SetOrGetThreadGlobals(true, this));
return p_prevThreadGlobals;
}
void ThreadGlobals::Initialize()
{
try
{
std::call_once(m_loggerInitOnceFlag, [this]()
{
m_pDiagnosticLogger = std::make_unique<DiagnosticLogger>();
m_pTelemetryLogger = std::make_unique<TelemetryTraceLogger>();
// The above make_unique for TelemetryTraceLogger will either create an object or will throw which is caught below.
m_pTelemetryLogger->Initialize();
});
}
catch (...)
{
// May throw std::system_error if any condition prevents calls to call_once from executing as specified
// May throw std::bad_alloc or any exception thrown by the constructor of TelemetryTraceLogger
// Loggers are best effort and shouldn't block core functionality. So eat up the exceptions here
}
}
ThreadGlobals* ThreadGlobals::GetForCurrentThread()
{
return SetOrGetThreadGlobals(false);
}
ThreadGlobals* SetOrGetThreadGlobals(bool setThreadGlobals, ThreadGlobals* pThreadGlobals)
{
thread_local AppInstaller::ThreadLocalStorage::ThreadGlobals* t_pThreadGlobals = nullptr;
if (setThreadGlobals == true)
{
AppInstaller::ThreadLocalStorage::ThreadGlobals* previous_pThreadGlobals = t_pThreadGlobals;
t_pThreadGlobals = pThreadGlobals;
return previous_pThreadGlobals;
}
return t_pThreadGlobals;
}
PreviousThreadGlobals::~PreviousThreadGlobals()
{
std::ignore = SetOrGetThreadGlobals(true, m_previous);
}
}