Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/KhronosGroup/Vulkan-Loader.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Giessen <charles@lunarg.com>2022-07-07 22:25:22 +0300
committerCharles Giessen <46324611+charles-lunarg@users.noreply.github.com>2022-07-07 23:41:01 +0300
commit0c7685be414961fdb310d76e3a41b3bb596c4071 (patch)
treec6566687387f140eb66216bbf05c7f10c53813f5
parentd8af0ae227fae96fd8e3d8c900ad2ebaf4f4e902 (diff)
Initialize properly when statically linked
Previously, the loader supported static linking. This capability was restricted to MacOS only, however the necessary functionality was never implemented. This commit adds the required code to properly initialize the loader when statically linked with MacOS Since the test framework was designed to dynamically load everything, it would require significant rearchitecting to support it. As such, a simple verification executable was added to the live_verification folder, instead of full support in the test framework.
-rw-r--r--loader/CMakeLists.txt1
-rw-r--r--loader/loader.h3
-rw-r--r--loader/vk_loader_platform.h15
-rw-r--r--tests/live_verification/CMakeLists.txt10
-rw-r--r--tests/live_verification/macos_static_loader_build.cpp88
5 files changed, 116 insertions, 1 deletions
diff --git a/loader/CMakeLists.txt b/loader/CMakeLists.txt
index 24074973c..3e99612bf 100644
--- a/loader/CMakeLists.txt
+++ b/loader/CMakeLists.txt
@@ -253,6 +253,7 @@ if(WIN32)
else()
if(APPLE AND BUILD_STATIC_LOADER)
add_library(vulkan STATIC ${NORMAL_LOADER_SRCS} ${OPT_LOADER_SRCS})
+ target_compile_definitions(vulkan PRIVATE BUILD_STATIC_LOADER)
else()
add_library(vulkan SHARED ${NORMAL_LOADER_SRCS} ${OPT_LOADER_SRCS})
endif()
diff --git a/loader/loader.h b/loader/loader.h
index 10fa14acd..3beb8b6cd 100644
--- a/loader/loader.h
+++ b/loader/loader.h
@@ -31,6 +31,9 @@
#include "loader_common.h"
+// Declare the once_init variable
+LOADER_PLATFORM_THREAD_ONCE_EXTERN_DEFINITION(once_init)
+
static inline VkPhysicalDevice loader_unwrap_physical_device(VkPhysicalDevice physicalDevice) {
struct loader_physical_device_tramp *phys_dev = (struct loader_physical_device_tramp *)physicalDevice;
if (PHYS_TRAMP_MAGIC_NUMBER != phys_dev->magic) {
diff --git a/loader/vk_loader_platform.h b/loader/vk_loader_platform.h
index 44e0730c5..864eaf4be 100644
--- a/loader/vk_loader_platform.h
+++ b/loader/vk_loader_platform.h
@@ -202,10 +202,23 @@ static inline bool loader_platform_is_path(const char *path) { return strchr(pat
// The once init functionality is not used when building a DLL on Windows. This is because there is no way to clean up the
// resources allocated by anything allocated by once init. This isn't a problem for static libraries, but it is for dynamic
// ones. When building a DLL, we use DllMain() instead to allow properly cleaning up resources.
+
+#if defined(__APPLE__) && defined(BUILD_STATIC_LOADER)
+static inline void loader_platform_thread_once_fn(pthread_once_t *ctl, void (*func)(void)) {
+ assert(func != NULL);
+ assert(ctl != NULL);
+ pthread_once(ctl, func);
+}
+#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) pthread_once_t var = PTHREAD_ONCE_INIT;
+#define LOADER_PLATFORM_THREAD_ONCE_EXTERN_DEFINITION(var) extern pthread_once_t var;
+#define LOADER_PLATFORM_THREAD_ONCE(ctl, func) loader_platform_thread_once_fn(ctl, func);
+#else
#define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var)
-#define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var)
+#define LOADER_PLATFORM_THREAD_ONCE_EXTERN_DEFINITION(var)
#define LOADER_PLATFORM_THREAD_ONCE(ctl, func)
+#endif
+
#if defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNXNTO__) || defined(__FreeBSD__)
// File IO
diff --git a/tests/live_verification/CMakeLists.txt b/tests/live_verification/CMakeLists.txt
index 1ce0e0dcd..87155e23a 100644
--- a/tests/live_verification/CMakeLists.txt
+++ b/tests/live_verification/CMakeLists.txt
@@ -20,3 +20,13 @@ target_link_libraries(dynamic_rendering_get_proc_addr testing_dependencies)
set_target_properties(dynamic_rendering_get_proc_addr ${LOADER_STANDARD_CXX_PROPERTIES})
add_subdirectory(dynamic_loader_behavior)
+
+if(APPLE AND BUILD_STATIC_LOADER)
+ add_executable(macos_static_loader_build macos_static_loader_build.cpp)
+ target_link_libraries(macos_static_loader_build loader_common_options vulkan Vulkan::Headers)
+ set_target_properties(macos_static_loader_build ${LOADER_STANDARD_CXX_PROPERTIES})
+ if (TEST_USE_THREAD_SANITIZER)
+ target_compile_options(macos_static_loader_build PUBLIC -fsanitize=thread)
+ target_link_options(macos_static_loader_build PUBLIC -fsanitize=thread)
+ endif()
+endif() \ No newline at end of file
diff --git a/tests/live_verification/macos_static_loader_build.cpp b/tests/live_verification/macos_static_loader_build.cpp
new file mode 100644
index 000000000..4292fa5d8
--- /dev/null
+++ b/tests/live_verification/macos_static_loader_build.cpp
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2022 The Khronos Group Inc.
+ * Copyright (c) 2022 Valve Corporation
+ * Copyright (c) 2022 LunarG, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and/or associated documentation files (the "Materials"), to
+ * deal in the Materials without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Materials, and to permit persons to whom the Materials are
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice(s) and this permission notice shall be included in
+ * all copies or substantial portions of the Materials.
+ *
+ * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ *
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
+ * USE OR OTHER DEALINGS IN THE MATERIALS.
+ *
+ * Author: Charles Giessen <charles@lunarg.com>
+ */
+
+#include <thread>
+#include <chrono>
+#include <vector>
+#include <atomic>
+
+#include "vulkan/vulkan.h"
+
+static std::atomic_bool is_running;
+
+void run_vk_code(int which_func) {
+ while (!is_running) {
+ // busy wait so all threads have a chance to spawn
+ }
+ while (is_running) {
+ switch (which_func) {
+ default:
+ case 0: {
+ VkInstance inst;
+ VkInstanceCreateInfo info{};
+ vkCreateInstance(&info, nullptr, &inst);
+ break;
+ }
+ case 1: {
+ uint32_t count = 0;
+ vkEnumerateInstanceExtensionProperties(nullptr, &count, nullptr);
+ std::vector<VkExtensionProperties> props(count, VkExtensionProperties{});
+ vkEnumerateInstanceExtensionProperties(nullptr, &count, props.data());
+ break;
+ }
+ case 2: {
+ uint32_t count = 0;
+ vkEnumerateInstanceLayerProperties(&count, nullptr);
+ std::vector<VkLayerProperties> layers(count, VkLayerProperties{});
+ vkEnumerateInstanceLayerProperties(&count, layers.data());
+
+ break;
+ }
+ case 3: {
+ uint32_t version = 0;
+ vkEnumerateInstanceVersion(&version);
+ break;
+ }
+ }
+ }
+}
+
+int main() {
+ uint32_t thread_count = 4;
+ is_running = false;
+ std::vector<std::thread> threads;
+ for (uint32_t i = 0; i < thread_count; i++) {
+ threads.emplace_back(run_vk_code, i % 4);
+ }
+ is_running = true;
+ std::this_thread::sleep_for(std::chrono::milliseconds(10));
+ is_running = false;
+ for (auto& t : threads) {
+ t.join();
+ }
+ return 0;
+} \ No newline at end of file