Skip to content

Commit 2f5d8e2

Browse files
committed
diagnostics thread safety and order independence
1 parent 51476af commit 2f5d8e2

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

IntelPresentMon/PresentMonAPI2/PresentMonDiagnostics.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,8 @@ extern "C" {
8484
bool enableLocation;
8585
};
8686

87-
// all pmDiagnostic functions must NOT be invoked concurrently
88-
// the only exceptions are pmDiagnosticSignalWaiter which can be called concurrently with the other functions
89-
// from any number of threads, and pmDiagnosticEnqueueMessage similarly
90-
// ideally, after setup is completed only one thread should be calling the get/dequeue/wait functions
87+
// NOTE: pmDiagnosticDequeueMessage and pmDiagnosticWaitForMessage must both be accessed
88+
// from the same single thread, never concurrently from multiple threads
9189

9290
// initialize and configure the diagnostic system; passing in nullptr yield default config
9391
PRESENTMON_API2_EXPORT PM_STATUS pmDiagnosticSetup(const PM_DIAGNOSTIC_CONFIGURATION* pConfig);

IntelPresentMon/PresentMonMiddleware/LogSetup.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ namespace pmon::util::log
1919
{
2020
namespace
2121
{
22+
// cache reference to diagnostics system here for C-Api interaction and attachment to new channels
23+
std::shared_ptr<DiagnosticDriver> pDiagnostics_;
24+
std::mutex diagnosticsMtx_;
25+
2226
// creates an independent logging channel or a copy channel, resets log level to default
2327
std::shared_ptr<IChannel> MakeChannel_(std::shared_ptr<IChannel> pCopyTargetChannel = {})
2428
{
@@ -46,6 +50,13 @@ namespace pmon::util::log
4650
pChannel->AttachComponent(std::make_shared<MsvcDebugDriver>(pFormatter), "drv:dbg");
4751
pChannel->AttachComponent(std::make_shared<BasicFileDriver>(pFormatter, pFileStrategy), "drv:file");
4852
}
53+
// connect diagnostic driver if present to new channel
54+
{
55+
std::lock_guard lk{ diagnosticsMtx_ };
56+
if (pDiagnostics_) {
57+
pChannel->AttachComponent(pDiagnostics_, "drv:diag");
58+
}
59+
}
4960
return pChannel;
5061
}
5162
// creates a channel for debug diagnostic purposes
@@ -54,14 +65,6 @@ namespace pmon::util::log
5465
GlobalPolicy::Get().SetLogLevelDefault();
5566
// channel
5667
auto pChannel = std::make_shared<Channel>();
57-
// error resolver
58-
auto pErrorResolver = std::make_shared<ErrorCodeResolver>();
59-
pErrorResolver->AddProvider(std::make_unique<win::HrErrorCodeProvider>());
60-
pErrorResolver->AddProvider(std::make_unique<pmapi::PmErrorCodeProvider>());
61-
// error resolving policy
62-
auto pErrPolicy = std::make_shared<ErrorCodeResolvePolicy>();
63-
pErrPolicy->SetResolver(std::move(pErrorResolver));
64-
pChannel->AttachComponent(std::move(pErrPolicy));
6568
// make and add the line-tracking policy
6669
pChannel->AttachComponent(std::make_shared<LinePolicy>());
6770
// configure drivers
@@ -74,8 +77,6 @@ namespace pmon::util::log
7477
GlobalPolicy::Get().SetLogLevel(Level::None);
7578
return {};
7679
}
77-
// cache reference to diagnostics system here for C-Api interaction
78-
std::shared_ptr<DiagnosticDriver> pDiagnostics_;
7980
}
8081

8182
std::shared_ptr<IChannel> GetDefaultChannel() noexcept
@@ -95,19 +96,23 @@ namespace pmon::util::log
9596

9697
void SetupDiagnosticLayer(const PM_DIAGNOSTIC_CONFIGURATION* pConfig)
9798
{
98-
if (!pDiagnostics_) {
99+
// create / replace diagnostic driver
100+
{
101+
std::lock_guard lk{ diagnosticsMtx_ };
99102
pDiagnostics_ = std::make_shared<log::DiagnosticDriver>(pConfig);
100103
}
104+
// attach to existing channel if present
101105
if (auto pChan = GetDefaultChannel()) {
102106
pChan->AttachComponent(pDiagnostics_, "drv:diag");
103107
}
104-
else {
108+
else { // otherwise make a minimal channel for diagnostic handling
105109
InjectDefaultChannel(MakeDiagnosticChannel_(pDiagnostics_));
106110
}
107111
}
108112

109113
std::shared_ptr<class DiagnosticDriver> GetDiagnostics()
110114
{
115+
std::lock_guard lk{ diagnosticsMtx_ };
111116
return pDiagnostics_;
112117
}
113118
}

0 commit comments

Comments
 (0)