Equalizer
1.4.1
|
00001 00002 /* Copyright (c) 2009-2012, Stefan Eilemann <eile@equalizergraphics.com> 00003 * 00004 * This library is free software; you can redistribute it and/or modify it under 00005 * the terms of the GNU Lesser General Public License version 2.1 as published 00006 * by the Free Software Foundation. 00007 * 00008 * This library is distributed in the hope that it will be useful, but WITHOUT 00009 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00010 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00011 * details. 00012 * 00013 * You should have received a copy of the GNU Lesser General Public License 00014 * along with this library; if not, write to the Free Software Foundation, Inc., 00015 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00016 */ 00017 00018 #ifndef CO_INSTANCECACHE_H 00019 #define CO_INSTANCECACHE_H 00020 00021 #include <co/api.h> 00022 #include <co/types.h> 00023 00024 #include <lunchbox/clock.h> // member 00025 #include <lunchbox/lock.h> // member 00026 #include <lunchbox/lockable.h> // member 00027 #include <lunchbox/stdExt.h> // member 00028 #include <lunchbox/thread.h> // member 00029 #include <lunchbox/uuid.h> // member 00030 00031 #include <iostream> 00032 00033 namespace co 00034 { 00036 class InstanceCache 00037 { 00038 public: 00040 CO_API InstanceCache( const uint64_t maxSize = LB_100MB ); 00041 00043 CO_API ~InstanceCache(); 00044 00054 CO_API bool add( const ObjectVersion& rev, const uint32_t instanceID, 00055 Command& command, const uint32_t usage = 0 ); 00056 00058 void remove( const NodeID& node ); 00059 00061 struct Data 00062 { 00063 Data(); 00064 CO_API bool operator != ( const Data& rhs ) const; 00065 CO_API bool operator == ( const Data& rhs ) const; 00066 00067 uint32_t masterInstanceID; 00068 ObjectDataIStreamDeque versions; 00069 CO_API static const Data NONE; 00070 }; 00071 00084 CO_API const Data& operator[]( const UUID& id ); 00085 00094 CO_API bool release( const UUID& id, const uint32_t count ); 00095 00104 CO_API bool erase( const UUID& id ); 00105 00107 uint64_t getSize() const { return _size; } 00108 00110 uint64_t getMaxSize() const { return _maxSize; } 00111 00113 void expire( const int64_t age ); 00114 00115 bool isEmpty() { return _items->empty(); } 00116 00117 private: 00118 struct Item 00119 { 00120 Item(); 00121 Data data; 00122 unsigned used; 00123 unsigned access; 00124 NodeID from; 00125 00126 typedef std::deque< int64_t > TimeDeque; 00127 TimeDeque times; 00128 }; 00129 00130 typedef stde::hash_map< lunchbox::uint128_t, Item > ItemHash; 00131 typedef ItemHash::iterator ItemHashIter; 00132 lunchbox::Lockable< ItemHash > _items; 00133 00134 const uint64_t _maxSize; 00135 uint64_t _size; 00136 00137 const lunchbox::Clock _clock; 00138 00139 void _releaseItems( const uint32_t minUsage ); 00140 void _releaseStreams( InstanceCache::Item& item ); 00141 void _releaseStreams( InstanceCache::Item& item, 00142 const int64_t minTime ); 00143 void _releaseFirstStream( InstanceCache::Item& item ); 00144 void _deleteStream( ObjectDataIStream* iStream ); 00145 00146 LB_TS_VAR( _thread ); 00147 }; 00148 00149 CO_API std::ostream& operator << ( std::ostream&, const InstanceCache& ); 00150 } 00151 #endif //CO_INSTANCECACHE_H 00152