diff base/Serialiser.cpp @ 1857:14c776dad920

Make Serialiser cancellable while waiting for its lock
author Chris Cannam
date Thu, 14 May 2020 16:35:39 +0100
parents d095214ffbaf
children
line wrap: on
line diff
--- a/base/Serialiser.cpp	Thu May 14 16:35:21 2020 +0100
+++ b/base/Serialiser.cpp	Thu May 14 16:35:39 2020 +0100
@@ -14,6 +14,7 @@
 */
 
 #include "Serialiser.h"
+#include "Debug.h"
 
 #include <iostream>
 
@@ -24,7 +25,12 @@
 Serialiser::m_mutexMap;
 
 Serialiser::Serialiser(QString id) :
-    m_id(id)
+    Serialiser(id, nullptr) { }
+
+Serialiser::Serialiser(QString id, const std::atomic<bool> *cancelled) :
+    m_id(id),
+    m_cancelled(cancelled),
+    m_locked(false)
 {
     m_mapMutex.lock();
     
@@ -43,14 +49,27 @@
 
     m_mapMutex.unlock();
 
-    idMutex->lock();
+    if (!m_cancelled) {
+        idMutex->lock();
+        m_locked = true;
+    } else {
+        // try to lock, polling the cancelled status occasionally
+        while (!m_locked && ! *m_cancelled) {
+            m_locked = idMutex->tryLock(500);
+            if (*m_cancelled) {
+                SVCERR << "Serialiser: cancelled!" << endl;
+            }
+        }
+    }
 }
 
 Serialiser::~Serialiser()
 {
     m_mapMutex.lock();
-    
-    m_mutexMap[m_id]->unlock();
+
+    if (m_locked) {
+        m_mutexMap[m_id]->unlock();
+    }
 
     m_mapMutex.unlock();
 }