GNU CommonC++
buffer.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction. Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License. This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++. If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way. To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
44 #ifndef CCXX_BUFFER_H_
45 #define CCXX_BUFFER_H_
46 
47 #ifndef CCXX_THREAD_H_
48 #include <cc++/thread.h>
49 #endif
50 #ifndef CCXX_STRING_H_
51 #include <cc++/string.h>
52 #endif
53 #ifdef CCXX_NAMESPACES
54 namespace ost {
55 #endif
56 
78 #ifdef WIN32
79 class __EXPORT Buffer : public Mutex
80 #else
81 class __EXPORT Buffer : public Conditional
82 #endif
83 {
84 private:
85 #ifdef WIN32
86  HANDLE sem_head, sem_tail;
87 #endif
88  size_t _size;
89  size_t _used;
90 
91 protected:
97  virtual size_t onPeek(void *buf) = 0;
98 
104  virtual size_t onWait(void *buf) = 0;
105 
111  virtual size_t onPost(void *buf) = 0;
112 
113 public:
118  static const size_t timeout;
119 
124  Buffer(size_t capacity);
129  virtual ~Buffer();
130 
135  inline size_t getSize(void)
136  {return _size;};
137 
144  inline size_t getUsed(void)
145  {return _used;};
146 
156  size_t wait(void *buf, timeout_t timeout = 0);
157 
166  size_t post(void *buf, timeout_t timeout = 0);
167 
174  size_t peek(void *buf);
175 
180  virtual bool isValid(void);
181 };
182 
191 {
192 private:
193  char *buf, *head, *tail;
194  size_t objsize;
195 
196 protected:
202  size_t onPeek(void *buf);
203 
209  size_t onWait(void *buf);
210 
216  size_t onPost(void *buf);
217 
218 public:
226  FixedBuffer(size_t capacity, size_t objsize);
227 
235 
239  virtual ~FixedBuffer();
240 
242 
243  bool isValid(void);
244 };
245 
261 class __EXPORT ThreadQueue : public Mutex, public Thread, public Semaphore
262 {
263 private:
264  void run(void); // private run method
265 
266 protected:
267  typedef struct _data {
268  struct _data *next;
269  unsigned len;
270  char data[1];
271  } data_t;
272 
274  bool started;
275 
276  data_t *first, *last; // head/tail of list
277 
279 
280  /*
281  * Overloading of final(). It demarks Semaphore to avoid deadlock.
282  */
283  virtual void final();
284 
289  virtual void startQueue(void);
290 
296  virtual void stopQueue(void);
297 
301  virtual void onTimer(void);
302 
311  virtual void runQueue(void *data) = 0;
312 
313 public:
321  ThreadQueue(const char *id, int pri, size_t stack = 0);
322 
326  virtual ~ThreadQueue();
327 
335  void setTimer(timeout_t timeout);
336 
345  void post(const void *data, unsigned len);
346 };
347 
348 
350 inline size_t get(Buffer &b, void *o, timeout_t t = 0)
351  {return b.wait(o, t);}
352 
354 inline size_t put(Buffer &b, void *o, timeout_t t = 0)
355  {return b.post(o, t);}
356 
358 inline size_t peek(Buffer &b, void *o)
359  {return b.peek(o);}
360 
361 
362 #ifdef CCXX_NAMESPACES
363 }
364 #endif
365 
366 #endif
367 
ost::FixedBuffer
A buffer class that holds a known capacity of fixed sized objects defined during creation.
Definition: buffer.h:191
ost::ThreadQueue::startQueue
virtual void startQueue(void)
Start of dequeing.
ost::ThreadQueue::_data::next
struct _data * next
Definition: buffer.h:268
ost::Buffer::peek
size_t peek(Buffer &b, void *o)
Definition: buffer.h:358
ost::Thread
Every thread of execution in an application is created by instantiating an object of a class derived ...
Definition: thread.h:1094
ost::FixedBuffer::FixedBuffer
FixedBuffer(size_t capacity, size_t objsize)
Create a buffer of known capacity for objects of a specified size.
ost::ThreadQueue::ThreadQueue
ThreadQueue(const char *id, int pri, size_t stack=0)
Create instance of our queue and give it a process priority.
ost::Buffer::~Buffer
virtual ~Buffer()
In derived functions, may be used to free the actual memory used to hold buffered data.
ost::ThreadQueue::_data
Definition: buffer.h:267
ost::ThreadQueue::timeout
timeout_t timeout
Definition: buffer.h:273
timeout_t
unsigned long timeout_t
Definition: thread.h:74
string.h
Common C++ generic string class.
ost::FixedBuffer::onPost
size_t onPost(void *buf)
Post an object of the appropriate size into the buffer.
HANDLE
int HANDLE
Definition: serial.h:60
ost::FixedBuffer::onWait
size_t onWait(void *buf)
Wait for and return a fixed object in the buffer.
ost::Mutex
The Mutex class is used to protect a section of code so that at any given time only a single thread c...
Definition: thread.h:187
ost::Buffer::wait
size_t wait(void *buf, timeout_t timeout=0)
Let one or more threads wait for an object to become available in the buffer.
ost::Buffer::get
size_t get(Buffer &b, void *o, timeout_t t=0)
Definition: buffer.h:350
ost::ThreadQueue::~ThreadQueue
virtual ~ThreadQueue()
Destroy the queue.
ost::Conditional
A conditional variable synchcronization object for one to one and one to many signal and control even...
Definition: thread.h:637
ost::FixedBuffer::~FixedBuffer
virtual ~FixedBuffer()
Destroy the fixed buffer and free the memory used to store objects.
ost::Buffer::onWait
virtual size_t onWait(void *buf)=0
Invoke derived class object request from buffer.
ost::ThreadQueue
Somewhat generic queue processing class to establish a producer consumer queue.
Definition: buffer.h:262
ost::Buffer::getSize
size_t getSize(void)
Return the capacity of the buffer as specified at creation.
Definition: buffer.h:135
ost::Buffer::onPeek
virtual size_t onPeek(void *buf)=0
Invoke derived class buffer peeking method.
ost::Buffer::peek
size_t peek(void *buf)
Peek at the current content (first object) in the buffer.
ost::FixedBuffer::onPeek
size_t onPeek(void *buf)
Return the first object in the buffer.
__EXPORT
#define __EXPORT
Definition: config.h:979
ost::ThreadQueue::started
bool started
Definition: buffer.h:274
ost::ThreadQueue::stopQueue
virtual void stopQueue(void)
End of dequeing, we expect the queue is empty for now.
ost::ThreadQueue::_data::len
unsigned len
Definition: buffer.h:269
ost::Semaphore
A semaphore is generally used as a synchronization object between multiple threads or to protect a li...
Definition: thread.h:734
ost::Buffer
The buffer class represents an IPC service that is built upon a buffer of fixed capacity that can be ...
Definition: buffer.h:83
ost::Buffer::post
size_t post(void *buf, timeout_t timeout=0)
Post an object into the buffer and enable a waiting thread to receive it.
ost::ThreadQueue::post
void post(const void *data, unsigned len)
Put some unspecified data into this queue.
ost::ThreadQueue::setTimer
void setTimer(timeout_t timeout)
Set the queue timeout.
ost
Definition: address.h:64
ost::ThreadQueue::name
String name
Definition: buffer.h:278
ost::FixedBuffer::isValid
bool isValid(void)
ost::Buffer::put
size_t put(Buffer &b, void *o, timeout_t t=0)
Definition: buffer.h:354
ost::Buffer::timeout
static const size_t timeout
value to return when a timed operation returned with a timeout.
Definition: buffer.h:118
ost::FixedBuffer::FixedBuffer
FixedBuffer(const FixedBuffer &fb)
Create a copy of an existing fixed size buffer and duplicate it's contents.
ost::Buffer::onPost
virtual size_t onPost(void *buf)=0
Invoke derived class posting of object to buffer.
ost::ThreadQueue::runQueue
virtual void runQueue(void *data)=0
Virtual callback method to handle processing of a queued data items.
ost::Buffer::isValid
virtual bool isValid(void)
New virtual to test if buffer is a valid object.
ost::ThreadQueue::onTimer
virtual void onTimer(void)
A derivable method to call when the timout is expired.
ost::ThreadQueue::last
data_t * last
Definition: buffer.h:276
ost::Buffer::getUsed
size_t getUsed(void)
Return the current capacity in use for the buffer.
Definition: buffer.h:144
thread.h
Synchronization and threading services.
ost::FixedBuffer::operator=
FixedBuffer & operator=(const FixedBuffer &fb)
ost::String
This is a generic and portable string class.
Definition: string.h:81
ost::Buffer::Buffer
Buffer(size_t capacity)
Create a buffer object of known capacity.