simplify piprotectedvariable

This commit is contained in:
2026-03-17 17:33:27 +03:00
parent 2798d7de9c
commit 8ccc05ee78
3 changed files with 964 additions and 21 deletions

View File

@@ -4,22 +4,22 @@
//! \~english Thread-safe variable
//! \~russian Потокобезопасная переменная
/*
PIP - Platform Independent Primitives
Thread-safe variable
Ivan Pelipenko peri4ko@yandex.ru
PIP - Platform Independent Primitives
Thread-safe variable
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIPROTECTEDVARIABLE_H
@@ -39,16 +39,22 @@ public:
//! \~russian Указателеподобная обертка, возвращаемая \a getRef(), пока защищенное значение остается заблокированным.
class PIP_EXPORT Pointer {
friend class PIProtectedVariable<T>;
NO_COPY_CLASS(Pointer);
public:
//! \~english Copies wrapper state for access to the same protected value.
//! \~russian Копирует состояние обертки для доступа к тому же защищенному значению.
Pointer(const Pointer & v): pv(v.pv), counter(v.counter + 1) {}
//! \~\english Move constructor - transfers ownership of the lock.
//! \~russian Конструктор перемещения - передает владение блокировкой.
Pointer(Pointer && other) noexcept: pv(other.pv), ownsLock(other.ownsLock) { other.ownsLock = false; }
//! \~english Destroys wrapper and releases the mutex when it owns the original lock.
//! \~russian Уничтожает обертку и освобождает мьютекс, когда она владеет исходной блокировкой.
//! \~\english Move assignment is deleted - Pointer can only be moved once.
//! \~russian Оператор перемещения удален - Pointer можно переместить только один раз.
Pointer & operator=(Pointer &&) = delete;
//! \~\english Destroys wrapper and releases the mutex.
//! \~russian Уничтожает обертку и освобождает мьютекс.
~Pointer() {
if (counter == 0) pv.mutex.unlock();
if (ownsLock) pv.mutex.unlock();
}
//! \~english Returns pointer access to the protected value.
@@ -61,10 +67,10 @@ public:
private:
Pointer() = delete;
Pointer(PIProtectedVariable<T> & v): pv(v) {}
Pointer(PIProtectedVariable<T> & v): pv(v), ownsLock(true) {}
PIProtectedVariable<T> & pv;
int counter = 0;
bool ownsLock = true;
};
//! \~english Replaces the protected value with \a v.