96 lines
3.4 KiB
C++
96 lines
3.4 KiB
C++
/*! \file pisemaphore.h
|
||
* \ingroup Semaphore
|
||
* \~\brief
|
||
* \~english Basic semaphore
|
||
* \~russian Простой семафор
|
||
*/
|
||
/*
|
||
PIP - Platform Independent Primitives
|
||
PISemaphore, PISemaphoreLocker
|
||
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 PISEMAPHORE_H
|
||
#define PISEMAPHORE_H
|
||
|
||
#include "piconditionvar.h"
|
||
|
||
|
||
class PIP_EXPORT PISemaphore {
|
||
public:
|
||
NO_COPY_CLASS(PISemaphore)
|
||
|
||
//! \~english Constructs semaphore with \"initial\" available resources.
|
||
//! \~russian Создает семафор с \"initial\" начальными свободными ресурсами.
|
||
PISemaphore(int initial = 0);
|
||
|
||
//! \~english Destroy semaphore.
|
||
//! \~russian Деструктор семафора.
|
||
~PISemaphore();
|
||
|
||
|
||
//! \~english Acquire \"cnt\" resources. If no available resources, than blocks until they freed.
|
||
//! \~russian Захватывает \"cnt\" ресурсов. Если свободных ресурсов недостаточно, то блокирует до их появления.
|
||
void acquire(int cnt = 1);
|
||
|
||
//! \~english Try to acquire \"cnt\" resources. Returns if operation was successfull.
|
||
//! \~russian Пробует захватывает \"cnt\" ресурсов. Возвращает успех захвата.
|
||
bool tryAcquire(int cnt = 1);
|
||
|
||
//! \~english Try to acquire \"cnt\" resources for \"timeout\". Returns if operation was successfull (timeout has not expired).
|
||
//! \~russian Пробует захватывает \"cnt\" ресурсов в течении \"timeout\". Возвращает успех захвата (не истек ли тайм-аут).
|
||
bool tryAcquire(int cnt, PISystemTime timeout);
|
||
|
||
//! \~english Release \"cnt\" resources.
|
||
//! \~russian Освобождает \"cnt\" ресурсов.
|
||
void release(int cnt = 1);
|
||
|
||
//! \~english Returns available resources count.
|
||
//! \~russian Возвращает количество свободных ресурсов.
|
||
int available() const;
|
||
|
||
private:
|
||
int count = 0;
|
||
mutable PIMutex mutex;
|
||
PIConditionVariable var;
|
||
};
|
||
|
||
|
||
class PIP_EXPORT PISemaphoreLocker {
|
||
public:
|
||
NO_COPY_CLASS(PISemaphoreLocker);
|
||
|
||
//! \~english Constructs and acquire \"cnt\" resources on semaphore "s" if "condition" is \c true.
|
||
//! \~russian Создается и захватывает \"cnt\" ресурсов у семафора "s" если "condition" \c true.
|
||
PISemaphoreLocker(PISemaphore & s, int cnt = 1, bool condition = true): sem(s), count(cnt), cond(condition) {
|
||
if (cond) sem.acquire(count);
|
||
}
|
||
|
||
//! \~english Release "cnt" resources on semaphore if "condition" was \c true.
|
||
//! \~russian Освобождает "cnt" ресурсов у семафора если "condition" был \c true.
|
||
~PISemaphoreLocker() {
|
||
if (cond) sem.release(count);
|
||
}
|
||
|
||
private:
|
||
PISemaphore & sem;
|
||
int count = 1;
|
||
bool cond = true;
|
||
};
|
||
|
||
|
||
#endif
|