diff options
| author | Jeremy Hayes <jeremy@lunarg.com> | 2016-04-12 13:48:52 -0600 |
|---|---|---|
| committer | Jeremy Hayes <jeremy@lunarg.com> | 2016-04-18 11:40:16 -0600 |
| commit | ed562ea5f59571db947be0f543385679b66e0149 (patch) | |
| tree | 726cd0a3f48523fe40001834c018ac6bd85f7bd0 | |
| parent | e67a3b24aa51463ffbd750429b7446735e6adc8c (diff) | |
| download | usermoji-ed562ea5f59571db947be0f543385679b66e0149.tar.xz | |
layers: threading - use std mutex
Change-Id: I773ea148807e5b634ad021ea58b001cd3d1bad87
| -rw-r--r-- | layers/threading.cpp | 18 | ||||
| -rw-r--r-- | layers/threading.h | 47 |
2 files changed, 25 insertions, 40 deletions
diff --git a/layers/threading.cpp b/layers/threading.cpp index 51381848..d23c9ff3 100644 --- a/layers/threading.cpp +++ b/layers/threading.cpp @@ -48,12 +48,6 @@ static void initThreading(layer_data *my_data, const VkAllocationCallbacks *pAllocator) { layer_debug_actions(my_data->report_data, my_data->logging_callback, pAllocator, "google_threading"); - - if (!threadingLockInitialized) { - loader_platform_thread_create_mutex(&threadingLock); - loader_platform_thread_init_cond(&threadingCond); - threadingLockInitialized = 1; - } } VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL @@ -102,12 +96,6 @@ VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyInstance(VkInstance instance layer_debug_report_destroy_instance(my_data->report_data); delete my_data->instance_dispatch_table; layer_data_map.erase(key); - - if (layer_data_map.empty()) { - // Release mutex when destroying last instance. - loader_platform_thread_delete_mutex(&threadingLock); - threadingLockInitialized = 0; - } } VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo, @@ -311,9 +299,8 @@ vkAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pAl // Record mapping from command buffer to command pool if (VK_SUCCESS == result) { for (uint32_t index = 0; index < pAllocateInfo->commandBufferCount; index++) { - loader_platform_thread_lock_mutex(&threadingLock); + std::lock_guard<std::mutex> lock(global_lock); command_pool_map[pCommandBuffers[index]] = pAllocateInfo->commandPool; - loader_platform_thread_unlock_mutex(&threadingLock); } } @@ -337,8 +324,7 @@ void VKAPI_CALL vkFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, finishWriteObject(my_data, commandPool); for (uint32_t index = 0; index < commandBufferCount; index++) { finishWriteObject(my_data, pCommandBuffers[index], lockCommandPool); - loader_platform_thread_lock_mutex(&threadingLock); + std::lock_guard<std::mutex> lock(global_lock); command_pool_map.erase(pCommandBuffers[index]); - loader_platform_thread_unlock_mutex(&threadingLock); } } diff --git a/layers/threading.h b/layers/threading.h index 21168751..6c3a055e 100644 --- a/layers/threading.h +++ b/layers/threading.h @@ -27,6 +27,8 @@ #ifndef THREADING_H #define THREADING_H +#include <condition_variable> +#include <mutex> #include <vector> #include "vk_layer_config.h" #include "vk_layer_logging.h" @@ -53,9 +55,8 @@ struct object_use_data { struct layer_data; -static int threadingLockInitialized = 0; -static loader_platform_thread_mutex threadingLock; -static loader_platform_thread_cond threadingCond; +static std::mutex global_lock; +static std::condition_variable global_condition; template <typename T> class counter { public: @@ -65,7 +66,7 @@ template <typename T> class counter { void startWrite(debug_report_data *report_data, T object) { bool skipCall = false; loader_platform_thread_id tid = loader_platform_get_thread_id(); - loader_platform_thread_lock_mutex(&threadingLock); + std::unique_lock<std::mutex> lock(global_lock); if (uses.find(object) == uses.end()) { // There is no current use of the object. Record writer thread. struct object_use_data *use_data = &uses[object]; @@ -84,7 +85,7 @@ template <typename T> class counter { if (skipCall) { // Wait for thread-safe access to object instead of skipping call. while (uses.find(object) != uses.end()) { - loader_platform_thread_cond_wait(&threadingCond, &threadingLock); + global_condition.wait(lock); } // There is now no current use of the object. Record writer thread. struct object_use_data *use_data = &uses[object]; @@ -111,7 +112,7 @@ template <typename T> class counter { if (skipCall) { // Wait for thread-safe access to object instead of skipping call. while (uses.find(object) != uses.end()) { - loader_platform_thread_cond_wait(&threadingCond, &threadingLock); + global_condition.wait(lock); } // There is now no current use of the object. Record writer thread. struct object_use_data *use_data = &uses[object]; @@ -130,25 +131,24 @@ template <typename T> class counter { } } } - loader_platform_thread_unlock_mutex(&threadingLock); } void finishWrite(T object) { // Object is no longer in use - loader_platform_thread_lock_mutex(&threadingLock); + std::unique_lock<std::mutex> lock(global_lock); uses[object].writer_count -= 1; if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) { uses.erase(object); } // Notify any waiting threads that this object may be safe to use - loader_platform_thread_cond_broadcast(&threadingCond); - loader_platform_thread_unlock_mutex(&threadingLock); + lock.unlock(); + global_condition.notify_all(); } void startRead(debug_report_data *report_data, T object) { bool skipCall = false; loader_platform_thread_id tid = loader_platform_get_thread_id(); - loader_platform_thread_lock_mutex(&threadingLock); + std::unique_lock<std::mutex> lock(global_lock); if (uses.find(object) == uses.end()) { // There is no current use of the object. Record reader count struct object_use_data *use_data = &uses[object]; @@ -164,7 +164,7 @@ template <typename T> class counter { if (skipCall) { // Wait for thread-safe access to object instead of skipping call. while (uses.find(object) != uses.end()) { - loader_platform_thread_cond_wait(&threadingCond, &threadingLock); + global_condition.wait(lock); } // There is no current use of the object. Record reader count struct object_use_data *use_data = &uses[object]; @@ -178,17 +178,16 @@ template <typename T> class counter { // There are other readers of the object. Increase reader count uses[object].reader_count += 1; } - loader_platform_thread_unlock_mutex(&threadingLock); } void finishRead(T object) { - loader_platform_thread_lock_mutex(&threadingLock); + std::unique_lock<std::mutex> lock(global_lock); uses[object].reader_count -= 1; if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) { uses.erase(object); } // Notify and waiting threads that this object may be safe to use - loader_platform_thread_cond_broadcast(&threadingCond); - loader_platform_thread_unlock_mutex(&threadingLock); + lock.unlock(); + global_condition.notify_all(); } counter(const char *name = "", VkDebugReportObjectTypeEXT type = VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT) { typeName = name; @@ -307,9 +306,9 @@ static std::unordered_map<VkCommandBuffer, VkCommandPool> command_pool_map; // VkCommandBuffer needs check for implicit use of command pool static void startWriteObject(struct layer_data *my_data, VkCommandBuffer object, bool lockPool = true) { if (lockPool) { - loader_platform_thread_lock_mutex(&threadingLock); + std::unique_lock<std::mutex> lock(global_lock); VkCommandPool pool = command_pool_map[object]; - loader_platform_thread_unlock_mutex(&threadingLock); + lock.unlock(); startWriteObject(my_data, pool); } my_data->c_VkCommandBuffer.startWrite(my_data->report_data, object); @@ -317,24 +316,24 @@ static void startWriteObject(struct layer_data *my_data, VkCommandBuffer object, static void finishWriteObject(struct layer_data *my_data, VkCommandBuffer object, bool lockPool = true) { my_data->c_VkCommandBuffer.finishWrite(object); if (lockPool) { - loader_platform_thread_lock_mutex(&threadingLock); + std::unique_lock<std::mutex> lock(global_lock); VkCommandPool pool = command_pool_map[object]; - loader_platform_thread_unlock_mutex(&threadingLock); + lock.unlock(); finishWriteObject(my_data, pool); } } static void startReadObject(struct layer_data *my_data, VkCommandBuffer object) { - loader_platform_thread_lock_mutex(&threadingLock); + std::unique_lock<std::mutex> lock(global_lock); VkCommandPool pool = command_pool_map[object]; - loader_platform_thread_unlock_mutex(&threadingLock); + lock.unlock(); startReadObject(my_data, pool); my_data->c_VkCommandBuffer.startRead(my_data->report_data, object); } static void finishReadObject(struct layer_data *my_data, VkCommandBuffer object) { my_data->c_VkCommandBuffer.finishRead(object); - loader_platform_thread_lock_mutex(&threadingLock); + std::unique_lock<std::mutex> lock(global_lock); VkCommandPool pool = command_pool_map[object]; - loader_platform_thread_unlock_mutex(&threadingLock); + lock.unlock(); finishReadObject(my_data, pool); } #endif // THREADING_H |
