00001 // -*- Mode: c++ -*- 00002 #ifndef TFW_H_ 00003 #define TFW_H_ 00004 00005 #include <pthread.h> 00006 #include <qmutex.h> 00007 #include <qwaitcondition.h> 00008 #include <qstring.h> 00009 00010 class ThreadedFileWriter 00011 { 00012 public: 00013 ThreadedFileWriter(const QString &fname, int flags, mode_t mode); 00014 ~ThreadedFileWriter(); 00015 00016 bool Open(void); 00017 00018 long long Seek(long long pos, int whence); 00019 uint Write(const void *data, uint count); 00020 00021 void SetWriteBufferSize(uint newSize = TFW_DEF_BUF_SIZE); 00022 void SetWriteBufferMinWriteSize(uint newMinSize = TFW_MIN_WRITE_SIZE); 00023 00024 uint BufUsed(void); 00025 uint BufFree(void); 00026 00027 void Sync(void); 00028 void Flush(void); 00029 00030 protected: 00031 static void *boot_writer(void *); 00032 void DiskLoop(void); 00033 00034 static void *boot_syncer(void *); 00035 void SyncLoop(void); 00036 00037 private: 00038 // file info 00039 QString filename; 00040 int flags; 00041 mode_t mode; 00042 int fd; 00043 00044 // state 00045 bool no_writes; 00046 bool flush; 00047 bool write_is_blocked; 00048 bool in_dtor; 00049 bool ignore_writes; 00050 long long tfw_min_write_size; 00051 00052 // buffer position state 00053 uint rpos; 00054 uint wpos; 00055 QMutex buflock; 00056 long long written; 00057 00058 // buffer 00059 char *buf; 00060 unsigned long tfw_buf_size; 00061 00062 // threads 00063 pthread_t writer; 00064 pthread_t syncer; 00065 00066 // wait conditions 00067 QWaitCondition bufferEmpty; 00068 QWaitCondition bufferHasData; 00069 QWaitCondition bufferSyncWait; 00070 QWaitCondition bufferWroteData; 00071 00072 private: 00073 // constants 00075 static const uint TFW_DEF_BUF_SIZE; 00077 static const uint TFW_MAX_WRITE_SIZE; 00079 static const uint TFW_MIN_WRITE_SIZE; 00080 }; 00081 00082 #endif
1.5.5