You.i Engine
YiLocklessCache.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2017. All rights reserved.
2 #ifndef _YI_LOCKLESS_CACHE_H_
3 #define _YI_LOCKLESS_CACHE_H_
4 
5 #include "framework/YiPredef.h"
6 #include "utility/YiUtilities.h"
7 
13 #define YI_LOCKLESS_CACHE_SIZE 3 // Triple Buffer
14 
24 template<typename YI_CACHE_TYPE>
26 {
27  YI_CACHE_TYPE m_cacheLists[YI_LOCKLESS_CACHE_SIZE];
28 
29  mutable volatile uint32_t m_uCurrentReadIndex;
30  mutable volatile uint32_t m_uNextReadIndex;
31 
32 public:
33 
37  inline CYILocklessCache() :
38  m_uCurrentReadIndex(0), m_uNextReadIndex(0)
39  {
40  }
41 
48  inline bool HasNextRead() const
49  {
50  return m_uCurrentReadIndex != m_uNextReadIndex;
51  }
52 
56  inline const YI_CACHE_TYPE &Read() const
57  {
58  m_uCurrentReadIndex = m_uNextReadIndex;
59  return m_cacheLists[m_uCurrentReadIndex];
60  }
61 
70  inline void Write(const YI_CACHE_TYPE &rData)
71  {
72  volatile uint32_t m_uUnusedIndex = 0;
73  while((m_uUnusedIndex == m_uNextReadIndex || m_uUnusedIndex == m_uCurrentReadIndex)) ++m_uUnusedIndex;
74  m_cacheLists[m_uUnusedIndex] = rData;
75  YI_SWAP(m_uNextReadIndex, m_uUnusedIndex);
76  }
77 };
78 
81 #endif /* _YI_LOCKLESS_CACHE_H_ */
const YI_CACHE_TYPE & Read() const
Definition: YiLocklessCache.h:56
CYILocklessCache()
Definition: YiLocklessCache.h:37
Lockless cache is a triple-buffered cache.
Definition: YiLocklessCache.h:25
bool HasNextRead() const
Definition: YiLocklessCache.h:48
void YI_SWAP(YI_SWAP_TYPE &a, YI_SWAP_TYPE &b)
Definition: YiUtilities.h:35
void Write(const YI_CACHE_TYPE &rData)
Definition: YiLocklessCache.h:70
#define YI_LOCKLESS_CACHE_SIZE
Definition: YiLocklessCache.h:13