CPN
Computational Process Networks
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
CPN::ThresholdQueue::TQImpl Class Reference

#include <CPNThresholdQueue.h>

+ Inheritance diagram for CPN::ThresholdQueue::TQImpl:
+ Collaboration diagram for CPN::ThresholdQueue::TQImpl:

Public Types

typedef ThresholdQueueBase::ulong ulong
 

Public Member Functions

 TQImpl (unsigned length, unsigned maxthres, unsigned numchan)
 
 TQImpl (const ThresholdQueueAttr &attr)
 
TQImplGrow (unsigned queueLen, unsigned maxThresh, bool copy)
 
void Reset (void)
 
void * GetRawEnqueuePtr (ulong enqueueThresh, ulong chan=0) const
 
void Enqueue (ulong count)
 
const void * GetRawDequeuePtr (ulong dequeueThresh, ulong chan=0) const
 
void Dequeue (ulong count)
 
ulong Count (void) const
 
ulong Freespace (void) const
 
bool Empty (void) const
 
bool Full (void) const
 
ulong QueueLength (void) const
 
ulong MaxThreshold (void) const
 
ulong NumChannels (void) const
 
ulong ChannelStride (void) const
 
ulong ElementsEnqueued (void) const
 
ulong ElementsDequeued (void) const
 
void Grow (ulong queueLen, ulong maxThresh)
 

Protected Member Functions

void AllocateBuf (ulong queueLen, ulong maxThresh, ulong numChans, bool useMBS)
 

Protected Attributes

ulong elementSize
 
ulong head
 
ulong tail
 
ulong queueLength
 
ulong maxThreshold
 
ulong numChannels
 
ulong channelStride
 
ulong chanOffset
 
ulong baseOffset
 
ulong elementsEnqueued
 
ulong elementsDequeued
 
void * base
 
MirrorBufferSetmbs
 

Detailed Description

The actual queue implementation.

Definition at line 79 of file CPNThresholdQueue.h.

Member Typedef Documentation

Definition at line 81 of file CPNThresholdQueue.h.

Constructor & Destructor Documentation

CPN::ThresholdQueue::TQImpl::TQImpl ( unsigned  length,
unsigned  maxthres,
unsigned  numchan 
)

Definition at line 206 of file CPNThresholdQueue.cc.

207  : ThresholdQueueBase(1, length, maxthresh, numchan)
208  {
209  }
ThresholdQueueBase(ulong elemSize, ulong queueLen, ulong maxThresh, ulong numChans=1)
CPN::ThresholdQueue::TQImpl::TQImpl ( const ThresholdQueueAttr attr)

Definition at line 211 of file CPNThresholdQueue.cc.

212  : ThresholdQueueBase(1, attr)
213  {
214  }
ThresholdQueueBase(ulong elemSize, ulong queueLen, ulong maxThresh, ulong numChans=1)

Member Function Documentation

void ThresholdQueueBase::AllocateBuf ( ulong  queueLen,
ulong  maxThresh,
ulong  numChans,
bool  useMBS 
)
protectedinherited

Definition at line 71 of file ThresholdQueueBase.cc.

References ThresholdQueueBase::base, ThresholdQueueBase::baseOffset, MirrorBufferSet::BufferSize(), ThresholdQueueBase::channelStride, ThresholdQueueBase::chanOffset, ThresholdQueueBase::elementSize, ThresholdQueueBase::maxThreshold, ThresholdQueueBase::mbs, MINIMUM_ALIGNMENT, MirrorBufferSet::MirrorSize(), ThresholdQueueBase::numChannels, ThresholdQueueBase::queueLength, ThresholdQueueBase::Reset(), and MirrorBufferSet::Supported().

Referenced by ThresholdQueueBase::Grow(), and ThresholdQueueBase::ThresholdQueueBase().

74 {
75  if (maxThresh<1) maxThresh = 1;
76  if (maxThresh>queueLen) queueLen = maxThresh;
77  numChannels = numChans;
78 
79  if ( useMBS && MirrorBufferSet::Supported() ) {
80  ulong bufSz = queueLen * elementSize;
81  ulong mirSz = maxThresh-1 + baseOffset + (numChannels-1)*chanOffset;
82  mirSz *= elementSize;
83  mbs = new MirrorBufferSet(bufSz, mirSz, numChannels);
84  // MirrorBufferSet may have just resized everything...
86  maxThreshold = mbs->MirrorSize() / elementSize + 1;
88  maxThreshold -= baseOffset + (numChannels-1)*chanOffset;
89  base = (char*)((void*)(*mbs)) + baseOffset;
90  // Corner case of queueLength == maxThreshold
92  } else {
93  queueLength = queueLen;
94  maxThreshold = maxThresh;
97  if (mod != 0) {
99  }
100  base = malloc(channelStride * numChannels * elementSize);
101  }
102  Reset();
103 }
static int Supported(void)
ulong MirrorSize(void) const
#define MINIMUM_ALIGNMENT
ulong BufferSize(void) const
MirrorBufferSet * mbs

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ulong ThresholdQueueBase::ChannelStride ( void  ) const
inlineinherited

Definition at line 114 of file ThresholdQueueBase.h.

References ThresholdQueueBase::channelStride.

Referenced by CPN::ThresholdQueue::UnlockedDequeueChannelStride(), and CPN::ThresholdQueue::UnlockedEnqueueChannelStride().

114 { return channelStride; }

+ Here is the caller graph for this function:

ThresholdQueueBase::ulong ThresholdQueueBase::Count ( void  ) const
inherited
void ThresholdQueueBase::Dequeue ( ulong  count)
inherited

Definition at line 205 of file ThresholdQueueBase.cc.

References ThresholdQueueBase::elementsDequeued, ThresholdQueueBase::head, and ThresholdQueueBase::queueLength.

Referenced by Grow(), ThresholdQueueBase::Grow(), CPN::ThresholdQueue::InternalDequeue(), and CPN::ThresholdQueue::InternalEnqueue().

208 {
209  register ulong newHead = head+count;
210  while (newHead>=2*queueLength) newHead -= 2*queueLength;
211  head = newHead;
212  elementsDequeued += count;
213 }

+ Here is the caller graph for this function:

ulong ThresholdQueueBase::ElementsDequeued ( void  ) const
inlineinherited

Definition at line 118 of file ThresholdQueueBase.h.

References ThresholdQueueBase::elementsDequeued.

Referenced by Grow(), ThresholdQueueBase::Grow(), and CPN::ThresholdQueue::UnlockedNumDequeued().

118 { return elementsDequeued; }

+ Here is the caller graph for this function:

ulong ThresholdQueueBase::ElementsEnqueued ( void  ) const
inlineinherited

Definition at line 117 of file ThresholdQueueBase.h.

References ThresholdQueueBase::elementsEnqueued.

Referenced by Grow(), ThresholdQueueBase::Grow(), and CPN::ThresholdQueue::UnlockedNumEnqueued().

117 { return elementsEnqueued; }

+ Here is the caller graph for this function:

bool ThresholdQueueBase::Empty ( void  ) const
inlineinherited

Definition at line 107 of file ThresholdQueueBase.h.

References ThresholdQueueBase::Count().

Referenced by CPN::RemoteQueue::InternalCheckStatus(), CPN::ThresholdQueue::UnlockedEmpty(), and CPN::RemoteQueue::UnlockedEmpty().

107 { return !Count(); }
ulong Count(void) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void ThresholdQueueBase::Enqueue ( ulong  count)
inherited

Definition at line 162 of file ThresholdQueueBase.cc.

References ThresholdQueueBase::base, ThresholdQueueBase::channelStride, ThresholdQueueBase::elementsEnqueued, ThresholdQueueBase::elementSize, ThresholdQueueBase::maxThreshold, ThresholdQueueBase::mbs, ThresholdQueueBase::numChannels, ThresholdQueueBase::queueLength, and ThresholdQueueBase::tail.

Referenced by ThresholdQueueBase::Grow(), and CPN::ThresholdQueue::InternalEnqueue().

165 {
166  if (!mbs) { // the mbs maintains circularity
167 
168  // we must mirror by ourselves
169  register ulong idx = tail;
170  while (idx>=queueLength) idx -= queueLength;
171 
172  // elems to copy from queue area to mirror area
173  register ulong countUp = 0;
174  if (idx<maxThreshold-1) countUp = maxThreshold-1-idx; // to mirror's lower edge
175  if (idx+count<maxThreshold-1) countUp = count;
176 
177  // elems to copy from mirror area to queue area
178  register ulong countDn = 0;
179  if (idx+count>queueLength) countDn = idx+count-queueLength;
180 
181  if (countUp || countDn)
182  for (ulong chan=0; chan<numChannels; chan++) {
183  char* chanBase = (char*)base + (chan*channelStride) * elementSize;
184 
185  // move data in the queue (below the threshold) to the mirror area
186  char* dst = chanBase + (idx + queueLength) * elementSize;
187  char* src = chanBase + idx * elementSize;
188  memcpy(dst,src,countUp*elementSize);
189 
190  // move data from the mirror area to the queue (below the threshold)
191  src = chanBase + queueLength * elementSize;
192  memcpy(chanBase,src,countDn*elementSize);
193  }
194  }
195 
196  // update the tail pointer
197  register ulong newTail = tail+count;
198  while (newTail>=2*queueLength) newTail -= 2*queueLength;
199  tail = newTail;
200  elementsEnqueued += count;
201 }
MirrorBufferSet * mbs

+ Here is the caller graph for this function:

ThresholdQueueBase::ulong ThresholdQueueBase::Freespace ( void  ) const
inherited

Definition at line 229 of file ThresholdQueueBase.cc.

References ThresholdQueueBase::Count(), and ThresholdQueueBase::queueLength.

Referenced by CPN::RemoteQueue::EnqueuePacket(), ThresholdQueueBase::Full(), ThresholdQueueBase::GetRawEnqueuePtr(), CPN::RemoteQueue::InternalCheckStatus(), and CPN::ThresholdQueue::UnlockedFreespace().

232 {
233  return queueLength - Count();
234 }
ulong Count(void) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool ThresholdQueueBase::Full ( void  ) const
inlineinherited

Definition at line 108 of file ThresholdQueueBase.h.

References ThresholdQueueBase::Freespace().

Referenced by CPN::ThresholdQueue::UnlockedFull().

108 { return !Freespace(); }
ulong Freespace(void) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const void * ThresholdQueueBase::GetRawDequeuePtr ( ulong  dequeueThresh,
ulong  chan = 0 
) const
inherited

Definition at line 148 of file ThresholdQueueBase.cc.

References ThresholdQueueBase::base, ThresholdQueueBase::channelStride, ThresholdQueueBase::Count(), ThresholdQueueBase::elementSize, ThresholdQueueBase::head, ThresholdQueueBase::MaxThreshold(), and ThresholdQueueBase::queueLength.

Referenced by ThresholdQueue< T >::GetDequeuePtr(), Grow(), ThresholdQueueBase::Grow(), CPN::ThresholdQueue::InternalEnqueue(), and CPN::ThresholdQueue::InternalGetRawDequeuePtr().

151 {
152  if (thresh>Count() || thresh>MaxThreshold()) return 0;
153 // if (chan>=numChannels) return 0;
154 // assert(chan<numChannels);
155  register ulong idx = head;
156  while (idx>=queueLength) idx -= queueLength;
157  return (char*) base + (chan*channelStride + idx) * elementSize;
158 }
ulong MaxThreshold(void) const
ulong Count(void) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void * ThresholdQueueBase::GetRawEnqueuePtr ( ulong  enqueueThresh,
ulong  chan = 0 
) const
inherited

Definition at line 134 of file ThresholdQueueBase.cc.

References ThresholdQueueBase::base, ThresholdQueueBase::channelStride, ThresholdQueueBase::elementSize, ThresholdQueueBase::Freespace(), ThresholdQueueBase::MaxThreshold(), ThresholdQueueBase::queueLength, and ThresholdQueueBase::tail.

Referenced by ThresholdQueue< T >::GetEnqueuePtr(), ThresholdQueueBase::Grow(), CPN::ThresholdQueue::InternalEnqueue(), and CPN::ThresholdQueue::InternalGetRawEnqueuePtr().

137 {
138  if (thresh>Freespace() || thresh>MaxThreshold()) return 0;
139 // if (chan>=numChannels) return 0;
140 // assert(chan<numChannels);
141  register ulong idx = tail;
142  while (idx>=queueLength) idx -= queueLength;
143  return (char*) base + (chan*channelStride + idx) * elementSize;
144 }
ulong MaxThreshold(void) const
ulong Freespace(void) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ThresholdQueue::TQImpl * CPN::ThresholdQueue::TQImpl::Grow ( unsigned  queueLen,
unsigned  maxThresh,
bool  copy 
)

Definition at line 216 of file CPNThresholdQueue.cc.

References ASSERT, ThresholdQueueBase::Count(), ThresholdQueueBase::Dequeue(), ThresholdQueueBase::ElementsDequeued(), ThresholdQueueBase::ElementsEnqueued(), CPN::QueueBase::Enqueue(), ThresholdQueueBase::GetRawDequeuePtr(), CPN::QueueBase::GetRawEnqueuePtr(), ThresholdQueueBase::head, ThresholdQueueBase::MaxThreshold(), CPN::QueueBase::MaxThreshold(), CPN::QueueBase::QueueLength(), and ThresholdQueueBase::tail.

Referenced by CPN::ThresholdQueue::UnlockedGrow().

216  {
217  // ignore the do-nothing case
218  if (queueLen <= QueueLength() && maxThresh <= MaxThreshold()) return 0;
219 
220  // we don't do any shrinking
221  if (maxThresh <= MaxThreshold()) maxThresh = MaxThreshold();
222  if (queueLen <= QueueLength()) queueLen = QueueLength();
223 
224  // keep our old info around
225  auto_ptr<TQImpl> oldQueue = auto_ptr<TQImpl>(new TQImpl(*this)); // just duplicate the pointers
226  // Save the head and tail, these are the only member variables
227  // that we care about that will be changed by dequeueing all the data.
228  ulong oldhead = head;
229  ulong oldtail = tail;
230 
231  // allocate a new buffer (or MirrorBufferSet)
232  AllocateBuf(queueLen,maxThresh,numChannels,mbs?1:0);
233 
234  // growth should not affect this member
235  elementsDequeued = oldQueue->ElementsDequeued();
236 
237  // copy all in oldQueue to our new buffer with existing mechanisms
238  ulong count;
239  while ( (count = oldQueue->Count()) != 0 ) {
240  if (count > oldQueue->MaxThreshold()) {
241  count = oldQueue->MaxThreshold();
242  }
243  for (ulong chan = 0; chan < numChannels; chan++) {
244  const void* src = oldQueue->GetRawDequeuePtr(count, chan);
245  void* dst = GetRawEnqueuePtr(count, chan);
246  ASSERT(src && dst);
247  memcpy(dst, src, count*elementSize);
248  }
249  Enqueue(count);
250  oldQueue->Dequeue(count);
251  }
252 
253  // growth should not affect this member
254  elementsEnqueued = oldQueue->ElementsEnqueued();
255  // reset things inside oldQueue to where they where before we copied
256  oldQueue->head = oldhead;
257  oldQueue->tail = oldtail;
258 
259  if (copy) {
260  return oldQueue.release();
261  }
262  return 0;
263  }
ThresholdQueueBase::ulong ulong
ulong MaxThreshold(void) const
void AllocateBuf(ulong queueLen, ulong maxThresh, ulong numChans, bool useMBS)
ulong QueueLength(void) const
TQImpl(unsigned length, unsigned maxthres, unsigned numchan)
void Enqueue(ulong count)
void * GetRawEnqueuePtr(ulong enqueueThresh, ulong chan=0) const
MirrorBufferSet * mbs
#define ASSERT(exp,...)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void ThresholdQueueBase::Grow ( ulong  queueLen,
ulong  maxThresh 
)
inherited

Definition at line 242 of file ThresholdQueueBase.cc.

References ThresholdQueueBase::AllocateBuf(), ThresholdQueueBase::Count(), ThresholdQueueBase::Dequeue(), ThresholdQueueBase::ElementsDequeued(), ThresholdQueueBase::elementsDequeued, ThresholdQueueBase::ElementsEnqueued(), ThresholdQueueBase::elementsEnqueued, ThresholdQueueBase::elementSize, ThresholdQueueBase::Enqueue(), ThresholdQueueBase::GetRawDequeuePtr(), ThresholdQueueBase::GetRawEnqueuePtr(), ThresholdQueueBase::MaxThreshold(), ThresholdQueueBase::mbs, ThresholdQueueBase::numChannels, and ThresholdQueueBase::QueueLength().

248 {
249  fprintf(stderr, "ThresholdQueueBase::Grow(%lu,%lu)\n", queueLen, maxThresh);
250 
251  // handle the default parameter case
252  if (!maxThresh) maxThresh = MaxThreshold();
253 
254  // ignore the do-nothing case
255  if (queueLen <= QueueLength() && maxThresh <= MaxThreshold()) return;
256 
257  // we don't do any shrinking
258  if (maxThresh <= MaxThreshold()) maxThresh = MaxThreshold();
259  if (queueLen <= QueueLength()) queueLen = QueueLength();
260 
261  // keep our old info around
262  ThresholdQueueBase oldQueue(*this); // just duplicate the pointers
263 
264  // allocate a new buffer (or MirrorBufferSet)
265  AllocateBuf(queueLen,maxThresh,numChannels,mbs?1:0);
266 
267  // growth should not affect this member
268  elementsDequeued = oldQueue.ElementsDequeued();
269 
270  // copy all in oldQueue to our new buffer with existing mechanisms
271  ulong count;
272  while ((count = oldQueue.Count()) != 0) {
273  if (count>oldQueue.MaxThreshold())
274  count = oldQueue.MaxThreshold();
275  for (ulong chan=0; chan<numChannels; chan++) {
276  const void* src = oldQueue.GetRawDequeuePtr(count,chan);
277  void* dst = GetRawEnqueuePtr(count,chan);
278  assert(src && dst);
279  memcpy(dst,src,count*elementSize);
280  }
281  Enqueue(count);
282  oldQueue.Dequeue(count);
283  }
284 
285  // growth should not affect this member
286  elementsEnqueued = oldQueue.ElementsEnqueued();
287 
288  // when oldQueue gets destructed, it will deallocate the old buffers
289 }
ulong MaxThreshold(void) const
void AllocateBuf(ulong queueLen, ulong maxThresh, ulong numChans, bool useMBS)
ulong QueueLength(void) const
void Enqueue(ulong count)
void * GetRawEnqueuePtr(ulong enqueueThresh, ulong chan=0) const
MirrorBufferSet * mbs

+ Here is the call graph for this function:

ulong ThresholdQueueBase::MaxThreshold ( void  ) const
inlineinherited
ulong ThresholdQueueBase::NumChannels ( void  ) const
inlineinherited
ulong ThresholdQueueBase::QueueLength ( void  ) const
inlineinherited

Definition at line 110 of file ThresholdQueueBase.h.

References ThresholdQueueBase::queueLength.

Referenced by CPN::RemoteQueue::EnqueuePacket(), ThresholdQueueBase::Grow(), and CPN::ThresholdQueue::UnlockedQueueLength().

110 { return queueLength; }

+ Here is the caller graph for this function:

void ThresholdQueueBase::Reset ( void  )
inherited

Member Data Documentation

void* ThresholdQueueBase::base
protectedinherited
ulong ThresholdQueueBase::baseOffset
protectedinherited
ulong ThresholdQueueBase::channelStride
protectedinherited
ulong ThresholdQueueBase::chanOffset
protectedinherited
ulong ThresholdQueueBase::elementsDequeued
protectedinherited
ulong ThresholdQueueBase::elementsEnqueued
protectedinherited
ulong ThresholdQueueBase::elementSize
protectedinherited
ulong ThresholdQueueBase::head
protectedinherited
ulong ThresholdQueueBase::maxThreshold
protectedinherited
MirrorBufferSet* ThresholdQueueBase::mbs
protectedinherited
ulong ThresholdQueueBase::numChannels
protectedinherited
ulong ThresholdQueueBase::queueLength
protectedinherited
ulong ThresholdQueueBase::tail
protectedinherited

The documentation for this class was generated from the following files: