svcore  1.9
FileReadThread.h
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #ifndef _FILE_READ_THREAD_H_
00017 #define _FILE_READ_THREAD_H_
00018 
00019 #include "base/Thread.h"
00020 
00021 #include <QMutex>
00022 #include <QWaitCondition>
00023 
00024 #include <map>
00025 #include <set>
00026 
00027 #include <stdint.h>
00028 
00029 class FileReadThread : public Thread
00030 {
00031     Q_OBJECT
00032 
00033 public:
00034     FileReadThread();
00035 
00036     virtual void run();
00037     virtual void finish();
00038 
00039     struct Request {
00040         int fd;
00041         QMutex *mutex; // used to synchronise access to fd; may be null
00042         off_t start;
00043         size_t size;
00044         char *data; // caller is responsible for allocating and deallocating
00045         bool successful; // set by FileReadThread after processing request
00046     };
00047     
00048     virtual int request(const Request &request);
00049     virtual void cancel(int token);
00050 
00051     virtual bool isReady(int token);
00052     virtual bool isCancelled(int token); // and safe to delete
00053     virtual bool haveRequest(int token);
00054     virtual bool getRequest(int token, Request &request);
00055     virtual void done(int token);
00056     
00057 protected:
00058     int m_nextToken;
00059     bool m_exiting;
00060     
00061     typedef std::map<int, Request> RequestQueue;
00062     RequestQueue m_queue;
00063     RequestQueue m_cancelledRequests;
00064     RequestQueue m_readyRequests;
00065     std::set<int> m_newlyCancelled;
00066 
00067     QMutex m_mutex;
00068     QWaitCondition m_condition;
00069 
00070     void process();
00071     void notifyCancelled();
00072 };
00073 
00074 #endif