blob: 05c64645ab39de989169af5b6b987957cdebed60 [file] [log] [blame]
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -07001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
Mike Stroyan3712d5c2015-04-02 11:59:05 -06004 *
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -07005 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and/or associated documentation files (the "Materials"), to
7 * deal in the Materials without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Materials, and to permit persons to whom the Materials
10 * are furnished to do so, subject to the following conditions:
Mike Stroyan3712d5c2015-04-02 11:59:05 -060011 *
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070012 * The above copyright notice(s) and this permission notice shall be included
13 * in all copies or substantial portions of the Materials.
Mike Stroyan3712d5c2015-04-02 11:59:05 -060014 *
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070015 * The Materials are Confidential Information as defined by the Khronos
16 * Membership Agreement until designated non-confidential by Khronos, at which
17 * point this condition clause shall be removed.
Mike Stroyan3712d5c2015-04-02 11:59:05 -060018 *
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070019 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Mike Stroyan3712d5c2015-04-02 11:59:05 -060020 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070021 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 *
23 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
26 * USE OR OTHER DEALINGS IN THE MATERIALS
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060027 *
28 * Author: Cody Northrop <cody@lunarg.com>
29 * Author: Mike Stroyan <mike@LunarG.com>
Mike Stroyan3712d5c2015-04-02 11:59:05 -060030 */
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070031
Mike Stroyan313f7e62015-08-10 16:42:53 -060032#ifndef THREADING_H
33#define THREADING_H
Mike Stroyan845bdc42015-11-02 15:30:20 -070034#include <vector>
Mike Stroyan313f7e62015-08-10 16:42:53 -060035#include "vk_layer_config.h"
36#include "vk_layer_logging.h"
Mike Stroyan3712d5c2015-04-02 11:59:05 -060037
Mike Stroyan31c50c82016-01-29 15:09:04 -070038#if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
39// If pointers are 64-bit, then there can be separate counters for each
40// NONDISPATCHABLE_HANDLE type. Otherwise they are all typedef uint64_t.
41#define DISTINCT_NONDISPATCHABLE_HANDLES
42#endif
43
Mike Stroyan3712d5c2015-04-02 11:59:05 -060044// Draw State ERROR codes
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070045typedef enum _THREADING_CHECKER_ERROR
46{
47 THREADING_CHECKER_NONE, // Used for INFO & other non-error messages
48 THREADING_CHECKER_MULTIPLE_THREADS, // Object used simultaneously by multiple threads
49 THREADING_CHECKER_SINGLE_THREAD_REUSE, // Object used simultaneously by recursion in single thread
Mike Stroyan3712d5c2015-04-02 11:59:05 -060050} THREADING_CHECKER_ERROR;
51
Mike Stroyan845bdc42015-11-02 15:30:20 -070052struct object_use_data {
53 loader_platform_thread_id thread;
54 int reader_count;
55 int writer_count;
56};
57
58struct layer_data;
59using namespace std;
60
61static int threadingLockInitialized = 0;
62static loader_platform_thread_mutex threadingLock;
63static loader_platform_thread_cond threadingCond;
64
65template <typename T> class counter {
66 public:
67 const char *typeName;
68 VkDebugReportObjectTypeEXT objectType;
69 unordered_map<T, object_use_data> uses;
70 void startWrite(debug_report_data *report_data, T object)
71 {
72 VkBool32 skipCall = VK_FALSE;
73 loader_platform_thread_id tid = loader_platform_get_thread_id();
74 loader_platform_thread_lock_mutex(&threadingLock);
75 if (uses.find(object) == uses.end()) {
76 // There is no current use of the object. Record writer thread.
77 struct object_use_data *use_data = &uses[object];
78 use_data->reader_count = 0;
79 use_data->writer_count = 1;
80 use_data->thread = tid;
81 } else {
82 struct object_use_data *use_data = &uses[object];
83 if (use_data->reader_count == 0) {
84 // There are no readers. Two writers just collided.
85 if (use_data->thread != tid) {
86 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, reinterpret_cast<uint64_t>(object),
87 /*location*/ 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING",
88 "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld",
89 typeName, use_data->thread, tid);
90 if (skipCall) {
91 // Wait for thread-safe access to object instead of skipping call.
92 while (uses.find(object) != uses.end()) {
93 loader_platform_thread_cond_wait(&threadingCond, &threadingLock);
94 }
95 // There is now no current use of the object. Record writer thread.
96 struct object_use_data *use_data = &uses[object];
97 use_data->thread = tid ;
98 use_data->reader_count = 0;
99 use_data->writer_count = 1;
100 } else {
101 // Continue with an unsafe use of the object.
102 use_data->writer_count += 1;
103 }
104 } else {
105 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, reinterpret_cast<uint64_t>(object),
106 /*location*/ 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING",
107 "THREADING ERROR : object of type %s is recursively used in thread %ld",
108 typeName, tid);
109 // There is no way to make recursion safe. Just forge ahead.
110 use_data->writer_count += 1;
111 }
112 } else {
113 // There are readers. This writer collided with them.
114 if (use_data->thread != tid) {
115 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, reinterpret_cast<uint64_t>(object),
116 /*location*/ 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING",
117 "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld",
118 typeName, use_data->thread, tid);
119 if (skipCall) {
120 // Wait for thread-safe access to object instead of skipping call.
121 while (uses.find(object) != uses.end()) {
122 loader_platform_thread_cond_wait(&threadingCond, &threadingLock);
123 }
124 // There is now no current use of the object. Record writer thread.
125 struct object_use_data *use_data = &uses[object];
126 use_data->thread = tid ;
127 use_data->reader_count = 0;
128 use_data->writer_count = 1;
129 } else {
130 // Continue with an unsafe use of the object.
131 use_data->writer_count += 1;
132 }
133 } else {
134 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, reinterpret_cast<uint64_t>(object),
135 /*location*/ 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING",
136 "THREADING ERROR : object of type %s is recursively used in thread %ld",
137 typeName, tid);
138 // There is no way to make recursion safe. Just forge ahead.
139 use_data->writer_count += 1;
140 }
141 }
142 }
143 loader_platform_thread_unlock_mutex(&threadingLock);
144 }
145
146 void finishWrite(T object)
147 {
148 // Object is no longer in use
149 loader_platform_thread_lock_mutex(&threadingLock);
150 uses[object].writer_count -= 1;
151 if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) {
152 uses.erase(object);
153 }
154 // Notify any waiting threads that this object may be safe to use
155 loader_platform_thread_cond_broadcast(&threadingCond);
156 loader_platform_thread_unlock_mutex(&threadingLock);
157 }
158
159 void startRead(debug_report_data *report_data, T object) {
160 VkBool32 skipCall = VK_FALSE;
161 loader_platform_thread_id tid = loader_platform_get_thread_id();
162 loader_platform_thread_lock_mutex(&threadingLock);
163 if (uses.find(object) == uses.end()) {
164 // There is no current use of the object. Record reader count
165 struct object_use_data *use_data = &uses[object];
166 use_data->reader_count = 1;
167 use_data->writer_count = 0;
168 use_data->thread = tid;
169 } else if (uses[object].writer_count > 0) {
170 // There is a writer of the object.
171 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, reinterpret_cast<uint64_t>(object),
172 /*location*/ 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING",
173 "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld",
174 typeName, uses[object].thread, tid);
175 if (skipCall) {
176 // Wait for thread-safe access to object instead of skipping call.
177 while (uses.find(object) != uses.end()) {
178 loader_platform_thread_cond_wait(&threadingCond, &threadingLock);
179 }
180 // There is no current use of the object. Record reader count
181 struct object_use_data *use_data = &uses[object];
182 use_data->reader_count = 1;
183 use_data->writer_count = 0;
184 use_data->thread = tid;
185 } else {
186 uses[object].reader_count += 1;
187 }
188 } else {
189 // There are other readers of the object. Increase reader count
190 uses[object].reader_count += 1;
191 }
192 loader_platform_thread_unlock_mutex(&threadingLock);
193 }
194 void finishRead(T object) {
195 loader_platform_thread_lock_mutex(&threadingLock);
196 uses[object].reader_count -= 1;
197 if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) {
198 uses.erase(object);
199 }
200 // Notify and waiting threads that this object may be safe to use
201 loader_platform_thread_cond_broadcast(&threadingCond);
202 loader_platform_thread_unlock_mutex(&threadingLock);
203 }
204 counter(const char *name = "",
205 VkDebugReportObjectTypeEXT type=VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT) {
206 typeName = name;
207 objectType=type;
208 }
209};
210
Cody Northrop55443ef2015-09-28 15:09:32 -0600211struct layer_data {
Mike Stroyan313f7e62015-08-10 16:42:53 -0600212 debug_report_data *report_data;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700213 std::vector<VkDebugReportCallbackEXT> logging_callback;
214 VkLayerDispatchTable* device_dispatch_table;
215 VkLayerInstanceDispatchTable* instance_dispatch_table;
216 counter<VkCommandBuffer> c_VkCommandBuffer;
217 counter<VkDevice> c_VkDevice;
218 counter<VkInstance> c_VkInstance;
219 counter<VkQueue> c_VkQueue;
Mike Stroyan31c50c82016-01-29 15:09:04 -0700220#ifdef DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan845bdc42015-11-02 15:30:20 -0700221 counter<VkBuffer> c_VkBuffer;
222 counter<VkBufferView> c_VkBufferView;
223 counter<VkCommandPool> c_VkCommandPool;
224 counter<VkDescriptorPool> c_VkDescriptorPool;
225 counter<VkDescriptorSet> c_VkDescriptorSet;
226 counter<VkDescriptorSetLayout> c_VkDescriptorSetLayout;
227 counter<VkDeviceMemory> c_VkDeviceMemory;
228 counter<VkEvent> c_VkEvent;
229 counter<VkFence> c_VkFence;
230 counter<VkFramebuffer> c_VkFramebuffer;
231 counter<VkImage> c_VkImage;
232 counter<VkImageView> c_VkImageView;
233 counter<VkPipeline> c_VkPipeline;
234 counter<VkPipelineCache> c_VkPipelineCache;
235 counter<VkPipelineLayout> c_VkPipelineLayout;
236 counter<VkQueryPool> c_VkQueryPool;
237 counter<VkRenderPass> c_VkRenderPass;
238 counter<VkSampler> c_VkSampler;
239 counter<VkSemaphore> c_VkSemaphore;
240 counter<VkShaderModule> c_VkShaderModule;
241 counter<VkDebugReportCallbackEXT> c_VkDebugReportCallbackEXT;
Mike Stroyan31c50c82016-01-29 15:09:04 -0700242#else // DISTINCT_NONDISPATCHABLE_HANDLES
243 counter<uint64_t> c_uint64_t;
244#endif // DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan845bdc42015-11-02 15:30:20 -0700245 layer_data():
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700246 report_data(nullptr),
Mike Stroyan845bdc42015-11-02 15:30:20 -0700247 c_VkCommandBuffer("VkCommandBuffer", VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT),
248 c_VkDevice("VkDevice", VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT),
249 c_VkInstance("VkInstance", VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT),
250 c_VkQueue("VkQueue", VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT),
Mike Stroyan31c50c82016-01-29 15:09:04 -0700251#ifdef DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan845bdc42015-11-02 15:30:20 -0700252 c_VkBuffer("VkBuffer", VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT),
253 c_VkBufferView("VkBufferView", VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT),
254 c_VkCommandPool("VkCommandPool", VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_POOL_EXT),
255 c_VkDescriptorPool("VkDescriptorPool", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT),
256 c_VkDescriptorSet("VkDescriptorSet", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT),
257 c_VkDescriptorSetLayout("VkDescriptorSetLayout", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT),
258 c_VkDeviceMemory("VkDeviceMemory", VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT),
259 c_VkEvent("VkEvent", VK_DEBUG_REPORT_OBJECT_TYPE_EVENT_EXT),
260 c_VkFence("VkFence", VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT),
261 c_VkFramebuffer("VkFramebuffer", VK_DEBUG_REPORT_OBJECT_TYPE_FRAMEBUFFER_EXT),
262 c_VkImage("VkImage", VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT),
263 c_VkImageView("VkImageView", VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT),
264 c_VkPipeline("VkPipeline", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT),
265 c_VkPipelineCache("VkPipelineCache", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_CACHE_EXT),
266 c_VkPipelineLayout("VkPipelineLayout", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_LAYOUT_EXT),
267 c_VkQueryPool("VkQueryPool", VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT),
268 c_VkRenderPass("VkRenderPass", VK_DEBUG_REPORT_OBJECT_TYPE_RENDER_PASS_EXT),
269 c_VkSampler("VkSampler", VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT),
270 c_VkSemaphore("VkSemaphore", VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT),
271 c_VkShaderModule("VkShaderModule", VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT),
272 c_VkDebugReportCallbackEXT("VkDebugReportCallbackEXT", VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT)
Mike Stroyan31c50c82016-01-29 15:09:04 -0700273#else // DISTINCT_NONDISPATCHABLE_HANDLES
274 c_uint64_t("NON_DISPATCHABLE_HANDLE", VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT)
275#endif // DISTINCT_NONDISPATCHABLE_HANDLES
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700276 {};
Cody Northrop55443ef2015-09-28 15:09:32 -0600277};
Mike Stroyan313f7e62015-08-10 16:42:53 -0600278
Mike Stroyan845bdc42015-11-02 15:30:20 -0700279#define WRAPPER(type) \
280static void startWriteObject(struct layer_data *my_data, type object){my_data->c_##type.startWrite(my_data->report_data, object);}\
281static void finishWriteObject(struct layer_data *my_data, type object){my_data->c_##type.finishWrite(object);}\
282static void startReadObject(struct layer_data *my_data, type object){my_data->c_##type.startRead(my_data->report_data, object);}\
283static void finishReadObject(struct layer_data *my_data, type object){my_data->c_##type.finishRead(object);}
Mike Stroyan313f7e62015-08-10 16:42:53 -0600284
Mike Stroyan845bdc42015-11-02 15:30:20 -0700285WRAPPER(VkDevice)
286WRAPPER(VkInstance)
287WRAPPER(VkQueue)
Mike Stroyan31c50c82016-01-29 15:09:04 -0700288#ifdef DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan845bdc42015-11-02 15:30:20 -0700289WRAPPER(VkBuffer)
290WRAPPER(VkBufferView)
291WRAPPER(VkCommandPool)
292WRAPPER(VkDescriptorPool)
293WRAPPER(VkDescriptorSet)
294WRAPPER(VkDescriptorSetLayout)
295WRAPPER(VkDeviceMemory)
296WRAPPER(VkEvent)
297WRAPPER(VkFence)
298WRAPPER(VkFramebuffer)
299WRAPPER(VkImage)
300WRAPPER(VkImageView)
301WRAPPER(VkPipeline)
302WRAPPER(VkPipelineCache)
303WRAPPER(VkPipelineLayout)
304WRAPPER(VkQueryPool)
305WRAPPER(VkRenderPass)
306WRAPPER(VkSampler)
307WRAPPER(VkSemaphore)
308WRAPPER(VkShaderModule)
309WRAPPER(VkDebugReportCallbackEXT)
Mike Stroyan31c50c82016-01-29 15:09:04 -0700310#else // DISTINCT_NONDISPATCHABLE_HANDLES
311WRAPPER(uint64_t)
312#endif // DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan845bdc42015-11-02 15:30:20 -0700313
314static std::unordered_map<void*, layer_data *> layer_data_map;
315static std::unordered_map<VkCommandBuffer, VkCommandPool> command_pool_map;
316
317// VkCommandBuffer needs check for implicit use of command pool
318static void startWriteObject(struct layer_data *my_data, VkCommandBuffer object, bool lockPool=true)
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700319{
Mike Stroyan845bdc42015-11-02 15:30:20 -0700320 if (lockPool) {
Mike Stroyan31c50c82016-01-29 15:09:04 -0700321 startWriteObject(my_data, command_pool_map[object]);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700322 }
323 my_data->c_VkCommandBuffer.startWrite(my_data->report_data, object);
324}
325static void finishWriteObject(struct layer_data *my_data, VkCommandBuffer object, bool lockPool=true)
326{
327 my_data->c_VkCommandBuffer.finishWrite(object);
328 if (lockPool) {
Mike Stroyan31c50c82016-01-29 15:09:04 -0700329 finishWriteObject(my_data, command_pool_map[object]);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700330 }
331}
332static void startReadObject(struct layer_data *my_data, VkCommandBuffer object, bool lockPool=false)
333{
334 if (lockPool) {
Mike Stroyan31c50c82016-01-29 15:09:04 -0700335 startReadObject(my_data, command_pool_map[object]);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700336 }
337 my_data->c_VkCommandBuffer.startRead(my_data->report_data, object);
338}
339static void finishReadObject(struct layer_data *my_data, VkCommandBuffer object, bool lockPool=false)
340{
341 my_data->c_VkCommandBuffer.finishRead(object);
342 if (lockPool) {
Mike Stroyan31c50c82016-01-29 15:09:04 -0700343 finishReadObject(my_data, command_pool_map[object]);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700344 }
Mike Stroyan313f7e62015-08-10 16:42:53 -0600345}
346#endif // THREADING_H