comparison data/fileio/FileReadThread.h @ 148:1a42221a1522

* Reorganising code base. This revision will not compile.
author Chris Cannam
date Mon, 31 Jul 2006 11:49:58 +0000
parents
children 4b2ea82fd0ed
comparison
equal deleted inserted replaced
147:3a13b0d4934e 148:1a42221a1522
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 "Thread.h"
20
21 #include <QMutex>
22 #include <QWaitCondition>
23
24 #include <map>
25 #include <set>
26
27 #include <stdint.h>
28
29 class FileReadThread : public Thread
30 {
31 Q_OBJECT
32
33 public:
34 FileReadThread();
35
36 virtual void run();
37 virtual void finish();
38
39 struct Request {
40 int fd;
41 QMutex *mutex; // used to synchronise access to fd; may be null
42 off_t start;
43 size_t size;
44 char *data; // caller is responsible for allocating and deallocating
45 bool successful; // set by FileReadThread after processing request
46 };
47
48 virtual int request(const Request &request);
49 virtual void cancel(int token);
50
51 virtual bool isReady(int token);
52 virtual bool isCancelled(int token); // and safe to delete
53 virtual bool getRequest(int token, Request &request);
54 virtual void done(int token);
55
56 protected:
57 int m_nextToken;
58 bool m_exiting;
59
60 typedef std::map<int, Request> RequestQueue;
61 RequestQueue m_queue;
62 RequestQueue m_cancelledRequests;
63 RequestQueue m_readyRequests;
64 std::set<int> m_newlyCancelled;
65
66 QMutex m_mutex;
67 QWaitCondition m_condition;
68
69 void process();
70 void notifyCancelled();
71 };
72
73 #endif