Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions loader/wsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ VKAPI_ATTR void VKAPI_CALL terminator_DestroySurfaceKHR(VkInstance instance, VkS
return;
}
#endif // VK_USE_PLATFORM_MACOS_MVK
// Every surface creation terminator holds loader_lock while it reserves a slot in loader_inst->surfaces_list and
// grows each driver's surface_list, either of which can reallocate the list. vkDestroySurfaceKHR only requires
// the surface itself to be externally synchronized, so take the same lock before touching that shared state.
loader_platform_thread_lock_mutex(&loader_lock);
for (struct loader_icd_term *icd_term = loader_inst->icd_terms; icd_term != NULL; icd_term = icd_term->next) {
if (icd_term->enabled_instance_extensions.khr_surface &&
icd_term->scanned_icd->interface_version >= ICD_VER_SUPPORTS_ICD_SURFACE_KHR &&
Expand All @@ -360,6 +364,7 @@ VKAPI_ATTR void VKAPI_CALL terminator_DestroySurfaceKHR(VkInstance instance, VkS
loader_instance_heap_free(loader_inst, icd_surface->create_info);
}
loader_release_object_from_list(&loader_inst->surfaces_list, icd_surface->surface_index);
loader_platform_thread_unlock_mutex(&loader_lock);
// NOLINTNEXTLINE(performance-no-int-to-ptr) - decoding the loader-internal pointer out of the handle
loader_instance_heap_free(loader_inst, (void *)(uintptr_t)surface);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/loader_threading_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,30 @@ TEST(Threading, SetDebugUtilsNameCreateDestroyLoop) {
set_debug_name_threads[i].join();
}
}

void create_destroy_surface_loop(FrameworkEnvironment* env, uint32_t num_loops, InstWrapper* inst) {
for (uint32_t i = 0; i < num_loops; i++) {
VkSurfaceKHR surface{};
ASSERT_EQ(VK_SUCCESS, create_surface(*inst, surface));
env->vulkan_functions.vkDestroySurfaceKHR(inst->inst, surface, nullptr);
}
}

TEST(Threading, SurfaceCreateDestroyLoop) {
const auto processor_count = std::thread::hardware_concurrency();
uint32_t num_loops = 100;
FrameworkEnvironment env{FrameworkSettings{}.set_log_filter("")};
env.add_icd(TEST_ICD_PATH_VERSION_2).setup_WSI().add_physical_device({});

InstWrapper inst{env.vulkan_functions};
inst.create_info.setup_WSI();
inst.CheckCreate();

std::vector<std::thread> surface_threads;
for (uint32_t i = 0; i < processor_count; i++) {
surface_threads.emplace_back(create_destroy_surface_loop, &env, num_loops, &inst);
}
for (uint32_t i = 0; i < processor_count; i++) {
surface_threads[i].join();
}
}
Loading