new class PISemaphore
doc for PIProtectedVariable
This commit is contained in:
@@ -32,24 +32,45 @@
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
class PIP_EXPORT PIProtectedVariable {
|
class PIP_EXPORT PIProtectedVariable {
|
||||||
public:
|
public:
|
||||||
|
//! \~english Sets value to copy of \"v\"
|
||||||
|
//! \~russian Устанавливает значение как копию \"v\"
|
||||||
void set(const T & v) {
|
void set(const T & v) {
|
||||||
PIMutexLocker _ml(mutex);
|
PIMutexLocker _ml(mutex);
|
||||||
var = v;
|
var = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! \~english Sets value by moving \"v\"
|
||||||
|
//! \~russian Устанавливает значение перемещением \"v\"
|
||||||
void set(T && v) {
|
void set(T && v) {
|
||||||
PIMutexLocker _ml(mutex);
|
PIMutexLocker _ml(mutex);
|
||||||
var = std::move(v);
|
var = std::move(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! \~english Returns copy of value
|
||||||
|
//! \~russian Возвращает копию значения
|
||||||
T get() const {
|
T get() const {
|
||||||
PIMutexLocker _ml(mutex);
|
PIMutexLocker _ml(mutex);
|
||||||
return var;
|
return var;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! \~english Lock mutex and returns reference of value
|
||||||
|
//! \~russian Блокирует мьютекс и возвращает ссылку на значение
|
||||||
T & lock() {
|
T & lock() {
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
return var;
|
return var;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! \~english Unlock mutex
|
||||||
|
//! \~russian Разблокирует мьютекс
|
||||||
void unlock() { mutex.unlock(); }
|
void unlock() { mutex.unlock(); }
|
||||||
|
|
||||||
|
//! \~english Sets value to copy of \"v\"
|
||||||
|
//! \~russian Устанавливает значение как копию \"v\"
|
||||||
|
PIProtectedVariable<T> & operator=(const T & v) {
|
||||||
|
set(v);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable PIMutex mutex;
|
mutable PIMutex mutex;
|
||||||
|
|||||||
152
libs/main/thread/pisemaphore.cpp
Normal file
152
libs/main/thread/pisemaphore.cpp
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//! \addtogroup Thread
|
||||||
|
//! \{
|
||||||
|
//! \class PISemaphore pisemaphore.h
|
||||||
|
//!
|
||||||
|
//! \~\brief
|
||||||
|
//! \~english Basic semaphore
|
||||||
|
//! \~russian Простой семафор
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
//! \~\details
|
||||||
|
//! \~english \section PISemaphore_sec0 Synopsis
|
||||||
|
//! \~russian \section PISemaphore_sec0 Краткий обзор
|
||||||
|
//!
|
||||||
|
//! \~english
|
||||||
|
//! %PISemaphore provides critical code section defence between several threads with resource counting.
|
||||||
|
//! Semaphore contains logic counter and functions to change it:
|
||||||
|
//! \a release(), \a acquire() and \a tryAcquire().
|
||||||
|
//!
|
||||||
|
//! For automatic acquire-release use \a PISemaphoreLocker.
|
||||||
|
//!
|
||||||
|
//! \~russian
|
||||||
|
//! %PISemaphore предоставляет межпотоковую защиту критических секций кода с подсчетом ресурсов.
|
||||||
|
//! Семафор состоит из логического счетчика и методов для его изменения:
|
||||||
|
//! \a release(), \a acquire() and \a tryAcquire().
|
||||||
|
//!
|
||||||
|
//! Для автоматического захвата-освобождения используйте \a PISemaphoreLocker.
|
||||||
|
//!
|
||||||
|
//! \~english \section PISemaphore_sec1 Usage
|
||||||
|
//! \~russian \section PISemaphore_sec1 Использование
|
||||||
|
//!
|
||||||
|
//! \~english
|
||||||
|
//!
|
||||||
|
//! \~russian
|
||||||
|
//!
|
||||||
|
//! \~\code
|
||||||
|
//! \endcode
|
||||||
|
//! \}
|
||||||
|
|
||||||
|
|
||||||
|
//! \addtogroup Thread
|
||||||
|
//! \{
|
||||||
|
//! \class PISemaphoreLocker pisemaphore.h
|
||||||
|
//!
|
||||||
|
//! \~\brief
|
||||||
|
//! \~english %PISemaphore autolocker
|
||||||
|
//! \~russian Автоблокировщик %PISemaphore
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
//! \~\details
|
||||||
|
//!
|
||||||
|
//! \~english
|
||||||
|
//! When a %PISemaphoreLocker object is created, it attempts to acquire the semaphore resources, if "condition" \c true.
|
||||||
|
//! When control leaves the scope in which the %PISemaphoreLocker object was created,
|
||||||
|
//! the %PISemaphoreLocker is destructed and the resources are released, if "condition" was \c true.
|
||||||
|
//!
|
||||||
|
//! If "condition" \c false this class do nothing.
|
||||||
|
//!
|
||||||
|
//! The %PISemaphoreLocker class is non-copyable.
|
||||||
|
//!
|
||||||
|
//! \~russian
|
||||||
|
//! При создании экземпляра %PISemaphoreLocker захватываются ресурсы семафора, если "condition" \c true.
|
||||||
|
//! Когда выполнение покидает область жизни объекта, вызывается его деструктор и ресурсы
|
||||||
|
//! освобождаются, если "condition" был \c true.
|
||||||
|
//!
|
||||||
|
//! Если "condition" \c false, то этот объект ничего не делает.
|
||||||
|
//!
|
||||||
|
//! Класс %PISemaphoreLocker некопируемый.
|
||||||
|
//!
|
||||||
|
//! \~\code
|
||||||
|
//! // critical section start
|
||||||
|
//! {
|
||||||
|
//! PISemaphoreLocker locker(mutex, 5);
|
||||||
|
//! // ... your code here
|
||||||
|
//! }
|
||||||
|
//! // critical section end
|
||||||
|
//! \endcode
|
||||||
|
//! \}
|
||||||
|
|
||||||
|
|
||||||
|
#include "pisemaphore.h"
|
||||||
|
|
||||||
|
|
||||||
|
PISemaphore::PISemaphore(int initial) {
|
||||||
|
count = initial;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PISemaphore::~PISemaphore() {
|
||||||
|
var.notifyAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PISemaphore::acquire(int cnt) {
|
||||||
|
PIMutexLocker _ml(mutex);
|
||||||
|
while (count < cnt) {
|
||||||
|
var.wait(mutex);
|
||||||
|
}
|
||||||
|
count -= cnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool PISemaphore::tryAcquire(int cnt) {
|
||||||
|
PIMutexLocker _ml(mutex);
|
||||||
|
if (count < cnt) return false;
|
||||||
|
count -= cnt;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool PISemaphore::tryAcquire(int cnt, PISystemTime timeout) {
|
||||||
|
PITimeMeasurer tm;
|
||||||
|
PIMutexLocker _ml(mutex);
|
||||||
|
while (count < cnt) {
|
||||||
|
PISystemTime remain = timeout - tm.elapsed();
|
||||||
|
if (remain.isNegative()) return false;
|
||||||
|
var.waitFor(mutex, remain);
|
||||||
|
}
|
||||||
|
count -= cnt;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PISemaphore::release(int cnt) {
|
||||||
|
PIMutexLocker _ml(mutex);
|
||||||
|
count += cnt;
|
||||||
|
var.notifyAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int PISemaphore::available() const {
|
||||||
|
PIMutexLocker _ml(mutex);
|
||||||
|
return count;
|
||||||
|
}
|
||||||
95
libs/main/thread/pisemaphore.h
Normal file
95
libs/main/thread/pisemaphore.h
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
/*! \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 // PIMUTEX_H
|
||||||
@@ -56,6 +56,7 @@
|
|||||||
#include "pimutex.h"
|
#include "pimutex.h"
|
||||||
#include "pipipelinethread.h"
|
#include "pipipelinethread.h"
|
||||||
#include "piprotectedvariable.h"
|
#include "piprotectedvariable.h"
|
||||||
|
#include "pisemaphore.h"
|
||||||
#include "pispinlock.h"
|
#include "pispinlock.h"
|
||||||
#include "pithread.h"
|
#include "pithread.h"
|
||||||
#include "pithreadnotifier.h"
|
#include "pithreadnotifier.h"
|
||||||
|
|||||||
72
main.cpp
72
main.cpp
@@ -12,8 +12,10 @@
|
|||||||
#include "pitranslator_p.h"
|
#include "pitranslator_p.h"
|
||||||
#include "pivaluetree_conversions.h"
|
#include "pivaluetree_conversions.h"
|
||||||
|
|
||||||
using namespace PICoutManipulators;
|
#include <windows.h>
|
||||||
|
|
||||||
|
using namespace PICoutManipulators;
|
||||||
|
/*
|
||||||
class MyStr: public PIString {
|
class MyStr: public PIString {
|
||||||
public:
|
public:
|
||||||
MyStr(): PIString() {}
|
MyStr(): PIString() {}
|
||||||
@@ -32,16 +34,70 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
class RWL {
|
||||||
|
public:
|
||||||
|
void lockWrite() { mutex.lock(); }
|
||||||
|
void unlockWrite() { mutex.unlock(); }
|
||||||
|
void lockRead() {}
|
||||||
|
void unlockRead() {}
|
||||||
|
PIMutex mutex;
|
||||||
|
};
|
||||||
|
|
||||||
|
PIMutex mutex;
|
||||||
|
PISemaphore sem(10);
|
||||||
|
|
||||||
int main(int argc, char * argv[]) {
|
int main(int argc, char * argv[]) {
|
||||||
PIProtectedVariable<MyStr> var;
|
/*sem.acquire(2);
|
||||||
// MyStr s("hello");
|
piCout << sem.tryAcquire(2);
|
||||||
var.set("hello");
|
piCout << sem.available();
|
||||||
auto & v = var.lock();
|
return 0;*/
|
||||||
// v = "world";
|
PIThread t_w(
|
||||||
var.unlock();
|
[] {
|
||||||
piCout << var.get();
|
// PIMutexLocker _ml(mutex);
|
||||||
|
piCout << "write start ...";
|
||||||
|
piMSleep(500);
|
||||||
|
sem.release(11);
|
||||||
|
piCout << "write end" << sem.available() << "\n";
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
1_Hz);
|
||||||
|
|
||||||
|
int cnt0 = 0, cnt1 = 0;
|
||||||
|
PIThread t_r0(
|
||||||
|
[&cnt0] {
|
||||||
|
// PIMutexLocker _ml(mutex);
|
||||||
|
piCout << "read0 start ...";
|
||||||
|
piMSleep(50);
|
||||||
|
sem.acquire(2);
|
||||||
|
bool ok = true; // sem.tryAcquire(2, 100_ms);
|
||||||
|
if (ok) ++cnt0;
|
||||||
|
piCout << "read0 end" << sem.available() << ok;
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
10_Hz);
|
||||||
|
PIThread t_r1(
|
||||||
|
[&cnt1] {
|
||||||
|
// PIMutexLocker _ml(mutex);
|
||||||
|
piCout << "read1 start ...";
|
||||||
|
piMSleep(50);
|
||||||
|
sem.acquire(2);
|
||||||
|
bool ok = true; // sem.tryAcquire(2, 100_ms);
|
||||||
|
if (ok) ++cnt1;
|
||||||
|
piCout << "read1 end" << sem.available() << ok;
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
11_Hz);
|
||||||
|
|
||||||
|
piSleep(5.);
|
||||||
|
|
||||||
|
t_r0.stopAndWait();
|
||||||
|
t_r1.stopAndWait();
|
||||||
|
t_w.stopAndWait();
|
||||||
|
|
||||||
|
piCout << cnt0 << cnt1;
|
||||||
|
|
||||||
/*PICodeParser parser;
|
/*PICodeParser parser;
|
||||||
parser.parseFile("client_server.h");
|
parser.parseFile("client_server.h");
|
||||||
|
|||||||
Reference in New Issue
Block a user