MythTV
master
|
This class supports the writing of recordings to disk. More...
#include <libmythbase/threadedfilewriter.h>
Classes | |
class | TFWBuffer |
Public Member Functions | |
ThreadedFileWriter (QString fname, int flags, mode_t mode) | |
Creates a threaded file writer. More... | |
~ThreadedFileWriter () | |
Commits all writes and closes the file. More... | |
bool | Open (void) |
Opens the file we will be writing to. More... | |
bool | ReOpen (const QString &newFilename="") |
Reopens the file we are writing to or opens a new file. More... | |
long long | Seek (long long pos, int whence) |
Seek to a position within stream; May be unsafe. More... | |
int | Write (const void *data, uint count) |
Writes data to the end of the write buffer. More... | |
void | SetWriteBufferMinWriteSize (uint newMinSize=kMinWriteSize) |
Sets the minumum number of bytes to write to disk in a single write. This is ignored during a Flush(void) More... | |
void | Sync (void) const |
Flush data written to the file descriptor to disk. More... | |
void | Flush (void) |
Allow DiskLoop() to flush buffer completely ignoring low watermark. More... | |
bool | SetBlocking (bool block=true) |
Set write blocking mode While in blocking mode, ThreadedFileWriter::Write will wait for buffers to be freed as required, so there's never any data loss. More... | |
bool | WritesFailing (void) const |
Protected Member Functions | |
void | DiskLoop (void) |
The thread run method that actually calls writes to disk. More... | |
void | SyncLoop (void) |
The thread run method that calls Sync(void). More... | |
void | TrimEmptyBuffers (void) |
Private Attributes | |
QString | m_filename |
int | m_flags |
mode_t | m_mode |
int | m_fd {-1} |
bool | m_flush {false} |
bool | m_inDtor {false} |
bool | m_ignoreWrites {false} |
uint | m_tfwMinWriteSize {kMinWriteSize} |
uint | m_totalBufferUse {0} |
QMutex | m_bufLock |
QList< TFWBuffer * > | m_writeBuffers |
QList< TFWBuffer * > | m_emptyBuffers |
TFWWriteThread * | m_writeThread {nullptr} |
TFWSyncThread * | m_syncThread {nullptr} |
QWaitCondition | m_bufferEmpty |
QWaitCondition | m_bufferHasData |
QWaitCondition | m_bufferSyncWait |
QWaitCondition | m_bufferWasFreed |
bool | m_warned {false} |
bool | m_blocking {false} |
bool | m_registered {false} |
Static Private Attributes | |
static const uint | kMaxBufferSize = 8 * 1024 * 1024 |
static const uint | kMinWriteSize = 64 * 1024 |
Minimum to write to disk in a single write, when not flushing buffer. More... | |
static const uint | kMaxBlockSize = 1 * 1024 * 1024 |
Maximum block size to write at a time. More... | |
Friends | |
class | TFWWriteThread |
class | TFWSyncThread |
This class supports the writing of recordings to disk.
This class allows us manage the buffering when writing to disk. We write to the kernel image of the disk using one thread, and sync the kernel's image of the disk to hardware using another thread. The goal here so to block as little as possible when the classes using this class want to add data to the stream.
Definition at line 42 of file threadedfilewriter.h.
|
inline |
Creates a threaded file writer.
Definition at line 50 of file threadedfilewriter.h.
ThreadedFileWriter::~ThreadedFileWriter | ( | ) |
Commits all writes and closes the file.
Definition at line 137 of file threadedfilewriter.cpp.
bool ThreadedFileWriter::Open | ( | void | ) |
Opens the file we will be writing to.
Definition at line 92 of file threadedfilewriter.cpp.
Referenced by StreamHandler::AddNamedOutputFile(), MythFileBuffer::MythFileBuffer(), RemoteFile::OpenInternal(), and ReOpen().
bool ThreadedFileWriter::ReOpen | ( | const QString & | newFilename = "" | ) |
Reopens the file we are writing to or opens a new file.
newFilename | optional name of new file to open |
Definition at line 63 of file threadedfilewriter.cpp.
Referenced by MythFileBuffer::ReOpen().
long long ThreadedFileWriter::Seek | ( | long long | pos, |
int | whence | ||
) |
Seek to a position within stream; May be unsafe.
This method is unsafe if Start() has been called and the call us not preceeded by StopReads(). You probably want to follow Seek() with a StartReads() in this case.
This method assumes that we don't seek very often. It does not use a high performance approach... we just block until the write thread empties the buffer.
Definition at line 297 of file threadedfilewriter.cpp.
Referenced by RemoteFile::SeekInternal(), and MythMediaBuffer::WriterSeek().
int ThreadedFileWriter::Write | ( | const void * | data, |
uint | count | ||
) |
Writes data to the end of the write buffer.
data | pointer to data to write to disk |
count | size of data in bytes |
Definition at line 190 of file threadedfilewriter.cpp.
Referenced by RemoteFile::Write(), MythMediaBuffer::Write(), and StreamHandler::WriteMPTS().
void ThreadedFileWriter::SetWriteBufferMinWriteSize | ( | uint | newMinSize = kMinWriteSize | ) |
Sets the minumum number of bytes to write to disk in a single write. This is ignored during a Flush(void)
Definition at line 374 of file threadedfilewriter.cpp.
void ThreadedFileWriter::Sync | ( | void | ) | const |
Flush data written to the file descriptor to disk.
This prevents freezing up Linux disk access on a running CFQ, AS, or Deadline as the disk write schedulers. It does this via two mechanism. One is a data sync using the best mechanism available (fdatasync then fsync). The second is by telling the kernel we do not intend to use the data just written anytime soon so other processes time-slices will not be used to deal with our excess dirty pages.
Definition at line 355 of file threadedfilewriter.cpp.
Referenced by MythMediaBuffer::Sync(), and SyncLoop().
void ThreadedFileWriter::Flush | ( | void | ) |
Allow DiskLoop() to flush buffer completely ignoring low watermark.
Definition at line 318 of file threadedfilewriter.cpp.
Referenced by RemoteFile::GetFileSize(), ReOpen(), MythMediaBuffer::WriterFlush(), MythMediaBuffer::~MythMediaBuffer(), and ~ThreadedFileWriter().
Set write blocking mode While in blocking mode, ThreadedFileWriter::Write will wait for buffers to be freed as required, so there's never any data loss.
block | False if not blocking, true if blocking |
Definition at line 613 of file threadedfilewriter.cpp.
Referenced by RemoteFile::SetBlocking(), and MythMediaBuffer::WriterSetBlocking().
|
inline |
Definition at line 65 of file threadedfilewriter.h.
|
protected |
The thread run method that actually calls writes to disk.
Definition at line 409 of file threadedfilewriter.cpp.
Referenced by TFWWriteThread::run().
|
protected |
The thread run method that calls Sync(void).
Definition at line 385 of file threadedfilewriter.cpp.
Referenced by TFWSyncThread::run().
|
protected |
Definition at line 586 of file threadedfilewriter.cpp.
Referenced by DiskLoop().
|
friend |
Definition at line 44 of file threadedfilewriter.h.
Referenced by Open().
|
friend |
Definition at line 45 of file threadedfilewriter.h.
Referenced by Open().
|
private |
Definition at line 74 of file threadedfilewriter.h.
Referenced by DiskLoop(), Open(), ReOpen(), SyncLoop(), and ~ThreadedFileWriter().
|
private |
Definition at line 75 of file threadedfilewriter.h.
Referenced by Open().
|
private |
Definition at line 76 of file threadedfilewriter.h.
Referenced by Open().
|
private |
Definition at line 77 of file threadedfilewriter.h.
Referenced by DiskLoop(), Open(), ReOpen(), Seek(), Sync(), and ~ThreadedFileWriter().
Definition at line 80 of file threadedfilewriter.h.
Referenced by DiskLoop(), Flush(), and Seek().
Definition at line 81 of file threadedfilewriter.h.
Referenced by DiskLoop(), SyncLoop(), and ~ThreadedFileWriter().
Definition at line 82 of file threadedfilewriter.h.
Referenced by DiskLoop(), Open(), SyncLoop(), and Write().
|
private |
Definition at line 83 of file threadedfilewriter.h.
Referenced by SetWriteBufferMinWriteSize().
|
private |
Definition at line 84 of file threadedfilewriter.h.
Referenced by DiskLoop(), Flush(), Seek(), and Write().
|
mutableprivate |
Definition at line 93 of file threadedfilewriter.h.
Referenced by DiskLoop(), Flush(), ReOpen(), Seek(), SetWriteBufferMinWriteSize(), SyncLoop(), Write(), and ~ThreadedFileWriter().
|
private |
Definition at line 94 of file threadedfilewriter.h.
Referenced by DiskLoop(), Flush(), Seek(), Write(), and ~ThreadedFileWriter().
|
private |
Definition at line 95 of file threadedfilewriter.h.
Referenced by DiskLoop(), TrimEmptyBuffers(), Write(), and ~ThreadedFileWriter().
|
private |
Definition at line 98 of file threadedfilewriter.h.
Referenced by Open(), and ~ThreadedFileWriter().
|
private |
Definition at line 99 of file threadedfilewriter.h.
Referenced by Open(), and ~ThreadedFileWriter().
|
private |
Definition at line 102 of file threadedfilewriter.h.
Referenced by DiskLoop(), Flush(), and Seek().
|
private |
Definition at line 103 of file threadedfilewriter.h.
Referenced by DiskLoop(), Flush(), Seek(), SetWriteBufferMinWriteSize(), Write(), and ~ThreadedFileWriter().
|
private |
Definition at line 104 of file threadedfilewriter.h.
Referenced by SyncLoop(), and ~ThreadedFileWriter().
|
private |
Definition at line 105 of file threadedfilewriter.h.
Referenced by DiskLoop(), and Write().
|
staticprivate |
Definition at line 108 of file threadedfilewriter.h.
Referenced by Write().
|
staticprivate |
Minimum to write to disk in a single write, when not flushing buffer.
Definition at line 110 of file threadedfilewriter.h.
Referenced by DiskLoop(), and Write().
|
staticprivate |
Maximum block size to write at a time.
Definition at line 112 of file threadedfilewriter.h.
Referenced by Write().
Definition at line 114 of file threadedfilewriter.h.
Referenced by Write().
Definition at line 115 of file threadedfilewriter.h.
Referenced by SetBlocking(), and Write().
Definition at line 116 of file threadedfilewriter.h.
Referenced by DiskLoop(), Open(), ReOpen(), SyncLoop(), and ~ThreadedFileWriter().