Tezzeret  1
Tezzeret
GmManagedArrayRingBuffer.hpp
1 #ifndef GMMANAGEDARRAYRINGBUFFER_HPP_
2 #define GMMANAGEDARRAYRINGBUFFER_HPP_
3 
4 #ifdef acs
5 #include <ti/sysbios/knl/Semaphore.h>
6 #endif
7 
8 #include "GmArrayRingBuffer.hpp"
9 #ifdef linux
10 #include <pthread.h>
11 #endif
12 
13 #include <array>
14 
15 namespace Geometrics
16 {
17  // This is just a buffer with some synchronization.
18  template <typename T, int C>
20  {
21  public:
22  // Statically defined buffer...
23  GmManagedArrayRingBuffer (Semaphore_Handle hasItemsSemaphore,
24  Bool bOkToOverrun,
25  Bool bNotices);
26  ~GmManagedArrayRingBuffer () /*override*/;
27  int WriteRecord (T *pcSource) /*override*/;
28  int ReadRecord (T *pcDestination) /*override*/;
29 
30  #ifdef linux
31  private:
32  pthread_mutex_t m_mutex;
33  #endif
34 
35  public:
36  Semaphore_Handle mHasItemsSemaphore;
37  };
38 
39 }
40 
41 
42 #ifndef _GEO_APPDEFS_HPP
43 #include <GeoAppDefs.hpp>
44 #endif
45 
46 #include "string.h"
47 
48 #ifdef linux
49 #include <stdint.h>
50 #include <pthread.h>
51 #endif
52 
53 #ifdef acs
54 #include <xdc/std.h>
55 #include <xdc/cfg/global.h>
56 #include <xdc/runtime/Error.h>
57 #include <xdc/runtime/System.h>
58 #include <xdc/runtime/Memory.h>
59 #include <ti/sysbios/knl/Swi.h>
60 #endif
61 #include "geotypes.hpp"
62 
63 #include "GmRingBuffer.hpp"
64 #include "GmArrayRingBuffer.hpp"
65 //#include "GmManagedArrayRingBuffer.hpp"
66 
67 using namespace Geometrics;
68 
69 //#define DEBUGGING_COMPILE_ISSUES
70 #ifdef DEBUGGING_COMPILE_ISSUES
71 GmManagedArrayRingBuffer<uint32_t,20> theArray(0,true,true);
72 #endif
73 
74 
75 template <typename T, int C>
76 GmManagedArrayRingBuffer<T,C>::GmManagedArrayRingBuffer (Semaphore_Handle hasItemsSemaphore,
77  Bool bOkToOverrun,
78  Bool bNotices)
79  : GmArrayRingBuffer<T,C>(bOkToOverrun, bNotices),
80  mHasItemsSemaphore (hasItemsSemaphore)
81 {
82  #ifdef linux
83  int status = pthread_mutex_init (&m_mutex, NULL);
84  #endif
85 #ifdef DEBUGGING_COMPILE_ISSUES
86  T aTestItem;
87  WriteRecord (&aTestItem);
88  ReadRecord (&aTestItem);
89 #endif
90 }
91 
92 template <typename T, int C>
94 {
95  #ifdef linux
96  pthread_mutex_destroy (&m_mutex);
97  #endif
98 }
99 
100 // ===============================================================
101 // Destructive read - fetch the data and advance the read pointer.
102 // The record is no longer available for reading.
103 // ===============================================================
104 template <typename T, int C>
105 int GmManagedArrayRingBuffer<T,C>::ReadRecord (T* pcDestination)
106 {
107  int iReadBuffer;
108 
109  #ifdef acs
110  UInt gateKey;
111  gateKey = Swi_disable();
112  #endif
113  #ifdef linux
114  pthread_mutex_lock (&m_mutex);
115  #endif
116 
117  if (this->IsEmpty ())
118  {
119 
120 #ifdef acs
121  Swi_restore (gateKey);
122 #endif
123 #ifdef linux
124  pthread_mutex_unlock (&m_mutex);
125 #endif
126  return GM_RING_BUFFER_EMPTY;
127  }
128 
129  // Prep to read the buffer. Don't yet mark it as having been read.
130  iReadBuffer = this->m_iLastBufferRead + 1;
131 
132  if (iReadBuffer == this->MaxSize())
133  iReadBuffer = 0;
134 
135  if (pcDestination != nullptr)
136  {
137  *pcDestination = this->GetBuffer (iReadBuffer);
138  }
139 
140  // A data read always unsets the overrun flag.
141  this->m_bNextWriteWouldOverrun = FALSE;
142 
143  // We are done with this buffer. Advance the counter.
144  this->m_iLastBufferRead = iReadBuffer;
145 // ToggleDebug ();
146 
147 
148 #ifdef acs
149  Swi_restore (gateKey);
150 #endif
151 #ifdef linux
152  pthread_mutex_unlock (&m_mutex);
153 #endif
154 
155  if (pcDestination == nullptr)
156  return GM_RING_BUFFER_NULL_DESTINATION;
157  else
158  return GM_RING_BUFFER_OK;
159 }
160 
161 
162 template <typename T, int C>
164 {
165  int iTest;
166 // iTest = CheckToWrite ( iSize);
167 // if (iTest == GM_RING_BUFFER_OK)
168  {
169  /********************************************************
170  * Need to serialize access to writes. This can get writes
171  * from multiple threads, interrupts, etc.
172  *********************************************************/
173 #ifdef acs
174  UInt gateKey;
175  gateKey = Swi_disable();
176 #endif
177 #ifdef linux
178  pthread_mutex_lock (&m_mutex);
179 #endif
180  iTest = InternalWriteRecord (pcSource);
181 #ifdef acs
182  /* Release the write lock. */
183  Swi_restore (gateKey);
184 
185  /******************************************************
186  * Let the de-queuing logic, which is in a task, know
187  * that data is available.
188  ******************************************************/
189  Semaphore_post (mHasItemsSemaphore);
190 #endif
191 #ifdef linux
192  pthread_mutex_unlock (&m_mutex);
193 #endif
194  }
195  return iTest;
196 }
197 
198 
199 #endif /* GMRINGBUFFER_HPP_ */
Definition: GmArrayRingBuffer.hpp:22
Definition: GmManagedArrayRingBuffer.hpp:20
GmApp/GmLoggingApp.hpp>
Definition: Analogs.hpp:47