From 1a3096c48bf4cec3d068e2f8164632faab73cd1b Mon Sep 17 00:00:00 2001 From: andrey Date: Fri, 17 Jul 2020 11:32:04 +0300 Subject: [PATCH] PIMutex typedef std::mutex, patch PIMutexLocker, replace volatile by atomic --- CMakeLists.txt | 4 +- lib/fftw/pifft_p.h | 1 - lib/main/concurrent/piconditionlock.h | 2 +- lib/main/core/piincludes.h | 2 +- lib/main/introspection/piintrospection_base.h | 1 - lib/main/thread/pimutex.cpp | 133 ------------------ lib/main/thread/pimutex.h | 47 +------ lib/main/thread/pithread.h | 2 +- lib/main/thread/pitimer.h | 2 +- 9 files changed, 12 insertions(+), 182 deletions(-) delete mode 100644 lib/main/thread/pimutex.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ff959ed..a550a01d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.0) cmake_policy(SET CMP0017 NEW) # need include() with .cmake project(pip) set(_PIP_MAJOR 1) -set(_PIP_MINOR 24) +set(_PIP_MINOR 99) set(_PIP_REVISION 0) -set(_PIP_SUFFIX ) +set(_PIP_SUFFIX _pre_alpha) set(_PIP_COMPANY SHS) set(_PIP_DOMAIN org.SHS) diff --git a/lib/fftw/pifft_p.h b/lib/fftw/pifft_p.h index 92c7f378..b4551a94 100644 --- a/lib/fftw/pifft_p.h +++ b/lib/fftw/pifft_p.h @@ -24,7 +24,6 @@ #define PIFFT_P_H #include "pivector.h" -#include "pimutex.h" #include "picout.h" #if defined(PIP_FFTW) || defined(PIP_FFTWf) || defined(PIP_FFTWl) || defined(PIP_FFTWq) # include "fftw3.h" diff --git a/lib/main/concurrent/piconditionlock.h b/lib/main/concurrent/piconditionlock.h index b44e4633..4919e693 100644 --- a/lib/main/concurrent/piconditionlock.h +++ b/lib/main/concurrent/piconditionlock.h @@ -20,7 +20,7 @@ #ifndef PICONDITIONLOCK_H #define PICONDITIONLOCK_H -#include "pimutex.h" +#include "pibase.h" /** diff --git a/lib/main/core/piincludes.h b/lib/main/core/piincludes.h index 3f40293f..1027228f 100644 --- a/lib/main/core/piincludes.h +++ b/lib/main/core/piincludes.h @@ -30,8 +30,8 @@ #include typedef std::mutex PIMutex; +//typedef std::lock_guard PIMutexLocker; class PIObject; -//class PIMutex; class PIString; class PIByteArray; class PIInit; diff --git a/lib/main/introspection/piintrospection_base.h b/lib/main/introspection/piintrospection_base.h index e838f722..e21f9830 100644 --- a/lib/main/introspection/piintrospection_base.h +++ b/lib/main/introspection/piintrospection_base.h @@ -23,7 +23,6 @@ #include "pibase.h" class PIString; -//class PIMutex; class PIThread; class PITimer; class PIPeer; diff --git a/lib/main/thread/pimutex.cpp b/lib/main/thread/pimutex.cpp deleted file mode 100644 index 1180d338..00000000 --- a/lib/main/thread/pimutex.cpp +++ /dev/null @@ -1,133 +0,0 @@ -/* - PIP - Platform Independent Primitives - Mutex - Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@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 PIMutex - * \brief Mutex - * \details - * \section PIMutex_sec0 Synopsis - * %PIMutex provides synchronization blocks between several threads. - * Using mutex guarantees execution of some code only one of threads. - * Mutex contains logic state and functions to change it: \a lock(), - * \a unlock() and \a tryLock(). - * - * \section PIMutex_sec1 Usage - * Block of code that should to be executed only one thread simultaniously - * should to be started with \a lock() and ended with \a unlock(). - * \snippet pimutex.cpp main - * "mutex" in this example is one for all threads. - * - * */ - -//#include "pimutex.h" -//#include "piincludes_p.h" -//#ifdef BLACKBERRY -//# include -//#endif - - -//PRIVATE_DEFINITION_START(PIMutex) -//#ifdef WINDOWS -// HANDLE -//#else -// pthread_mutex_t -//#endif -// mutex; -//PRIVATE_DEFINITION_END(PIMutex) - - -//PIMutex::PIMutex(): inited_(false) { -// //printf("new Mutex %p\n", this); -//#ifdef WINDOWS -// PRIVATE->mutex = 0; -//#endif -// init(); -//} - - -//PIMutex::~PIMutex() { -// //printf("del Mutex %p\n", this); -// destroy(); -//} - - -//void PIMutex::lock() { -//#ifdef WINDOWS -//// std::cout << (ullong)PRIVATE->mutex << "locking..." << std::endl; -//// DWORD wr = -// WaitForSingleObject(PRIVATE->mutex, INFINITE); -//// std::cout << (ullong)PRIVATE->mutex << " lock wr=" << wr << std::endl; -//#else -// pthread_mutex_lock(&(PRIVATE->mutex)); -//#endif -//} - - -//void PIMutex::unlock() { -//#ifdef WINDOWS -//// BOOL wr = -//// ReleaseMutex(PRIVATE->mutex); -// SetEvent(PRIVATE->mutex); -//// std::cout << (ullong)PRIVATE->mutex << " unlock wr=" << wr << std::endl; -//#else -// pthread_mutex_unlock(&(PRIVATE->mutex)); -//#endif -//} - - -//bool PIMutex::tryLock() { -// bool ret = -//#ifdef WINDOWS -// (WaitForSingleObject(PRIVATE->mutex, 0) == WAIT_OBJECT_0); -//#else -// (pthread_mutex_trylock(&(PRIVATE->mutex)) == 0); -//#endif -// return ret; -//} - - -//void PIMutex::init() { -// if (inited_) destroy(); -//#ifdef WINDOWS -// PRIVATE->mutex = CreateEvent(NULL, FALSE, TRUE, NULL); -//// std::cout << "create " << (ullong)PRIVATE->mutex << std::endl; -//#else -// pthread_mutexattr_t attr; -// memset(&attr, 0, sizeof(attr)); -// pthread_mutexattr_init(&attr); -// pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); -// memset(&(PRIVATE->mutex), 0, sizeof(PRIVATE->mutex)); -// pthread_mutex_init(&(PRIVATE->mutex), &attr); -// pthread_mutexattr_destroy(&attr); -//#endif -// inited_ = true; -//} - - -//void PIMutex::destroy() { -// if (inited_) { -//#ifdef WINDOWS -//// std::cout << "destroy " << (ullong)PRIVATE->mutex << std::endl; -// if (PRIVATE->mutex) CloseHandle(PRIVATE->mutex); -// PRIVATE->mutex = 0; -//#else -// pthread_mutex_destroy(&(PRIVATE->mutex)); -//#endif -// } -// inited_ = false; -//} diff --git a/lib/main/thread/pimutex.h b/lib/main/thread/pimutex.h index 24ce7868..0c72f50b 100644 --- a/lib/main/thread/pimutex.h +++ b/lib/main/thread/pimutex.h @@ -26,51 +26,16 @@ #include "piinit.h" -//class PIP_EXPORT PIMutex -//{ -//public: -// //! Constructs unlocked mutex -// explicit PIMutex(); - -// //! Destroy mutex -// ~PIMutex(); - - -// //! \brief Lock mutex -// //! \details If mutex is unlocked it set to locked state and returns immediate. -// //! If mutex is already locked function blocks until mutex will be unlocked -// void lock(); - -// //! \brief Unlock mutex -// //! \details In any case this function returns immediate -// void unlock(); - -// //! \brief Try to lock mutex -// //! \details If mutex is unlocked it set to locked state and returns "true" immediate. -// //! If mutex is already locked function returns immediate an returns "false" -// bool tryLock(); - -// //! Returns if mutex is locked -// bool isLocked() const; - -//private: -// NO_COPY_CLASS(PIMutex) - -// void init(); -// void destroy(); -// bool inited_; -// PRIVATE_DECLARATION -//}; - - class PIP_EXPORT PIMutexLocker { public: - PIMutexLocker(PIMutex * m, bool condition = true): mutex(m), cond(condition) {if (cond) mutex->lock();} - PIMutexLocker(PIMutex & m, bool condition = true): mutex(&m), cond(condition) {if (cond) mutex->lock();} - ~PIMutexLocker() {if (cond) mutex->unlock();} + PIMutexLocker(PIMutex & m, bool condition = true): mutex(m), cond(condition) {if (cond) mutex.lock();} + ~PIMutexLocker() {if (cond) mutex.unlock();} + + PIMutexLocker(const PIMutexLocker&) = delete; + PIMutexLocker& operator=(const PIMutexLocker&) = delete; private: - PIMutex * mutex; + PIMutex & mutex; std::atomic_bool cond; }; diff --git a/lib/main/thread/pithread.h b/lib/main/thread/pithread.h index a2805bea..ababe98e 100644 --- a/lib/main/thread/pithread.h +++ b/lib/main/thread/pithread.h @@ -246,7 +246,7 @@ protected: //! Function executed once at the end of thread. virtual void end() {;} - volatile bool terminating, running_, lockRun; + std::atomic_bool terminating, running_, lockRun; int delay_, policy_; llong tid_; void * data_; diff --git a/lib/main/thread/pitimer.h b/lib/main/thread/pitimer.h index 2d58b847..e5109cab 100644 --- a/lib/main/thread/pitimer.h +++ b/lib/main/thread/pitimer.h @@ -68,7 +68,7 @@ protected: double interval_, deferred_delay; bool deferred_, deferred_mode; // mode: true - date, false - delay - volatile bool running_; + std::atomic_bool running_; PIDateTime deferred_datetime; };