From 83f9a6ff75a0ec9a17af2ba80a67474b729ed50e Mon Sep 17 00:00:00 2001
From: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
Date: Tue, 8 Jul 2014 10:49:33 -0400
Subject: [PATCH] iax: make IAX lock global

Refs #51555

Change-Id: I7b4583a4e6b63a83f69947eec1e674cd9ba580ec
---
 daemon/src/iax/iaxvoiplink.cpp | 36 +++++++++++++++++-----------------
 daemon/src/iax/iaxvoiplink.h   |  8 ++++----
 2 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/daemon/src/iax/iaxvoiplink.cpp b/daemon/src/iax/iaxvoiplink.cpp
index 2c8276ed65..f4cf4abed4 100644
--- a/daemon/src/iax/iaxvoiplink.cpp
+++ b/daemon/src/iax/iaxvoiplink.cpp
@@ -48,11 +48,11 @@
 AccountMap IAXVoIPLink::iaxAccountMap_;
 IAXCallMap IAXVoIPLink::iaxCallMap_;
 std::mutex IAXVoIPLink::iaxCallMapMutex_;
+std::mutex IAXVoIPLink::mutexIAX = {};
 
 IAXVoIPLink::IAXVoIPLink(const std::string& accountID) :
     regSession_(NULL)
     , nextRefreshStamp_(0)
-    , mutexIAX_()
     , rawBuffer_(RAW_BUFFER_SIZE, AudioFormat::MONO)
     , resampledData_(RAW_BUFFER_SIZE * 4, AudioFormat::MONO)
     , encodedData_()
@@ -103,7 +103,7 @@ IAXVoIPLink::terminate()
     for (auto & item : iaxCallMap_) {
         auto& call = item.second;
         if (call) {
-            std::lock_guard<std::mutex> lock(mutexIAX_);
+            std::lock_guard<std::mutex> lock(mutexIAX);
             iax_hangup(call->session, const_cast<char*>("Dumped Call"));
             call.reset();
         }
@@ -121,7 +121,7 @@ IAXVoIPLink::getEvent()
     iax_event *event = NULL;
 
     {
-        std::lock_guard<std::mutex> lock(mutexIAX_);
+        std::lock_guard<std::mutex> lock(mutexIAX);
         event = iax_get_event(0);
     }
 
@@ -129,7 +129,7 @@ IAXVoIPLink::getEvent()
 
         // If we received an 'ACK', libiax2 tells apps to ignore them.
         if (event->etype == IAX_EVENT_NULL) {
-            std::lock_guard<std::mutex> lock(mutexIAX_);
+            std::lock_guard<std::mutex> lock(mutexIAX);
             iax_event_free(event);
             event = iax_get_event(0);
             continue;
@@ -148,7 +148,7 @@ IAXVoIPLink::getEvent()
         }
 
         {
-            std::lock_guard<std::mutex> lock(mutexIAX_);
+            std::lock_guard<std::mutex> lock(mutexIAX);
             iax_event_free(event);
             event = iax_get_event(0);
         }
@@ -237,7 +237,7 @@ IAXVoIPLink::sendAudioFromMic()
         compSize = audioCodec->encode(in->getData(), encodedData_, RAW_BUFFER_SIZE);
 
         if (currentCall->session and samples > 0) {
-            std::lock_guard<std::mutex> lock(mutexIAX_);
+            std::lock_guard<std::mutex> lock(mutexIAX);
 
             if (iax_send_voice(currentCall->session, currentCall->format, encodedData_, compSize, outSamples) == -1)
                 ERROR("IAX: Error sending voice data.");
@@ -266,7 +266,7 @@ IAXVoIPLink::sendRegister(Account& a)
     if (account.getUsername().empty())
         throw VoipLinkException("Account username is empty");
 
-    std::lock_guard<std::mutex> lock(mutexIAX_);
+    std::lock_guard<std::mutex> lock(mutexIAX);
 
     if (regSession_)
         iax_destroy(regSession_);
@@ -284,7 +284,7 @@ void
 IAXVoIPLink::sendUnregister(Account& a, std::function<void(bool)> cb)
 {
     if (regSession_) {
-        std::lock_guard<std::mutex> lock(mutexIAX_);
+        std::lock_guard<std::mutex> lock(mutexIAX);
         iax_destroy(regSession_);
         regSession_ = NULL;
     }
@@ -320,7 +320,7 @@ IAXVoIPLink::answer(Call *call)
     Manager::instance().addStream(call->getCallId());
 
     {
-        std::lock_guard<std::mutex> lock(mutexIAX_);
+        std::lock_guard<std::mutex> lock(mutexIAX);
         call->answer();
     }
 
@@ -342,7 +342,7 @@ IAXVoIPLink::hangup(const std::string& id, int reason UNUSED)
             throw VoipLinkException("Could not find call");
 
         {
-            std::lock_guard<std::mutex> lock(mutexIAX_);
+            std::lock_guard<std::mutex> lock(mutexIAX);
             iax_hangup(call->session, (char*) "Dumped Call");
         }
         call->session = NULL;
@@ -382,7 +382,7 @@ IAXVoIPLink::onhold(const std::string& id)
         if (!call)
             throw VoipLinkException("Could not find call");
         {
-            std::lock_guard<std::mutex> lock(mutexIAX_);
+            std::lock_guard<std::mutex> lock(mutexIAX);
             iax_quelch_moh(call->session, true);
         }
         call->setState(Call::HOLD);
@@ -402,7 +402,7 @@ IAXVoIPLink::offhold(const std::string& id)
             throw VoipLinkException("Could not find call");
 
         {
-            std::lock_guard<std::mutex> lock(mutexIAX_);
+            std::lock_guard<std::mutex> lock(mutexIAX);
             iax_unquelch(call->session);
         }
 
@@ -424,7 +424,7 @@ IAXVoIPLink::transfer(const std::string& id, const std::string& to)
         if (!call)
             return;
         {
-            std::lock_guard<std::mutex> lock(mutexIAX_);
+            std::lock_guard<std::mutex> lock(mutexIAX);
             iax_transfer(call->session, callto);
         }
     }
@@ -445,7 +445,7 @@ IAXVoIPLink::refuse(const std::string& id)
         if (!call)
             return;
         {
-            std::lock_guard<std::mutex> lock(mutexIAX_);
+            std::lock_guard<std::mutex> lock(mutexIAX);
             iax_reject(call->session, (char*) "Call rejected manually.");
         }
     }
@@ -462,7 +462,7 @@ IAXVoIPLink::carryingDTMFdigits(const std::string& id, char code)
         return;
 
     {
-        std::lock_guard<std::mutex> lock(mutexIAX_);
+        std::lock_guard<std::mutex> lock(mutexIAX);
         iax_send_dtmf(call->session, code);
     }
 }
@@ -479,7 +479,7 @@ IAXVoIPLink::sendTextMessage(const std::string& callID,
         return;
 
     {
-        std::lock_guard<std::mutex> lock(mutexIAX_);
+        std::lock_guard<std::mutex> lock(mutexIAX);
         sfl::InstantMessaging::send_iax_message(call->session, callID, message.c_str());
     }
 }
@@ -526,7 +526,7 @@ IAXVoIPLink::getIaxCall(const std::string& id)
 void
 IAXVoIPLink::iaxOutgoingInvite(IAXCall* call)
 {
-    std::lock_guard<std::mutex> lock(mutexIAX_);
+    std::lock_guard<std::mutex> lock(mutexIAX);
 
     call->session = iax_session_new();
 
@@ -764,7 +764,7 @@ void IAXVoIPLink::iaxHandleRegReply(iax_event* event)
         return;
 
     {
-        std::lock_guard<std::mutex> lock(mutexIAX_);
+        std::lock_guard<std::mutex> lock(mutexIAX);
         iax_destroy(regSession_);
         regSession_ = NULL;
     }
diff --git a/daemon/src/iax/iaxvoiplink.h b/daemon/src/iax/iaxvoiplink.h
index 92a664fa50..76defcdd28 100644
--- a/daemon/src/iax/iaxvoiplink.h
+++ b/daemon/src/iax/iaxvoiplink.h
@@ -199,6 +199,10 @@ class IAXVoIPLink : public VoIPLink {
         static std::shared_ptr<IAXCall> getIaxCall(const std::string& id);
         static void removeIaxCall(const std::string &id);
 
+        /** Mutex for iax_ calls, since we're the only one dealing with the incorporated
+         * iax_stuff inside this class. */
+        static std::mutex mutexIAX;
+
     private:
         NON_COPYABLE(IAXVoIPLink);
 
@@ -286,10 +290,6 @@ class IAXVoIPLink : public VoIPLink {
          * to force a registration. */
         int nextRefreshStamp_;
 
-        /** Mutex for iax_ calls, since we're the only one dealing with the incorporated
-         * iax_stuff inside this class. */
-        std::mutex mutexIAX_;
-
         /** encoder/decoder/resampler buffers */
         AudioBuffer rawBuffer_;
         AudioBuffer resampledData_;
-- 
GitLab