PIMutex typedef std::mutex, patch PIMutexLocker, replace volatile by atomic

This commit is contained in:
2020-07-17 11:32:04 +03:00
parent 6f5c864e9f
commit 1a3096c48b
9 changed files with 12 additions and 182 deletions

View File

@@ -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)

View File

@@ -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"

View File

@@ -20,7 +20,7 @@
#ifndef PICONDITIONLOCK_H
#define PICONDITIONLOCK_H
#include "pimutex.h"
#include "pibase.h"
/**

View File

@@ -30,8 +30,8 @@
#include <mutex>
typedef std::mutex PIMutex;
//typedef std::lock_guard<std::mutex> PIMutexLocker;
class PIObject;
//class PIMutex;
class PIString;
class PIByteArray;
class PIInit;

View File

@@ -23,7 +23,6 @@
#include "pibase.h"
class PIString;
//class PIMutex;
class PIThread;
class PITimer;
class PIPeer;

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
/** \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 <pthread.h>
//#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;
//}

View File

@@ -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;
};

View File

@@ -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_;

View File

@@ -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;
};