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 protected:
28 void run(void) override; // MThread
29 private:
31};
32
33class TFWSyncThread : public MThread
34{
35 public:
36 explicit TFWSyncThread(ThreadedFileWriter *p) : MThread("TFWSync"), m_parent(p) {}
37 ~TFWSyncThread() override { wait(); m_parent = nullptr; }
38 protected:
39 void run(void) override; // MThread
40 private:
42};
43
45{
46 friend class TFWWriteThread;
47 friend class TFWSyncThread;
48 public:
52 ThreadedFileWriter(QString fname, int flags, mode_t mode)
53 : m_filename(std::move(fname)), m_flags(flags), m_mode(mode) {}
55
56 bool Open(void);
57 bool ReOpen(const QString& newFilename = "");
58
59 long long Seek(long long pos, int whence);
60 int Write(const void *data, uint count);
61
62 void SetWriteBufferMinWriteSize(uint newMinSize = kMinWriteSize);
63
64 void Sync(void) const;
65 void Flush(void);
66 bool SetBlocking(bool block = true);
67 bool WritesFailing(void) const { return m_ignoreWrites; }
68
69 protected:
70 void DiskLoop(void);
71 void SyncLoop(void);
72 void TrimEmptyBuffers(void);
73
74 private:
75 // file info
76 QString m_filename;
78 mode_t m_mode;
79 int m_fd {-1};
80
81 // state
82 bool m_flush {false}; // protected by buflock
83 bool m_inDtor {false}; // protected by buflock
84 bool m_ignoreWrites {false}; // protected by buflock
85 uint m_tfwMinWriteSize {kMinWriteSize}; // protected by buflock
86 uint m_totalBufferUse {0}; // protected by buflock
87
88 // buffers
90 {
91 public:
92 std::vector<char> data;
93 QDateTime lastUsed;
94 };
95 mutable QMutex m_bufLock;
96 QList<TFWBuffer*> m_writeBuffers; // protected by buflock
97 QList<TFWBuffer*> m_emptyBuffers; // protected by buflock
98
99 // threads
100 TFWWriteThread *m_writeThread {nullptr};
101 TFWSyncThread *m_syncThread {nullptr};
102
103 // wait conditions
104 QWaitCondition m_bufferEmpty;
105 QWaitCondition m_bufferHasData;
106 QWaitCondition m_bufferSyncWait;
107 QWaitCondition m_bufferWasFreed;
108
109 // constants
110 static const uint kMaxBufferSize;
112 static const uint kMinWriteSize;
114 static const uint kMaxBlockSize;
115
116 bool m_warned {false};
117 bool m_blocking {false};
118 bool m_registered {false};
119};
120
121#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:284
~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: compat.h:60
#define MBASE_PUBLIC
Definition: mythbaseexp.h:8