MythTV  0.26-pre
Classes | Public Member Functions | Protected Member Functions | Private Attributes | Static Private Attributes | Friends
ThreadedFileWriter Class Reference

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

#include <ThreadedFileWriter.h>

List of all members.

Classes

class  TFWBuffer

Public Member Functions

 ThreadedFileWriter (const QString &fname, int flags, mode_t mode)
 Creates a threaded file writer.
 ~ThreadedFileWriter ()
 Commits all writes and closes the file.
bool Open (void)
 Opens the file we will be writing to.
bool ReOpen (QString newFilename="")
 Reopens the file we are writing to or opens a new file.
long long Seek (long long pos, int whence)
 Seek to a position within stream; May be unsafe.
uint Write (const void *data, uint count)
 Writes data to the end of the write buffer.
void SetWriteBufferMinWriteSize (uint newMinSize=kMinWriteSize)
 Sets the minumum number of bytes to write to disk in a single write.
void Sync (void)
 Flush data written to the file descriptor to disk.
void Flush (void)
 Allow DiskLoop() to flush buffer completely ignoring low watermark.

Protected Member Functions

void DiskLoop (void)
 The thread run method that actually calls writes to disk.
void SyncLoop (void)
 The thread run method that calls Sync(void).
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

Static Private Attributes

static const uint kMaxBufferSize = 128 * 1024 * 1024
static const uint kMinWriteSize = 64 * 1024
 Minimum to write to disk in a single write, when not flushing buffer.

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 40 of file ThreadedFileWriter.h.


Constructor & Destructor Documentation

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

Creates a threaded file writer.

Definition at line 59 of file ThreadedFileWriter.cpp.

Commits all writes and closes the file.

Definition at line 148 of file ThreadedFileWriter.cpp.


Member Function Documentation

Opens the file we will be writing to.

Returns:
true if we successfully open the file.

Definition at line 104 of file ThreadedFileWriter.cpp.

Referenced by FileRingBuffer::FileRingBuffer(), named_output_file_common(), and 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 80 of file ThreadedFileWriter.cpp.

Referenced by FileRingBuffer::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 269 of file ThreadedFileWriter.cpp.

Referenced by RingBuffer::WriterSeek().

uint 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 198 of file ThreadedFileWriter.cpp.

Referenced by ASIStreamHandler::run(), and RingBuffer::Write().

Sets the minumum number of bytes to write to disk in a single write.

This is ignored during a Flush(void)

Definition at line 346 of file ThreadedFileWriter.cpp.

Referenced by RingBuffer::SetWriteBufferMinWriteSize().

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 327 of file ThreadedFileWriter.cpp.

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

Allow DiskLoop() to flush buffer completely ignoring low watermark.

Definition at line 290 of file ThreadedFileWriter.cpp.

Referenced by ReOpen(), RingBuffer::WriterFlush(), RingBuffer::~RingBuffer(), and ~ThreadedFileWriter().

The thread run method that actually calls writes to disk.

Definition at line 374 of file ThreadedFileWriter.cpp.

Referenced by TFWWriteThread::run().

The thread run method that calls Sync(void).

Definition at line 357 of file ThreadedFileWriter.cpp.

Referenced by TFWSyncThread::run().

Definition at line 535 of file ThreadedFileWriter.cpp.

Referenced by DiskLoop().


Friends And Related Function Documentation

friend class TFWWriteThread [friend]

Definition at line 42 of file ThreadedFileWriter.h.

Referenced by Open().

friend class TFWSyncThread [friend]

Definition at line 43 of file ThreadedFileWriter.h.

Referenced by Open().


Member Data Documentation

QString ThreadedFileWriter::filename [private]

Definition at line 67 of file ThreadedFileWriter.h.

Referenced by Open().

mode_t ThreadedFileWriter::mode [private]

Definition at line 68 of file ThreadedFileWriter.h.

Referenced by Open().

Definition at line 69 of file ThreadedFileWriter.h.

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

Definition at line 72 of file ThreadedFileWriter.h.

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

Definition at line 73 of file ThreadedFileWriter.h.

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

Definition at line 74 of file ThreadedFileWriter.h.

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

Definition at line 75 of file ThreadedFileWriter.h.

Referenced by SetWriteBufferMinWriteSize().

Definition at line 76 of file ThreadedFileWriter.h.

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

QMutex ThreadedFileWriter::buflock [mutable, private]

Definition at line 86 of file ThreadedFileWriter.h.

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

Definition at line 87 of file ThreadedFileWriter.h.

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

Definition at line 90 of file ThreadedFileWriter.h.

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

Definition at line 91 of file ThreadedFileWriter.h.

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

QWaitCondition ThreadedFileWriter::bufferEmpty [private]

Definition at line 94 of file ThreadedFileWriter.h.

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

QWaitCondition ThreadedFileWriter::bufferHasData [private]
QWaitCondition ThreadedFileWriter::bufferSyncWait [private]

Definition at line 96 of file ThreadedFileWriter.h.

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

const uint ThreadedFileWriter::kMaxBufferSize = 128 * 1024 * 1024 [static, private]

Definition at line 99 of file ThreadedFileWriter.h.

Referenced by Write().

const uint ThreadedFileWriter::kMinWriteSize = 64 * 1024 [static, private]

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

Definition at line 101 of file ThreadedFileWriter.h.

Referenced by DiskLoop(), and Write().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends