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

#include <ThresholdQueueBase.h>

+ Inheritance diagram for ThresholdQueueBase:
+ Collaboration diagram for ThresholdQueueBase:

Public Types

typedef unsigned long ulong
 

Public Member Functions

 ThresholdQueueBase (ulong elemSize, ulong queueLen, ulong maxThresh, ulong numChans=1)
 
 ThresholdQueueBase (ulong elemSize, const ThresholdQueueAttr &attr)
 
 ~ThresholdQueueBase (void)
 
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

Definition at line 86 of file ThresholdQueueBase.h.

Member Typedef Documentation

typedef unsigned long ThresholdQueueBase::ulong

Definition at line 90 of file ThresholdQueueBase.h.

Constructor & Destructor Documentation

ThresholdQueueBase::ThresholdQueueBase ( ulong  elemSize,
ulong  queueLen,
ulong  maxThresh,
ulong  numChans = 1 
)

Definition at line 20 of file ThresholdQueueBase.cc.

References AllocateBuf(), maxThreshold, numChannels, queueLength, and Reset().

23 : elementSize(elemSize),
24  queueLength(queueLen),
25  maxThreshold(maxThresh),
26  numChannels(numChans),
27  chanOffset(0), baseOffset(0),
28  base(0), mbs(0)
29 {
30  if (maxThreshold<1)
31  maxThreshold = 1;
34 
35  Reset();
36 
37  bool useMBS = 1;
39 }
void AllocateBuf(ulong queueLen, ulong maxThresh, ulong numChans, bool useMBS)
MirrorBufferSet * mbs

+ Here is the call graph for this function:

ThresholdQueueBase::ThresholdQueueBase ( ulong  elemSize,
const ThresholdQueueAttr attr 
)

Definition at line 42 of file ThresholdQueueBase.cc.

References AllocateBuf(), ThresholdQueueAttr::baseOffset, baseOffset, ThresholdQueueAttr::chanOffset, chanOffset, elementSize, maxThreshold, numChannels, MirrorBufferSet::PageSize(), queueLength, Reset(), and ThresholdQueueAttr::useMBS.

44 : elementSize(elemSize),
45  queueLength(attr.queueLength),
48  chanOffset(0), baseOffset(0),
49  base(0), mbs(0)
50 {
51  if (maxThreshold<1)
52  maxThreshold = 1;
55 
56  Reset();
57 
58  bool useMBS = attr.useMBS;
59  if (useMBS) {
60  // there is no reason for an offset bigger than the page size
64  }
65 
67 }
void AllocateBuf(ulong queueLen, ulong maxThresh, ulong numChans, bool useMBS)
static ulong PageSize(void)
MirrorBufferSet * mbs

+ Here is the call graph for this function:

ThresholdQueueBase::~ThresholdQueueBase ( void  )

Definition at line 120 of file ThresholdQueueBase.cc.

References base, and mbs.

122 {
123  if (mbs) {
124  delete mbs;
125  mbs = 0;
126  } else {
127  free(base);
128  }
129  base = 0;
130 }
MirrorBufferSet * mbs

Member Function Documentation

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

Definition at line 71 of file ThresholdQueueBase.cc.

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

Referenced by Grow(), and 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
inline

Definition at line 114 of file ThresholdQueueBase.h.

References 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
void ThresholdQueueBase::Dequeue ( ulong  count)

Definition at line 205 of file ThresholdQueueBase.cc.

References elementsDequeued, head, and queueLength.

Referenced by CPN::ThresholdQueue::TQImpl::Grow(), 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
inline

Definition at line 118 of file ThresholdQueueBase.h.

References elementsDequeued.

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

118 { return elementsDequeued; }

+ Here is the caller graph for this function:

ulong ThresholdQueueBase::ElementsEnqueued ( void  ) const
inline

Definition at line 117 of file ThresholdQueueBase.h.

References elementsEnqueued.

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

117 { return elementsEnqueued; }

+ Here is the caller graph for this function:

bool ThresholdQueueBase::Empty ( void  ) const
inline

Definition at line 107 of file ThresholdQueueBase.h.

References 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)

Definition at line 162 of file ThresholdQueueBase.cc.

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

Referenced by 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

Definition at line 229 of file ThresholdQueueBase.cc.

References Count(), and queueLength.

Referenced by CPN::RemoteQueue::EnqueuePacket(), Full(), 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
inline

Definition at line 108 of file ThresholdQueueBase.h.

References 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

Definition at line 148 of file ThresholdQueueBase.cc.

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

Referenced by ThresholdQueue< T >::GetDequeuePtr(), CPN::ThresholdQueue::TQImpl::Grow(), 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

Definition at line 134 of file ThresholdQueueBase.cc.

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

Referenced by ThresholdQueue< T >::GetEnqueuePtr(), 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:

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

Definition at line 242 of file ThresholdQueueBase.cc.

References AllocateBuf(), Count(), Dequeue(), ElementsDequeued(), elementsDequeued, ElementsEnqueued(), elementsEnqueued, elementSize, Enqueue(), GetRawDequeuePtr(), GetRawEnqueuePtr(), MaxThreshold(), mbs, numChannels, and 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
inline
ulong ThresholdQueueBase::NumChannels ( void  ) const
inline
ulong ThresholdQueueBase::QueueLength ( void  ) const
inline

Definition at line 110 of file ThresholdQueueBase.h.

References queueLength.

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

110 { return queueLength; }

+ Here is the caller graph for this function:

void ThresholdQueueBase::Reset ( void  )

Definition at line 108 of file ThresholdQueueBase.cc.

References elementsDequeued, elementsEnqueued, head, and tail.

Referenced by AllocateBuf(), CPN::ThresholdQueue::InternalReset(), CPN::RemoteQueue::ResetPacket(), and ThresholdQueueBase().

110 {
111  elementsEnqueued = 0;
112  elementsDequeued = 0;
113 
114  head = 0;
115  tail = 0;
116 }

+ Here is the caller graph for this function:

Member Data Documentation

void* ThresholdQueueBase::base
protected
ulong ThresholdQueueBase::baseOffset
protected

Definition at line 127 of file ThresholdQueueBase.h.

Referenced by AllocateBuf(), and ThresholdQueueBase().

ulong ThresholdQueueBase::channelStride
protected
ulong ThresholdQueueBase::chanOffset
protected

Definition at line 127 of file ThresholdQueueBase.h.

Referenced by AllocateBuf(), and ThresholdQueueBase().

ulong ThresholdQueueBase::elementsDequeued
protected

Definition at line 128 of file ThresholdQueueBase.h.

Referenced by Dequeue(), ElementsDequeued(), Grow(), and Reset().

ulong ThresholdQueueBase::elementsEnqueued
protected

Definition at line 128 of file ThresholdQueueBase.h.

Referenced by ElementsEnqueued(), Enqueue(), Grow(), and Reset().

ulong ThresholdQueueBase::elementSize
protected
ulong ThresholdQueueBase::head
protected
ulong ThresholdQueueBase::maxThreshold
protected

Definition at line 125 of file ThresholdQueueBase.h.

Referenced by AllocateBuf(), Enqueue(), MaxThreshold(), and ThresholdQueueBase().

MirrorBufferSet* ThresholdQueueBase::mbs
protected

Definition at line 130 of file ThresholdQueueBase.h.

Referenced by AllocateBuf(), Enqueue(), Grow(), and ~ThresholdQueueBase().

ulong ThresholdQueueBase::numChannels
protected

Definition at line 126 of file ThresholdQueueBase.h.

Referenced by AllocateBuf(), Enqueue(), Grow(), NumChannels(), and ThresholdQueueBase().

ulong ThresholdQueueBase::queueLength
protected
ulong ThresholdQueueBase::tail
protected

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