File indexing completed on 2024-05-12 15:58:21
0001 /* 0002 * SPDX-FileCopyrightText: 2015 Dmitry Kazakov <dimula73@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef __KIS_IMAGE_BARRIER_LOCKER_H 0008 #define __KIS_IMAGE_BARRIER_LOCKER_H 0009 0010 #include <mutex> 0011 0012 template <typename ImagePointer> 0013 struct PointerPolicyAlwaysPresent 0014 { 0015 typedef ImagePointer ImagePointerType; 0016 static inline void barrierLock(ImagePointer image) { 0017 image->barrierLock(); 0018 } 0019 0020 static inline void unlock(ImagePointer image) { 0021 image->unlock(); 0022 } 0023 }; 0024 0025 template <typename ImagePointer> 0026 struct PointerPolicyAllowNull 0027 { 0028 typedef ImagePointer ImagePointerType; 0029 0030 static inline void barrierLock(ImagePointer image) { 0031 if (image) { 0032 image->barrierLock(); 0033 } 0034 } 0035 0036 static inline void unlock(ImagePointer image) { 0037 if (image) { 0038 image->unlock(); 0039 } 0040 } 0041 }; 0042 0043 /** 0044 * A simple class that implements std::lock_guard concept for locking KisImage. 0045 * It barrier-locks the image during construction and unlocks it on destruction. 0046 * 0047 * \p PointerPolicy defines how the image pointer is handled 0048 */ 0049 0050 template <class PointerPolicy> 0051 class KisImageBarrierLockerImpl { 0052 public: 0053 typedef typename PointerPolicy::ImagePointerType ImagePointer; 0054 public: 0055 inline KisImageBarrierLockerImpl(ImagePointer image) 0056 : m_image(image) 0057 { 0058 PointerPolicy::barrierLock(m_image); 0059 } 0060 0061 inline KisImageBarrierLockerImpl(ImagePointer image, std::adopt_lock_t) 0062 : m_image(image) 0063 { 0064 } 0065 0066 inline ~KisImageBarrierLockerImpl() { 0067 PointerPolicy::unlock(m_image); 0068 } 0069 0070 private: 0071 KisImageBarrierLockerImpl(const KisImageBarrierLockerImpl<PointerPolicy> &rhs); 0072 ImagePointer m_image; 0073 }; 0074 0075 // Lock guard for the image passed as KisImageSP pointer. Pointer cannot be null. 0076 typedef KisImageBarrierLockerImpl<PointerPolicyAlwaysPresent<KisImageSP>> KisImageBarrierLocker; 0077 0078 // Lock guard for the image passed as a raw KisImage* pointer. Pointer cannot be null. 0079 typedef KisImageBarrierLockerImpl<PointerPolicyAlwaysPresent<KisImage*>> KisImageBarrierLockerRaw; 0080 0081 // Lock guard for the image passed as KisImageSP pointer that *can* be null. 0082 typedef KisImageBarrierLockerImpl<PointerPolicyAllowNull<KisImageSP>> KisImageBarrierLockerAllowNull; 0083 0084 #endif /* __KIS_IMAGE_BARRIER_LOCKER_H */