MythTV  master
Classes | Public Member Functions | Protected Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
ThreadedFileWriter Class Reference

This class supports the writing of recordings to disk. More...

#include <threadedfilewriter.h>

Collaboration diagram for ThreadedFileWriter:
Collaboration graph
[legend]

Classes

class  TFWBuffer
 

Public Member Functions

 ThreadedFileWriter (const 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. More...
 
void Sync (void)
 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_in_dtor {false}
 
bool m_ignore_writes {false}
 
uint m_tfw_min_write_size {kMinWriteSize}
 
uint m_totalBufferUse {0}
 
QMutex m_buflock
 
QList< TFWBuffer * > m_writeBuffers
 
QList< TFWBuffer * > m_emptyBuffers
 
TFWWriteThreadm_writeThread {nullptr}
 
TFWSyncThreadm_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
 

Detailed Description

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 41 of file threadedfilewriter.h.

Constructor & Destructor Documentation

◆ ThreadedFileWriter()

ThreadedFileWriter::ThreadedFileWriter ( const QString &  fname,
int  flags,
mode_t  mode 
)
inline

Creates a threaded file writer.

Definition at line 49 of file threadedfilewriter.h.

◆ ~ThreadedFileWriter()

ThreadedFileWriter::~ThreadedFileWriter ( )

Commits all writes and closes the file.

Definition at line 137 of file threadedfilewriter.cpp.

Member Function Documentation

◆ Open()

bool ThreadedFileWriter::Open ( void  )

Opens the file we will be writing to.

Returns
true if we successfully open the file.

Definition at line 92 of file threadedfilewriter.cpp.

Referenced by StreamHandler::AddNamedOutputFile(), FileRingBuffer::FileRingBuffer(), RemoteFile::OpenInternal(), and ReOpen().

◆ ReOpen()

bool ThreadedFileWriter::ReOpen ( const QString &  newFilename = "")

Reopens the file we are writing to or opens a new file.

Returns
true if we successfully open the file.
Parameters
newFilenameoptional name of new file to open

Definition at line 63 of file threadedfilewriter.cpp.

Referenced by FileRingBuffer::ReOpen().

◆ Seek()

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 RingBuffer::WriterSeek().

◆ Write()

int ThreadedFileWriter::Write ( const void *  data,
uint  count 
)

Writes data to the end of the write buffer.

Parameters
datapointer to data to write to disk
countsize of data in bytes

Definition at line 190 of file threadedfilewriter.cpp.

Referenced by RemoteFile::Write(), RingBuffer::Write(), and StreamHandler::WriteMPTS().

◆ SetWriteBufferMinWriteSize()

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.

Referenced by RingBuffer::SetWriteBufferMinWriteSize().

◆ Sync()

void ThreadedFileWriter::Sync ( void  )

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.

Note
We used to also use sync_file_range on Linux, however this is incompatible with newer filesystems such as BRTFS and does not actually sync any blocks that have not been allocated yet so it was never really appropriate for ThreadedFileWriter.
We use standard posix calls for this, so any operating system supporting the calls will benefit, but this has been designed with Linux in mind. Other OS's may benefit from revisiting this function.

Definition at line 355 of file threadedfilewriter.cpp.

Referenced by RingBuffer::Sync(), and SyncLoop().

◆ Flush()

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(), RingBuffer::WriterFlush(), RingBuffer::~RingBuffer(), and ~ThreadedFileWriter().

◆ SetBlocking()

bool ThreadedFileWriter::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.

Returns
old mode value
Parameters
blockFalse if not blocking, true if blocking

Definition at line 612 of file threadedfilewriter.cpp.

Referenced by RemoteFile::SetBlocking(), and RingBuffer::WriterSetBlocking().

◆ WritesFailing()

bool ThreadedFileWriter::WritesFailing ( void  ) const
inline

Definition at line 64 of file threadedfilewriter.h.

◆ DiskLoop()

void ThreadedFileWriter::DiskLoop ( void  )
protected

The thread run method that actually calls writes to disk.

Definition at line 409 of file threadedfilewriter.cpp.

Referenced by TFWWriteThread::run().

◆ SyncLoop()

void ThreadedFileWriter::SyncLoop ( void  )
protected

The thread run method that calls Sync(void).

Definition at line 385 of file threadedfilewriter.cpp.

Referenced by TFWSyncThread::run().

◆ TrimEmptyBuffers()

void ThreadedFileWriter::TrimEmptyBuffers ( void  )
protected

Definition at line 585 of file threadedfilewriter.cpp.

Referenced by DiskLoop().

Friends And Related Function Documentation

◆ TFWWriteThread

friend class TFWWriteThread
friend

Definition at line 43 of file threadedfilewriter.h.

Referenced by Open().

◆ TFWSyncThread

friend class TFWSyncThread
friend

Definition at line 44 of file threadedfilewriter.h.

Referenced by Open().

Member Data Documentation

◆ m_filename

QString ThreadedFileWriter::m_filename
private

Definition at line 73 of file threadedfilewriter.h.

Referenced by DiskLoop(), Open(), ReOpen(), SyncLoop(), and ~ThreadedFileWriter().

◆ m_flags

int ThreadedFileWriter::m_flags
private

Definition at line 74 of file threadedfilewriter.h.

Referenced by Open().

◆ m_mode

mode_t ThreadedFileWriter::m_mode
private

Definition at line 75 of file threadedfilewriter.h.

Referenced by Open().

◆ m_fd

int ThreadedFileWriter::m_fd {-1}
private

Definition at line 76 of file threadedfilewriter.h.

Referenced by DiskLoop(), Open(), ReOpen(), Seek(), Sync(), and ~ThreadedFileWriter().

◆ m_flush

bool ThreadedFileWriter::m_flush {false}
private

Definition at line 79 of file threadedfilewriter.h.

Referenced by DiskLoop(), Flush(), and Seek().

◆ m_in_dtor

bool ThreadedFileWriter::m_in_dtor {false}
private

Definition at line 80 of file threadedfilewriter.h.

Referenced by DiskLoop(), SyncLoop(), and ~ThreadedFileWriter().

◆ m_ignore_writes

bool ThreadedFileWriter::m_ignore_writes {false}
private

Definition at line 81 of file threadedfilewriter.h.

Referenced by DiskLoop(), Open(), SyncLoop(), and Write().

◆ m_tfw_min_write_size

uint ThreadedFileWriter::m_tfw_min_write_size {kMinWriteSize}
private

Definition at line 82 of file threadedfilewriter.h.

Referenced by SetWriteBufferMinWriteSize().

◆ m_totalBufferUse

uint ThreadedFileWriter::m_totalBufferUse {0}
private

Definition at line 83 of file threadedfilewriter.h.

Referenced by DiskLoop(), Flush(), Seek(), and Write().

◆ m_buflock

QMutex ThreadedFileWriter::m_buflock
mutableprivate

◆ m_writeBuffers

QList<TFWBuffer*> ThreadedFileWriter::m_writeBuffers
private

Definition at line 93 of file threadedfilewriter.h.

Referenced by DiskLoop(), Flush(), Seek(), Write(), and ~ThreadedFileWriter().

◆ m_emptyBuffers

QList<TFWBuffer*> ThreadedFileWriter::m_emptyBuffers
private

Definition at line 94 of file threadedfilewriter.h.

Referenced by DiskLoop(), TrimEmptyBuffers(), Write(), and ~ThreadedFileWriter().

◆ m_writeThread

TFWWriteThread* ThreadedFileWriter::m_writeThread {nullptr}
private

Definition at line 97 of file threadedfilewriter.h.

Referenced by Open(), and ~ThreadedFileWriter().

◆ m_syncThread

TFWSyncThread* ThreadedFileWriter::m_syncThread {nullptr}
private

Definition at line 98 of file threadedfilewriter.h.

Referenced by Open(), and ~ThreadedFileWriter().

◆ m_bufferEmpty

QWaitCondition ThreadedFileWriter::m_bufferEmpty
private

Definition at line 101 of file threadedfilewriter.h.

Referenced by DiskLoop(), Flush(), and Seek().

◆ m_bufferHasData

QWaitCondition ThreadedFileWriter::m_bufferHasData
private

◆ m_bufferSyncWait

QWaitCondition ThreadedFileWriter::m_bufferSyncWait
private

Definition at line 103 of file threadedfilewriter.h.

Referenced by SyncLoop(), and ~ThreadedFileWriter().

◆ m_bufferWasFreed

QWaitCondition ThreadedFileWriter::m_bufferWasFreed
private

Definition at line 104 of file threadedfilewriter.h.

Referenced by DiskLoop(), and Write().

◆ kMaxBufferSize

const uint ThreadedFileWriter::kMaxBufferSize = 8 * 1024 * 1024
staticprivate

Definition at line 107 of file threadedfilewriter.h.

Referenced by Write().

◆ kMinWriteSize

const uint ThreadedFileWriter::kMinWriteSize = 64 * 1024
staticprivate

Minimum to write to disk in a single write, when not flushing buffer.

Definition at line 109 of file threadedfilewriter.h.

Referenced by DiskLoop(), and Write().

◆ kMaxBlockSize

const uint ThreadedFileWriter::kMaxBlockSize = 1 * 1024 * 1024
staticprivate

Maximum block size to write at a time.

Definition at line 111 of file threadedfilewriter.h.

Referenced by Write().

◆ m_warned

bool ThreadedFileWriter::m_warned {false}
private

Definition at line 113 of file threadedfilewriter.h.

Referenced by Write().

◆ m_blocking

bool ThreadedFileWriter::m_blocking {false}
private

Definition at line 114 of file threadedfilewriter.h.

Referenced by SetBlocking(), and Write().

◆ m_registered

bool ThreadedFileWriter::m_registered {false}
private

Definition at line 115 of file threadedfilewriter.h.

Referenced by DiskLoop(), Open(), ReOpen(), SyncLoop(), and ~ThreadedFileWriter().


The documentation for this class was generated from the following files: