FIFO template classes
The header file Fifo.h provides different FIFO Template classes. All classes recognize when their memory is full and will not accept new elements until an element is removed. The element types can be primitive data types ("Plain Old Datatypes", PODs). It is not possible to store element types with virtual tables in a FIFO buffer. Pointers are allowed (irrespective of type).
CFiFoListBase
The class CFifoListBase is the base implementation of a FIFO. This template class has a StorageType instance that is responsible for storing elements of one type. It uses a SyncType instance to maintain synchronized access to the storage and data it contains.
There are different protection options:
- CNoSync / SyncObects.h: No access protection within the FIFO. An external CriticalSection can be used.
- CSpinLock / TcSpinlock.h: Protection by a spinlock, as described above.
The Add(), Remove() and Clear() methods have corresponding _Unprotected_ options, which can be used with the Lock() and Unlock() methods to implement a synchronized sequence of Add() and Delete() operations.
Methods:
- CFiFoListBase(); The constructor.
- Lock() and Unlock(); Are passed to the SyncType synchronization instance.
- Clear(), Clear_Unprotected(); Are passed on to the StorageType instance.
- Add(), Add_Unprotected(); Add an element.
- AddBlock(); Add multiple elements. If a block is larger than the free memory, only the elements that match the free memory are added. The number of added elements is returned.
- Remove(), Remove_Unprotected(); Removes an element from the FIFO. Returns FALSE if no element was present, otherwise TRUE.
- RemoveBlock(); Removes multiple elements from the FIFO. If fewer elements are stored there, only the stored elements are removed. The number of removed elements is returned.
- GetEntryXBeforeHead(); Fetches the element before the xth element from the head without removing the element. For example, if 0 is used as parameter, the first element is fetched. Otherwise the parameter must be greater than 0. Returns FALSE if the element does not exist, otherwise TRUE.
- Count(); Returns the number of stored elements.
- Size(); Returns the size of the memory, i.e. the maximum number of elements the memory can hold.
- FreeCount(); Returns the number of elements added to the memory.
- GetNextEntry(); Iterates over the elements and starts at the head.
CFiFoList
The class CFiFoList is based on CFiFoListBase and uses CFiFoListStorageStatic as storage type in conjunction with the synchronization type CSyncDefault.
This implementation does not add methods and requires element types and memory size as template parameters.
CFiFoListDyn
The class CFiFoListDyn uses the dynamic StorageType CFifoListStorageDynamic in conjunction with the synchronization type CSyncDefault.
This implementation does not add methods and requires element types and memory size as template parameters.
Handover3Buffer
The class CHandover3Buffer is used to pass elements from one task to another. The memory contains 3 elements of a given type. The writing task can always write the element. The reading task can always read the element when an element has been written. It reads the element that was written fully. Elements are not removed after they are read. Access via indices for reading and writing to memory is synchronized by the InterlockedExchangeR0 function, so that it is accessible from the Windows and RT context.
Methods:
- CHandover3Buffer(); The constructor.
- Write(); Writes an element into memory. Returns FALSE if the memory has an invalid state. Otherwise TRUE.
- Read(); Reads an element from memory. Returns FALSE if the memory is empty or is in an invalid state. Otherwise TRUE.
CHandoverFifo
The CHandoverFifo class is also used to exchange elements between tasks. The size of the memory is always a power of two. It can be set via the template parameter DEPTH_EXP. The default value is 3, resulting in a size of 8 elements.
Methods:
- Insert()/Add(); Writes an element into memory via a pointer or reference. Returns FALSE if no memory is available, otherwise TRUE.
- Remove(); Removes and returns an element from memory via a pointer or reference. Returns FALSE if no element is available, otherwise TRUE.
Sample:
TcHandoverSample.tszip