Initialize properly when statically linked
authorCharles Giessen <charles@lunarg.com>
Thu, 7 Jul 2022 19:25:22 +0000 (13:25 -0600)
committerCharles Giessen <46324611+charles-lunarg@users.noreply.github.com>
Thu, 7 Jul 2022 20:41:01 +0000 (14:41 -0600)
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.

loader/CMakeLists.txt
loader/loader.h
loader/vk_loader_platform.h
tests/live_verification/CMakeLists.txt
tests/live_verification/macos_static_loader_build.cpp [new file with mode: 0644]

index 24074973c4027bf1fd54afcdd21c7d0d4f0801bb..3e99612bfa28a75e6be4e4638f969492d4bb03c1 100644 (file)
@@ -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()
index 10fa14acd4de956dd7164c3398b8b9534a03d75c..3beb8b6cd007404c40d5bdcec12d3e7a2191097b 100644 (file)
@@ -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) {
index 44e0730c5a4e0503ff51a6268210e23ea626054c..864eaf4be22b4bdd6dff96cce95e7978fdcd9d9a 100644 (file)
@@ -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
index 1ce0e0dcdc4f257b57cb5aaab33127d822a076f7..87155e23ac6e2910fa26f0b22c16323730ae0eda 100644 (file)
@@ -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 (file)
index 0000000..4292fa5
--- /dev/null
@@ -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