annotate data/fileio/FileReadThread.h @ 263:71dfc6ab3b54

* Threaded mp3/ogg file reading. Not activated yet, as it doesn't work in context (SV needs to know the duration of its main model at the outset)
author Chris Cannam
date Thu, 24 May 2007 16:20:22 +0000
parents 4b2ea82fd0ed
children 3e0f1f7bec85
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@148 7 This file copyright 2006 Chris Cannam.
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 _FILE_READ_THREAD_H_
Chris@148 17 #define _FILE_READ_THREAD_H_
Chris@148 18
Chris@150 19 #include "base/Thread.h"
Chris@148 20
Chris@148 21 #include <QMutex>
Chris@148 22 #include <QWaitCondition>
Chris@148 23
Chris@148 24 #include <map>
Chris@148 25 #include <set>
Chris@148 26
Chris@148 27 #include <stdint.h>
Chris@148 28
Chris@148 29 class FileReadThread : public Thread
Chris@148 30 {
Chris@148 31 Q_OBJECT
Chris@148 32
Chris@148 33 public:
Chris@148 34 FileReadThread();
Chris@148 35
Chris@148 36 virtual void run();
Chris@148 37 virtual void finish();
Chris@148 38
Chris@148 39 struct Request {
Chris@148 40 int fd;
Chris@148 41 QMutex *mutex; // used to synchronise access to fd; may be null
Chris@148 42 off_t start;
Chris@148 43 size_t size;
Chris@148 44 char *data; // caller is responsible for allocating and deallocating
Chris@148 45 bool successful; // set by FileReadThread after processing request
Chris@148 46 };
Chris@148 47
Chris@148 48 virtual int request(const Request &request);
Chris@148 49 virtual void cancel(int token);
Chris@148 50
Chris@148 51 virtual bool isReady(int token);
Chris@148 52 virtual bool isCancelled(int token); // and safe to delete
Chris@148 53 virtual bool getRequest(int token, Request &request);
Chris@148 54 virtual void done(int token);
Chris@148 55
Chris@148 56 protected:
Chris@148 57 int m_nextToken;
Chris@148 58 bool m_exiting;
Chris@148 59
Chris@148 60 typedef std::map<int, Request> RequestQueue;
Chris@148 61 RequestQueue m_queue;
Chris@148 62 RequestQueue m_cancelledRequests;
Chris@148 63 RequestQueue m_readyRequests;
Chris@148 64 std::set<int> m_newlyCancelled;
Chris@148 65
Chris@148 66 QMutex m_mutex;
Chris@148 67 QWaitCondition m_condition;
Chris@148 68
Chris@148 69 void process();
Chris@148 70 void notifyCancelled();
Chris@148 71 };
Chris@148 72
Chris@148 73 #endif