Skip to content

Commit 69947c2

Browse files
authored
Add blocking wait to comms creation (#141)
The current code has a racy initialization of the VkDevice communications module, and can crash if a second device sees a valid commsModule but gets a nullptr for commsWrapper which is still being built by the first context. This change adds locking around comms creation to ensure that any device sees a completely inited comms stack. Lock is always taken for sake of simplicity - this isn't a performance critical code path.
1 parent 562b02d commit 69947c2

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

layer_gpu_profile/source/device.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ std::unique_ptr<Comms::CommsModule> Device::commsModule;
5050
/* See header for documentation. */
5151
std::unique_ptr<ProfileComms> Device::commsWrapper;
5252

53+
extern std::mutex g_vulkanLock;
54+
5355
/* See header for documentation. */
5456
void Device::store(VkDevice handle, std::unique_ptr<Device> device)
5557
{
@@ -119,10 +121,13 @@ Device::Device(Instance* _instance,
119121
}
120122

121123
// Init the shared comms module for the first device built
122-
if (!commsModule)
123124
{
124-
commsModule = std::make_unique<Comms::CommsModule>("lglcomms");
125-
commsWrapper = std::make_unique<ProfileComms>(*commsModule);
125+
std::lock_guard<std::mutex> lock { g_vulkanLock };
126+
if (!commsModule)
127+
{
128+
commsModule = std::make_unique<Comms::CommsModule>("lglcomms");
129+
commsWrapper = std::make_unique<ProfileComms>(*commsModule);
130+
}
126131
}
127132

128133
// Create events for CPU<>GPU synchronization

layer_gpu_timeline/source/device.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ std::unique_ptr<Comms::CommsModule> Device::commsModule;
4646
/* See header for documentation. */
4747
std::unique_ptr<TimelineComms> Device::commsWrapper;
4848

49+
extern std::mutex g_vulkanLock;
50+
4951
/* See header for documentation. */
5052
void Device::store(VkDevice handle, std::unique_ptr<Device> device)
5153
{
@@ -115,10 +117,13 @@ Device::Device(Instance* _instance,
115117
}
116118

117119
// Init the shared comms module for the first device built
118-
if (!commsModule)
119120
{
120-
commsModule = std::make_unique<Comms::CommsModule>("lglcomms");
121-
commsWrapper = std::make_unique<TimelineComms>(*commsModule);
121+
std::lock_guard<std::mutex> lock { g_vulkanLock };
122+
if (!commsModule)
123+
{
124+
commsModule = std::make_unique<Comms::CommsModule>("lglcomms");
125+
commsWrapper = std::make_unique<TimelineComms>(*commsModule);
126+
}
122127
}
123128

124129
// Determine the driver version and emit the preamble message

0 commit comments

Comments
 (0)