defaultcircularbuffer.h

来自「C++封装的视频采集代码」· C头文件 代码 · 共 271 行

H
271
字号
#ifndef DEFAULTCIRCULARBUFFER_H#define DEFAULTCIRCULARBUFFER_H#include "CircularBuffer.h"#include "CircularBufferPosition.h"#include "Utils/UnCachedBuffer.h"#include "Utils/Vector.h"#include "SysLib/oxsemi_sem.h"#include "MarginTesterTotalFreeSpace.h"#include <memory>#include <utility>//#define INUSE_TRACKINGnamespace oxsemi{    namespace circular_buffer    {        class DefaultCircularBuffer : public CircularBuffer        {            // These typedef will be used as the threshold testing Policy by this            // class.             typedef  MarginTesterTotalFreeSpace   WriteThresholdPolicy;            typedef  MarginTesterTotalFreeSpace   ReadThresholdPolicy;            // typedef  MarginTesterForNonSplitBuffers   WriteThresholdPolicy;            // typedef  MarginTesterForNonSplitBuffers   ReadThresholdPolicy;                                public:            DefaultCircularBuffer(                unsigned maxEmptyEventListeners,                unsigned maxReadThresholdEventListeners,                unsigned maxWriteThresholdEventListeners,                unsigned long size,                int numberOfReaders);            ~DefaultCircularBuffer();            std::pair<bool, CircularBufferResidentObject*> AcquireForRead(                unsigned readerNumber,                CircularBufferResidentObject* previousObject,                bool blockIfNoObjectsAvailable,                unsigned long sizeHint);            bool AcquireForWrite(                CircularBufferResidentObject& object,                CircularBufferResidentObject* previousObject,                bool blockIfInsufficientSpace);            bool FlushAvailableForRead();            bool FlushAvailableForRead(unsigned readerNumber);            void ForceUnblockAll();            unsigned long GetNumberOfObjects(unsigned readerNumber) const { return numberOfObjects_[readerNumber]; }            unsigned GetNumberOfReaders() const { return numberOfReaders_; }            std::pair<bool, unsigned long> GetReadLimit(unsigned readerNumber);            unsigned long GetSpaceAvailableForWrite() const;            unsigned long GetReadThreshold(unsigned readerNumber) const;            unsigned long GetWriteThreshold() const;            bool IsAboveReadThreshold(unsigned readerNumber) const;            bool IsAboveWriteThreshold() const;            bool IsComplete() const;            bool IsEmpty(unsigned readerNumber) const;                                    /**             * @param readerNumber An unsigned specifying the number of the             *  reader whose read pointer is to be advanced             * @return An unsigned int - the number of bytes used in the             *  circular buffer.             */            unsigned int BytesUsed(unsigned int readerNumber) const;            bool RegisterResidentClass(                CircularBufferResidentObject::Id& id,                ObjectPool& pool);            void ReleaseFromRead(                unsigned readerNumber,                CircularBufferResidentObject* object,                bool pending);            void ReleaseFromWrite(                const CircularBufferResidentObject& object,                bool discard);            void ReleaseFromWrite(                const Vector<CircularBufferResidentObject*>& buffers,                bool discard);            bool ShuttingDown();            void SetReadThreshold(                unsigned readerNumber,                unsigned long threshold);            void SetReaderBlockState(bool readersCanBlock);            void SetWriteThreshold(unsigned long threshold);            void SetReadLimit(                unsigned readerNumber,                unsigned long limit);            bool TrimMostRecentlyAcquiredObjectForWrite(                CircularBufferResidentObject& object,                CircularBufferResidentObject* previousObject);        protected:            void DoNotify(EmptyEventListener* listener);            void DoNotify(                ReadThresholdEventListener* listener,                const unsigned& readerNumber);            void DoNotify(WriteThresholdEventListener* listener);        private:            struct ReaderPositionInfo            {                CircularBufferPosition position_;                bool empty_;            };            static const int INITIAL_KNOWN_OBJECT_TYPES_SIZE = 1;#ifdef INUSE_TRACKING            static const unsigned long INITIAL_INUSE_CONTAINER_SIZE = 5;#endif            typedef Vector<std::pair<CircularBufferResidentObject::Id, ObjectPool*> > KNOWN_TYPES_CONTAINTER_TYPE;#ifdef INUSE_TRACKING            typedef Vector<CircularBufferResidentObject*> InUseContainer;#endif            mutable                             syslib::oxsemi_msem mutex_;            std::auto_ptr<UnCachedBuffer>       buffer_;            CircularBufferPosition              writePosition_;            bool                                writeEmpty_;            CircularBufferPosition              mostRecentAcquireAfterPosition_;            unsigned                            numberOfReaders_;            ReaderPositionInfo*                 readPositions_;            KNOWN_TYPES_CONTAINTER_TYPE         knownObjectTypes_;            CircularBufferResidentObject::Id    nextFreeId_;            syslib::oxsemi_csem                 readSemaphore_;            unsigned                            readWaitCount_;            syslib::oxsemi_csem                 writeSemaphore_;            unsigned                            writeWaitCount_;            bool                                doNotBlock_;            bool                                doNotBlockReaders_;            ReadThresholdPolicy*                mpReadThresholds_;            WriteThresholdPolicy                writeThreshold_;            std::pair<bool, unsigned long>*     readLimitInfo_;            unsigned long*                      numberOfObjects_;            unsigned long*                      limitCount_;            bool                                lastAnyReadBuffersAvailable_;            unsigned                            outstandingWriteAcquisitions_;            unsigned*                           outstandingReadAcquisitions_;#ifdef INUSE_TRACKING            InUseContainer                      writeInUse_;            InUseContainer**                    readerInUse_;#endif            /**             * Inherited from Notifier, will be called after a new listener has              * been added.             */            void PostAdditionAction( ReadThresholdEventListener* pListener );                                    /**             * Carry out actions required after objects have been released from             * read. These are notifying all writers of the freed space in the             * buffer, notifying buffer empty listeners if there are no objects             * available for reading and notifying write threshold listeners if             * the amount of space in the buffer available to writers has cross-             * ed the threshold             *             * @param wasBelowWriteThreshold A bool specifying whether the amount             *  of buffer space available for writing was below the event noti-             *  fication threshold before whatever action the caller performs             *  was performed             */            void CommonReleaseFromRead(bool wasBelowWriteThreshold);            /**             * Assuming the passed positions are no more than one loop of the             * circular buffer apart, calculate how much in advance of the first             * position is the second position. If the first position is in ad-             * vance of the second, return zero             *             * @param earlierPosition A const CircularBufferPosition& giving the             *  first position             * @param laterPosition A const CircularBufferPosition& giving the             *  second position             * @return An unsigned long Giving the number of bytes by which the             *  second position is in advance of the first             */            unsigned long DefaultCircularBuffer::GetDifference(                const CircularBufferPosition& earlierPosition,                const CircularBufferPosition& laterPosition);            /**             * @param id A CircularBufferResidentObject::Id giving the id for             *  which to find the matching object pool             * @return An ObjectPool* pointing to the object pool matching the             *  specified ID. Will be null if no match was found             */            ObjectPool* FindPoolForType(CircularBufferResidentObject::Id id);            /**             * @param readPosition A const CircularBufferPosition& referring to             *  a position within this circular buffer at which the available             *  space for reading is to be determined             * @return CircularBufferSpaceInfo Giving the sizes of upto two             *  sections of the circular buffer that are available for reading             *  based on the specified circularBufferPosition being the current             *  read position within the buffer             */            CircularBufferSpaceInfo GetReadSpaceInfo(const CircularBufferPosition& readPosition) const;            /**             * @param writePosition A const CircularBufferPosition& referring to             *  a position within this circular buffer at which the available             *  space for writing is to be determined             * @return CircularBufferSpaceInfo Giving the sizes of upto two             *  sections of the circular buffer that are available for writing             *  based on the specified circularBufferPosition being the current             *  write position within the buffer             */            CircularBufferSpaceInfo GetWriteSpaceInfo(const CircularBufferPosition& writePosition) const;            /**             * @param readerNumber An unsigned specifying the reader to be tested             *  for having the oldest position             * @return A std::pair<bool, int> whose first element specifies             *  whether the specified reader is one of the oldest and whose             *  second element specifies the number of reader who have the old-             *  est position             */            std::pair<bool, unsigned> DefaultCircularBuffer::IsOldestReader(unsigned readerNumber) const;            /**             * Wake any readers blocked awaiting objects for read, so they may             * re-attempt to acquire an object             */            void NotifyReaders();            /**             * Wake the writer blocked awaiting space to write an object into             * the buffer, so it may re-attempt to find space for the object             */            void NotifyWriters();        };    }}#endif        //  #if !defined(__DEFAULTCIRCULARBUFFER_H__)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?