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:
28  ThreadedFileWriter *m_parent {nullptr};
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:
38  ThreadedFileWriter *m_parent {nullptr};
39 };
40 
42 {
43  friend class TFWWriteThread;
44  friend class TFWSyncThread;
45  public:
49  ThreadedFileWriter(const QString &fname, int flags, mode_t mode)
50  : m_filename(fname), m_flags(flags), m_mode(mode) {}
52 
53  bool Open(void);
54  bool ReOpen(const QString& newFilename = "");
55 
56  long long Seek(long long pos, int whence);
57  int Write(const void *data, uint count);
58 
59  void SetWriteBufferMinWriteSize(uint newMinSize = kMinWriteSize);
60 
61  void Sync(void);
62  void Flush(void);
63  bool SetBlocking(bool block = true);
64  bool WritesFailing(void) const { return m_ignore_writes; }
65 
66  protected:
67  void DiskLoop(void);
68  void SyncLoop(void);
69  void TrimEmptyBuffers(void);
70 
71  private:
72  // file info
73  QString m_filename;
74  int m_flags;
75  mode_t m_mode;
76  int m_fd {-1};
77 
78  // state
79  bool m_flush {false}; // protected by buflock
80  bool m_in_dtor {false}; // protected by buflock
81  bool m_ignore_writes {false}; // protected by buflock
82  uint m_tfw_min_write_size {kMinWriteSize}; // protected by buflock
83  uint m_totalBufferUse {0}; // protected by buflock
84 
85  // buffers
86  class TFWBuffer
87  {
88  public:
89  vector<char> data;
90  QDateTime lastUsed;
91  };
92  mutable QMutex m_buflock;
93  QList<TFWBuffer*> m_writeBuffers; // protected by buflock
94  QList<TFWBuffer*> m_emptyBuffers; // protected by buflock
95 
96  // threads
97  TFWWriteThread *m_writeThread {nullptr};
98  TFWSyncThread *m_syncThread {nullptr};
99 
100  // wait conditions
101  QWaitCondition m_bufferEmpty;
102  QWaitCondition m_bufferHasData;
103  QWaitCondition m_bufferSyncWait;
104  QWaitCondition m_bufferWasFreed;
105 
106  // constants
107  static const uint kMaxBufferSize;
109  static const uint kMinWriteSize;
111  static const uint kMaxBlockSize;
112 
113  bool m_warned {false};
114  bool m_blocking {false};
115  bool m_registered {false};
116 };
117 
118 #endif
QList< TFWBuffer * > m_emptyBuffers
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
ThreadedFileWriter(const QString &fname, int flags, mode_t mode)
Creates a threaded file writer.
TFWWriteThread(ThreadedFileWriter *p)
unsigned int uint
Definition: compat.h:140
QWaitCondition m_bufferWasFreed
TFWSyncThread(ThreadedFileWriter *p)
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
static int run(MythMediaDevice *dev=nullptr, bool startRandomShow=false)
bool WritesFailing(void) const
QWaitCondition m_bufferEmpty
virtual ~TFWWriteThread()
virtual ~TFWSyncThread()
QWaitCondition m_bufferSyncWait
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.
QList< TFWBuffer * > m_writeBuffers
QWaitCondition m_bufferHasData
static const uint kMaxBufferSize