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
21
22class 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
32class 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;
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
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
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:300
~TFWSyncThread() override
TFWSyncThread(ThreadedFileWriter *p)
void run(void) override
Runs ThreadedFileWriter::SyncLoop(void)
ThreadedFileWriter * m_parent
void run(void) override
Runs ThreadedFileWriter::DiskLoop(void)
TFWWriteThread(ThreadedFileWriter *p)
~TFWWriteThread() override
ThreadedFileWriter * m_parent
This class supports the writing of recordings to disk.
QWaitCondition m_bufferHasData
QWaitCondition m_bufferEmpty
static const uint kMaxBufferSize
bool WritesFailing(void) const
static const uint kMinWriteSize
Minimum to write to disk in a single write, when not flushing buffer.
ThreadedFileWriter(QString fname, int flags, mode_t mode)
Creates a threaded file writer.
QWaitCondition m_bufferWasFreed
QList< TFWBuffer * > m_emptyBuffers
static const uint kMaxBlockSize
Maximum block size to write at a time.
QList< TFWBuffer * > m_writeBuffers
QWaitCondition m_bufferSyncWait
unsigned int uint
Definition: freesurround.h:24
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
STL namespace.