From 915acd079bf6dd75aae32fff41c4fb8a2c3d00c8 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 21 Nov 2010 22:46:47 +0000 Subject: [PATCH] enabled recursive lock behavior on main bitdht thread to handle self-lock in DHT callbacks git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3849 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- src/udp/udpbitdht.cc | 3 ++- src/util/bdthreads.h | 28 ++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/udp/udpbitdht.cc b/src/udp/udpbitdht.cc index 4a69d38..5f2a184 100644 --- a/src/udp/udpbitdht.cc +++ b/src/udp/udpbitdht.cc @@ -51,7 +51,7 @@ /*************************************/ UdpBitDht::UdpBitDht(UdpPublisher *pub, bdNodeId *id, std::string appVersion, std::string bootstrapfile, bdDhtFunctions *fns) - :UdpSubReceiver(pub), mFns(fns) + :UdpSubReceiver(pub), dhtMtx(true), mFns(fns) { std::string usedVersion; @@ -210,6 +210,7 @@ void UdpBitDht::run() } { + bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/ mBitDhtManager->iteration(); } sleep(1); diff --git a/src/util/bdthreads.h b/src/util/bdthreads.h index a64ea6f..1ba260e 100644 --- a/src/util/bdthreads.h +++ b/src/util/bdthreads.h @@ -27,6 +27,7 @@ */ +#include #include #include @@ -36,14 +37,29 @@ class bdMutex { public: - bdMutex() { pthread_mutex_init(&realMutex, NULL); } - ~bdMutex() { pthread_mutex_destroy(&realMutex); } -void lock() { pthread_mutex_lock(&realMutex); } -void unlock() { pthread_mutex_unlock(&realMutex); } -bool trylock() { return (0 == pthread_mutex_trylock(&realMutex)); } + bdMutex(bool recursive = false) + { + if(recursive) + { + pthread_mutexattr_t att ; + pthread_mutexattr_init(&att) ; + pthread_mutexattr_settype(&att,PTHREAD_MUTEX_RECURSIVE) ; + + if( pthread_mutex_init(&realMutex, &att)) + std::cerr << "ERROR: Could not initialize mutex !" << std::endl ; + } + else + if( pthread_mutex_init(&realMutex, NULL)) + std::cerr << "ERROR: Could not initialize mutex !" << std::endl ; + } + + ~bdMutex() { pthread_mutex_destroy(&realMutex); } + void lock() { pthread_mutex_lock(&realMutex); } + void unlock() { pthread_mutex_unlock(&realMutex); } + bool trylock() { return (0 == pthread_mutex_trylock(&realMutex)); } private: - pthread_mutex_t realMutex; + pthread_mutex_t realMutex; }; class bdStackMutex