Mercurial > hg > svcore
comparison 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 |
comparison
equal
deleted
inserted
replaced
536:beb51f558e9c | 537:3cc4b7cd2aa5 |
---|---|
2 | 2 |
3 /* | 3 /* |
4 Sonic Visualiser | 4 Sonic Visualiser |
5 An audio file viewer and annotation editor. | 5 An audio file viewer and annotation editor. |
6 Centre for Digital Music, Queen Mary, University of London. | 6 Centre for Digital Music, Queen Mary, University of London. |
7 This file copyright 2006 Chris Cannam. | 7 This file copyright 2006-2009 Chris Cannam and QMUL. |
8 | 8 |
9 This program is free software; you can redistribute it and/or | 9 This program is free software; you can redistribute it and/or |
10 modify it under the terms of the GNU General Public License as | 10 modify it under the terms of the GNU General Public License as |
11 published by the Free Software Foundation; either version 2 of the | 11 published by the Free Software Foundation; either version 2 of the |
12 License, or (at your option) any later version. See the file | 12 License, or (at your option) any later version. See the file |
28 class MatrixFile : public QObject | 28 class MatrixFile : public QObject |
29 { | 29 { |
30 Q_OBJECT | 30 Q_OBJECT |
31 | 31 |
32 public: | 32 public: |
33 enum Mode { ReadOnly, ReadWrite }; | 33 enum Mode { ReadOnly, WriteOnly }; |
34 | 34 |
35 /** | 35 /** |
36 * Construct a MatrixFile object reading from and/or writing to | 36 * Construct a MatrixFile object reading from and/or writing to |
37 * the matrix file with the given base name in the application's | 37 * the matrix file with the given base name in the application's |
38 * temporary directory. | 38 * temporary directory. |
39 * | 39 * |
40 * If mode is ReadOnly, the file must exist and be readable. | 40 * If mode is ReadOnly, the file must exist and be readable. |
41 * | 41 * |
42 * If mode is ReadWrite and the file does not exist, it will be | 42 * If mode is WriteOnly, the file must not exist. |
43 * created. If mode is ReadWrite and the file does exist, the | |
44 * existing file will be used and the mode will be reset to | |
45 * ReadOnly. Call getMode() to check whether this has occurred | |
46 * after construction. | |
47 * | 43 * |
48 * cellSize specifies the size in bytes of the object type stored | 44 * cellSize specifies the size in bytes of the object type stored |
49 * in the matrix. For example, use cellSize = sizeof(float) for a | 45 * in the matrix. For example, use cellSize = sizeof(float) for a |
50 * matrix of floats. The MatrixFile object doesn't care about the | 46 * matrix of floats. The MatrixFile object doesn't care about the |
51 * objects themselves, it just deals with raw data of a given size. | 47 * objects themselves, it just deals with raw data of a given size. |
52 * | 48 * |
53 * If eagerCache is true, blocks from the file will be cached for | 49 * width and height specify the dimensions of the file. These |
54 * read. If eagerCache is false, only columns that have been set | 50 * cannot be changed after construction. |
55 * by calling setColumnAt on this MatrixFile (i.e. columns for | 51 * |
56 * which haveSetColumnAt returns true) will be cached. | 52 * MatrixFiles are reference counted by name. When the last |
53 * MatrixFile with a given name is destroyed, the file is removed. | |
54 * These are temporary files; the normal usage is to have one | |
55 * MatrixFile of WriteOnly type creating the file and then | |
56 * persisting until all readers are complete. | |
57 * | |
58 * MatrixFile has no built-in cache and is not thread-safe. Use a | |
59 * separate MatrixFile in each thread. | |
57 */ | 60 */ |
58 MatrixFile(QString fileBase, Mode mode, size_t cellSize, bool eagerCache); | 61 MatrixFile(QString fileBase, Mode mode, size_t cellSize, |
62 size_t width, size_t height); | |
59 virtual ~MatrixFile(); | 63 virtual ~MatrixFile(); |
60 | 64 |
61 Mode getMode() const { return m_mode; } | 65 Mode getMode() const { return m_mode; } |
62 | 66 |
63 size_t getWidth() const { return m_width; } | 67 size_t getWidth() const { return m_width; } |
64 size_t getHeight() const { return m_height; } | 68 size_t getHeight() const { return m_height; } |
65 size_t getCellSize() const { return m_cellSize; } | 69 size_t getCellSize() const { return m_cellSize; } |
66 | 70 |
67 void resize(size_t width, size_t height); | 71 void close(); // does not decrement ref count; that happens in dtor |
68 void reset(); | |
69 | 72 |
70 bool haveSetColumnAt(size_t x) const { return m_columnBitset->get(x); } | 73 bool haveSetColumnAt(size_t x) const; |
71 void getColumnAt(size_t x, void *data); // may throw FileReadFailed | 74 void getColumnAt(size_t x, void *data); // may throw FileReadFailed |
72 void setColumnAt(size_t x, const void *data); | 75 void setColumnAt(size_t x, const void *data); |
73 | |
74 void suspend(); | |
75 | 76 |
76 protected: | 77 protected: |
77 int m_fd; | 78 int m_fd; |
78 Mode m_mode; | 79 Mode m_mode; |
79 int m_flags; | 80 int m_flags; |
81 size_t m_cellSize; | 82 size_t m_cellSize; |
82 size_t m_width; | 83 size_t m_width; |
83 size_t m_height; | 84 size_t m_height; |
84 size_t m_headerSize; | 85 size_t m_headerSize; |
85 QString m_fileName; | 86 QString m_fileName; |
86 size_t m_defaultCacheWidth; | |
87 size_t m_prevX; | |
88 | |
89 struct Cache { | |
90 size_t x; | |
91 size_t width; | |
92 char *data; | |
93 }; | |
94 | |
95 Cache m_cache; | |
96 bool m_eagerCache; | |
97 | |
98 bool getFromCache(size_t x, size_t ystart, size_t ycount, void *data); | |
99 void primeCache(size_t x, bool left); | |
100 | |
101 void resume(); | |
102 | |
103 bool seekTo(size_t x, size_t y); | |
104 | |
105 static FileReadThread *m_readThread; | |
106 int m_requestToken; | |
107 | |
108 size_t m_requestingX; | |
109 size_t m_requestingWidth; | |
110 char *m_spareData; | |
111 | 87 |
112 static std::map<QString, int> m_refcount; | 88 static std::map<QString, int> m_refcount; |
113 static QMutex m_refcountMutex; | 89 static QMutex m_createMutex; |
114 QMutex m_fdMutex; | |
115 QMutex m_cacheMutex; | |
116 | 90 |
117 typedef std::map<QString, ResizeableBitset *> ResizeableBitsetMap; | 91 void initialise(); |
118 static ResizeableBitsetMap m_columnBitsets; | 92 bool seekTo(size_t x, size_t y) const; |
119 static QMutex m_columnBitsetWriteMutex; | |
120 ResizeableBitset *m_columnBitset; | |
121 }; | 93 }; |
122 | 94 |
123 #endif | 95 #endif |
124 | 96 |