blob: 311ce7b14304dbd1fbfb93fd2ce3b6ec384574a2 [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 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Mike Stroyan3712d5c2015-04-02 11:59:05 -06008 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06009 * http://www.apache.org/licenses/LICENSE-2.0
Mike Stroyan3712d5c2015-04-02 11:59:05 -060010 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060016 *
17 * Author: Cody Northrop <cody@lunarg.com>
18 * Author: Mike Stroyan <mike@LunarG.com>
Mike Stroyan3712d5c2015-04-02 11:59:05 -060019 */
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070020
Mike Stroyan313f7e62015-08-10 16:42:53 -060021#ifndef THREADING_H
22#define THREADING_H
Jeremy Hayesb350beb2016-04-12 13:48:52 -060023#include <condition_variable>
24#include <mutex>
Mike Stroyan845bdc42015-11-02 15:30:20 -070025#include <vector>
Mike Stroyan313f7e62015-08-10 16:42:53 -060026#include "vk_layer_config.h"
27#include "vk_layer_logging.h"
Mike Stroyan3712d5c2015-04-02 11:59:05 -060028
Jon Ashburn5484e0c2016-03-08 17:48:44 -070029#if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined(_M_IA64) || \
30 defined(__aarch64__) || defined(__powerpc64__)
Mike Stroyan31c50c82016-01-29 15:09:04 -070031// If pointers are 64-bit, then there can be separate counters for each
32// NONDISPATCHABLE_HANDLE type. Otherwise they are all typedef uint64_t.
33#define DISTINCT_NONDISPATCHABLE_HANDLES
34#endif
35
Mike Stroyan3712d5c2015-04-02 11:59:05 -060036// Draw State ERROR codes
Mark Lobodzinski14817552016-05-19 17:08:10 -060037enum THREADING_CHECKER_ERROR {
Jon Ashburn5484e0c2016-03-08 17:48:44 -070038 THREADING_CHECKER_NONE, // Used for INFO & other non-error messages
39 THREADING_CHECKER_MULTIPLE_THREADS, // Object used simultaneously by multiple threads
40 THREADING_CHECKER_SINGLE_THREAD_REUSE, // Object used simultaneously by recursion in single thread
Mark Lobodzinski14817552016-05-19 17:08:10 -060041};
Mike Stroyan3712d5c2015-04-02 11:59:05 -060042
Mike Stroyan845bdc42015-11-02 15:30:20 -070043struct object_use_data {
44 loader_platform_thread_id thread;
45 int reader_count;
46 int writer_count;
47};
48
49struct layer_data;
Mike Stroyan845bdc42015-11-02 15:30:20 -070050
Mike Stroyan845bdc42015-11-02 15:30:20 -070051template <typename T> class counter {
Jon Ashburn5484e0c2016-03-08 17:48:44 -070052 public:
Mike Stroyan845bdc42015-11-02 15:30:20 -070053 const char *typeName;
54 VkDebugReportObjectTypeEXT objectType;
Mike Stroyan1a080012016-01-29 15:33:21 -070055 std::unordered_map<T, object_use_data> uses;
Mike Stroyanb3dd7902016-06-30 13:21:37 -060056 std::mutex counter_lock;
57 std::condition_variable counter_condition;
Jon Ashburn5484e0c2016-03-08 17:48:44 -070058 void startWrite(debug_report_data *report_data, T object) {
Dustin Graves080069b2016-04-05 13:48:15 -060059 bool skipCall = false;
Mike Stroyan845bdc42015-11-02 15:30:20 -070060 loader_platform_thread_id tid = loader_platform_get_thread_id();
Mike Stroyanb3dd7902016-06-30 13:21:37 -060061 std::unique_lock<std::mutex> lock(counter_lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -070062 if (uses.find(object) == uses.end()) {
63 // There is no current use of the object. Record writer thread.
64 struct object_use_data *use_data = &uses[object];
65 use_data->reader_count = 0;
66 use_data->writer_count = 1;
67 use_data->thread = tid;
68 } else {
69 struct object_use_data *use_data = &uses[object];
70 if (use_data->reader_count == 0) {
71 // There are no readers. Two writers just collided.
72 if (use_data->thread != tid) {
Dustin Graves37bef952016-02-05 16:06:21 -070073 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object),
Jon Ashburn5484e0c2016-03-08 17:48:44 -070074 /*location*/ 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING",
75 "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld",
76 typeName, use_data->thread, tid);
Mike Stroyan845bdc42015-11-02 15:30:20 -070077 if (skipCall) {
78 // Wait for thread-safe access to object instead of skipping call.
79 while (uses.find(object) != uses.end()) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -060080 counter_condition.wait(lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -070081 }
82 // There is now no current use of the object. Record writer thread.
83 struct object_use_data *use_data = &uses[object];
Jon Ashburn5484e0c2016-03-08 17:48:44 -070084 use_data->thread = tid;
Mike Stroyan845bdc42015-11-02 15:30:20 -070085 use_data->reader_count = 0;
86 use_data->writer_count = 1;
87 } else {
88 // Continue with an unsafe use of the object.
Jon Ashburn5484e0c2016-03-08 17:48:44 -070089 use_data->thread = tid;
Mike Stroyan845bdc42015-11-02 15:30:20 -070090 use_data->writer_count += 1;
91 }
92 } else {
Mike Stroyanc8774502016-02-05 09:11:32 -070093 // This is either safe multiple use in one call, or recursive use.
Mike Stroyan845bdc42015-11-02 15:30:20 -070094 // There is no way to make recursion safe. Just forge ahead.
95 use_data->writer_count += 1;
96 }
97 } else {
98 // There are readers. This writer collided with them.
99 if (use_data->thread != tid) {
Dustin Graves37bef952016-02-05 16:06:21 -0700100 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object),
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700101 /*location*/ 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING",
102 "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld",
103 typeName, use_data->thread, tid);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700104 if (skipCall) {
105 // Wait for thread-safe access to object instead of skipping call.
106 while (uses.find(object) != uses.end()) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600107 counter_condition.wait(lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700108 }
109 // There is now no current use of the object. Record writer thread.
110 struct object_use_data *use_data = &uses[object];
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700111 use_data->thread = tid;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700112 use_data->reader_count = 0;
113 use_data->writer_count = 1;
114 } else {
115 // Continue with an unsafe use of the object.
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700116 use_data->thread = tid;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700117 use_data->writer_count += 1;
118 }
119 } else {
Mike Stroyanc8774502016-02-05 09:11:32 -0700120 // This is either safe multiple use in one call, or recursive use.
Mike Stroyan845bdc42015-11-02 15:30:20 -0700121 // There is no way to make recursion safe. Just forge ahead.
122 use_data->writer_count += 1;
123 }
124 }
125 }
Mike Stroyan845bdc42015-11-02 15:30:20 -0700126 }
127
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700128 void finishWrite(T object) {
Mike Stroyan845bdc42015-11-02 15:30:20 -0700129 // Object is no longer in use
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600130 std::unique_lock<std::mutex> lock(counter_lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700131 uses[object].writer_count -= 1;
132 if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) {
133 uses.erase(object);
134 }
135 // Notify any waiting threads that this object may be safe to use
Jeremy Hayesb350beb2016-04-12 13:48:52 -0600136 lock.unlock();
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600137 counter_condition.notify_all();
Mike Stroyan845bdc42015-11-02 15:30:20 -0700138 }
139
140 void startRead(debug_report_data *report_data, T object) {
Dustin Graves080069b2016-04-05 13:48:15 -0600141 bool skipCall = false;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700142 loader_platform_thread_id tid = loader_platform_get_thread_id();
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600143 std::unique_lock<std::mutex> lock(counter_lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700144 if (uses.find(object) == uses.end()) {
145 // There is no current use of the object. Record reader count
146 struct object_use_data *use_data = &uses[object];
147 use_data->reader_count = 1;
148 use_data->writer_count = 0;
149 use_data->thread = tid;
Mike Stroyanc8774502016-02-05 09:11:32 -0700150 } else if (uses[object].writer_count > 0 && uses[object].thread != tid) {
Mike Stroyan845bdc42015-11-02 15:30:20 -0700151 // There is a writer of the object.
Dustin Graves37bef952016-02-05 16:06:21 -0700152 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object),
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700153 /*location*/ 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING",
154 "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld", typeName,
155 uses[object].thread, tid);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700156 if (skipCall) {
157 // Wait for thread-safe access to object instead of skipping call.
158 while (uses.find(object) != uses.end()) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600159 counter_condition.wait(lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700160 }
161 // There is no current use of the object. Record reader count
162 struct object_use_data *use_data = &uses[object];
163 use_data->reader_count = 1;
164 use_data->writer_count = 0;
165 use_data->thread = tid;
166 } else {
167 uses[object].reader_count += 1;
168 }
169 } else {
170 // There are other readers of the object. Increase reader count
171 uses[object].reader_count += 1;
172 }
Mike Stroyan845bdc42015-11-02 15:30:20 -0700173 }
174 void finishRead(T object) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600175 std::unique_lock<std::mutex> lock(counter_lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700176 uses[object].reader_count -= 1;
177 if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) {
178 uses.erase(object);
179 }
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600180 // Notify any waiting threads that this object may be safe to use
Jeremy Hayesb350beb2016-04-12 13:48:52 -0600181 lock.unlock();
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600182 counter_condition.notify_all();
Mike Stroyan845bdc42015-11-02 15:30:20 -0700183 }
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700184 counter(const char *name = "", VkDebugReportObjectTypeEXT type = VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT) {
Mike Stroyan845bdc42015-11-02 15:30:20 -0700185 typeName = name;
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700186 objectType = type;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700187 }
188};
189
Cody Northrop55443ef2015-09-28 15:09:32 -0600190struct layer_data {
Chia-I Wu59d0a332016-05-16 11:21:03 +0800191 VkInstance instance;
192
Mike Stroyan313f7e62015-08-10 16:42:53 -0600193 debug_report_data *report_data;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700194 std::vector<VkDebugReportCallbackEXT> logging_callback;
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700195 VkLayerDispatchTable *device_dispatch_table;
196 VkLayerInstanceDispatchTable *instance_dispatch_table;
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600197 // The following are for keeping track of the temporary callbacks that can
198 // be used in vkCreateInstance and vkDestroyInstance:
199 uint32_t num_tmp_callbacks;
200 VkDebugReportCallbackCreateInfoEXT *tmp_dbg_create_infos;
201 VkDebugReportCallbackEXT *tmp_callbacks;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700202 counter<VkCommandBuffer> c_VkCommandBuffer;
203 counter<VkDevice> c_VkDevice;
204 counter<VkInstance> c_VkInstance;
205 counter<VkQueue> c_VkQueue;
Mike Stroyan31c50c82016-01-29 15:09:04 -0700206#ifdef DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan845bdc42015-11-02 15:30:20 -0700207 counter<VkBuffer> c_VkBuffer;
208 counter<VkBufferView> c_VkBufferView;
209 counter<VkCommandPool> c_VkCommandPool;
210 counter<VkDescriptorPool> c_VkDescriptorPool;
211 counter<VkDescriptorSet> c_VkDescriptorSet;
212 counter<VkDescriptorSetLayout> c_VkDescriptorSetLayout;
213 counter<VkDeviceMemory> c_VkDeviceMemory;
214 counter<VkEvent> c_VkEvent;
215 counter<VkFence> c_VkFence;
216 counter<VkFramebuffer> c_VkFramebuffer;
217 counter<VkImage> c_VkImage;
218 counter<VkImageView> c_VkImageView;
219 counter<VkPipeline> c_VkPipeline;
220 counter<VkPipelineCache> c_VkPipelineCache;
221 counter<VkPipelineLayout> c_VkPipelineLayout;
222 counter<VkQueryPool> c_VkQueryPool;
223 counter<VkRenderPass> c_VkRenderPass;
224 counter<VkSampler> c_VkSampler;
225 counter<VkSemaphore> c_VkSemaphore;
226 counter<VkShaderModule> c_VkShaderModule;
227 counter<VkDebugReportCallbackEXT> c_VkDebugReportCallbackEXT;
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700228#else // DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan31c50c82016-01-29 15:09:04 -0700229 counter<uint64_t> c_uint64_t;
230#endif // DISTINCT_NONDISPATCHABLE_HANDLES
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700231 layer_data()
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600232 : report_data(nullptr), num_tmp_callbacks(0), tmp_dbg_create_infos(nullptr), tmp_callbacks(nullptr),
233 c_VkCommandBuffer("VkCommandBuffer", VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT),
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700234 c_VkDevice("VkDevice", VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT),
235 c_VkInstance("VkInstance", VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT),
236 c_VkQueue("VkQueue", VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT),
Mike Stroyan31c50c82016-01-29 15:09:04 -0700237#ifdef DISTINCT_NONDISPATCHABLE_HANDLES
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700238 c_VkBuffer("VkBuffer", VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT),
239 c_VkBufferView("VkBufferView", VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT),
240 c_VkCommandPool("VkCommandPool", VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_POOL_EXT),
241 c_VkDescriptorPool("VkDescriptorPool", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT),
242 c_VkDescriptorSet("VkDescriptorSet", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT),
243 c_VkDescriptorSetLayout("VkDescriptorSetLayout", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT),
244 c_VkDeviceMemory("VkDeviceMemory", VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT),
245 c_VkEvent("VkEvent", VK_DEBUG_REPORT_OBJECT_TYPE_EVENT_EXT), c_VkFence("VkFence", VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT),
246 c_VkFramebuffer("VkFramebuffer", VK_DEBUG_REPORT_OBJECT_TYPE_FRAMEBUFFER_EXT),
247 c_VkImage("VkImage", VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT),
248 c_VkImageView("VkImageView", VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT),
249 c_VkPipeline("VkPipeline", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT),
250 c_VkPipelineCache("VkPipelineCache", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_CACHE_EXT),
251 c_VkPipelineLayout("VkPipelineLayout", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_LAYOUT_EXT),
252 c_VkQueryPool("VkQueryPool", VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT),
253 c_VkRenderPass("VkRenderPass", VK_DEBUG_REPORT_OBJECT_TYPE_RENDER_PASS_EXT),
254 c_VkSampler("VkSampler", VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT),
255 c_VkSemaphore("VkSemaphore", VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT),
256 c_VkShaderModule("VkShaderModule", VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT),
257 c_VkDebugReportCallbackEXT("VkDebugReportCallbackEXT", VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT)
258#else // DISTINCT_NONDISPATCHABLE_HANDLES
259 c_uint64_t("NON_DISPATCHABLE_HANDLE", VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT)
Mike Stroyan31c50c82016-01-29 15:09:04 -0700260#endif // DISTINCT_NONDISPATCHABLE_HANDLES
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700261 {};
Cody Northrop55443ef2015-09-28 15:09:32 -0600262};
Mike Stroyan313f7e62015-08-10 16:42:53 -0600263
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700264#define WRAPPER(type) \
265 static void startWriteObject(struct layer_data *my_data, type object) { \
266 my_data->c_##type.startWrite(my_data->report_data, object); \
267 } \
268 static void finishWriteObject(struct layer_data *my_data, type object) { my_data->c_##type.finishWrite(object); } \
269 static void startReadObject(struct layer_data *my_data, type object) { \
270 my_data->c_##type.startRead(my_data->report_data, object); \
271 } \
272 static void finishReadObject(struct layer_data *my_data, type object) { my_data->c_##type.finishRead(object); }
Mike Stroyan313f7e62015-08-10 16:42:53 -0600273
Mike Stroyan845bdc42015-11-02 15:30:20 -0700274WRAPPER(VkDevice)
275WRAPPER(VkInstance)
276WRAPPER(VkQueue)
Mike Stroyan31c50c82016-01-29 15:09:04 -0700277#ifdef DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan845bdc42015-11-02 15:30:20 -0700278WRAPPER(VkBuffer)
279WRAPPER(VkBufferView)
280WRAPPER(VkCommandPool)
281WRAPPER(VkDescriptorPool)
282WRAPPER(VkDescriptorSet)
283WRAPPER(VkDescriptorSetLayout)
284WRAPPER(VkDeviceMemory)
285WRAPPER(VkEvent)
286WRAPPER(VkFence)
287WRAPPER(VkFramebuffer)
288WRAPPER(VkImage)
289WRAPPER(VkImageView)
290WRAPPER(VkPipeline)
291WRAPPER(VkPipelineCache)
292WRAPPER(VkPipelineLayout)
293WRAPPER(VkQueryPool)
294WRAPPER(VkRenderPass)
295WRAPPER(VkSampler)
296WRAPPER(VkSemaphore)
297WRAPPER(VkShaderModule)
298WRAPPER(VkDebugReportCallbackEXT)
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700299#else // DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan31c50c82016-01-29 15:09:04 -0700300WRAPPER(uint64_t)
301#endif // DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan845bdc42015-11-02 15:30:20 -0700302
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700303static std::unordered_map<void *, layer_data *> layer_data_map;
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600304static std::mutex command_pool_lock;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700305static std::unordered_map<VkCommandBuffer, VkCommandPool> command_pool_map;
306
307// VkCommandBuffer needs check for implicit use of command pool
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700308static void startWriteObject(struct layer_data *my_data, VkCommandBuffer object, bool lockPool = true) {
Mike Stroyan845bdc42015-11-02 15:30:20 -0700309 if (lockPool) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600310 std::unique_lock<std::mutex> lock(command_pool_lock);
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700311 VkCommandPool pool = command_pool_map[object];
Jeremy Hayesb350beb2016-04-12 13:48:52 -0600312 lock.unlock();
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700313 startWriteObject(my_data, pool);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700314 }
315 my_data->c_VkCommandBuffer.startWrite(my_data->report_data, object);
316}
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700317static void finishWriteObject(struct layer_data *my_data, VkCommandBuffer object, bool lockPool = true) {
Mike Stroyan845bdc42015-11-02 15:30:20 -0700318 my_data->c_VkCommandBuffer.finishWrite(object);
319 if (lockPool) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600320 std::unique_lock<std::mutex> lock(command_pool_lock);
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700321 VkCommandPool pool = command_pool_map[object];
Jeremy Hayesb350beb2016-04-12 13:48:52 -0600322 lock.unlock();
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700323 finishWriteObject(my_data, pool);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700324 }
325}
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700326static void startReadObject(struct layer_data *my_data, VkCommandBuffer object) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600327 std::unique_lock<std::mutex> lock(command_pool_lock);
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700328 VkCommandPool pool = command_pool_map[object];
Jeremy Hayesb350beb2016-04-12 13:48:52 -0600329 lock.unlock();
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700330 startReadObject(my_data, pool);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700331 my_data->c_VkCommandBuffer.startRead(my_data->report_data, object);
332}
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700333static void finishReadObject(struct layer_data *my_data, VkCommandBuffer object) {
Mike Stroyan845bdc42015-11-02 15:30:20 -0700334 my_data->c_VkCommandBuffer.finishRead(object);
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600335 std::unique_lock<std::mutex> lock(command_pool_lock);
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700336 VkCommandPool pool = command_pool_map[object];
Jeremy Hayesb350beb2016-04-12 13:48:52 -0600337 lock.unlock();
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700338 finishReadObject(my_data, pool);
Mike Stroyan313f7e62015-08-10 16:42:53 -0600339}
340#endif // THREADING_H