Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CACHEITEM_H
00029 #define CACHEITEM_H
00030
00031 #include <time.h>
00032
00033 #include "Global.h"
00034 #include "ApplicationClock.h"
00035 #include "QGpCoreToolsDLLExport.h"
00036 #include "Trace.h"
00037
00038 namespace QGpCoreTools {
00039
00040 class CacheProcess;
00041
00042 class QGPCORETOOLS_EXPORT CacheItem
00043 {
00044 public:
00045 inline CacheItem( );
00046 inline CacheItem(const CacheItem& o);
00047 virtual ~CacheItem();
00048
00049 inline void addProcess(CacheProcess * p) const;
00050 inline void removeProcess(CacheProcess * p) const;
00051 quint64 nextPredictedTime() const;
00052 protected:
00053 friend class Cache;
00054 friend class AllocatedCacheItem;
00055
00056 inline void lockData() const;
00057 inline void unlockData() const;
00058
00059
00060 bool isSaved() const {return _saved;}
00061 virtual bool isAllocated() const=0;
00062 virtual int dataSize() const=0;
00063 virtual bool allocate() const=0;
00064 virtual void free() const=0;
00065 virtual void save(QDir& d) const=0;
00066 virtual void load(QDir& d) const=0;
00067
00068 inline QString swapFileName() const {return QString("cache_data_%1").arg((ulong) this, 0, 16);}
00069 virtual QString debugName() const {return swapFileName();}
00070 private:
00071 mutable QMutex _dataLock, _processLock;
00072 mutable int _lockCount;
00073 mutable QSet<CacheProcess *> _processes;
00074 mutable quint64 _lastAccess;
00075 mutable bool _saved:1;
00076 };
00077
00078 inline CacheItem::CacheItem( ) : _dataLock(QMutex::Recursive)
00079 {
00080 TRACE;
00081 _lastAccess=ApplicationClock::elapsed();
00082 _saved=false;
00083 _lockCount=0;
00084 }
00085
00086 inline CacheItem::CacheItem(const CacheItem& ) : _dataLock(QMutex::Recursive)
00087 {
00088 TRACE;
00089 _lastAccess=ApplicationClock::elapsed();
00090 _saved=false;
00091 _lockCount=0;
00092 }
00093
00094 inline void CacheItem::lockData() const
00095 {
00096 _dataLock.lock();
00097 _lockCount++;
00098 }
00099
00100 inline void CacheItem::unlockData() const
00101 {
00102 _lockCount--;
00103 _lastAccess=ApplicationClock::elapsed();
00104 _dataLock.unlock();
00105 }
00106
00107 inline void CacheItem::addProcess(CacheProcess * p) const
00108 {
00109 _processLock.lock();
00110 _processes.insert(p);
00111 _processLock.unlock();
00112 }
00113
00114 inline void CacheItem::removeProcess(CacheProcess * p) const
00115 {
00116 _processLock.lock();
00117 _processes.remove(p);
00118 _processLock.unlock();
00119 }
00120
00121 }
00122
00123 #endif // CACHEITEM_H