Mercurial > hg > svcore
comparison base/FileReadThread.h @ 95:040a151d0897
* Add file reader thread, and make the matrix file code use it to preload
  fft cache data without glitching
| author | Chris Cannam | 
|---|---|
| date | Thu, 04 May 2006 13:59:57 +0000 | 
| parents | |
| children | 1aebdc68ec6d | 
   comparison
  equal
  deleted
  inserted
  replaced
| 94:5b8392e80ed6 | 95:040a151d0897 | 
|---|---|
| 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
| 2 | |
| 3 /* | |
| 4 Sonic Visualiser | |
| 5 An audio file viewer and annotation editor. | |
| 6 Centre for Digital Music, Queen Mary, University of London. | |
| 7 This file copyright 2006 Chris Cannam. | |
| 8 | |
| 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 | |
| 11 published by the Free Software Foundation; either version 2 of the | |
| 12 License, or (at your option) any later version. See the file | |
| 13 COPYING included with this distribution for more information. | |
| 14 */ | |
| 15 | |
| 16 #ifndef _FILE_READ_THREAD_H_ | |
| 17 #define _FILE_READ_THREAD_H_ | |
| 18 | |
| 19 #include <QThread> | |
| 20 #include <QMutex> | |
| 21 #include <QWaitCondition> | |
| 22 | |
| 23 #include <map> | |
| 24 #include <set> | |
| 25 | |
| 26 #include <stdint.h> | |
| 27 | |
| 28 class FileReadThread : public QThread | |
| 29 { | |
| 30 Q_OBJECT | |
| 31 | |
| 32 public: | |
| 33 FileReadThread(); | |
| 34 | |
| 35 virtual void run(); | |
| 36 virtual void finish(); | |
| 37 | |
| 38 struct Request { | |
| 39 int fd; | |
| 40 QMutex *mutex; // used to synchronise access to fd; may be null | |
| 41 off_t start; | |
| 42 size_t size; | |
| 43 char *data; // caller is responsible for allocating and deallocating | |
| 44 }; | |
| 45 | |
| 46 virtual int request(const Request &request); | |
| 47 virtual void cancel(int token); | |
| 48 | |
| 49 virtual bool isReady(int token); | |
| 50 virtual bool isCancelled(int token); // and safe to delete | |
| 51 virtual bool getRequest(int token, Request &request); | |
| 52 virtual void done(int token); | |
| 53 | |
| 54 signals: | |
| 55 void ready(int token, bool successful); | |
| 56 void cancelled(int token); | |
| 57 | |
| 58 protected: | |
| 59 int m_nextToken; | |
| 60 bool m_exiting; | |
| 61 | |
| 62 typedef std::map<int, Request> RequestQueue; | |
| 63 RequestQueue m_queue; | |
| 64 RequestQueue m_cancelledRequests; | |
| 65 RequestQueue m_readyRequests; | |
| 66 std::set<int> m_newlyCancelled; | |
| 67 | |
| 68 QMutex m_mutex; | |
| 69 QWaitCondition m_condition; | |
| 70 | |
| 71 void process(); | |
| 72 void notifyCancelled(); | |
| 73 }; | |
| 74 | |
| 75 #endif | 
