diff --git a/CMakeLists.txt b/CMakeLists.txt index 357ffe0c..7936ad08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_policy(SET CMP0017 NEW) # need include() with .cmake project(pip) set(pip_MAJOR 2) set(pip_MINOR 9) -set(pip_REVISION 0) +set(pip_REVISION 1) set(pip_SUFFIX ) set(pip_COMPANY SHS) set(pip_DOMAIN org.SHS) diff --git a/libs/main/thread/pimutex.cpp b/libs/main/thread/pimutex.cpp index e605f3b4..be259ed3 100644 --- a/libs/main/thread/pimutex.cpp +++ b/libs/main/thread/pimutex.cpp @@ -1,6 +1,6 @@ /* PIP - Platform Independent Primitives - Mutex + PIMutex, PIMutexLocker 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 diff --git a/libs/main/thread/pimutex.h b/libs/main/thread/pimutex.h index a551625d..9165c725 100644 --- a/libs/main/thread/pimutex.h +++ b/libs/main/thread/pimutex.h @@ -1,9 +1,9 @@ /*! \file pimutex.h - * \brief PIMutexLocker + * \brief PIMutex, PIMutexLocker */ /* PIP - Platform Independent Primitives - PIMutexLocker + PIMutex, PIMutexLocker 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 diff --git a/libs/main/thread/pispinlock.cpp b/libs/main/thread/pispinlock.cpp new file mode 100644 index 00000000..3262f39b --- /dev/null +++ b/libs/main/thread/pispinlock.cpp @@ -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 . +*/ + +/** \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! + * */ diff --git a/libs/main/thread/pispinlock.h b/libs/main/thread/pispinlock.h new file mode 100644 index 00000000..6de315c4 --- /dev/null +++ b/libs/main/thread/pispinlock.h @@ -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 . +*/ + +#ifndef PISPINLOCK_H +#define PISPINLOCK_H + +#include "piinit.h" +#include + + +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 diff --git a/libs/main/thread/pithreadmodule.h b/libs/main/thread/pithreadmodule.h index 5973dfe4..cb1128e1 100644 --- a/libs/main/thread/pithreadmodule.h +++ b/libs/main/thread/pithreadmodule.h @@ -21,6 +21,7 @@ #define PITHREADMODULE_H #include "pimutex.h" +#include "pispinlock.h" #include "pithread.h" #include "pitimer.h" #include "pipipelinethread.h"