From 2e7b8d03c97c7aafcf76934ae25b52df886e31c1 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Tue, 6 Jul 2021 19:53:32 +0200 Subject: [PATCH] Add MutexProtectedObject --- .../Data/Concurent/MutexProtectedObject.hpp | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 openVulkanoCpp/Data/Concurent/MutexProtectedObject.hpp diff --git a/openVulkanoCpp/Data/Concurent/MutexProtectedObject.hpp b/openVulkanoCpp/Data/Concurent/MutexProtectedObject.hpp new file mode 100644 index 0000000..64994c8 --- /dev/null +++ b/openVulkanoCpp/Data/Concurent/MutexProtectedObject.hpp @@ -0,0 +1,111 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include "Spintex.hpp" +#include +#include + +namespace openVulkanoCpp +{ + template + class MutexProtectedObject + { + protected: + T object; + MUTEX mutex; + + public: + class Accessor + { + std::unique_lock lock; + T& ref; + + public: + Accessor(MUTEX& m, T& obj) + : lock(m), ref(obj) + {} + + T* operator->() { return &ref; } + + T& Get() { return ref; } + }; + + MutexProtectedObject() = default; + + MutexProtectedObject(const T& initValue) : object(initValue) {} + + template + MutexProtectedObject(Args&&... args) + : object(std::forward(args)...) + {} + + MutexProtectedObject(const MutexProtectedObject&) = delete; + + Accessor Access() + { + return Accessor(mutex, object); + } + + MutexProtectedObject& operator =(const T& value) + { + Accessor accessor = Access(); + object = value; + return *this; + } + + operator T() + { + Accessor accessor = Access(); + return accessor.Get(); + } + + const T& UnsafeAccess() const { return object; } + }; + + template using SpintexProtectedObject = MutexProtectedObject; + + template + class SharedMutexProtectedObject : public MutexProtectedObject + { + public: + class SharedAccessor + { + std::shared_lock lock; + T& ref; + public: + SharedAccessor(MUTEX& m, T& obj) + : lock(m), ref(obj) + {} + + + const T* operator->() const { return &ref; } + + const T& Get() const { return ref; } + + T* operator->() { return &ref; } + + T& Get() { return ref; } + }; + + SharedMutexProtectedObject() = default; + + SharedMutexProtectedObject(const T& initValue) : MutexProtectedObject(initValue) {} + + template + SharedMutexProtectedObject(Args&&... args) + : MutexProtectedObject(std::forward(args)...) + {} + + SharedMutexProtectedObject(const SharedMutexProtectedObject&) = delete; + + SharedAccessor SharedAccess() + { + return SharedAccessor(MutexProtectedObject::mutex, MutexProtectedObject::object); + } + }; +}