GNU CommonC++
buffer.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef CCXX_BUFFER_H_
44 #define CCXX_BUFFER_H_
45 
46 #ifndef CCXX_THREAD_H_
47 #include <cc++/thread.h>
48 #endif
49 
50 #ifdef CCXX_NAMESPACES
51 namespace ost {
52 #endif
53 
75 #ifdef WIN32
76 class __EXPORT Buffer : public Mutex
77 #else
78 class __EXPORT Buffer : public Conditional
79 #endif
80 {
81 private:
82 #ifdef WIN32
83  HANDLE sem_head, sem_tail;
84 #endif
85  size_t _size;
86  size_t _used;
87 
88 protected:
94  virtual size_t onPeek(void *buf) = 0;
95 
101  virtual size_t onWait(void *buf) = 0;
102 
108  virtual size_t onPost(void *buf) = 0;
109 
110 public:
115  static const size_t timeout;
116 
121  Buffer(size_t capacity);
126  virtual ~Buffer();
127 
132  inline size_t getSize(void)
133  {return _size;};
134 
141  inline size_t getUsed(void)
142  {return _used;};
143 
153  size_t wait(void *buf, timeout_t timeout = 0);
154 
163  size_t post(void *buf, timeout_t timeout = 0);
164 
171  size_t peek(void *buf);
172 
177  virtual bool isValid(void);
178  };
179 
188 {
189 private:
190  char *buf, *head, *tail;
191  size_t objsize;
192 
193 protected:
199  size_t onPeek(void *buf);
200 
206  size_t onWait(void *buf);
207 
213  size_t onPost(void *buf);
214 
215 public:
223  FixedBuffer(size_t capacity, size_t objsize);
224 
231  FixedBuffer(const FixedBuffer &fb);
232 
236  virtual ~FixedBuffer();
237 
238  FixedBuffer &operator=(const FixedBuffer &fb);
239 
240  bool isValid(void);
241  };
242 
258 class __EXPORT ThreadQueue : public Mutex, public Thread, public Semaphore
259 {
260 private:
261  typedef struct _data {
262  struct _data *next;
263  unsigned len;
264  char data[1];
265  } data_t;
266 
267  timeout_t timeout;
268  bool started;
269 
270  data_t *first, *last; // head/tail of list
271 
272  void run(void); // private run method
273 
274 protected:
275  const char *name; // used for save/restore file
276 
281  virtual void startQueue(void);
282 
288  virtual void stopQueue(void);
289 
293  virtual void onTimer(void);
294 
303  virtual void runQueue(void *data) = 0;
304 
305 public:
313  ThreadQueue(const char *id, int pri, size_t stack = 0);
314 
318  virtual ~ThreadQueue();
319 
327  void setTimer(timeout_t timeout);
328 
337  void post(const void *data, unsigned len);
338 };
339 
340 
342 inline size_t get(Buffer &b, void *o, timeout_t t = 0)
343  {return b.wait(o, t);}
344 
346 inline size_t put(Buffer &b, void *o, timeout_t t = 0)
347  {return b.post(o, t);}
348 
350 inline size_t peek(Buffer &b, void *o)
351  {return b.peek(o);}
352 
353 
354 #ifdef CCXX_NAMESPACES
355 }
356 #endif
357 
358 #endif
359