Improve thumbnail parallelism

This commit is contained in:
Steve Herrell
2010-10-29 11:00:25 +02:00
parent 93385fddfa
commit 6cb28a32ea
5 changed files with 357 additions and 182 deletions

View File

@@ -16,150 +16,240 @@
* You should have received a copy of the GNU General Public License
* along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
*/
#include <set>
#include <thumbimageupdater.h>
#include <gtkmm.h>
#include <guiutils.h>
#define threadNum 1
ThumbImageUpdater thumbImageUpdater;
#define THREAD_NUM 4
ThumbImageUpdater::ThumbImageUpdater ()
: tostop(false), stopped(true), qMutex(NULL), startMutex(NULL), threadPool(NULL) {
}
#define DEBUG(format,args...)
//#define DEBUG(format,args...) printf("ThumbImageUpdate::%s: " format "\n", __FUNCTION__, ## args)
ThumbImageUpdater::~ThumbImageUpdater ()
struct
Job
{
delete threadPool;
Job(Thumbnail* thumbnail, const rtengine::procparams::ProcParams& pparams,
int height, bool* priority,
ThumbImageUpdateListener* listener):
thumbnail_(thumbnail),
pparams_(pparams),
height_(height),
priority_(priority),
listener_(listener)
{}
Job():
thumbnail_(0),
listener_(0)
{}
Thumbnail* thumbnail_;
rtengine::procparams::ProcParams pparams_;
int height_;
bool* priority_;
ThumbImageUpdateListener* listener_;
};
typedef std::list<Job> JobList;
class
ThumbImageUpdater::Impl
{
public:
Impl():
threadPool_(new Glib::ThreadPool(THREAD_NUM,0)),
active_(0),
inactive_waiting_(false)
{}
Glib::ThreadPool* threadPool_;
Glib::Mutex mutex_;
JobList jobs_;
unsigned int active_;
bool inactive_waiting_;
Glib::Cond inactive_;
void
processNextJob(void)
{
Job j;
{
Glib::Mutex::Lock lock(mutex_);
// nothing to do; could be jobs have been removed
if ( jobs_.empty() )
{
DEBUG("processing: nothing to do (%d,%d)",paused_,jobs_.empty());
return;
}
JobList::iterator i;
// see if any priority jobs exist
for ( i = jobs_.begin(); i != jobs_.end(); ++i)
{
if ( *(i->priority_) )
{
DEBUG("processing(priority) %s",i->thumbnail_->getFileName().c_str());
break;
}
}
// if none, then use first
if ( i == jobs_.end() )
{
i = jobs_.begin();
DEBUG("processing(first) %s",i->thumbnail_->getFileName().c_str());
}
// copy found job
j = *i;
// remove so not run again
jobs_.erase(i);
DEBUG("%d job(s) remaining",jobs_.size());
++active_;
}
// unlock and do processing; will relock on block exit, then call listener
double scale = 1.0;
rtengine::IImage8* img = 0;
{
img = j.thumbnail_->processThumbImage(j.pparams_, j.height_, scale);
}
if (img)
{
DEBUG("pushing image %s",j.thumbnail_->getFileName().c_str());
j.listener_->updateImage(img, scale, j.pparams_.crop);
}
{
Glib::Mutex::Lock lock(mutex_);
if ( --active_ == 0 &&
inactive_waiting_ )
{
inactive_waiting_ = false;
inactive_.signal();
}
}
}
};
ThumbImageUpdater*
ThumbImageUpdater::getInstance(void)
{
// this will not be deleted...
static ThumbImageUpdater* instance_ = 0;
if ( instance_ == 0 )
{
instance_ = new ThumbImageUpdater();
}
return instance_;
}
void ThumbImageUpdater::add (Thumbnail* t, const rtengine::procparams::ProcParams& params, int height, bool* priority, ThumbImageUpdateListener* l) {
ThumbImageUpdater::ThumbImageUpdater():
impl_(new Impl())
{
}
if (!qMutex)
qMutex = new Glib::Mutex ();
if (!startMutex)
startMutex = new Glib::Mutex ();
void
ThumbImageUpdater::add(Thumbnail* t, const rtengine::procparams::ProcParams& params,
int height, bool* priority, ThumbImageUpdateListener* l)
{
// nobody listening?
if ( l == 0 )
{
return;
}
Glib::Mutex::Lock lock(impl_->mutex_);
qMutex->lock ();
// look up if an older version is in the queue
std::list<Job>::iterator i;
for (i=jqueue.begin(); i!=jqueue.end(); i++)
if (i->thumbnail==t && i->listener==l) {
i->pparams = params;
i->height = height;
i->priority = priority;
break;
}
// not found, create and append new job
if (i==jqueue.end ()) {
Job j;
j.thumbnail = t;
j.pparams = params;
j.height = height;
j.listener = l;
j.priority = priority;
jqueue.push_back (j);
}
qMutex->unlock ();
}
JobList::iterator i(impl_->jobs_.begin());
for ( ; i != impl_->jobs_.end(); ++i )
{
if ( i->thumbnail_ == t &&
i->listener_ == l )
{
DEBUG("updating job %s",t->getFileName().c_str());
// we have one, update queue entry, will be picked up by thread when processed
i->pparams_ = params;
i->height_ = height;
i->priority_ = priority;
return;
}
}
void ThumbImageUpdater::process () {
// create a new job and append to queue
DEBUG("queing job %s",t->getFileName().c_str());
impl_->jobs_.push_back(Job(t,params,height,priority,l));
if (stopped) {
stopped = false;
if(!threadPool)
threadPool = new Glib::ThreadPool(threadNum,1);
//thread = Glib::Thread::create (sigc::mem_fun(*this, &ThumbImageUpdater::process_), (unsigned long int)0, true, true, Glib::THREAD_PRIORITY_NORMAL);
process_();
}
}
void ThumbImageUpdater::process_ () {
stopped = false;
tostop = false;
while (!tostop && !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();
Job current = *i;
if (current.listener)
threadPool->push(sigc::bind(sigc::mem_fun(*this, &ThumbImageUpdater::processJob), current));
jqueue.erase (i);
}
stopped = true;
//printf("Threads # %d \n", threadPool->get_num_threads());
}
void ThumbImageUpdater::processJob (Job current) {
if (current.listener) {
double scale = 1.0;
rtengine::IImage8* img = current.thumbnail->processThumbImage (current.pparams, current.height, scale);
if (img)
current.listener->updateImage (img, scale, current.pparams.crop);
}
}
void ThumbImageUpdater::stop () {
gdk_threads_leave();
tostop = true;
if (threadPool) {
threadPool->shutdown(TRUE);
threadPool = NULL;
}
gdk_threads_enter();
}
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 ();
DEBUG("adding run request %s",t->getFileName().c_str());
impl_->threadPool_->push(sigc::mem_fun(*impl_, &ThumbImageUpdater::Impl::processNextJob));
}
void
ThumbImageUpdater::removeJobs(ThumbImageUpdateListener* listener)
{
DEBUG("removeJobs(%p)",listener);
Glib::Mutex::Lock lock(impl_->mutex_);
for( JobList::iterator i(impl_->jobs_.begin()); i != impl_->jobs_.end(); )
{
if (i->listener_ == listener)
{
DEBUG("erasing specific job");
JobList::iterator e(i++);
impl_->jobs_.erase(e);
}
else
{
++i;
}
}
while ( impl_->active_ != 0 )
{
// XXX this is nasty... it would be nicer if we weren't called with
// this lock held
GThreadUnLock unlock;
DEBUG("waiting for running jobs1");
impl_->inactive_waiting_ = true;
impl_->inactive_.wait(impl_->mutex_);
}
}
void
ThumbImageUpdater::removeAllJobs(void)
{
DEBUG("stop");
Glib::Mutex::Lock lock(impl_->mutex_);
impl_->jobs_.clear();
while ( impl_->active_ != 0 )
{
// XXX this is nasty... it would be nicer if we weren't called with
// this lock held
GThreadUnLock unlock;
DEBUG("waiting for running jobs2");
impl_->inactive_waiting_ = true;
impl_->inactive_.wait(impl_->mutex_);
}
}