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

@@ -19,21 +19,18 @@
#include <thumbimageupdater.h>
#include <gtkmm.h>
#define threadNum 4 // IF LCMS GETS THREAD SAFETY WE CAN ENABLE MORE THREADS
#define threadNum 8
ThumbImageUpdater thumbImageUpdater;
ThumbImageUpdater::ThumbImageUpdater ()
: tostop(false), stopped(true), qMutex(NULL), startMutex(NULL) {
//threadPool = new Glib::Thread* [threadNum];
threadPool = 0;;
: tostop(false), stopped(true), qMutex(NULL), startMutex(NULL), threadPool(NULL) {
}
//ThumbImageUpdater::~ThumbImageUpdater ()
//{
// delete threadPool;
//}
ThumbImageUpdater::~ThumbImageUpdater ()
{
delete threadPool;
}
void ThumbImageUpdater::add (Thumbnail* t, const rtengine::procparams::ProcParams& params, int height, bool* priority, ThumbImageUpdateListener* l) {
@@ -68,17 +65,13 @@ void ThumbImageUpdater::add (Thumbnail* t, const rtengine::procparams::ProcParam
void ThumbImageUpdater::process () {
if (stopped) {
#undef THREAD_PRIORITY_LOW
stopped = false;
// if (thread)
// thread->join();
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);
//qMutex->lock ();
// threadPool->push(sigc::mem_fun(*this, &ThumbImageUpdater::process_));
//qMutex->unlock ();
threadPool = new Glib::ThreadPool(threadNum,0);
//thread = Glib::Thread::create (sigc::mem_fun(*this, &ThumbImageUpdater::process_), (unsigned long int)0, true, true, Glib::THREAD_PRIORITY_NORMAL);
process_();
}
}
@@ -87,23 +80,13 @@ void ThumbImageUpdater::process_ () {
stopped = false;
tostop = false;
// GError *err;
while (!tostop && !jqueue.empty ()) {
qMutex->lock ();
//int threads = 0;
//for (; threads<threadNum && !jqueue.empty (); threads++) {
// find first entry having update priority, if any
// while(!jqueue.empty())
// {
std::list<Job>::iterator i;
for (i=jqueue.begin (); i!=jqueue.end(); i++)
if (*(i->priority))
break;
if (i==jqueue.end())
i = jqueue.begin();
@@ -111,28 +94,12 @@ void ThumbImageUpdater::process_ () {
if (current.listener)
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
// g_thread_pool_push(threadPool, (gpointer)current, &err);
//threadPool[threads] =
// Glib::Thread::create (sigc::bind(sigc::mem_fun(*this, &ThumbImageUpdater::processJob), current), 0, false, true, Glib::THREAD_PRIORITY_LOW);
jqueue.erase (i);
//else
// threadPool[threads] = NULL;
// }
qMutex->unlock ();
// for (int j=0; j<threads; j++)
// if (threadPool[j])
// threadPool[j]-> join ();
//for(int j =0; j <threadNum;j++)
// threadPool[j] = NULL;
}
stopped = true;
//printf("Threads # %d \n", threadPool->get_num_threads());
}
void ThumbImageUpdater::processJob (Job current) {
@@ -148,17 +115,13 @@ void ThumbImageUpdater::processJob (Job current) {
void ThumbImageUpdater::stop () {
if (stopped) {
tostop = true;
return; }
gdk_threads_leave();
tostop = true;
Glib::Thread::self()->yield();
if (!stopped)
if (threadPool) {
threadPool->shutdown(TRUE);
thread->join ();
threadPool = NULL;
}
gdk_threads_enter();
}

View File

@@ -52,6 +52,7 @@ class ThumbImageUpdater {
public:
ThumbImageUpdater ();
~ThumbImageUpdater ();
void add (Thumbnail* t, const rtengine::procparams::ProcParams& params, int height, bool* priority, ThumbImageUpdateListener* l);
void process ();