diff --git a/daemon/src/scoped_lock.cpp b/daemon/src/scoped_lock.cpp index e92f5bb4a037f7cf36fa59468bf6ec869ebad189..ab3918785dbd55d810fa6d5b30a5b0453fa3e260 100644 --- a/daemon/src/scoped_lock.cpp +++ b/daemon/src/scoped_lock.cpp @@ -36,6 +36,11 @@ ScopedLock::ScopedLock(pthread_mutex_t &mutex) : mutex_(mutex) { pthread_mutex_lock(&mutex_); } +ScopedLock::ScopedLock(pthread_mutex_t &mutex, bool &acquired) : mutex_(mutex) { + acquired = (pthread_mutex_trylock(&mutex_) == 0); +} + + ScopedLock::~ScopedLock() { pthread_mutex_unlock(&mutex_); } diff --git a/daemon/src/scoped_lock.h b/daemon/src/scoped_lock.h index 3473927043efcd966d36c84dfeca81d23082d71c..0dc323da8d61bf0c8bd52d9fb6bf0a6b6bfd5520 100644 --- a/daemon/src/scoped_lock.h +++ b/daemon/src/scoped_lock.h @@ -42,7 +42,9 @@ namespace sfl { class ScopedLock { public: - ScopedLock(pthread_mutex_t &mutex); + explicit ScopedLock(pthread_mutex_t &mutex); + // acquired will be set to true if the mutex was locked immediately + ScopedLock(pthread_mutex_t &mutex, bool &acquired); ~ScopedLock(); private: diff --git a/daemon/src/sip/sipvoiplink.cpp b/daemon/src/sip/sipvoiplink.cpp index 0c46b422aa0795c0e46a391e289f8ca33e8019c1..c2dcc535c6bbca37ad131b33f5c2ad87a9532819 100644 --- a/daemon/src/sip/sipvoiplink.cpp +++ b/daemon/src/sip/sipvoiplink.cpp @@ -41,6 +41,7 @@ #include "array_size.h" #include "manager.h" #include "logger.h" +#include "scoped_lock.h" #include "sip/sdp.h" #include "sipcall.h" @@ -74,12 +75,9 @@ #include <arpa/inet.h> #include <resolv.h> #include <istream> -// #include <fstream> #include <utility> // for std::pair #include <algorithm> -#include <map> - using namespace sfl; SIPVoIPLink *SIPVoIPLink::instance_ = 0; @@ -436,6 +434,12 @@ SIPVoIPLink::SIPVoIPLink() : sipTransport(endpt_, cp_, pool_), sipAccountMap_(), throw VoipLinkException(#ret " failed"); \ } while (0) + pthread_mutex_init(&sipCallMapMutex_, NULL); + +#ifdef SFL_VIDEO + pthread_mutex_init(&keyframeRequestsMutex_, NULL); +#endif + srand(time(NULL)); // to get random number for RANDOM_PORT TRY(pj_init()); @@ -540,6 +544,11 @@ SIPVoIPLink::~SIPVoIPLink() std::for_each(sipAccountMap_.begin(), sipAccountMap_.end(), unloadAccount); sipAccountMap_.clear(); + + pthread_mutex_destroy(&sipCallMapMutex_); +#ifdef SFL_VIDEO + pthread_mutex_destroy(&keyframeRequestsMutex_); +#endif } SIPVoIPLink* SIPVoIPLink::instance() @@ -1130,7 +1139,7 @@ void SIPVoIPLink::sendTextMessage(const std::string &callID, void SIPVoIPLink::clearSipCallMap() { - ost::MutexLock m(sipCallMapMutex_); + sfl::ScopedLock m(sipCallMapMutex_); for (SipCallMap::const_iterator iter = sipCallMap_.begin(); iter != sipCallMap_.end(); ++iter) @@ -1142,14 +1151,14 @@ SIPVoIPLink::clearSipCallMap() void SIPVoIPLink::addSipCall(SIPCall* call) { if (call and getSipCall(call->getCallId()) == NULL) { - ost::MutexLock m(sipCallMapMutex_); + sfl::ScopedLock m(sipCallMapMutex_); sipCallMap_[call->getCallId()] = call; } } void SIPVoIPLink::removeSipCall(const std::string& id) { - ost::MutexLock m(sipCallMapMutex_); + sfl::ScopedLock m(sipCallMapMutex_); DEBUG("Removing call %s from list", id.c_str()); @@ -1160,7 +1169,7 @@ void SIPVoIPLink::removeSipCall(const std::string& id) SIPCall* SIPVoIPLink::getSipCall(const std::string& id) { - ost::MutexLock m(sipCallMapMutex_); + sfl::ScopedLock m(sipCallMapMutex_); SipCallMap::iterator iter = sipCallMap_.find(id); @@ -1173,16 +1182,16 @@ SIPVoIPLink::getSipCall(const std::string& id) SIPCall* SIPVoIPLink::tryGetSIPCall(const std::string &id) { - if (not sipCallMapMutex_.tryEnterMutex()) { - ERROR("Could not lock call map mutex"); - sipCallMapMutex_.leaveMutex(); - return 0; - } - SipCallMap::iterator iter = sipCallMap_.find(id); + bool acquired = false; + sfl::ScopedLock m(sipCallMapMutex_, acquired); SIPCall *call = 0; - if (iter != sipCallMap_.end()) - call = iter->second; - sipCallMapMutex_.leaveMutex(); + if (acquired) { + SipCallMap::iterator iter = sipCallMap_.find(id); + if (iter != sipCallMap_.end()) + call = iter->second; + } else + ERROR("Could not acquire SIPCallMap mutex"); + return call; } @@ -1370,7 +1379,7 @@ dtmfSend(SIPCall &call, char code, const std::string &dtmf) void SIPVoIPLink::enqueueKeyframeRequest(const std::string &id) { - ost::MutexLock m(instance_->keyframeRequestsMutex_); + sfl::ScopedLock m(instance_->keyframeRequestsMutex_); instance_->keyframeRequests_.push(id); } @@ -1380,7 +1389,7 @@ SIPVoIPLink::dequeKeyframeRequests() { int max_requests = 20; while (not keyframeRequests_.empty() and max_requests--) { - ost::MutexLock m(keyframeRequestsMutex_); + sfl::ScopedLock m(keyframeRequestsMutex_); const std::string &id(keyframeRequests_.front()); requestKeyframe(id); keyframeRequests_.pop(); diff --git a/daemon/src/sip/sipvoiplink.h b/daemon/src/sip/sipvoiplink.h index 005a94a3e2c24fa217179bd64a5d21568880e15f..1c8fbdc6bc806cbc667d0ec57d547365b94459b0 100644 --- a/daemon/src/sip/sipvoiplink.h +++ b/daemon/src/sip/sipvoiplink.h @@ -41,19 +41,18 @@ #endif #include <map> - -#include <pjsip.h> -#include <pjlib.h> -#include <pjsip_ua.h> -#include <pjlib-util.h> -#include <pjnath.h> -#include <pjnath/stun_config.h> +#include <pthread.h> + +#include "pjsip.h" +#include "pjlib.h" +#include "pjsip_ua.h" +#include "pjlib-util.h" +#include "pjnath.h" +#include "pjnath/stun_config.h" #ifdef SFL_VIDEO #include <queue> #endif -#include "cc_thread.h" - #include "sipaccount.h" #include "voiplink.h" #include "siptransport.h" @@ -309,7 +308,7 @@ class SIPVoIPLink : public VoIPLink { */ AccountMap sipAccountMap_; - ost::Mutex sipCallMapMutex_; + pthread_mutex_t sipCallMapMutex_; SipCallMap sipCallMap_; /** @@ -327,7 +326,7 @@ class SIPVoIPLink : public VoIPLink { #ifdef SFL_VIDEO void dequeKeyframeRequests(); void requestKeyframe(const std::string &callID); - ost::Mutex keyframeRequestsMutex_; + pthread_mutex_t keyframeRequestsMutex_; std::queue<std::string> keyframeRequests_; #endif