MythTV  master
threadedfilewriter.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 #ifndef TFW_H_
3 #define TFW_H_
4 
5 #include <vector>
6 using namespace std;
7 
8 #include <QWaitCondition>
9 #include <QDateTime>
10 #include <QString>
11 #include <QMutex>
12 
13 #include <cstdint>
14 #include <fcntl.h>
15 
16 #include "mythbaseexp.h"
17 #include "mthread.h"
18 
19 class ThreadedFileWriter;
20 
21 class TFWWriteThread : public MThread
22 {
23  public:
24  explicit TFWWriteThread(ThreadedFileWriter *p) : MThread("TFWWrite"), m_parent(p) {}
25  virtual ~TFWWriteThread() { wait(); m_parent = nullptr; }
26  void run(void) override; // MThread
27  private:
29 };
30 
31 class TFWSyncThread : public MThread
32 {
33  public:
34  explicit TFWSyncThread(ThreadedFileWriter *p) : MThread("TFWSync"), m_parent(p) {}
35  virtual ~TFWSyncThread() { wait(); m_parent = nullptr; }
36  void run(void) override; // MThread
37  private:
39 };
40 
42 {
43  friend class TFWWriteThread;
44  friend class TFWSyncThread;
45  public:
46  ThreadedFileWriter(const QString &fname, int flags, mode_t mode);
48 
49  bool Open(void);
50  bool ReOpen(QString newFilename = "");
51 
52  long long Seek(long long pos, int whence);
53  int Write(const void *data, uint count);
54 
55  void SetWriteBufferMinWriteSize(uint newMinSize = kMinWriteSize);
56 
57  void Sync(void);
58  void Flush(void);
59  bool SetBlocking(bool block = true);
60  bool WritesFailing(void) const { return ignore_writes; }
61 
62  protected:
63  void DiskLoop(void);
64  void SyncLoop(void);
65  void TrimEmptyBuffers(void);
66 
67  private:
68  // file info
69  QString filename;
70  int flags;
71  mode_t mode;
72  int fd;
73 
74  // state
75  bool flush; // protected by buflock
76  bool in_dtor; // protected by buflock
77  bool ignore_writes; // protected by buflock
78  uint tfw_min_write_size; // protected by buflock
79  uint totalBufferUse; // protected by buflock
80 
81  // buffers
82  class TFWBuffer
83  {
84  public:
85  vector<char> data;
86  QDateTime lastUsed;
87  };
88  mutable QMutex buflock;
89  QList<TFWBuffer*> writeBuffers; // protected by buflock
90  QList<TFWBuffer*> emptyBuffers; // protected by buflock
91 
92  // threads
95 
96  // wait conditions
97  QWaitCondition bufferEmpty;
98  QWaitCondition bufferHasData;
99  QWaitCondition bufferSyncWait;
100  QWaitCondition bufferWasFreed;
101 
102  // constants
103  static const uint kMaxBufferSize;
105  static const uint kMinWriteSize;
107  static const uint kMaxBlockSize;
108 
109  bool m_warned;
112 };
113 
114 #endif
ThreadedFileWriter * m_parent
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
QWaitCondition bufferSyncWait
QList< TFWBuffer * > emptyBuffers
TFWWriteThread(ThreadedFileWriter *p)
QWaitCondition bufferEmpty
TFWSyncThread * syncThread
unsigned int uint
Definition: compat.h:140
TFWSyncThread(ThreadedFileWriter *p)
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
static int run(MythMediaDevice *dev=nullptr, bool startRandomShow=false)
bool WritesFailing(void) const
QList< TFWBuffer * > writeBuffers
TFWWriteThread * writeThread
virtual ~TFWWriteThread()
virtual ~TFWSyncThread()
ThreadedFileWriter * m_parent
static const uint kMaxBlockSize
Maximum block size to write at a time.
This class supports the writing of recordings to disk.
static const uint kMinWriteSize
Minimum to write to disk in a single write, when not flushing buffer.
QWaitCondition bufferWasFreed
static const uint kMaxBufferSize
QWaitCondition bufferHasData