GNU CommonC++
|
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation. 00002 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. 00003 // 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 // 00018 // As a special exception, you may use this file as part of a free software 00019 // library without restriction. Specifically, if other files instantiate 00020 // templates or use macros or inline functions from this file, or you compile 00021 // this file and link it with other files to produce an executable, this 00022 // file does not by itself cause the resulting executable to be covered by 00023 // the GNU General Public License. This exception does not however 00024 // invalidate any other reasons why the executable file might be covered by 00025 // the GNU General Public License. 00026 // 00027 // This exception applies only to the code released under the name GNU 00028 // Common C++. If you copy code from other releases into a copy of GNU 00029 // Common C++, as the General Public License permits, the exception does 00030 // not apply to the code that you add in this way. To avoid misleading 00031 // anyone as to the status of such modified files, you must delete 00032 // this exception notice from them. 00033 // 00034 // If you write modifications of your own for GNU Common C++, it is your choice 00035 // whether to permit this exception to apply to your modifications. 00036 // If you do not wish that, delete this exception notice. 00037 // 00038 00044 #ifndef CCXX_BUFFER_H_ 00045 #define CCXX_BUFFER_H_ 00046 00047 #ifndef CCXX_THREAD_H_ 00048 #include <cc++/thread.h> 00049 #endif 00050 #ifndef CCXX_STRING_H_ 00051 #include <cc++/string.h> 00052 #endif 00053 #ifdef CCXX_NAMESPACES 00054 namespace ost { 00055 #endif 00056 00078 #ifdef WIN32 00079 class __EXPORT Buffer : public Mutex 00080 #else 00081 class __EXPORT Buffer : public Conditional 00082 #endif 00083 { 00084 private: 00085 #ifdef WIN32 00086 HANDLE sem_head, sem_tail; 00087 #endif 00088 size_t _size; 00089 size_t _used; 00090 00091 protected: 00097 virtual size_t onPeek(void *buf) = 0; 00098 00104 virtual size_t onWait(void *buf) = 0; 00105 00111 virtual size_t onPost(void *buf) = 0; 00112 00113 public: 00118 static const size_t timeout; 00119 00124 Buffer(size_t capacity); 00129 virtual ~Buffer(); 00130 00135 inline size_t getSize(void) 00136 {return _size;}; 00137 00144 inline size_t getUsed(void) 00145 {return _used;}; 00146 00156 size_t wait(void *buf, timeout_t timeout = 0); 00157 00166 size_t post(void *buf, timeout_t timeout = 0); 00167 00174 size_t peek(void *buf); 00175 00180 virtual bool isValid(void); 00181 }; 00182 00190 class __EXPORT FixedBuffer : public Buffer 00191 { 00192 private: 00193 char *buf, *head, *tail; 00194 size_t objsize; 00195 00196 protected: 00202 size_t onPeek(void *buf); 00203 00209 size_t onWait(void *buf); 00210 00216 size_t onPost(void *buf); 00217 00218 public: 00226 FixedBuffer(size_t capacity, size_t objsize); 00227 00234 FixedBuffer(const FixedBuffer &fb); 00235 00239 virtual ~FixedBuffer(); 00240 00241 FixedBuffer &operator=(const FixedBuffer &fb); 00242 00243 bool isValid(void); 00244 }; 00245 00261 class __EXPORT ThreadQueue : public Mutex, public Thread, public Semaphore 00262 { 00263 private: 00264 void run(void); // private run method 00265 00266 protected: 00267 typedef struct _data { 00268 struct _data *next; 00269 unsigned len; 00270 char data[1]; 00271 } data_t; 00272 00273 timeout_t timeout; 00274 bool started; 00275 00276 data_t *first, *last; // head/tail of list 00277 00278 String name; 00279 00280 /* 00281 * Overloading of final(). It demarks Semaphore to avoid deadlock. 00282 */ 00283 virtual void final(); 00284 00289 virtual void startQueue(void); 00290 00296 virtual void stopQueue(void); 00297 00301 virtual void onTimer(void); 00302 00311 virtual void runQueue(void *data) = 0; 00312 00313 public: 00321 ThreadQueue(const char *id, int pri, size_t stack = 0); 00322 00326 virtual ~ThreadQueue(); 00327 00335 void setTimer(timeout_t timeout); 00336 00345 void post(const void *data, unsigned len); 00346 }; 00347 00348 00350 inline size_t get(Buffer &b, void *o, timeout_t t = 0) 00351 {return b.wait(o, t);} 00352 00354 inline size_t put(Buffer &b, void *o, timeout_t t = 0) 00355 {return b.post(o, t);} 00356 00358 inline size_t peek(Buffer &b, void *o) 00359 {return b.peek(o);} 00360 00361 00362 #ifdef CCXX_NAMESPACES 00363 } 00364 #endif 00365 00366 #endif 00367