From da1e0ef0772b078bd295301bd675afdee00d40e9 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sun, 23 Oct 2011 07:43:50 +0000 Subject: Switched ito* to synchro*, except the server, I'm thinking of takeing the core in a different direction anyway. Added the Deflate class, it uses zlib, and can do raw (headerless) deflate streams, zlib format, or gzip format. It's easy to use and quite versitile. --- src/synchroheap.h | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 src/synchroheap.h (limited to 'src/synchroheap.h') diff --git a/src/synchroheap.h b/src/synchroheap.h new file mode 100644 index 0000000..4dd898d --- /dev/null +++ b/src/synchroheap.h @@ -0,0 +1,151 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_SYNCHRO_HEAP_H +#define BU_SYNCHRO_HEAP_H + +#include "bu/heap.h" +#include "bu/mutex.h" +#include "bu/condition.h" + +namespace Bu +{ + template, + typename itemalloc=std::allocator > + class SynchroHeap + { + public: + SynchroHeap() + { + } + + virtual ~SynchroHeap() + { + } + + void enqueue( item i ) + { + imData.lock(); + hData.enqueue( i ); + icBlock.signal(); + imData.unlock(); + } + + item dequeue( bool bBlock=false ) + { + imData.lock(); + if( hData.isEmpty() ) + { + imData.unlock(); + + if( bBlock ) + { + icBlock.lock(); + + while( hData.isEmpty() ) + icBlock.wait(); + + imData.lock(); + try + { + item iRet = hData.dequeue(); + imData.unlock(); + icBlock.unlock(); + return iRet; + } + catch(...) + { + imData.unlock(); + icBlock.unlock(); + throw; + } + } + throw HeapException("Heap empty."); + } + else + { + try + { + item iRet = hData.dequeue(); + imData.unlock(); + return iRet; + } + catch(...) + { + imData.unlock(); + throw; + } + } + } + + item dequeue( int iSec, int iUSec ) + { + imData.lock(); + if( hData.isEmpty() ) + { + imData.unlock(); + + icBlock.lock(); + + icBlock.wait( iSec, iUSec ); + + imData.lock(); + try + { + item iRet = hData.dequeue(); + imData.unlock(); + icBlock.unlock(); + return iRet; + } + catch(...) + { + imData.unlock(); + icBlock.unlock(); + throw; + } + } + else + { + try + { + item iRet = hData.dequeue(); + imData.unlock(); + return iRet; + } + catch(...) + { + imData.unlock(); + throw; + } + } + } + + bool isEmpty() + { + imData.lock(); + bool bRet = hData.isEmpty(); + imData.unlock(); + return bRet; + } + + int getSize() + { + imData.lock(); + int iRet = hData.getSize(); + imData.unlock(); + return iRet; + } + + private: + Heap< item, cmpfunc, itemalloc > hData; + Mutex imData; + Condition icBlock; + }; +}; + +#endif + -- cgit v1.2.3