improve stability for Cache Manager and thumbnails and avoid race conditions.

preparation step 3 for making dcraw multithread safe.
This commit is contained in:
Steve Herrell
2010-10-28 17:39:29 +02:00
parent 3c747f1d77
commit d0a9100e93
3 changed files with 51 additions and 31 deletions

View File

@@ -194,7 +194,7 @@ void CacheManager::renameEntry (const std::string& oldfilename, const std::strin
t->setFileName (newfilename); t->setFileName (newfilename);
openEntries[newfilename] = t; openEntries[newfilename] = t;
t->updateCache (); t->updateCache ();
t->reSaveThumbnail (); t->saveThumbnail ();
} }
} }

View File

@@ -36,10 +36,9 @@ Thumbnail::Thumbnail (CacheManager* cm, const Glib::ustring& fname, CacheImageDa
: fname(fname), cfs(*cf), cachemgr(cm), ref(1), enqueueNumber(0), tpp(NULL), : fname(fname), cfs(*cf), cachemgr(cm), ref(1), enqueueNumber(0), tpp(NULL),
pparamsValid(false), needsReProcessing(true), lastImg(NULL) { pparamsValid(false), needsReProcessing(true), lastImg(NULL) {
mutex = new Glib::Mutex ();
cfs.load (getCacheFileName ("data")+".txt"); cfs.load (getCacheFileName ("data")+".txt");
loadProcParams (); loadProcParams ();
loadThumbnail (); _loadThumbnail ();
generateExifDateTimeStrings (); generateExifDateTimeStrings ();
} }
@@ -47,18 +46,14 @@ Thumbnail::Thumbnail (CacheManager* cm, const Glib::ustring& fname, const std::s
: fname(fname), cachemgr(cm), ref(1), enqueueNumber(0), tpp(NULL), pparamsValid(false), : fname(fname), cachemgr(cm), ref(1), enqueueNumber(0), tpp(NULL), pparamsValid(false),
needsReProcessing(true), lastImg(NULL) { needsReProcessing(true), lastImg(NULL) {
mutex = new Glib::Mutex ();
cfs.md5 = md5; cfs.md5 = md5;
generateThumbnailImage (); _generateThumbnailImage ();
loadProcParams (); loadProcParams ();
cfs.recentlySaved = false; cfs.recentlySaved = false;
} }
void Thumbnail::generateThumbnailImage (bool internal) { void Thumbnail::_generateThumbnailImage () {
if (!internal)
mutex->lock ();
// delete everything loaded into memory // delete everything loaded into memory
delete tpp; delete tpp;
@@ -103,7 +98,7 @@ void Thumbnail::generateThumbnailImage (bool internal) {
} }
if (tpp) { if (tpp) {
// save thumbnail image to cache // save thumbnail image to cache
saveThumbnail (); _saveThumbnail ();
cfs.supported = true; cfs.supported = true;
} }
needsReProcessing = true; needsReProcessing = true;
@@ -111,9 +106,11 @@ void Thumbnail::generateThumbnailImage (bool internal) {
cfs.save (getCacheFileName ("data")+".txt"); cfs.save (getCacheFileName ("data")+".txt");
generateExifDateTimeStrings (); generateExifDateTimeStrings ();
}
if (!internal)
mutex->unlock (); void Thumbnail::generateThumbnailImage () {
Glib::Mutex::Lock lock(mutex);
_generateThumbnailImage();
} }
bool Thumbnail::isSupported () { bool Thumbnail::isSupported () {
@@ -231,8 +228,24 @@ bool Thumbnail::isEnqueued () {
return enqueueNumber > 0; return enqueueNumber > 0;
} }
void Thumbnail::increaseRef () { ref++; } void Thumbnail::increaseRef ()
void Thumbnail::decreaseRef () { ref--; if (!ref) cachemgr->closeThumbnail (this); } {
Glib::Mutex::Lock lock(mutex);
++ref;
}
void Thumbnail::decreaseRef ()
{
Glib::Mutex::Lock lock(mutex);
if ( ref != 0 )
{
--ref;
if ( ref == 0 )
{
cachemgr->closeThumbnail (this);
}
}
}
void Thumbnail::getThumbnailSize (int &w, int &h) { void Thumbnail::getThumbnailSize (int &w, int &h) {
@@ -244,14 +257,13 @@ void Thumbnail::getThumbnailSize (int &w, int &h) {
rtengine::IImage8* Thumbnail::processThumbImage (const rtengine::procparams::ProcParams& pparams, int h, double& scale) { rtengine::IImage8* Thumbnail::processThumbImage (const rtengine::procparams::ProcParams& pparams, int h, double& scale) {
mutex->lock (); Glib::Mutex::Lock lock(mutex);
if (!tpp) if (!tpp)
return NULL; return NULL;
rtengine::IImage8* res = tpp->processImage (pparams, h, rtengine::TI_Bilinear, scale); rtengine::IImage8* res = tpp->processImage (pparams, h, rtengine::TI_Bilinear, scale);
mutex->unlock ();
return res; return res;
} }
@@ -340,10 +352,7 @@ void Thumbnail::infoFromImage (const Glib::ustring& fname, rtengine::RawMetaData
delete idata; delete idata;
} }
void Thumbnail::loadThumbnail (bool internal, bool firstTrial) { void Thumbnail::_loadThumbnail(bool firstTrial) {
if (!internal)
mutex->lock ();
needsReProcessing = true; needsReProcessing = true;
delete tpp; delete tpp;
@@ -357,9 +366,9 @@ void Thumbnail::loadThumbnail (bool internal, bool firstTrial) {
succ = succ && tpp->readImage (getCacheFileName ("images")); succ = succ && tpp->readImage (getCacheFileName ("images"));
if (!succ && firstTrial) { if (!succ && firstTrial) {
generateThumbnailImage (true); _generateThumbnailImage ();
if (cfs.supported && firstTrial) if (cfs.supported && firstTrial)
loadThumbnail (true, false); _loadThumbnail (false);
} }
else if (!succ) { else if (!succ) {
delete tpp; delete tpp;
@@ -375,11 +384,14 @@ void Thumbnail::loadThumbnail (bool internal, bool firstTrial) {
tpp->init (); tpp->init ();
} }
if (!internal)
mutex->unlock ();
} }
void Thumbnail::saveThumbnail () { void Thumbnail::loadThumbnail (bool firstTrial) {
Glib::Mutex::Lock lock(mutex);
_loadThumbnail(firstTrial);
}
void Thumbnail::_saveThumbnail () {
if (!tpp) if (!tpp)
return; return;
@@ -406,6 +418,12 @@ void Thumbnail::saveThumbnail () {
tpp->writeData (getCacheFileName ("data")+".txt"); tpp->writeData (getCacheFileName ("data")+".txt");
} }
void Thumbnail::saveThumbnail ()
{
Glib::Mutex::Lock lock(mutex);
_saveThumbnail();
}
void Thumbnail::updateCache () { void Thumbnail::updateCache () {
if (pparamsValid) { if (pparamsValid) {

View File

@@ -31,7 +31,7 @@
class CacheManager; class CacheManager;
class Thumbnail { class Thumbnail {
Glib::Mutex* mutex; Glib::Mutex mutex;
Glib::ustring fname; // file name corresponding to the thumbnail Glib::ustring fname; // file name corresponding to the thumbnail
CacheImageData cfs; // cache entry corresponding to the thumbnail CacheImageData cfs; // cache entry corresponding to the thumbnail
@@ -61,9 +61,11 @@ class Thumbnail {
// vector of listeners // vector of listeners
std::vector<ThumbnailListener*> listeners; std::vector<ThumbnailListener*> listeners;
void _loadThumbnail (bool firstTrial=true);
void _saveThumbnail ();
void _generateThumbnailImage ();
void infoFromImage (const Glib::ustring& fname, rtengine::RawMetaDataLocation* rml=NULL); void infoFromImage (const Glib::ustring& fname, rtengine::RawMetaDataLocation* rml=NULL);
void loadThumbnail (bool internal=false, bool firstTrial=true); void loadThumbnail (bool firstTrial=true);
void saveThumbnail ();
void generateExifDateTimeStrings (); void generateExifDateTimeStrings ();
Glib::ustring getCacheFileName (Glib::ustring subdir); Glib::ustring getCacheFileName (Glib::ustring subdir);
@@ -91,7 +93,7 @@ class Thumbnail {
void getThumbnailSize (int &w, int &h); void getThumbnailSize (int &w, int &h);
void getFinalSize (const rtengine::procparams::ProcParams& pparams, int& w, int& h) { if (tpp) tpp->getFinalSize (pparams, w, h); } void getFinalSize (const rtengine::procparams::ProcParams& pparams, int& w, int& h) { if (tpp) tpp->getFinalSize (pparams, w, h); }
void generateThumbnailImage (bool internal=false); void generateThumbnailImage ();
const Glib::ustring& getExifString (); const Glib::ustring& getExifString ();
const Glib::ustring& getDateTimeString (); const Glib::ustring& getDateTimeString ();
@@ -122,7 +124,7 @@ class Thumbnail {
void decreaseRef (); void decreaseRef ();
void updateCache (); void updateCache ();
void reSaveThumbnail () { mutex->lock (); saveThumbnail(); mutex->unlock(); } void saveThumbnail ();
}; };