add PIReadWriteLock, PIReadLocker and PIWriteLocker
This commit is contained in:
130
libs/main/thread/pireadwritelock.h
Normal file
130
libs/main/thread/pireadwritelock.h
Normal file
@@ -0,0 +1,130 @@
|
||||
/*! \file pireadwritelock.h
|
||||
* \ingroup Read/Write lock
|
||||
* \~\brief
|
||||
* \~english Read/Write lock
|
||||
* \~russian Блокировка чтения/записи
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
PIReadWriteLock, PIReadLocker, PIWriteLocker
|
||||
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 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/>.
|
||||
*/
|
||||
|
||||
#ifndef PIREADWRITELOCK_H
|
||||
#define PIREADWRITELOCK_H
|
||||
|
||||
#include "piconditionvar.h"
|
||||
|
||||
|
||||
class PIP_EXPORT PIReadWriteLock {
|
||||
public:
|
||||
NO_COPY_CLASS(PIReadWriteLock)
|
||||
|
||||
//! \~english Constructs %PIReadWriteLock.
|
||||
//! \~russian Создает %PIReadWriteLock.
|
||||
PIReadWriteLock();
|
||||
|
||||
//! \~english Destroy %PIReadWriteLock.
|
||||
//! \~russian Деструктор %PIReadWriteLock.
|
||||
~PIReadWriteLock();
|
||||
|
||||
|
||||
//! \~english Lock for write. If already locked for write or read, than wait until all locks released.
|
||||
//! \~russian Заблокировать на запись. Если уже заблокировано на запись или чтение, то ждёт освобождения блокировок.
|
||||
void lockWrite();
|
||||
|
||||
//! \~english Try to lock for write. Returns if operation was successfull.
|
||||
//! \~russian Пробует заблокировать на запись. Возвращает успех операции.
|
||||
bool tryLockWrite();
|
||||
|
||||
//! \~english Try to lock for write for \"timeout\". Returns if operation was successfull (timeout has not expired).
|
||||
//! \~russian Пробует заблокировать на запись в течении \"timeout\". Возвращает успех операции (не истек ли тайм-аут).
|
||||
bool tryLockWrite(PISystemTime timeout);
|
||||
|
||||
//! \~english Release lock for write.
|
||||
//! \~russian Освобождает блокировку на запись.
|
||||
void unlockWrite();
|
||||
|
||||
|
||||
//! \~english Lock for read. If already locked for write, than wait until write lock released.
|
||||
//! \~russian Заблокировать на чтение. Если уже заблокировано на запись, то ждёт освобождения записывающей блокировки.
|
||||
void lockRead();
|
||||
|
||||
//! \~english Try to lock for read. Returns if operation was successfull.
|
||||
//! \~russian Пробует заблокировать на чтение. Возвращает успех операции.
|
||||
bool tryLockRead();
|
||||
|
||||
//! \~english Try to lock for read for \"timeout\". Returns if operation was successfull (timeout has not expired).
|
||||
//! \~russian Пробует заблокировать на чтение в течении \"timeout\". Возвращает успех операции (не истек ли тайм-аут).
|
||||
bool tryLockRead(PISystemTime timeout);
|
||||
|
||||
//! \~english Release lock for read.
|
||||
//! \~russian Освобождает блокировку на чтение.
|
||||
void unlockRead();
|
||||
|
||||
private:
|
||||
int reading = 0;
|
||||
bool writing = false;
|
||||
PIMutex mutex;
|
||||
PIConditionVariable var;
|
||||
};
|
||||
|
||||
|
||||
class PIP_EXPORT PIReadLocker {
|
||||
public:
|
||||
NO_COPY_CLASS(PIReadLocker);
|
||||
|
||||
//! \~english Constructs and lock for read %PIReadWriteLock "l" if "condition" is \c true.
|
||||
//! \~russian Создается и блокирует на чтение %PIReadWriteLock "l" если "condition" \c true.
|
||||
PIReadLocker(PIReadWriteLock & l, bool condition = true): rwl(l), cond(condition) {
|
||||
if (cond) rwl.lockRead();
|
||||
}
|
||||
|
||||
//! \~english Release read lock on %PIReadWriteLock if "condition" was \c true.
|
||||
//! \~russian Освобождает блокировку на чтение %PIReadWriteLock если "condition" был \c true.
|
||||
~PIReadLocker() {
|
||||
if (cond) rwl.unlockRead();
|
||||
}
|
||||
|
||||
private:
|
||||
PIReadWriteLock & rwl;
|
||||
bool cond = true;
|
||||
};
|
||||
|
||||
|
||||
class PIP_EXPORT PIWriteLocker {
|
||||
public:
|
||||
NO_COPY_CLASS(PIWriteLocker);
|
||||
|
||||
//! \~english Constructs and lock for write %PIReadWriteLock "l" if "condition" is \c true.
|
||||
//! \~russian Создается и блокирует на запись %PIReadWriteLock "l" если "condition" \c true.
|
||||
PIWriteLocker(PIReadWriteLock & l, bool condition = true): rwl(l), cond(condition) {
|
||||
if (cond) rwl.lockWrite();
|
||||
}
|
||||
|
||||
//! \~english Release write lock on %PIReadWriteLock if "condition" was \c true.
|
||||
//! \~russian Освобождает блокировку на запись %PIReadWriteLock если "condition" был \c true.
|
||||
~PIWriteLocker() {
|
||||
if (cond) rwl.unlockWrite();
|
||||
}
|
||||
|
||||
private:
|
||||
PIReadWriteLock & rwl;
|
||||
bool cond = true;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user