FileReadThread.h
Go to the documentation of this file.
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 SV_FILE_READ_THREAD_H
17 #define SV_FILE_READ_THREAD_H
18 
19 #include "base/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:
35 
36  void run() override;
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 haveRequest(int token);
54  virtual bool getRequest(int token, Request &request);
55  virtual void done(int token);
56 
57 protected:
59  bool m_exiting;
60 
61  typedef std::map<int, Request> RequestQueue;
62  RequestQueue m_queue;
63  RequestQueue m_cancelledRequests;
64  RequestQueue m_readyRequests;
65  std::set<int> m_newlyCancelled;
66 
67  QMutex m_mutex;
68  QWaitCondition m_condition;
69 
70  void process();
71  void notifyCancelled();
72 };
73 
74 #endif
std::map< int, Request > RequestQueue
QWaitCondition m_condition
virtual bool haveRequest(int token)
virtual void cancel(int token)
RequestQueue m_cancelledRequests
RequestQueue m_readyRequests
std::set< int > m_newlyCancelled
virtual void finish()
RequestQueue m_queue
virtual int request(const Request &request)
Definition: Thread.h:24
virtual bool isCancelled(int token)
virtual void done(int token)
virtual bool getRequest(int token, Request &request)
virtual bool isReady(int token)
void run() override