1 /*
2  * Copyright (c) 2022 The Khronos Group Inc.
3  * Copyright (c) 2022 Valve Corporation
4  * Copyright (c) 2022 LunarG, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and/or associated documentation files (the "Materials"), to
8  * deal in the Materials without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Materials, and to permit persons to whom the Materials are
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice(s) and this permission notice shall be included in
14  * all copies or substantial portions of the Materials.
15  *
16  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  *
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23  * USE OR OTHER DEALINGS IN THE MATERIALS.
24  *
25  * Author: Charles Giessen <charles@lunarg.com>
26  */
27 
28 #include <thread>
29 #include <chrono>
30 #include <vector>
31 #include <atomic>
32 
33 #include "vulkan/vulkan.h"
34 
35 static std::atomic_bool is_running;
36 
run_vk_code(int which_func)37 void run_vk_code(int which_func) {
38     while (!is_running) {
39         // busy wait so all threads have a chance to spawn
40     }
41     while (is_running) {
42         switch (which_func) {
43             default:
44             case 0: {
45                 VkInstance inst;
46                 VkInstanceCreateInfo info{};
47                 vkCreateInstance(&info, nullptr, &inst);
48                 break;
49             }
50             case 1: {
51                 uint32_t count = 0;
52                 vkEnumerateInstanceExtensionProperties(nullptr, &count, nullptr);
53                 std::vector<VkExtensionProperties> props(count, VkExtensionProperties{});
54                 vkEnumerateInstanceExtensionProperties(nullptr, &count, props.data());
55                 break;
56             }
57             case 2: {
58                 uint32_t count = 0;
59                 vkEnumerateInstanceLayerProperties(&count, nullptr);
60                 std::vector<VkLayerProperties> layers(count, VkLayerProperties{});
61                 vkEnumerateInstanceLayerProperties(&count, layers.data());
62 
63                 break;
64             }
65             case 3: {
66                 uint32_t version = 0;
67                 vkEnumerateInstanceVersion(&version);
68                 break;
69             }
70         }
71     }
72 }
73 
main()74 int main() {
75     uint32_t thread_count = 4;
76     is_running = false;
77     std::vector<std::thread> threads;
78     for (uint32_t i = 0; i < thread_count; i++) {
79         threads.emplace_back(run_vk_code, i % 4);
80     }
81     is_running = true;
82     std::this_thread::sleep_for(std::chrono::milliseconds(10));
83     is_running = false;
84     for (auto& t : threads) {
85         t.join();
86     }
87     return 0;
88 }