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 (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 filename
 
int flags
 
mode_t mode
 
int fd
 
bool flush
 
bool in_dtor
 
bool ignore_writes
 
uint tfw_min_write_size
 
uint totalBufferUse
 
QMutex buflock
 
QList< TFWBuffer * > writeBuffers
 
QList< TFWBuffer * > emptyBuffers
 
TFWWriteThreadwriteThread
 
TFWSyncThreadsyncThread
 
QWaitCondition bufferEmpty
 
QWaitCondition bufferHasData
 
QWaitCondition bufferSyncWait
 
QWaitCondition bufferWasFreed
 
bool m_warned
 
bool m_blocking
 
bool m_registered
 

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 
)

Creates a threaded file writer.

Definition at line 60 of file threadedfilewriter.cpp.

◆ ~ThreadedFileWriter()

ThreadedFileWriter::~ThreadedFileWriter ( )

Commits all writes and closes the file.

Definition at line 156 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 111 of file threadedfilewriter.cpp.

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

◆ ReOpen()

bool ThreadedFileWriter::ReOpen ( 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 82 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 316 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 209 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 393 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 374 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 337 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 631 of file threadedfilewriter.cpp.

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

◆ WritesFailing()

bool ThreadedFileWriter::WritesFailing ( void  ) const
inline

Definition at line 60 of file threadedfilewriter.h.

◆ DiskLoop()

void ThreadedFileWriter::DiskLoop ( void  )
protected

The thread run method that actually calls writes to disk.

Definition at line 428 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 404 of file threadedfilewriter.cpp.

Referenced by TFWSyncThread::run().

◆ TrimEmptyBuffers()

void ThreadedFileWriter::TrimEmptyBuffers ( void  )
protected

Definition at line 604 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

◆ filename

QString ThreadedFileWriter::filename
private

◆ flags

int ThreadedFileWriter::flags
private

Definition at line 70 of file threadedfilewriter.h.

Referenced by Open().

◆ mode

mode_t ThreadedFileWriter::mode
private

Definition at line 71 of file threadedfilewriter.h.

Referenced by Open().

◆ fd

int ThreadedFileWriter::fd
private

Definition at line 72 of file threadedfilewriter.h.

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

◆ flush

bool ThreadedFileWriter::flush
private

Definition at line 75 of file threadedfilewriter.h.

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

◆ in_dtor

bool ThreadedFileWriter::in_dtor
private

Definition at line 76 of file threadedfilewriter.h.

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

◆ ignore_writes

bool ThreadedFileWriter::ignore_writes
private

Definition at line 77 of file threadedfilewriter.h.

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

◆ tfw_min_write_size

uint ThreadedFileWriter::tfw_min_write_size
private

Definition at line 78 of file threadedfilewriter.h.

Referenced by SetWriteBufferMinWriteSize().

◆ totalBufferUse

uint ThreadedFileWriter::totalBufferUse
private

Definition at line 79 of file threadedfilewriter.h.

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

◆ buflock

QMutex ThreadedFileWriter::buflock
mutableprivate

◆ writeBuffers

QList<TFWBuffer*> ThreadedFileWriter::writeBuffers
private

Definition at line 89 of file threadedfilewriter.h.

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

◆ emptyBuffers

QList<TFWBuffer*> ThreadedFileWriter::emptyBuffers
private

Definition at line 90 of file threadedfilewriter.h.

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

◆ writeThread

TFWWriteThread* ThreadedFileWriter::writeThread
private

Definition at line 93 of file threadedfilewriter.h.

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

◆ syncThread

TFWSyncThread* ThreadedFileWriter::syncThread
private

Definition at line 94 of file threadedfilewriter.h.

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

◆ bufferEmpty

QWaitCondition ThreadedFileWriter::bufferEmpty
private

Definition at line 97 of file threadedfilewriter.h.

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

◆ bufferHasData

QWaitCondition ThreadedFileWriter::bufferHasData
private

◆ bufferSyncWait

QWaitCondition ThreadedFileWriter::bufferSyncWait
private

Definition at line 99 of file threadedfilewriter.h.

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

◆ bufferWasFreed

QWaitCondition ThreadedFileWriter::bufferWasFreed
private

Definition at line 100 of file threadedfilewriter.h.

Referenced by DiskLoop(), and Write().

◆ kMaxBufferSize

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

Definition at line 103 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 105 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 107 of file threadedfilewriter.h.

Referenced by Write().

◆ m_warned

bool ThreadedFileWriter::m_warned
private

Definition at line 109 of file threadedfilewriter.h.

Referenced by Write().

◆ m_blocking

bool ThreadedFileWriter::m_blocking
private

Definition at line 110 of file threadedfilewriter.h.

Referenced by SetBlocking(), and Write().

◆ m_registered

bool ThreadedFileWriter::m_registered
private

Definition at line 111 of file threadedfilewriter.h.

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


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