XCube Stream Reader SDK
x3c/include/X3cFileBuf.h
00001 /*
00002  * X3cFileBuf.h
00003  *
00004  *  Created on: Jan 23, 2011
00005  *      Author: nilham
00006  */
00007 
00008 #ifndef X3CFILEBUF_H_
00009 #define X3CFILEBUF_H_
00010 
00011 #include <pthread.h>
00012 
00013 /*
00014  * Forward declaration to avoid circular inclusions.
00015  */
00016 class X3cFileThreadReader;
00017 
00018 /*
00019  * Class for controlling access to buffer area.
00020  *
00021  * Notice that the methods all shall be synchronized
00022  * through mutex access. It may not be necessary
00023  * for some methods, but is there for consistency.
00024  */
00025 class X3cFileBuf
00026 {
00027 public:
00028         /*
00029          * Construct and set buffer start and end pointers.
00030          */
00031         X3cFileBuf(X3cFileThreadReader *parent, unsigned int bufNum, char *start, size_t len);
00032         /*
00033          * Destroy, destroy, destroy...
00034          */
00035         virtual ~X3cFileBuf();
00036         /*
00037          * Close the buffer.
00038          * Assumption is that close is ONLY called from reader thread,
00039          * in which case we know that it's only the writer thread that
00040          * has to be released.
00041          */
00042         void close();
00043         /*
00044          * Wait until buffer is empty.
00045          * Returns true if wait was OK and false if buffer was closed.
00046          */
00047         bool waitForWrite();
00048         /*
00049          * Wait until buffer isn't empty.
00050          * Returns true if wait was OK and false if buffer was closed.
00051          */
00052         bool waitForRead();
00053         /*
00054          * Get length of real data in the buffer.
00055          * Notice: May be zero, which indicates either unloaded buffer or EOF.
00056          */
00057         size_t getDataLen();
00058         /*
00059          * Mark buffer as filled and release buffer for read.
00060          *
00061          * @realLen - Actual amount written into buffer,
00062          *            which may be less than the buffer size
00063          *            when EOF is encountered.
00064          */
00065         void setFilled(size_t realLen);
00066         /*
00067          * Mark buffer as empty and release buffer for write.
00068          */
00069         void setEmpty();
00070         /*
00071          * Get the pointer to the buffer.
00072          */
00073         char *getBufPtr();
00074         /*
00075          * Get the size of the buffer.
00076          */
00077         size_t getBufSize();
00078         /*
00079          * Set EOF flag.
00080          */
00081         void setEof(bool eof);
00082         /*
00083          * Check EOF flag.
00084          */
00085         bool isEof();
00086         /*
00087          * See if buf is flagged empty.
00088          */
00089         bool isEmpty();
00090 
00091 private:
00092         X3cFileThreadReader *parent;
00093         bool valid;
00094         bool empty;
00095         bool busy;
00096         bool eof;
00097         char *start;
00098         size_t dataLen;
00099         size_t bufLen;
00100         pthread_mutex_t mRWMutex;
00101         pthread_cond_t writeWaiter;
00102         pthread_cond_t readWaiter;
00103         unsigned int bufNum;
00104 };
00105 
00106 #endif /* X3CFILEBUF_H_ */
 All Classes Files Functions Variables Typedefs Friends Defines