diff data/fft/FFTDataServer.h @ 537:3cc4b7cd2aa5

* Merge from one-fftdataserver-per-fftmodel branch. This bit of reworking (which is not described very accurately by the title of the branch) turns the MatrixFile object into something that either reads or writes, but not both, and separates the FFT file cache reader and writer implementations separately. This allows the FFT data server to have a single thread owning writers and one reader per "customer" thread, and for all locking to be vastly simplified and concentrated in the data server alone (because none of the classes it makes use of is used in more than one thread at a time). The result is faster and more trustworthy code.
author Chris Cannam
date Tue, 27 Jan 2009 13:25:10 +0000
parents 115f60df1e4d
children a5a17152b6df
line wrap: on
line diff
--- a/data/fft/FFTDataServer.h	Mon Jan 26 15:18:32 2009 +0000
+++ b/data/fft/FFTDataServer.h	Tue Jan 27 13:25:10 2009 +0000
@@ -21,8 +21,13 @@
 #include "base/StorageAdviser.h"
 
 #include "FFTapi.h"
+#include "FFTFileCacheReader.h"
+#include "FFTFileCacheWriter.h"
+#include "FFTMemoryCache.h"
 
 #include <QMutex>
+#include <QReadWriteLock>
+#include <QReadLocker>
 #include <QWaitCondition>
 #include <QString>
 
@@ -31,7 +36,6 @@
 
 class DenseTimeValueModel;
 class Model;
-class FFTCache;
 
 class FFTDataServer
 {
@@ -140,33 +144,86 @@
     size_t m_cacheWidthPower;
     size_t m_cacheWidthMask;
 
-    int m_lastUsedCache;
-    FFTCache *getCache(size_t x, size_t &col) {
-        col   = x & m_cacheWidthMask;
+    struct CacheBlock {
+        FFTMemoryCache *memoryCache;
+        typedef std::map<QThread *, FFTFileCacheReader *> ThreadReaderMap;
+        ThreadReaderMap fileCacheReader;
+        FFTFileCacheWriter *fileCacheWriter;
+        CacheBlock() : memoryCache(0), fileCacheWriter(0) { }
+        ~CacheBlock() {
+            delete memoryCache; 
+            while (!fileCacheReader.empty()) {
+                delete fileCacheReader.begin()->second;
+                fileCacheReader.erase(fileCacheReader.begin());
+            }
+            delete fileCacheWriter;
+        }
+    };
+
+    typedef std::vector<CacheBlock *> CacheVector;
+    CacheVector m_caches;
+    QReadWriteLock m_cacheVectorLock; // locks cache lookup, not use
+
+    FFTCacheReader *getCacheReader(size_t x, size_t &col) {
+        Profiler profiler("FFTDataServer::getCacheReader");
+        col = x & m_cacheWidthMask;
         int c = x >> m_cacheWidthPower;
-        // The only use of m_lastUsedCache without a lock is to
-        // establish whether a cache has been created at all (they're
-        // created on demand, but not destroyed until the server is).
-        if (c == m_lastUsedCache) return m_caches[c];
-        else return getCacheAux(c);
+        m_cacheVectorLock.lockForRead();
+        CacheBlock *cb(m_caches.at(c));
+        if (cb) {
+            if (cb->memoryCache) return cb->memoryCache;
+            if (cb->fileCacheWriter) {
+                QThread *me = QThread::currentThread();
+                CacheBlock::ThreadReaderMap &map = cb->fileCacheReader;
+                if (map.find(me) == map.end()) {
+                    m_cacheVectorLock.unlock();
+                    if (!makeCacheReader(c)) return 0;
+                    return getCacheReader(x, col);
+                }
+                FFTCacheReader *reader = cb->fileCacheReader.at(me);
+                m_cacheVectorLock.unlock();
+                return reader;
+            }
+            // if cb exists but cb->fileCacheWriter doesn't, creation
+            // must have failed: don't try again
+            m_cacheVectorLock.unlock();
+            return 0;
+        }
+        m_cacheVectorLock.unlock();
+        if (!makeCache(c)) return 0;
+        return getCacheReader(x, col);
     }
+    
+    FFTCacheWriter *getCacheWriter(size_t x, size_t &col) {
+        Profiler profiler("FFTDataServer::getCacheWriter");
+        col = x & m_cacheWidthMask;
+        int c = x >> m_cacheWidthPower;
+        {
+            QReadLocker locker(&m_cacheVectorLock);
+            CacheBlock *cb(m_caches.at(c));
+            if (cb) {
+                if (cb->memoryCache) return cb->memoryCache;
+                if (cb->fileCacheWriter) return cb->fileCacheWriter;
+                // if cb exists, creation must have failed: don't try again
+                return 0;
+            }
+        }
+        if (!makeCache(c)) return 0;
+        return getCacheWriter(x, col);
+    }
+
     bool haveCache(size_t x) {
         int c = x >> m_cacheWidthPower;
-        if (c == m_lastUsedCache) return true;
-        else return (m_caches[c] != 0);
+        return (m_caches[c] != 0);
     }
-
-    typedef std::vector<FFTCache *> CacheVector;
-    CacheVector m_caches;
     
-    typedef std::deque<int> IntQueue;
-    IntQueue m_dormantCaches;
-
+    bool makeCache(int c);
+    bool makeCacheReader(int c);
+    
     StorageAdviser::Criteria m_criteria;
 
     void getStorageAdvice(size_t w, size_t h, bool &memory, bool &compact);
         
-    FFTCache *getCacheAux(size_t c);
     QMutex m_writeMutex;
     QWaitCondition m_condition;
 
@@ -199,6 +256,7 @@
 
     void deleteProcessingData();
     void fillColumn(size_t x, bool lockHeld);
+    void fillComplete();
 
     QString generateFileBasename() const;
     static QString generateFileBasename(const DenseTimeValueModel *model,