diff --git a/libbitdht/src/udp/udpbitdht.cc b/libbitdht/src/udp/udpbitdht.cc index 4a69d3805..5f2a184a4 100644 --- a/libbitdht/src/udp/udpbitdht.cc +++ b/libbitdht/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/libbitdht/src/util/bdthreads.h b/libbitdht/src/util/bdthreads.h index a64ea6fa9..1ba260e54 100644 --- a/libbitdht/src/util/bdthreads.h +++ b/libbitdht/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