Refactor thumbimageupdater threading schema. Use glib threadpool class, 8 threads, remove container thread and fix stop code. This should fix some crashes caused by thread conflicts occurred if folders switched quickly multiple times.

This commit is contained in:
Andrey Skvortsov
2010-09-18 22:53:56 -07:00
parent f7f984033d
commit 1464eed466
2 changed files with 235 additions and 271 deletions

View File

@@ -1,202 +1,165 @@
/* /*
* This file is part of RawTherapee. * This file is part of RawTherapee.
* *
* Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com> * Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
* *
* RawTherapee is free software: you can redistribute it and/or modify * RawTherapee is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* RawTherapee is distributed in the hope that it will be useful, * RawTherapee is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with RawTherapee. If not, see <http://www.gnu.org/licenses/>. * along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <thumbimageupdater.h> #include <thumbimageupdater.h>
#include <gtkmm.h> #include <gtkmm.h>
#define threadNum 4 // IF LCMS GETS THREAD SAFETY WE CAN ENABLE MORE THREADS #define threadNum 8
ThumbImageUpdater thumbImageUpdater; ThumbImageUpdater thumbImageUpdater;
ThumbImageUpdater::ThumbImageUpdater () ThumbImageUpdater::ThumbImageUpdater ()
: tostop(false), stopped(true), qMutex(NULL), startMutex(NULL) { : tostop(false), stopped(true), qMutex(NULL), startMutex(NULL), threadPool(NULL) {
//threadPool = new Glib::Thread* [threadNum]; }
threadPool = 0;;
ThumbImageUpdater::~ThumbImageUpdater ()
} {
delete threadPool;
//ThumbImageUpdater::~ThumbImageUpdater () }
//{
// delete threadPool; void ThumbImageUpdater::add (Thumbnail* t, const rtengine::procparams::ProcParams& params, int height, bool* priority, ThumbImageUpdateListener* l) {
//}
if (!qMutex)
void ThumbImageUpdater::add (Thumbnail* t, const rtengine::procparams::ProcParams& params, int height, bool* priority, ThumbImageUpdateListener* l) { qMutex = new Glib::Mutex ();
if (!startMutex)
if (!qMutex) startMutex = new Glib::Mutex ();
qMutex = new Glib::Mutex ();
if (!startMutex) qMutex->lock ();
startMutex = new Glib::Mutex (); // look up if an older version is in the queue
std::list<Job>::iterator i;
qMutex->lock (); for (i=jqueue.begin(); i!=jqueue.end(); i++)
// look up if an older version is in the queue if (i->thumbnail==t && i->listener==l) {
std::list<Job>::iterator i; i->pparams = params;
for (i=jqueue.begin(); i!=jqueue.end(); i++) i->height = height;
if (i->thumbnail==t && i->listener==l) { i->priority = priority;
i->pparams = params; break;
i->height = height; }
i->priority = priority; // not found, create and append new job
break; if (i==jqueue.end ()) {
} Job j;
// not found, create and append new job j.thumbnail = t;
if (i==jqueue.end ()) { j.pparams = params;
Job j; j.height = height;
j.thumbnail = t; j.listener = l;
j.pparams = params; j.priority = priority;
j.height = height; jqueue.push_back (j);
j.listener = l; }
j.priority = priority; qMutex->unlock ();
jqueue.push_back (j); }
}
qMutex->unlock (); void ThumbImageUpdater::process () {
}
if (stopped) {
void ThumbImageUpdater::process () { stopped = false;
if (stopped) { if(!threadPool)
#undef THREAD_PRIORITY_LOW threadPool = new Glib::ThreadPool(threadNum,0);
stopped = false;
// if (thread) //thread = Glib::Thread::create (sigc::mem_fun(*this, &ThumbImageUpdater::process_), (unsigned long int)0, true, true, Glib::THREAD_PRIORITY_NORMAL);
// thread->join(); process_();
}
if(!threadPool) }
threadPool = new Glib::ThreadPool(4,0);
thread = Glib::Thread::create(sigc::mem_fun(*this, &ThumbImageUpdater::process_), (unsigned long int)0, true, true, Glib::THREAD_PRIORITY_LOW); void ThumbImageUpdater::process_ () {
//qMutex->lock ();
// threadPool->push(sigc::mem_fun(*this, &ThumbImageUpdater::process_)); stopped = false;
//qMutex->unlock (); tostop = false;
}
} while (!tostop && !jqueue.empty ()) {
void ThumbImageUpdater::process_ () { std::list<Job>::iterator i;
for (i=jqueue.begin (); i!=jqueue.end(); i++)
stopped = false; if (*(i->priority))
tostop = false; break;
// GError *err; if (i==jqueue.end())
while (!tostop && !jqueue.empty ()) { i = jqueue.begin();
qMutex->lock (); Job current = *i;
//int threads = 0; if (current.listener)
//for (; threads<threadNum && !jqueue.empty (); threads++) { threadPool->push(sigc::bind(sigc::mem_fun(*this, &ThumbImageUpdater::processJob), current));
// find first entry having update priority, if any
jqueue.erase (i);
}
// while(!jqueue.empty())
// { stopped = true;
std::list<Job>::iterator i; //printf("Threads # %d \n", threadPool->get_num_threads());
for (i=jqueue.begin (); i!=jqueue.end(); i++)
if (*(i->priority)) }
break;
void ThumbImageUpdater::processJob (Job current) {
if (i==jqueue.end()) if (current.listener) {
i = jqueue.begin(); double scale = 1.0;
rtengine::IImage8* img = current.thumbnail->processThumbImage (current.pparams, current.height, scale);
Job current = *i; if (img)
if (current.listener) current.listener->updateImage (img, scale, current.pparams.crop);
threadPool->push(sigc::bind(sigc::mem_fun(*this, &ThumbImageUpdater::processJob), current)); }
// if(! threadPool) }
// threadPool = g_thread_pool_new((void*)&ThumbImageUpdater::processJob, (gpointer)&current, 4, FALSE, &err);
// else void ThumbImageUpdater::stop () {
// g_thread_pool_push(threadPool, (gpointer)current, &err);
gdk_threads_leave();
//threadPool[threads] = tostop = true;
// Glib::Thread::create (sigc::bind(sigc::mem_fun(*this, &ThumbImageUpdater::processJob), current), 0, false, true, Glib::THREAD_PRIORITY_LOW);
if (threadPool) {
jqueue.erase (i); threadPool->shutdown(TRUE);
//else threadPool = NULL;
// threadPool[threads] = NULL; }
// } gdk_threads_enter();
qMutex->unlock (); }
// for (int j=0; j<threads; j++) void ThumbImageUpdater::removeJobs () {
// if (threadPool[j])
// threadPool[j]-> join (); if (!qMutex)
return;
//for(int j =0; j <threadNum;j++)
// threadPool[j] = NULL; qMutex->lock ();
} while (!jqueue.empty())
stopped = true; jqueue.pop_front ();
} qMutex->unlock ();
}
void ThumbImageUpdater::processJob (Job current) {
void ThumbImageUpdater::removeJobs (ThumbImageUpdateListener* listener) {
if (current.listener) {
double scale = 1.0; if (!qMutex)
rtengine::IImage8* img = current.thumbnail->processThumbImage (current.pparams, current.height, scale); return;
if (img)
current.listener->updateImage (img, scale, current.pparams.crop); qMutex->lock ();
} bool ready = false;
while (!ready) {
} ready = true;
std::list<Job>::iterator i;
void ThumbImageUpdater::stop () { for (i=jqueue.begin(); i!=jqueue.end(); i++)
if (i->listener == listener) {
if (stopped) { jqueue.erase (i);
tostop = true; ready = false;
return; } break;
}
gdk_threads_leave(); }
tostop = true; qMutex->unlock ();
}
Glib::Thread::self()->yield();
if (!stopped) void ThumbImageUpdater::terminate () {
threadPool->shutdown(TRUE);
thread->join (); stop ();
gdk_threads_enter(); removeJobs ();
} }
void ThumbImageUpdater::removeJobs () {
if (!qMutex)
return;
qMutex->lock ();
while (!jqueue.empty())
jqueue.pop_front ();
qMutex->unlock ();
}
void ThumbImageUpdater::removeJobs (ThumbImageUpdateListener* listener) {
if (!qMutex)
return;
qMutex->lock ();
bool ready = false;
while (!ready) {
ready = true;
std::list<Job>::iterator i;
for (i=jqueue.begin(); i!=jqueue.end(); i++)
if (i->listener == listener) {
jqueue.erase (i);
ready = false;
break;
}
}
qMutex->unlock ();
}
void ThumbImageUpdater::terminate () {
stop ();
removeJobs ();
}

View File

@@ -1,69 +1,70 @@
/* /*
* This file is part of RawTherapee. * This file is part of RawTherapee.
* *
* Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com> * Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
* *
* RawTherapee is free software: you can redistribute it and/or modify * RawTherapee is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* RawTherapee is distributed in the hope that it will be useful, * RawTherapee is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with RawTherapee. If not, see <http://www.gnu.org/licenses/>. * along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _THUMBIMAGEUPDATER_ #ifndef _THUMBIMAGEUPDATER_
#define _THUMBIMAGEUPDATER_ #define _THUMBIMAGEUPDATER_
#include <glibmm.h> #include <glibmm.h>
#include <rtengine.h> #include <rtengine.h>
#include <thumbnail.h> #include <thumbnail.h>
#include <glib.h> #include <glib.h>
class ThumbImageUpdateListener { class ThumbImageUpdateListener {
public: public:
virtual void updateImage (rtengine::IImage8* img, double scale, rtengine::procparams::CropParams cropParams) {} virtual void updateImage (rtengine::IImage8* img, double scale, rtengine::procparams::CropParams cropParams) {}
}; };
class ThumbImageUpdater { class ThumbImageUpdater {
struct Job { struct Job {
Thumbnail* thumbnail; Thumbnail* thumbnail;
rtengine::procparams::ProcParams pparams; rtengine::procparams::ProcParams pparams;
int height; int height;
bool* priority; bool* priority;
ThumbImageUpdateListener* listener; ThumbImageUpdateListener* listener;
}; };
protected: protected:
bool tostop; bool tostop;
bool stopped; bool stopped;
std::list<Job> jqueue; std::list<Job> jqueue;
Glib::Thread* thread; Glib::Thread* thread;
Glib::Mutex* qMutex; Glib::Mutex* qMutex;
Glib::Mutex* startMutex; Glib::Mutex* startMutex;
//Glib::Thread **threadPool; //Glib::Thread **threadPool;
Glib::ThreadPool * threadPool; Glib::ThreadPool * threadPool;
public: public:
ThumbImageUpdater (); ThumbImageUpdater ();
~ThumbImageUpdater ();
void add (Thumbnail* t, const rtengine::procparams::ProcParams& params, int height, bool* priority, ThumbImageUpdateListener* l);
void process (); void add (Thumbnail* t, const rtengine::procparams::ProcParams& params, int height, bool* priority, ThumbImageUpdateListener* l);
void stop (); void process ();
void removeJobs (); void stop ();
void removeJobs (ThumbImageUpdateListener* listener); void removeJobs ();
void terminate (); void removeJobs (ThumbImageUpdateListener* listener);
void terminate ();
void process_ ();
void processJob (Job current); void process_ ();
}; void processJob (Job current);
};
extern ThumbImageUpdater thumbImageUpdater;
extern ThumbImageUpdater thumbImageUpdater;
#endif
#endif