fix liblinphone threading issue
Stefan Schuermans

Stefan Schuermans commited on 2019-08-28 11:43:44
Showing 2 changed files, with 14 additions and 6 deletions.


introduce global interlock for liblinphone instances,
this fixes the crashes when multiple SIP phones are configured
... ...
@@ -34,10 +34,13 @@ namespace Blinker {
34 34
 
35 35
 /**
36 36
  * everything using the linphone library happens in a separate thread,
37
- * because some call to linphone take long (up to two seconds)
37
+ * because some calls to linphone take long (up to two seconds)
38 38
  */
39 39
 namespace linphone {
40 40
 
41
+/// interlock for liblinphone, higher in mutex hierarchy than shared data mutex
42
+static std::mutex g_mtx;
43
+
41 44
 static char const *loglevel2str(OrtpLogLevel lev)
42 45
 {
43 46
   switch(lev) {
... ...
@@ -165,7 +168,7 @@ struct Data {
165 168
 };
166 169
 
167 170
 /**
168
- * @breif set playback file
171
+ * @brief set playback file
169 172
  * @param[in,out] data data of SIP client
170 173
  * @param[in] name name of sound file (without extension)
171 174
  */
... ...
@@ -557,6 +560,7 @@ void worker(SipPhone::ConfigData const &configData,
557 560
 
558 561
   // main loop of worker - with shared data locked
559 562
   std::chrono::milliseconds timeout(10);
563
+  std::unique_lock<std::mutex> g_lock(g_mtx);
560 564
   std::unique_lock<std::mutex> lock(sharedData.mtx);
561 565
   while (sharedData.run) {
562 566
 
... ...
@@ -609,8 +613,10 @@ void worker(SipPhone::ConfigData const &configData,
609 613
       lock.lock();
610 614
     }
611 615
 
612
-    // nothing to do, wait for signal
613
-    sharedData.cond.wait_for(lock, timeout);
616
+    // nothing to do, wait for signal (with both locks released)
617
+    lock.unlock();
618
+    sharedData.cond.wait_for(g_lock, timeout);
619
+    lock.lock();
614 620
 
615 621
   } // while (sharedData.run)
616 622
 }
... ...
@@ -32,8 +32,10 @@ public:
32 32
   };
33 33
   /// data shared between linphone thread and main Blinker thread
34 34
   struct SharedData {
35
-    std::mutex mtx; ///< protection for this structure
36
-    std::condition_variable cond; ///< worker threads waits on this condition
35
+    /// protection for this structure
36
+    std::mutex mtx;
37
+    /// worker threads waits on this condition, related mutex is g_mtx
38
+    std::condition_variable cond;
37 39
     /// Blinker -> SIP
38 40
     //@{
39 41
     bool run; ///< whether to keep worker running (false = exit)
40 42