Fix RsMutex Debug and add rs_mutex_debug qmake option.

This commit is contained in:
Phenom 2020-12-08 17:15:16 +01:00
parent 8f5656433a
commit 2ff3d83c30
3 changed files with 26 additions and 26 deletions

View File

@ -22,20 +22,20 @@
* * * *
*******************************************************************************/ *******************************************************************************/
#include <iostream>
#include <ctime>
#include <thread>
#include <chrono>
#ifdef RSMUTEX_DEBUG
#include <cstdio>
#include <sys/time.h>
#endif
#include "rsthreads.h" #include "rsthreads.h"
#include "util/rsdebug.h" #include "util/rsdebug.h"
#include "util/rserrno.h" #include "util/rserrno.h"
#include <chrono>
#include <ctime>
#include <iostream>
#include <thread>
#ifdef RS_MUTEX_DEBUG
#include <cstdio>
#include <sys/time.h>
#endif
#ifdef __APPLE__ #ifdef __APPLE__
int __attribute__((weak)) pthread_setname_np(const char *__buf) ; int __attribute__((weak)) pthread_setname_np(const char *__buf) ;
@ -280,8 +280,8 @@ void RsMutex::lock()
{ {
RsErr() << __PRETTY_FUNCTION__ << "pthread_mutex_lock returned: " RsErr() << __PRETTY_FUNCTION__ << "pthread_mutex_lock returned: "
<< rsErrnoName(err) << rsErrnoName(err)
#ifdef RSMUTEX_DEBUG #ifdef RS_MUTEX_DEBUG
<< " name: " << name << " name: " << name()
#endif #endif
<< std::endl; << std::endl;
@ -295,7 +295,7 @@ void RsMutex::lock()
_thread_id = pthread_self(); _thread_id = pthread_self();
} }
#ifdef RSMUTEX_DEBUG #ifdef RS_MUTEX_DEBUG
double RsStackMutex::getCurrentTS() double RsStackMutex::getCurrentTS()
{ {

View File

@ -39,8 +39,6 @@
# include "util/rstime.h" # include "util/rstime.h"
#endif #endif
//#define RSMUTEX_DEBUG
/** /**
* @brief Provide mutexes that keep track of the owner. Based on pthread mutex. * @brief Provide mutexes that keep track of the owner. Based on pthread mutex.
*/ */
@ -49,13 +47,13 @@ class RsMutex
public: public:
RsMutex(const std::string& name) : _thread_id(0) RsMutex(const std::string& name) : _thread_id(0)
#ifdef RSMUTEX_DEBUG #ifdef RS_MUTEX_DEBUG
, _name(name) , _name(name)
#endif #endif
{ {
pthread_mutex_init(&realMutex, nullptr); pthread_mutex_init(&realMutex, nullptr);
#ifndef RSMUTEX_DEBUG #ifndef RS_MUTEX_DEBUG
(void) name; // remove unused parameter warnings (void) name; // remove unused parameter warnings
#endif #endif
} }
@ -68,7 +66,7 @@ public:
void unlock(); void unlock();
bool trylock() { return (0 == pthread_mutex_trylock(&realMutex)); } bool trylock() { return (0 == pthread_mutex_trylock(&realMutex)); }
#ifdef RSMUTEX_DEBUG #ifdef RS_MUTEX_DEBUG
const std::string& name() const { return _name ; } const std::string& name() const { return _name ; }
#endif #endif
@ -76,7 +74,7 @@ private:
pthread_mutex_t realMutex; pthread_mutex_t realMutex;
pthread_t _thread_id; pthread_t _thread_id;
#ifdef RSMUTEX_DEBUG #ifdef RS_MUTEX_DEBUG
std::string _name; std::string _name;
#endif #endif
}; };
@ -98,7 +96,7 @@ private:
/** /**
* Provide mutexes that automatically lock/unlock on creation/destruction and * Provide mutexes that automatically lock/unlock on creation/destruction and
* have powerfull debugging facilities (if RSMUTEX_DEBUG is defined at * have powerfull debugging facilities (if RS_MUTEX_DEBUG is defined at
* compiletime). * compiletime).
* In most of the cases you should not use this directly instead * In most of the cases you should not use this directly instead
* @see RS_STACK_MUTEX(m) * @see RS_STACK_MUTEX(m)
@ -110,7 +108,7 @@ public:
RsStackMutex(RsMutex &mtx) : mMtx(mtx) RsStackMutex(RsMutex &mtx) : mMtx(mtx)
{ {
mMtx.lock(); mMtx.lock();
#ifdef RSMUTEX_DEBUG #ifdef RS_MUTEX_DEBUG
double ts = getCurrentTS(); double ts = getCurrentTS();
_time_stamp = ts; _time_stamp = ts;
_lineno = 0; _lineno = 0;
@ -120,11 +118,11 @@ public:
RsStackMutex(RsMutex &mtx, const char *function_name, const char *file_name, RsStackMutex(RsMutex &mtx, const char *function_name, const char *file_name,
int lineno) : mMtx(mtx) int lineno) : mMtx(mtx)
#ifdef RSMUTEX_DEBUG #ifdef RS_MUTEX_DEBUG
, _info(std::string(function_name)+" in file "+file_name), _lineno(lineno) , _info(std::string(function_name)+" in file "+file_name), _lineno(lineno)
#endif #endif
{ {
#ifdef RSMUTEX_DEBUG #ifdef RS_MUTEX_DEBUG
double ts = getCurrentTS(); double ts = getCurrentTS();
_time_stamp = ts; _time_stamp = ts;
pthread_t owner = mMtx.owner(); pthread_t owner = mMtx.owner();
@ -135,7 +133,7 @@ public:
mMtx.lock(); mMtx.lock();
#ifdef RSMUTEX_DEBUG #ifdef RS_MUTEX_DEBUG
ts = getCurrentTS(); ts = getCurrentTS();
if(ts - _time_stamp > 1.0) if(ts - _time_stamp > 1.0)
@ -152,7 +150,7 @@ public:
{ {
mMtx.unlock(); mMtx.unlock();
#ifdef RSMUTEX_DEBUG #ifdef RS_MUTEX_DEBUG
double ts = getCurrentTS(); double ts = getCurrentTS();
if(ts - _time_stamp > 1.0) if(ts - _time_stamp > 1.0)
@ -166,7 +164,7 @@ public:
private: private:
RsMutex &mMtx; RsMutex &mMtx;
#ifdef RSMUTEX_DEBUG #ifdef RS_MUTEX_DEBUG
static double getCurrentTS(); static double getCurrentTS();
double _time_stamp; double _time_stamp;
std::string _info; std::string _info;

View File

@ -577,6 +577,8 @@ rs_broadcast_discovery:DEFINES *= RS_BROADCAST_DISCOVERY
no_rs_dh_init_check:DEFINES *= RS_DISABLE_DIFFIE_HELLMAN_INIT_CHECK no_rs_dh_init_check:DEFINES *= RS_DISABLE_DIFFIE_HELLMAN_INIT_CHECK
debug { debug {
rs_mutex_debug:DEFINES *= RS_MUTEX_DEBUG
QMAKE_CXXFLAGS -= -O2 -fomit-frame-pointer QMAKE_CXXFLAGS -= -O2 -fomit-frame-pointer
QMAKE_CFLAGS -= -O2 -fomit-frame-pointer QMAKE_CFLAGS -= -O2 -fomit-frame-pointer