annotate data/fileio/MatrixFile.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 3e0f1f7bec85
children 107d3f3705c9
rev   line source
Chris@148 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@148 2
Chris@148 3 /*
Chris@148 4 Sonic Visualiser
Chris@148 5 An audio file viewer and annotation editor.
Chris@148 6 Centre for Digital Music, Queen Mary, University of London.
Chris@537 7 This file copyright 2006-2009 Chris Cannam and QMUL.
Chris@148 8
Chris@148 9 This program is free software; you can redistribute it and/or
Chris@148 10 modify it under the terms of the GNU General Public License as
Chris@148 11 published by the Free Software Foundation; either version 2 of the
Chris@148 12 License, or (at your option) any later version. See the file
Chris@148 13 COPYING included with this distribution for more information.
Chris@148 14 */
Chris@148 15
Chris@148 16 #ifndef _MATRIX_FILE_CACHE_H_
Chris@148 17 #define _MATRIX_FILE_CACHE_H_
Chris@148 18
Chris@148 19 #include "base/ResizeableBitset.h"
Chris@148 20
Chris@148 21 #include "FileReadThread.h"
Chris@148 22
Chris@148 23 #include <sys/types.h>
Chris@148 24 #include <QString>
Chris@148 25 #include <QMutex>
Chris@148 26 #include <map>
Chris@148 27
Chris@148 28 class MatrixFile : public QObject
Chris@148 29 {
Chris@148 30 Q_OBJECT
Chris@148 31
Chris@148 32 public:
Chris@537 33 enum Mode { ReadOnly, WriteOnly };
Chris@148 34
Chris@148 35 /**
Chris@148 36 * Construct a MatrixFile object reading from and/or writing to
Chris@148 37 * the matrix file with the given base name in the application's
Chris@148 38 * temporary directory.
Chris@148 39 *
Chris@148 40 * If mode is ReadOnly, the file must exist and be readable.
Chris@148 41 *
Chris@537 42 * If mode is WriteOnly, the file must not exist.
Chris@148 43 *
Chris@148 44 * cellSize specifies the size in bytes of the object type stored
Chris@148 45 * in the matrix. For example, use cellSize = sizeof(float) for a
Chris@148 46 * matrix of floats. The MatrixFile object doesn't care about the
Chris@148 47 * objects themselves, it just deals with raw data of a given size.
Chris@148 48 *
Chris@537 49 * width and height specify the dimensions of the file. These
Chris@537 50 * cannot be changed after construction.
Chris@537 51 *
Chris@537 52 * MatrixFiles are reference counted by name. When the last
Chris@537 53 * MatrixFile with a given name is destroyed, the file is removed.
Chris@537 54 * These are temporary files; the normal usage is to have one
Chris@537 55 * MatrixFile of WriteOnly type creating the file and then
Chris@537 56 * persisting until all readers are complete.
Chris@537 57 *
Chris@537 58 * MatrixFile has no built-in cache and is not thread-safe. Use a
Chris@537 59 * separate MatrixFile in each thread.
Chris@148 60 */
Chris@537 61 MatrixFile(QString fileBase, Mode mode, size_t cellSize,
Chris@537 62 size_t width, size_t height);
Chris@148 63 virtual ~MatrixFile();
Chris@148 64
Chris@148 65 Mode getMode() const { return m_mode; }
Chris@148 66
Chris@148 67 size_t getWidth() const { return m_width; }
Chris@148 68 size_t getHeight() const { return m_height; }
Chris@148 69 size_t getCellSize() const { return m_cellSize; }
Chris@148 70
Chris@537 71 void close(); // does not decrement ref count; that happens in dtor
Chris@148 72
Chris@537 73 bool haveSetColumnAt(size_t x) const;
Chris@455 74 void getColumnAt(size_t x, void *data); // may throw FileReadFailed
Chris@148 75 void setColumnAt(size_t x, const void *data);
Chris@148 76
Chris@148 77 protected:
Chris@148 78 int m_fd;
Chris@148 79 Mode m_mode;
Chris@148 80 int m_flags;
Chris@148 81 mode_t m_fmode;
Chris@148 82 size_t m_cellSize;
Chris@148 83 size_t m_width;
Chris@148 84 size_t m_height;
Chris@148 85 size_t m_headerSize;
Chris@148 86 QString m_fileName;
Chris@148 87
Chris@148 88 static std::map<QString, int> m_refcount;
Chris@537 89 static QMutex m_createMutex;
Chris@148 90
Chris@537 91 void initialise();
Chris@537 92 bool seekTo(size_t x, size_t y) const;
Chris@148 93 };
Chris@148 94
Chris@148 95 #endif
Chris@148 96