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