This commit is contained in:
2026-03-17 18:13:23 +03:00
parent 8ccc05ee78
commit ac877f1024
3 changed files with 48 additions and 28 deletions

View File

@@ -25,6 +25,7 @@
#ifndef PIPROTECTEDVARIABLE_H
#define PIPROTECTEDVARIABLE_H
#include "picout.h"
#include "pimutex.h"
//! \~\ingroup Thread
@@ -34,6 +35,8 @@
template<typename T>
class PIP_EXPORT PIProtectedVariable {
public:
PIProtectedVariable(T v = T()): var(std::move(v)) {}
//! \~\brief
//! \~english Pointer-like wrapper returned by \a getRef() while the protected value remains locked.
//! \~russian Указателеподобная обертка, возвращаемая \a getRef(), пока защищенное значение остается заблокированным.
@@ -44,17 +47,23 @@ public:
public:
//! \~\english Move constructor - transfers ownership of the lock.
//! \~russian Конструктор перемещения - передает владение блокировкой.
Pointer(Pointer && other) noexcept: pv(other.pv), ownsLock(other.ownsLock) { other.ownsLock = false; }
Pointer(Pointer && other): pv(other.pv) { other.can_unlock = false; };
//! \~\english Move assignment is deleted - Pointer can only be moved once.
//! \~russian Оператор перемещения удален - Pointer можно переместить только один раз.
Pointer & operator=(Pointer &&) = delete;
//! \~\english Move assignment - transfers ownership of the lock.
//! \~russian Оператор перемещения - передает владение блокировкой.
Pointer & operator=(Pointer && other) {
pv = other.pv;
other.can_unlock = false;
};
//! \~\english Destroys wrapper and releases the mutex.
//! \~russian Уничтожает обертку и освобождает мьютекс.
~Pointer() {
if (ownsLock) pv.mutex.unlock();
if (can_unlock) {
pv.mutex.unlock();
piCout << "mutex.unlock()" << &(pv.mutex);
}
}
//! \~english Returns pointer access to the protected value.
@@ -66,11 +75,14 @@ public:
T & operator*() { return pv.var; }
private:
Pointer() = delete;
Pointer(PIProtectedVariable<T> & v): pv(v), ownsLock(true) {}
explicit Pointer() = delete;
explicit Pointer(PIProtectedVariable<T> & v): pv(v) {
pv.mutex.lock();
piCout << "mutex.lock()" << &(pv.mutex);
}
PIProtectedVariable<T> & pv;
bool ownsLock = true;
bool can_unlock = true;
};
//! \~english Replaces the protected value with \a v.
@@ -82,10 +94,7 @@ public:
//! \~english Locks the value and returns wrapper-based access to it.
//! \~russian Блокирует значение и возвращает обертку для доступа к нему.
Pointer getRef() {
mutex.lock();
return Pointer(*this);
}
Pointer getRef() { return Pointer(*this); }
//! \~english Returns a copy of the protected value.
//! \~russian Возвращает копию защищенного значения.
@@ -104,7 +113,7 @@ public:
private:
mutable PIMutex mutex;
T var;
T var = {};
};