Added a name to the RsMutex class.

You can enable the debugging of the waiting time for a lock of RsMutex in rsthreads.h with
#define RSMUTEX_DEBUG 300
That means all locks waiting longer than 300ms are logged into the stderr.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4392 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2011-07-04 22:59:39 +00:00
parent a94568ac33
commit de87a89437
42 changed files with 103 additions and 64 deletions

View File

@ -63,7 +63,7 @@ std::ostream &operator<<(std::ostream &out, const CacheData &d)
********************************* Cache Store / Source *************************/
CacheSource::CacheSource(uint16_t t, bool m, CacheStrapper *cs, std::string cachedir)
:cacheType(t), multiCache(m), mStrapper(cs), cacheDir(cachedir)
:cacheType(t), multiCache(m), mStrapper(cs), cacheDir(cachedir), cMutex("CacheSource")
{
return;
}
@ -218,7 +218,7 @@ void CacheSource::listCaches(std::ostream &out)
CacheStore::CacheStore(uint16_t t, bool m,
CacheStrapper *cs, CacheTransfer *cft, std::string cachedir)
:cacheType(t), multiCache(m), mStrapper(cs),
cacheTransfer(cft), cacheDir(cachedir)
cacheTransfer(cft), cacheDir(cachedir), cMutex("CacheStore")
{
/* not much */
return;
@ -543,7 +543,7 @@ void CacheStore::locked_storeCacheEntry(const CacheData &data)
********************************* CacheStrapper ********************************/
CacheStrapper::CacheStrapper(p3ConnectMgr *cm)
:p3Config(CONFIG_TYPE_CACHE), mConnMgr(cm)
:p3Config(CONFIG_TYPE_CACHE), mConnMgr(cm), csMtx("CacheStrapper")
{
return;
}

View File

@ -50,7 +50,7 @@
// ***********/
FileIndexMonitor::FileIndexMonitor(CacheStrapper *cs, NotifyBase *cb_in,std::string cachedir, std::string pid,const std::string& config_dir)
:CacheSource(RS_SERVICE_TYPE_FILE_INDEX, false, cs, cachedir), fi(pid),
:CacheSource(RS_SERVICE_TYPE_FILE_INDEX, false, cs, cachedir), fiMutex("FileIndexMonitor"), fi(pid),
pendingDirs(false), pendingForceCacheWrite(false),
mForceCheck(false), mInCheck(false),cb(cb_in), hashCache(config_dir+"/" + "file_cache.lst"),useHashCache(true)

View File

@ -50,7 +50,7 @@ static const char FILE_CACHE_SEPARATOR_CHAR = '|' ;
* #define FI_DEBUG_ALL 1
****/
static RsMutex FIndexPtrMtx ;
static RsMutex FIndexPtrMtx("FIndexPtrMtx") ;
std::tr1::unordered_set<void*> FileIndex::_pointers ;
void FileIndex::registerEntry(void*p)

View File

@ -72,7 +72,7 @@ virtual int dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdI
p3BitDht::p3BitDht(std::string id, pqiConnectCb *cb, UdpStack *udpstack, std::string bootstrapfile)
:pqiNetAssistConnect(id, cb)
:pqiNetAssistConnect(id, cb), dhtMtx("p3BitDht")
{
std::string dhtVersion = "RS51"; // should come from elsewhere!
bdNodeId ownId;

View File

@ -100,6 +100,8 @@ ftController::ftController(CacheStrapper *cs, ftDataMultiplex *dm, std::string c
last_clean_time(0),
mDataplex(dm),
mTurtle(NULL),
ctrlMutex("ftController"),
doneMutex("ftController"),
mFtActive(false),
mDefaultChunkStrategy(FileChunksInfo::CHUNK_STRATEGY_RANDOM)
{

View File

@ -64,7 +64,7 @@ ftRequest::ftRequest(uint32_t type, std::string peerId, std::string hash, uint64
}
ftDataMultiplex::ftDataMultiplex(std::string ownId, ftDataSend *server, ftSearch *search)
:RsQueueThread(DMULTIPLEX_MIN, DMULTIPLEX_MAX, DMULTIPLEX_RELAX),
:RsQueueThread(DMULTIPLEX_MIN, DMULTIPLEX_MAX, DMULTIPLEX_RELAX), dataMtx("ftDataMultiplex"),
mDataSend(server), mSearch(search), mOwnId(ownId)
{
return;

View File

@ -37,7 +37,7 @@
*****/
ftExtraList::ftExtraList()
:p3Config(CONFIG_TYPE_FT_EXTRA_LIST)
:p3Config(CONFIG_TYPE_FT_EXTRA_LIST), extMutex("p3Config")
{
return;
}

View File

@ -24,7 +24,7 @@
static const time_t UPLOAD_CHUNK_MAPS_TIME = 20 ; // time to ask for a new chunkmap from uploaders in seconds.
ftFileProvider::ftFileProvider(const std::string& path, uint64_t size, const std::string& hash)
: mSize(size), hash(hash), file_name(path), fd(NULL)
: mSize(size), hash(hash), file_name(path), fd(NULL), ftcMutex("ftFileProvider")
{
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/

View File

@ -63,7 +63,7 @@ ftServer::ftServer(p3ConnectMgr *connMgr)
mCacheStrapper(NULL),
mFiStore(NULL), mFiMon(NULL),
mFtController(NULL), mFtExtra(NULL),
mFtDataplex(NULL), mFtSearch(NULL)
mFtDataplex(NULL), mFtSearch(NULL), srvMutex("ftServer")
{
mCacheStrapper = new ftCacheStrapper(connMgr);
}

View File

@ -71,7 +71,7 @@ const uint32_t FT_TM_CRC_MAP_STATE_HAVE = 2 ;
#define FT_TM_FLAG_CHUNK_CRC 4
ftTransferModule::ftTransferModule(ftFileCreator *fc, ftDataMultiplex *dm, ftController *c)
:mFileCreator(fc), mMultiplexor(dm), mFtController(c), mFlag(FT_TM_FLAG_DOWNLOADING)
:mFileCreator(fc), mMultiplexor(dm), mFtController(c), tfMtx("ftTransferModule"), mFlag(FT_TM_FLAG_DOWNLOADING)
{
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
@ -518,7 +518,7 @@ class HashThread: public RsThread
{
public:
HashThread(ftFileCreator *m)
: _m(m),_finished(false),_hash("") {}
: _hashThreadMtx("HashThread"), _m(m),_finished(false),_hash("") {}
virtual void run()
{

View File

@ -134,7 +134,7 @@ gpg_error_t pgp_pwd_callback(void *hook, const char *uid_hint, const char *passp
}
AuthGPGimpl::AuthGPGimpl()
:p3Config(CONFIG_TYPE_AUTHGPG),gpgmeInit(false),gpgmeKeySelected(false)
:p3Config(CONFIG_TYPE_AUTHGPG), gpgMtxEngine("AuthGPG-engine"), gpgMtxData("AuthGPG-data"),gpgmeInit(false),gpgmeKeySelected(false), gpgMtxService("AuthGPG-service")
{
{
RsStackMutex stack(gpgMtxEngine); /******* LOCKED ******/

View File

@ -124,7 +124,7 @@ sslcert::sslcert(X509 *x509, std::string pid)
AuthSSLimpl::AuthSSLimpl()
: p3Config(CONFIG_TYPE_AUTHSSL), sslctx(NULL),
mOwnCert(NULL), mOwnPrivateKey(NULL), mOwnPublicKey(NULL), init(0)
mOwnCert(NULL), sslMtx("AuthSSL"), mOwnPrivateKey(NULL), mOwnPublicKey(NULL), init(0)
{
}

View File

@ -43,7 +43,7 @@
p3ConfigMgr::p3ConfigMgr(std::string dir, std::string fname, std::string signame)
:basedir(dir), metafname(fname), metasigfname(signame),
:basedir(dir), metafname(fname), metasigfname(signame), cfgMtx("p3ConfigMgr"),
mConfigSaveActive(true)
{
oldConfigType = checkForGlobalSigConfig();
@ -1295,7 +1295,7 @@ bool p3GeneralConfig::loadList(std::list<RsItem *>& load)
bool pqiConfig::globalConfigType = false;
pqiConfig::pqiConfig(uint32_t t)
:ConfInd(2), type(t)
: cfgMtx("pqiConfig"), ConfInd(2), type(t)
{
return;
}

View File

@ -176,7 +176,7 @@ void pqiNetStatus::print(std::ostream &out)
p3ConnectMgr::p3ConnectMgr()
:p3Config(CONFIG_TYPE_PEERS),
:p3Config(CONFIG_TYPE_PEERS), connMtx("p3ConnectMgr"),
mNetStatus(RS_NET_UNKNOWN),
mStatusChanged(false)
{

View File

@ -89,7 +89,7 @@ dhtPeerEntry::dhtPeerEntry()
}
p3DhtMgr::p3DhtMgr(std::string id, pqiConnectCb *cb)
:pqiNetAssistConnect(id, cb), mStunRequired(true)
:pqiNetAssistConnect(id, cb), dhtMtx("p3DhtMgr"), mStunRequired(true)
{
/* setup own entry */
dhtMtx.lock(); /* LOCK MUTEX */

View File

@ -66,7 +66,7 @@ class p3Notify: public RsNotify, public pqiNotify
{
public:
p3Notify() { return; }
p3Notify() : noteMtx("p3Notify") { return; }
virtual ~p3Notify() { return; }
/* Output for retroshare-gui */

View File

@ -38,7 +38,7 @@ const int pqihandlerzone = 34283;
#define RSITEM_DEBUG 1
****/
pqihandler::pqihandler(SecurityPolicy *Global)
pqihandler::pqihandler(SecurityPolicy *Global) : coreMtx("pqihandler")
{
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/

View File

@ -33,7 +33,7 @@ const int pqiservicezone = 60478;
* #define SERVICE_DEBUG 1
****/
p3ServiceServer::p3ServiceServer()
p3ServiceServer::p3ServiceServer() : srvMtx("p3ServiceServer")
{
RsStackMutex stack(srvMtx); /********* LOCKED *********/

View File

@ -58,7 +58,7 @@ pqistreamer::pqistreamer(RsSerialiser *rss, std::string id, BinInterface *bio_in
pkt_wpending(NULL),
totalRead(0), totalSent(0),
currRead(0), currSent(0),
avgReadCount(0), avgSentCount(0)
avgReadCount(0), avgSentCount(0), streamerMtx("pqistreamer")
{
avgLastUpdate = currReadTS = currSentTS = time(NULL);

View File

@ -40,7 +40,7 @@
****/
RsServer::RsServer(RsIface &i, NotifyBase &callback)
:RsControl(i, callback)
:RsControl(i, callback), coreMutex("RsServer")
{
ftserver = NULL;

View File

@ -80,7 +80,7 @@ class RsIfaceReal: public RsIface
{
public:
RsIfaceReal(NotifyBase &callback)
:RsIface(callback)
:RsIface(callback), rsIfaceMutex("RsIface")
{ return; }
virtual void lockData()

View File

@ -42,7 +42,7 @@
*/
p3ChatService::p3ChatService(p3ConnectMgr *cm)
:p3Service(RS_SERVICE_TYPE_CHAT), p3Config(CONFIG_TYPE_CHAT), mConnMgr(cm)
:p3Service(RS_SERVICE_TYPE_CHAT), p3Config(CONFIG_TYPE_CHAT), mChatMtx("p3ChatService"), mConnMgr(cm)
{
addSerialType(new RsChatSerialiser());

View File

@ -86,7 +86,7 @@ p3disc::p3disc(p3ConnectMgr *cm, pqipersongrp *pqih)
:p3Service(RS_SERVICE_TYPE_DISC),
p3Config(CONFIG_TYPE_P3DISC),
mConnMgr(cm),
mPqiPersonGrp(pqih)
mPqiPersonGrp(pqih), mDiscMtx("p3disc")
{
RsStackMutex stack(mDiscMtx); /********** STACK LOCKED MTX ******/

View File

@ -105,7 +105,7 @@ p3GroupDistrib::p3GroupDistrib(uint16_t subtype,
:CacheSource(subtype, true, cs, sourcedir),
CacheStore(subtype, true, cs, cft, storedir),
p3Config(configId), p3ThreadedService(subtype),
mHistoricalCaches(true),
mHistoricalCaches(true), distribMtx(""),
mStorePeriod(storePeriod),
mPubPeriod(pubPeriod),
mLastPublishTime(0),
@ -119,9 +119,14 @@ p3GroupDistrib::p3GroupDistrib(uint16_t subtype,
mLastCacheDocUpdate = time(NULL);
mHistoricalCachesLoaded = false;
#ifdef RSMUTEX_DEBUG
distribMtx.setName("p3GroupDistrib - " + keyBackUpDir.substr(keyBackUpDir.find_last_of('/') + 1));
#endif
mOwnId = AuthSSL::getAuthSSL()->OwnId();
addSerialType(new RsDistribSerialiser(getRsItemService(getType())));
return;
}

View File

@ -56,7 +56,7 @@ const int msgservicezone = 54319;
p3MsgService::p3MsgService(p3ConnectMgr *cm)
:p3Service(RS_SERVICE_TYPE_MSG), p3Config(CONFIG_TYPE_MSGS),
mConnMgr(cm), msgChanged(1), mMsgUniqueId(1)
mConnMgr(cm), mMsgMtx("p3MsgService"), msgChanged(1), mMsgUniqueId(1)
{
addSerialType(new RsMsgSerialiser());

View File

@ -49,7 +49,7 @@ PhotoSet::PhotoSet()
p3PhotoService::p3PhotoService(uint16_t type, CacheStrapper *cs, CacheTransfer *cft,
std::string sourcedir, std::string storedir)
:CacheSource(type, true, cs, sourcedir),
CacheStore(type, true, cs, cft, storedir),
CacheStore(type, true, cs, cft, storedir), mPhotoMtx("p3PhotoService"),
mUpdated(true)
{

View File

@ -58,7 +58,7 @@ class p3Service: public pqiService
p3Service(uint16_t type)
:pqiService((((uint32_t) RS_PKT_VERSION_SERVICE) << 24) + (((uint32_t) type) << 8)),
rsSerialiser(NULL)
srvMtx("p3Service"), rsSerialiser(NULL)
{
rsSerialiser = new RsSerialiser();
return;

View File

@ -44,7 +44,7 @@ std::ostream& operator<<(std::ostream& out, const StatusInfo& si)
RsStatus *rsStatus = NULL;
p3StatusService::p3StatusService(p3ConnectMgr *cm)
:p3Service(RS_SERVICE_TYPE_STATUS), p3Config(CONFIG_TYPE_STATUS), mConnMgr(cm)
:p3Service(RS_SERVICE_TYPE_STATUS), p3Config(CONFIG_TYPE_STATUS), mConnMgr(cm), mStatusMtx("p3StatusService")
{
addSerialType(new RsStatusSerialiser());

View File

@ -72,7 +72,7 @@ static const double RTT_ALPHA = 0.875;
static double getCurrentTS();
TcpStream::TcpStream(UdpSubReceiver *lyr)
:inSize(0), outSizeRead(0), outSizeNet(0),
: tcpMtx("TcpStream"), inSize(0), outSizeRead(0), outSizeNet(0),
state(TCP_CLOSED),
inStreamActive(false),
outStreamActive(false),

View File

@ -32,7 +32,7 @@
UdpPeerReceiver::UdpPeerReceiver(UdpPublisher *pub)
:UdpSubReceiver(pub)
:UdpSubReceiver(pub), peerMtx("UdpSubReceiver")
{
return;
}

View File

@ -45,7 +45,7 @@ int displayUdpRelayPacketHeader(const void *data, const int size);
#define MAX_RELAY_UDP_PACKET_SIZE 1024
UdpRelayReceiver::UdpRelayReceiver(UdpPublisher *pub)
:UdpSubReceiver(pub)
:UdpSubReceiver(pub), udppeerMtx("UdpSubReceiver"), relayMtx("UdpSubReceiver")
{
mClassLimit.resize(UDP_RELAY_NUM_CLASS);
mClassCount.resize(UDP_RELAY_NUM_CLASS);

View File

@ -48,7 +48,7 @@ const double TOU_SUCCESS_LPF_FACTOR = 0.90;
UdpStunner::UdpStunner(UdpPublisher *pub)
:UdpSubReceiver(pub), eaddrKnown(false), eaddrStable(false),
:UdpSubReceiver(pub), stunMtx("UdpSubReceiver"), eaddrKnown(false), eaddrStable(false),
mStunLastRecvResp(0), mStunLastRecvAny(0),
mStunLastSendStun(0), mStunLastSendAny(0)
{

View File

@ -88,7 +88,7 @@ static const uint32_t MAX_TUNNEL_REQS_PER_SECOND= 1 ; /// maximum number of t
static const uint32_t MAX_ALLOWED_SR_IN_CACHE = 120 ; /// maximum number of search requests allowed in cache. That makes 2 per sec.
p3turtle::p3turtle(p3ConnectMgr *cm,ftServer *fs)
:p3Service(RS_SERVICE_TYPE_TURTLE), p3Config(CONFIG_TYPE_TURTLE), mConnMgr(cm)
:p3Service(RS_SERVICE_TYPE_TURTLE), p3Config(CONFIG_TYPE_TURTLE), mConnMgr(cm), mTurtleMtx("p3turtle")
{
RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/

View File

@ -840,7 +840,7 @@ bool upnphandler::shutdown_upnp()
*/
upnphandler::upnphandler()
:toEnable(false), toStart(false), toStop(false),
: dataMtx("upnpState"), toEnable(false), toStart(false), toStop(false),
eport(0), eport_curr(0),
upnpState(RS_UPNP_S_UNINITIALISED),
upnpConfig(NULL)

View File

@ -154,7 +154,7 @@ DNSResolver::~DNSResolver()
delete _thread_running ;
}
DNSResolver::DNSResolver()
DNSResolver::DNSResolver() : _rdnsMtx("DNSResolver")
{
RsStackMutex mut(_rdnsMtx) ;

View File

@ -271,7 +271,7 @@ ExtAddrFinder::~ExtAddrFinder()
free (_addr) ;
}
ExtAddrFinder::ExtAddrFinder()
ExtAddrFinder::ExtAddrFinder() : _addrMtx("ExtAddrFinder")
{
#ifdef EXTADDRSEARCH_DEBUG
std::cerr << "ExtAddrFinder: Creating new ExtAddrFinder." << std::endl ;

View File

@ -45,7 +45,7 @@ static int lineCount = 0;
static std::string crashfile;
static int debugTS = 0;
static RsMutex logMtx;
static RsMutex logMtx("logMtx");
int locked_setDebugFile(const char *fname);
int locked_getZoneLevel(int zone);

View File

@ -43,7 +43,7 @@ time_t RsDiscSpace::_last_check[3] = { 0,0,0 } ;
uint32_t RsDiscSpace::_size_limit_mb = 100 ;
uint32_t RsDiscSpace::_current_size[3] = { 10000,10000,10000 } ;
bool RsDiscSpace::_last_res[3] = { true,true,true };
RsMutex RsDiscSpace::_mtx ;
RsMutex RsDiscSpace::_mtx("RsDiscSpace") ;
bool RsDiscSpace::crossSystemDiskStats(const char *file, uint64_t& free_blocks, uint64_t& block_size)
{

View File

@ -5,7 +5,7 @@
uint32_t RSRandom::index = 0 ;
std::vector<uint32_t> RSRandom::MT(RSRandom::N,0u) ;
RsMutex RSRandom::rndMtx ;
RsMutex RSRandom::rndMtx("RSRandom") ;
#ifdef WINDOWS_SYS
static bool auto_seed = RSRandom::seed( (time(NULL) + ((uint32_t) pthread_self().p)*0x1293fe)^0x18e34a12 ) ;

View File

@ -30,6 +30,10 @@
#include <errno.h> /* for usleep() */
#include <iostream>
#ifdef RSMUTEX_DEBUG
#include <stdio.h>
#endif
/*******
* #define DEBUG_THREADS 1
*******/
@ -86,7 +90,7 @@ pthread_t createThread(RsThread &thread)
}
RsThread::RsThread ()
RsThread::RsThread () : mMutex("RsThread")
{
mIsRunning = true;
@ -169,3 +173,30 @@ void RsQueueThread::run()
#endif
}
}
void RsMutex::lock()
{
#ifdef RSMUTEX_DEBUG
clock_t t1 = clock();
#endif
#ifdef RSTHREAD_SELF_LOCKING_GUARD
if(!trylock())
if(!pthread_equal(_thread_id,pthread_self()))
#endif
pthread_mutex_lock(&realMutex);
_thread_id = pthread_self() ;
#ifdef RSTHREAD_SELF_LOCKING_GUARD
++_cnt ;
#endif
#ifdef RSMUTEX_DEBUG
clock_t t2 = clock();
double duration = (double) (t2 - t1) / CLOCKS_PER_SEC;
if (duration * 1000 > RSMUTEX_DEBUG) {
fprintf(stderr, "RsMutex::lock() %s --> %.3fs\n", name.c_str(), duration);
fflush(stderr);
}
#endif
}

View File

@ -37,16 +37,20 @@
/* RsIface Thread Wrappers */
#undef RSTHREAD_SELF_LOCKING_GUARD
//#define RSMUTEX_DEBUG 300 // Milliseconds for print in the stderr
class RsMutex
{
public:
RsMutex()
RsMutex(const std::string &name)
{
pthread_mutex_init(&realMutex, NULL);
#ifdef RSTHREAD_SELF_LOCKING_GUARD
_thread_id = 0 ;
#endif
#ifdef RSMUTEX_DEBUG
this->name = name;
#endif
}
~RsMutex()
@ -54,19 +58,14 @@ class RsMutex
pthread_mutex_destroy(&realMutex);
}
void lock()
#ifdef RSMUTEX_DEBUG
void setName(const std::string &name)
{
#ifdef RSTHREAD_SELF_LOCKING_GUARD
if(!trylock())
if(!pthread_equal(_thread_id,pthread_self()))
#endif
pthread_mutex_lock(&realMutex);
_thread_id = pthread_self() ;
#ifdef RSTHREAD_SELF_LOCKING_GUARD
++_cnt ;
#endif
this->name = name;
}
#endif
void lock();
void unlock()
{
#ifdef RSTHREAD_SELF_LOCKING_GUARD
@ -89,6 +88,9 @@ class RsMutex
#ifdef RSTHREAD_SELF_LOCKING_GUARD
uint32_t _cnt ;
#endif
#ifdef RSMUTEX_DEBUG
std::string name;
#endif
};
class RsStackMutex
@ -150,5 +152,4 @@ virtual bool doWork() = 0;
float mRelaxFactor;
};
#endif

View File

@ -4,7 +4,7 @@
using namespace RsMemoryManagement ;
RsMutex SmallObject::_mtx ;
RsMutex SmallObject::_mtx("SmallObject") ;
SmallObjectAllocator SmallObject::_allocator(RsMemoryManagement::MAX_SMALL_OBJECT_SIZE) ;
void Chunk::init(size_t blockSize,unsigned char blocks)