ThreadedFileWriter Class Reference

#include <ThreadedFileWriter.h>

List of all members.


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


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.
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 SetWriteBufferSize (uint newSize=TFW_DEF_BUF_SIZE)
 Sets the total size of the write buffer.
void SetWriteBufferMinWriteSize (uint newMinSize=TFW_MIN_WRITE_SIZE)
 Sets the minumum number of bytes to write to disk in a single write.
uint BufUsed (void)
 Number of bytes queued for write by the write thread.
uint BufFree (void)
 Number of bytes that can be written without blocking.
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 safe_write().
void SyncLoop (void)
 The thread run method that calls Sync(void).

Static Protected Member Functions

static void * boot_writer (void *)
 Thunk that runs ThreadedFileWriter::DiskLoop(void).
static void * boot_syncer (void *)
 Thunk that runs ThreadedFileWriter::SyncLoop(void).

Private Attributes

QString filename
int flags
mode_t mode
int fd
bool no_writes
bool flush
bool write_is_blocked
bool in_dtor
bool ignore_writes
long long tfw_min_write_size
uint rpos
 points to end of data written to disk
uint wpos
 points to end of data added to buffer
QMutex buflock
 lock needed to update rpos and wpos
long long written
char * buf
unsigned long tfw_buf_size
pthread_t writer
pthread_t syncer
QWaitCondition bufferEmpty
QWaitCondition bufferHasData
QWaitCondition bufferSyncWait
QWaitCondition bufferWroteData

Static Private Attributes

static const uint TFW_DEF_BUF_SIZE = 2*1024*1024
 Default buffer size.
static const uint TFW_MAX_WRITE_SIZE = TFW_DEF_BUF_SIZE / 4
 Maximum to write to disk in a single write.
static const uint TFW_MIN_WRITE_SIZE = TFW_DEF_BUF_SIZE / 32
 Minimum to write to disk in a single write, when not flushing buffer.

Constructor & Destructor Documentation

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

Creates a threaded file writer.

Definition at line 120 of file ThreadedFileWriter.cpp.

ThreadedFileWriter::~ThreadedFileWriter (  ) 

Commits all writes and closes the file.

Definition at line 168 of file ThreadedFileWriter.cpp.


Member Function Documentation

bool ThreadedFileWriter::Open ( void   ) 

Opens the file we will be writing to.

Returns:
true if we successfully open the file.

Definition at line 141 of file ThreadedFileWriter.cpp.

Referenced by RingBuffer::RingBuffer().

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 257 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.

NOTE: This blocks while buffer is in use by the write to disk thread.

Parameters:
data pointer to data to write to disk
count size of data in bytes

Definition at line 201 of file ThreadedFileWriter.cpp.

Referenced by RingBuffer::Write().

void ThreadedFileWriter::SetWriteBufferSize ( uint  newSize = TFW_DEF_BUF_SIZE  ) 

Sets the total size of the write buffer.

WARNING: This is not safe when another thread is writing to the buffer.

Definition at line 307 of file ThreadedFileWriter.cpp.

Referenced by RingBuffer::SetWriteBufferSize().

void ThreadedFileWriter::SetWriteBufferMinWriteSize ( uint  newMinSize = TFW_MIN_WRITE_SIZE  ) 

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

This is ignored during a Flush(void)

Definition at line 327 of file ThreadedFileWriter.cpp.

Referenced by RingBuffer::SetWriteBufferMinWriteSize().

uint ThreadedFileWriter::BufUsed ( void   ) 

Number of bytes queued for write by the write thread.

Definition at line 426 of file ThreadedFileWriter.cpp.

Referenced by DiskLoop(), Flush(), and RingBuffer::IsIOBound().

uint ThreadedFileWriter::BufFree ( void   ) 

Number of bytes that can be written without blocking.

Definition at line 435 of file ThreadedFileWriter.cpp.

Referenced by RingBuffer::IsIOBound(), and Write().

void ThreadedFileWriter::Sync ( void   ) 

flush data written to the file descriptor to disk.

NOTE: This doesn't even try flush our queue of data. This only ensures that data which has already been sent to the kernel for this file is written to disk. This means that if this backend is writing the data over a network filesystem like NFS, then the data will be visible to the NFS server after this is called. It is also useful in preventing the kernel from buffering up so many writes that they steal the CPU for a long time when the write to disk actually occurs.

Definition at line 291 of file ThreadedFileWriter.cpp.

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

void ThreadedFileWriter::Flush ( void   ) 

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

Definition at line 267 of file ThreadedFileWriter.cpp.

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

void * ThreadedFileWriter::boot_writer ( void *  wotsit  )  [static, protected]

Thunk that runs ThreadedFileWriter::DiskLoop(void).

Definition at line 97 of file ThreadedFileWriter.cpp.

Referenced by Open().

void ThreadedFileWriter::DiskLoop ( void   )  [protected]

The thread run method that actually calls safe_write().

Definition at line 350 of file ThreadedFileWriter.cpp.

Referenced by boot_writer().

void * ThreadedFileWriter::boot_syncer ( void *  wotsit  )  [static, protected]

Thunk that runs ThreadedFileWriter::SyncLoop(void).

Definition at line 110 of file ThreadedFileWriter.cpp.

Referenced by Open().

void ThreadedFileWriter::SyncLoop ( void   )  [protected]

The thread run method that calls Sync(void).

Definition at line 338 of file ThreadedFileWriter.cpp.

Referenced by boot_syncer().


Member Data Documentation

QString ThreadedFileWriter::filename [private]

Definition at line 39 of file ThreadedFileWriter.h.

Referenced by DiskLoop(), and Open().

Definition at line 40 of file ThreadedFileWriter.h.

Referenced by Open().

mode_t ThreadedFileWriter::mode [private]

Definition at line 41 of file ThreadedFileWriter.h.

Referenced by Open().

int ThreadedFileWriter::fd [private]

Definition at line 42 of file ThreadedFileWriter.h.

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

Definition at line 45 of file ThreadedFileWriter.h.

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

Definition at line 46 of file ThreadedFileWriter.h.

Referenced by DiskLoop(), and Flush().

Definition at line 47 of file ThreadedFileWriter.h.

Definition at line 48 of file ThreadedFileWriter.h.

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

Definition at line 49 of file ThreadedFileWriter.h.

Referenced by DiskLoop(), and Open().

Definition at line 50 of file ThreadedFileWriter.h.

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

points to end of data written to disk

Definition at line 53 of file ThreadedFileWriter.h.

Referenced by BufFree(), BufUsed(), DiskLoop(), and SetWriteBufferSize().

points to end of data added to buffer

Definition at line 54 of file ThreadedFileWriter.h.

Referenced by BufFree(), BufUsed(), SetWriteBufferSize(), and Write().

QMutex ThreadedFileWriter::buflock [private]

lock needed to update rpos and wpos

Definition at line 55 of file ThreadedFileWriter.h.

Referenced by BufFree(), BufUsed(), DiskLoop(), SetWriteBufferSize(), and Write().

long long ThreadedFileWriter::written [private]

Definition at line 56 of file ThreadedFileWriter.h.

Referenced by DiskLoop(), and SyncLoop().

char* ThreadedFileWriter::buf [private]

Definition at line 59 of file ThreadedFileWriter.h.

Referenced by DiskLoop(), Open(), SetWriteBufferSize(), Write(), and ~ThreadedFileWriter().

unsigned long ThreadedFileWriter::tfw_buf_size [private]

Definition at line 60 of file ThreadedFileWriter.h.

Referenced by BufFree(), BufUsed(), DiskLoop(), Open(), SetWriteBufferSize(), and Write().

Definition at line 63 of file ThreadedFileWriter.h.

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

Definition at line 64 of file ThreadedFileWriter.h.

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

QWaitCondition ThreadedFileWriter::bufferEmpty [private]

Definition at line 67 of file ThreadedFileWriter.h.

Referenced by DiskLoop(), and Flush().

QWaitCondition ThreadedFileWriter::bufferHasData [private]

Definition at line 68 of file ThreadedFileWriter.h.

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

QWaitCondition ThreadedFileWriter::bufferSyncWait [private]

Definition at line 69 of file ThreadedFileWriter.h.

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

QWaitCondition ThreadedFileWriter::bufferWroteData [private]

Definition at line 70 of file ThreadedFileWriter.h.

Referenced by DiskLoop(), and Write().

const uint ThreadedFileWriter::TFW_DEF_BUF_SIZE = 2*1024*1024 [static, private]

Default buffer size.

Definition at line 75 of file ThreadedFileWriter.h.

Referenced by Open().

Maximum to write to disk in a single write.

Definition at line 77 of file ThreadedFileWriter.h.

Referenced by DiskLoop().

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

Definition at line 79 of file ThreadedFileWriter.h.

Referenced by Open().


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

Generated on Sat Dec 18 05:16:11 2010 for MythTV by  doxygen 1.5.5