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 <cstdint>
6 #include <fcntl.h>
7 #include <utility>
8 #include <vector>
9 
10 // Qt headers
11 #include <QWaitCondition>
12 #include <QDateTime>
13 #include <QString>
14 #include <QMutex>
15 
16 // MythTV headers
17 #include "mythbaseexp.h"
18 #include "mthread.h"
19 
20 class ThreadedFileWriter;
21 
22 class TFWWriteThread : public MThread
23 {
24  public:
25  explicit TFWWriteThread(ThreadedFileWriter *p) : MThread("TFWWrite"), m_parent(p) {}
26  ~TFWWriteThread() override { wait(); m_parent = nullptr; }
27  void run(void) override; // MThread
28  private:
30 };
31 
32 class TFWSyncThread : public MThread
33 {
34  public:
35  explicit TFWSyncThread(ThreadedFileWriter *p) : MThread("TFWSync"), m_parent(p) {}
36  ~TFWSyncThread() override { wait(); m_parent = nullptr; }
37  void run(void) override; // MThread
38  private:
40 };
41 
43 {
44  friend class TFWWriteThread;
45  friend class TFWSyncThread;
46  public:
50  ThreadedFileWriter(QString fname, int flags, mode_t mode)
51  : m_filename(std::move(fname)), m_flags(flags), m_mode(mode) {}
53 
54  bool Open(void);
55  bool ReOpen(const QString& newFilename = "");
56 
57  long long Seek(long long pos, int whence);
58  int Write(const void *data, uint count);
59 
60  void SetWriteBufferMinWriteSize(uint newMinSize = kMinWriteSize);
61 
62  void Sync(void) const;
63  void Flush(void);
64  bool SetBlocking(bool block = true);
65  bool WritesFailing(void) const { return m_ignoreWrites; }
66 
67  protected:
68  void DiskLoop(void);
69  void SyncLoop(void);
70  void TrimEmptyBuffers(void);
71 
72  private:
73  // file info
74  QString m_filename;
75  int m_flags;
76  mode_t m_mode;
77  int m_fd {-1};
78 
79  // state
80  bool m_flush {false}; // protected by buflock
81  bool m_inDtor {false}; // protected by buflock
82  bool m_ignoreWrites {false}; // protected by buflock
83  uint m_tfwMinWriteSize {kMinWriteSize}; // protected by buflock
84  uint m_totalBufferUse {0}; // protected by buflock
85 
86  // buffers
87  class TFWBuffer
88  {
89  public:
90  std::vector<char> data;
91  QDateTime lastUsed;
92  };
93  mutable QMutex m_bufLock;
94  QList<TFWBuffer*> m_writeBuffers; // protected by buflock
95  QList<TFWBuffer*> m_emptyBuffers; // protected by buflock
96 
97  // threads
98  TFWWriteThread *m_writeThread {nullptr};
99  TFWSyncThread *m_syncThread {nullptr};
100 
101  // wait conditions
102  QWaitCondition m_bufferEmpty;
103  QWaitCondition m_bufferHasData;
104  QWaitCondition m_bufferSyncWait;
105  QWaitCondition m_bufferWasFreed;
106 
107  // constants
108  static const uint kMaxBufferSize;
110  static const uint kMinWriteSize;
112  static const uint kMaxBlockSize;
113 
114  bool m_warned {false};
115  bool m_blocking {false};
116  bool m_registered {false};
117 };
118 
119 #endif
ThreadedFileWriter::m_bufLock
QMutex m_bufLock
Definition: threadedfilewriter.h:93
ThreadedFileWriter::m_flags
int m_flags
Definition: threadedfilewriter.h:75
TFWSyncThread::TFWSyncThread
TFWSyncThread(ThreadedFileWriter *p)
Definition: threadedfilewriter.h:35
ThreadedFileWriter::m_mode
mode_t m_mode
Definition: threadedfilewriter.h:76
TFWWriteThread::~TFWWriteThread
~TFWWriteThread() override
Definition: threadedfilewriter.h:26
ThreadedFileWriter::ThreadedFileWriter
ThreadedFileWriter(QString fname, int flags, mode_t mode)
Creates a threaded file writer.
Definition: threadedfilewriter.h:50
ThreadedFileWriter::m_bufferWasFreed
QWaitCondition m_bufferWasFreed
Definition: threadedfilewriter.h:105
ThreadedFileWriter::WritesFailing
bool WritesFailing(void) const
Definition: threadedfilewriter.h:65
MThread::wait
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:300
mythbaseexp.h
ThreadedFileWriter::m_bufferEmpty
QWaitCondition m_bufferEmpty
Definition: threadedfilewriter.h:102
ThreadedFileWriter::m_emptyBuffers
QList< TFWBuffer * > m_emptyBuffers
Definition: threadedfilewriter.h:95
TFWWriteThread::run
void run(void) override
Runs ThreadedFileWriter::DiskLoop(void)
Definition: threadedfilewriter.cpp:27
ThreadedFileWriter::TFWBuffer::lastUsed
QDateTime lastUsed
Definition: threadedfilewriter.h:91
ThreadedFileWriter::m_filename
QString m_filename
Definition: threadedfilewriter.h:74
TFWSyncThread::~TFWSyncThread
~TFWSyncThread() override
Definition: threadedfilewriter.h:36
ThreadedFileWriter::m_writeBuffers
QList< TFWBuffer * > m_writeBuffers
Definition: threadedfilewriter.h:94
TFWSyncThread
Definition: threadedfilewriter.h:32
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
hardwareprofile.config.p
p
Definition: config.py:33
ThreadedFileWriter::m_bufferSyncWait
QWaitCondition m_bufferSyncWait
Definition: threadedfilewriter.h:104
ThreadedFileWriter::TFWBuffer::data
std::vector< char > data
Definition: threadedfilewriter.h:90
TFWWriteThread
Definition: threadedfilewriter.h:22
uint
unsigned int uint
Definition: compat.h:81
ThreadedFileWriter::kMaxBufferSize
static const uint kMaxBufferSize
Definition: threadedfilewriter.h:108
ThreadedFileWriter
This class supports the writing of recordings to disk.
Definition: threadedfilewriter.h:42
TFWWriteThread::TFWWriteThread
TFWWriteThread(ThreadedFileWriter *p)
Definition: threadedfilewriter.h:25
std
Definition: mythchrono.h:23
TFWSyncThread::m_parent
ThreadedFileWriter * m_parent
Definition: threadedfilewriter.h:39
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
mthread.h
ThreadedFileWriter::m_bufferHasData
QWaitCondition m_bufferHasData
Definition: threadedfilewriter.h:103
TFWSyncThread::run
void run(void) override
Runs ThreadedFileWriter::SyncLoop(void)
Definition: threadedfilewriter.cpp:35
ThreadedFileWriter::kMinWriteSize
static const uint kMinWriteSize
Minimum to write to disk in a single write, when not flushing buffer.
Definition: threadedfilewriter.h:110
ThreadedFileWriter::TFWBuffer
Definition: threadedfilewriter.h:87
ThreadedFileWriter::kMaxBlockSize
static const uint kMaxBlockSize
Maximum block size to write at a time.
Definition: threadedfilewriter.h:112
TFWWriteThread::m_parent
ThreadedFileWriter * m_parent
Definition: threadedfilewriter.h:29