Add PISpinlock and PISpinlockLocker
This commit is contained in:
@@ -3,7 +3,7 @@ cmake_policy(SET CMP0017 NEW) # need include() with .cmake
|
|||||||
project(pip)
|
project(pip)
|
||||||
set(pip_MAJOR 2)
|
set(pip_MAJOR 2)
|
||||||
set(pip_MINOR 9)
|
set(pip_MINOR 9)
|
||||||
set(pip_REVISION 0)
|
set(pip_REVISION 1)
|
||||||
set(pip_SUFFIX )
|
set(pip_SUFFIX )
|
||||||
set(pip_COMPANY SHS)
|
set(pip_COMPANY SHS)
|
||||||
set(pip_DOMAIN org.SHS)
|
set(pip_DOMAIN org.SHS)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
PIP - Platform Independent Primitives
|
PIP - Platform Independent Primitives
|
||||||
Mutex
|
PIMutex, PIMutexLocker
|
||||||
Ivan Pelipenko peri4ko@yandex.ru, Stephan Fomenko, Andrey Bychkov work.a.b@yandex.ru
|
Ivan Pelipenko peri4ko@yandex.ru, Stephan Fomenko, Andrey Bychkov work.a.b@yandex.ru
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
/*! \file pimutex.h
|
/*! \file pimutex.h
|
||||||
* \brief PIMutexLocker
|
* \brief PIMutex, PIMutexLocker
|
||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
PIP - Platform Independent Primitives
|
PIP - Platform Independent Primitives
|
||||||
PIMutexLocker
|
PIMutex, PIMutexLocker
|
||||||
Ivan Pelipenko peri4ko@yandex.ru, Stephan Fomenko, Andrey Bychkov work.a.b@yandex.ru
|
Ivan Pelipenko peri4ko@yandex.ru, Stephan Fomenko, Andrey Bychkov work.a.b@yandex.ru
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
|||||||
30
libs/main/thread/pispinlock.cpp
Normal file
30
libs/main/thread/pispinlock.cpp
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
PIP - Platform Independent Primitives
|
||||||
|
PISpinlock
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** \class PISpinlock
|
||||||
|
* \brief Spinlock
|
||||||
|
* \details
|
||||||
|
* \section PISpinlock_sec0 Synopsis
|
||||||
|
* %PISpinlock provides synchronization blocks between several threads.
|
||||||
|
* PISpinlock functionality similar to PIMutex, but working on atomic
|
||||||
|
* type and \a lock() method wait with 100% CPU load.
|
||||||
|
* \note
|
||||||
|
* Use this type instead of PIMutex when less waiting time is more
|
||||||
|
* important than CPU load!
|
||||||
|
* */
|
||||||
75
libs/main/thread/pispinlock.h
Normal file
75
libs/main/thread/pispinlock.h
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
/*! \file pispinlock.h
|
||||||
|
* \brief PISpinlock
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
PIP - Platform Independent Primitives
|
||||||
|
PISpinlock
|
||||||
|
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 PISPINLOCK_H
|
||||||
|
#define PISPINLOCK_H
|
||||||
|
|
||||||
|
#include "piinit.h"
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
|
||||||
|
class PIP_EXPORT PISpinlock
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
NO_COPY_CLASS(PISpinlock)
|
||||||
|
|
||||||
|
//! Constructs unlocked spinlock
|
||||||
|
explicit PISpinlock() {flag.clear();}
|
||||||
|
|
||||||
|
//! Destroy spinlock
|
||||||
|
~PISpinlock() {}
|
||||||
|
|
||||||
|
|
||||||
|
//! \brief Lock spinlock
|
||||||
|
//! \details If spinlock is unlocked it set to locked state and returns immediate.
|
||||||
|
//! If spinlock is already locked function blocks until spinlock will be unlocked
|
||||||
|
void lock() {while (flag.test_and_set(std::memory_order_acquire));}
|
||||||
|
|
||||||
|
//! \brief Unlock spinlock
|
||||||
|
//! \details In any case this function returns immediate
|
||||||
|
void unlock() {flag.clear(std::memory_order_release);}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::atomic_flag flag;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
//! \brief PISpinlockLocker
|
||||||
|
//! \details
|
||||||
|
//! When a PISpinlockLocker object is created, it attempts to lock the spinlock it is given, if "condition" true.
|
||||||
|
//! When control leaves the scope in which the PISpinlockLocker object was created,
|
||||||
|
//! the PISpinlockLocker is destructed and the spinlock is released, if "condition" true.
|
||||||
|
//! If "condition" false this class do nothing.
|
||||||
|
//! The PISpinlockLocker class is non-copyable.
|
||||||
|
class PIP_EXPORT PISpinlockLocker
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
NO_COPY_CLASS(PISpinlockLocker)
|
||||||
|
PISpinlockLocker(PISpinlock & s, bool condition = true): spinlock(s), cond(condition) {if (cond) spinlock.lock();}
|
||||||
|
~PISpinlockLocker() {if (cond) spinlock.unlock();}
|
||||||
|
private:
|
||||||
|
PISpinlock & spinlock;
|
||||||
|
bool cond;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // PISPINLOCK_H
|
||||||
@@ -21,6 +21,7 @@
|
|||||||
#define PITHREADMODULE_H
|
#define PITHREADMODULE_H
|
||||||
|
|
||||||
#include "pimutex.h"
|
#include "pimutex.h"
|
||||||
|
#include "pispinlock.h"
|
||||||
#include "pithread.h"
|
#include "pithread.h"
|
||||||
#include "pitimer.h"
|
#include "pitimer.h"
|
||||||
#include "pipipelinethread.h"
|
#include "pipipelinethread.h"
|
||||||
|
|||||||
Reference in New Issue
Block a user