creaImageIO_lib
creaImageIO::MultiThreadImageReader Class Reference

Class that allows parallel lectures of several images. More...

#include <creaImageIOMultiThreadImageReader.h>

Inheritance diagram for creaImageIO::MultiThreadImageReader:
Collaboration diagram for creaImageIO::MultiThreadImageReader:

Classes

class  ImageToLoad
 Class that represents an image to be loaded. More...
 
struct  ImageToLoadPtrFilenameComparator
 ImageToLoadPtr comparator on filename (for image map) More...
 
struct  ImageToLoadPtrIndexer
 ImageToLoadPtr indexer for image queue. More...
 
struct  ImageToLoadPtrInversePriorityComparator
 ImageToLoadPtr comparator on inverse priority (for image to unload queue) More...
 
struct  ImageToLoadPtrPriorityComparator
 ImageToLoadPtr comparator on priority (for image queue) More...
 
struct  ImageToUnloadPtrIndexer
 ImageToLoadPtr indexer for to unload image queue. More...
 

Public Types

enum  EventType {
  ThreadedReaderStarted, ThreadedReaderStopped, ImageLoaded, ImageUnloaded,
  Error, ThreadedReaderStarted, ThreadedReaderStopped, ImageLoaded,
  ImageUnloaded, Error
}
 
enum  EventType {
  ThreadedReaderStarted, ThreadedReaderStopped, ImageLoaded, ImageUnloaded,
  Error, ThreadedReaderStarted, ThreadedReaderStopped, ImageLoaded,
  ImageUnloaded, Error
}
 

Public Member Functions

 MultiThreadImageReader (int number_of_threads=1)
 Ctor with the number of threads to use. More...
 
 ~MultiThreadImageReader ()
 Dtor. More...
 
bool Start ()
 
void Stop ()
 Stops the reader = stops the threads and delete the images loaded. More...
 
void Request (MultiThreadImageReaderUser *user, const std::string &filename, int priority)
 
vtkImageData * GetImage (const std::string &filename)
 (no user callback but image returned) More...
 
int GetMaximalPriority ()
 
void OnMultiThreadImageReaderEvent (const std::string &filename, EventType type, vtkImageData *image)
 
void getAttributes (const std::string filename, std::map< std::string, std::string > &infos, std::vector< std::string > i_attr)
 Function to read attributes for a file. More...
 
 MultiThreadImageReader (int number_of_threads=1)
 Ctor with the number of threads to use. More...
 
 ~MultiThreadImageReader ()
 Dtor. More...
 
bool Start ()
 
void Stop ()
 Stops the reader = stops the threads and delete the images loaded. More...
 
void Request (MultiThreadImageReaderUser *user, const std::string &filename, int priority)
 
vtkImageData * GetImage (const std::string &filename)
 (no user callback but image returned) More...
 
int GetMaximalPriority ()
 
void OnMultiThreadImageReaderEvent (const std::string &filename, EventType type, vtkImageData *image)
 
void getAttributes (const std::string filename, std::map< std::string, std::string > &infos, std::vector< std::string > i_attr)
 Function to read attributes for a file. More...
 
void MultiThreadImageReaderEventLock ()
 
void MultiThreadImageReaderEventLock ()
 
void MultiThreadImageReaderEventUnlock ()
 
void MultiThreadImageReaderEventUnlock ()
 
wxMutex & GetMultiThreadImageReaderUserMutex ()
 
QMutex * GetMultiThreadImageReaderUserMutex ()
 

Protected Types

typedef ImageToLoadImageToLoadPtr
 Type of pointer on an ImageToLoad struct. More...
 
typedef std::map
< ImageToLoadPtr, vtkImageData
*, ImageToLoadPtrFilenameComparator
ImageMapType
 The type of map of images. More...
 
typedef std::vector
< boost::shared_ptr
< ThreadedImageReader > > 
ThreadedImageReaderListType
 The type of list of threaded readers. More...
 
typedef ImageToLoadImageToLoadPtr
 Type of pointer on an ImageToLoad struct. More...
 
typedef std::map
< ImageToLoadPtr, vtkImageData
*, ImageToLoadPtrFilenameComparator
ImageMapType
 The type of map of images. More...
 
typedef std::vector
< boost::shared_ptr
< ThreadedImageReader > > 
ThreadedImageReaderListType
 The type of list of threaded readers. More...
 

Protected Member Functions

int GetMaximalPriorityWithoutLocking ()
 
void SignalImageRead (ImageToLoadPtr p, bool purge)
 The callback from threaded readers when an image is read. More...
 
void UpdateUnloadPriority (ImageToLoadPtr p, int priority)
 
int GetMaximalPriorityWithoutLocking ()
 
void SignalImageRead (ImageToLoadPtr p, bool purge)
 The callback from threaded readers when an image is read. More...
 
void UpdateUnloadPriority (ImageToLoadPtr p, int priority)
 

Protected Attributes

bool mDone
 
ImageMapType mImages
 The map of images. More...
 
ImageToLoadPtrPriorityComparator mComparator
 Comparator for the image to load queue. More...
 
ImageToLoadPtrIndexer mIndexer
 Indexer for the image to load queue. More...
 
IndexedHeap< ImageToLoadPtr,
ImageToLoadPtrPriorityComparator,
ImageToLoadPtrIndexer
mQueue
 The image to load priority queue. More...
 
ThreadedImageReaderListType mThreadedImageReaderList
 
int mNumberOfThreadedReadersRunning
 The number of currently running threaded readers. More...
 
std::string mRequestedFilename
 For GetImage : the filename requested. More...
 
vtkImageData * mRequestedImage
 For GetImage : the image requested. More...
 
ImageReadermReader
 If number of threads == 0 then uses an internal non-threaded reader. More...
 
IndexedHeap< ImageToLoadPtr,
ImageToLoadPtrInversePriorityComparator,
ImageToUnloadPtrIndexer
mUnloadQueue
 The image to unload priority queue. More...
 
long mTotalMem
 
long mTotalMemMax
 

Friends

class ThreadedImageReader
 

Detailed Description

Class that allows parallel lectures of several images.

TAKE CARE : For the moment it only supports a SINGLE USER

Definition at line 100 of file creaImageIOMultiThreadImageReader.h.

Member Typedef Documentation

The type of map of images.

Definition at line 234 of file creaImageIOMultiThreadImageReader.h.

The type of map of images.

Definition at line 235 of file creaImageIOQMultiThreadImageReader.h.

Type of pointer on an ImageToLoad struct.

Definition at line 186 of file creaImageIOMultiThreadImageReader.h.

Type of pointer on an ImageToLoad struct.

Definition at line 187 of file creaImageIOQMultiThreadImageReader.h.

The type of list of threaded readers.

Definition at line 247 of file creaImageIOMultiThreadImageReader.h.

The type of list of threaded readers.

Definition at line 248 of file creaImageIOQMultiThreadImageReader.h.

Member Enumeration Documentation

Enumerator
ThreadedReaderStarted 
ThreadedReaderStopped 
ImageLoaded 
ImageUnloaded 
Error 
ThreadedReaderStarted 
ThreadedReaderStopped 
ImageLoaded 
ImageUnloaded 
Error 

Definition at line 61 of file creaImageIOMultiThreadImageReader.h.

62  {
67  Error
68  }
Enumerator
ThreadedReaderStarted 
ThreadedReaderStopped 
ImageLoaded 
ImageUnloaded 
Error 
ThreadedReaderStarted 
ThreadedReaderStopped 
ImageLoaded 
ImageUnloaded 
Error 

Definition at line 62 of file creaImageIOQMultiThreadImageReader.h.

63  {
68  Error
69  }

Constructor & Destructor Documentation

creaImageIO::MultiThreadImageReader::MultiThreadImageReader ( int  number_of_threads = 1)

Ctor with the number of threads to use.

Definition at line 86 of file creaImageIOMultiThreadImageReader.cpp.

References mComparator, mDone, mIndexer, mNumberOfThreadedReadersRunning, mQueue, mReader, mThreadedImageReaderList, and ThreadedImageReader.

87  : //mDoNotSignal(false),
88  mReader(0),
89  mTotalMem(0),
90  mTotalMemMax(1000000)
91  {
92  // std::cout << "#### MultiThreadImageReader::MultiThreadImageReader("
93  // << " #threads= " << number_of_threads <<" )"<<std::endl;
94 
95  mDone = false;
96  // Create the threads
97  for (int i=0; i<number_of_threads; i++)
98  {
99  //ThreadedImageReader* t = new ThreadedImageReader(this);
100  boost::shared_ptr<ThreadedImageReader> t(new ThreadedImageReader(this), ThreadedImageReader::deleter());
101  mThreadedImageReaderList.push_back(t);
102  std::cout << " ===> Thread "<<i
103  <<" successfully added"<< std::endl;
104  }
106  // Init the queue
107  mQueue.set(mComparator);
108  mQueue.set(mIndexer);
109  //
110  // no thread : alloc self reader
111 // if (number_of_threads==0)
112 // {
113  mReader = new ImageReader();
114 // }
115  }
creaImageIO::MultiThreadImageReader::~MultiThreadImageReader ( )

Dtor.

Definition at line 213 of file creaImageIOMultiThreadImageReader.cpp.

References mReader, mThreadedImageReaderList, and Stop().

214  {
215  // std::cout << "#### MultiThreadImageReader::~MultiThreadImageReader()"
216  // <<std::endl;
217  Stop();
218  if (mReader) delete mReader;
219  mThreadedImageReaderList.clear();
220  }

Here is the call graph for this function:

creaImageIO::MultiThreadImageReader::MultiThreadImageReader ( int  number_of_threads = 1)

Ctor with the number of threads to use.

creaImageIO::MultiThreadImageReader::~MultiThreadImageReader ( )

Dtor.

Member Function Documentation

void creaImageIO::MultiThreadImageReader::getAttributes ( const std::string  filename,
std::map< std::string, std::string > &  infos,
std::vector< std::string >  i_attr 
)

Function to read attributes for a file.

Definition at line 243 of file creaImageIOMultiThreadImageReader.cpp.

References creaImageIO::ImageReader::getAttributes(), and mReader.

Referenced by creaImageIO::GimmickView::getAttributes().

245  {
246  mReader->getAttributes(filename, infos, i_attr);
247  }

Here is the call graph for this function:

Here is the caller graph for this function:

void creaImageIO::MultiThreadImageReader::getAttributes ( const std::string  filename,
std::map< std::string, std::string > &  infos,
std::vector< std::string >  i_attr 
)

Function to read attributes for a file.

vtkImageData * creaImageIO::MultiThreadImageReader::GetImage ( const std::string &  filename)

(no user callback but image returned)

Request the image "filename" immediately Blocks until image loaded

Definition at line 347 of file creaImageIOMultiThreadImageReader.cpp.

References creaImageIO::MultiThreadImageReader::ImageToLoad::GetImage(), GetMaximalPriorityWithoutLocking(), mImages, mReader, creaImageIO::ImageReader::ReadImage(), creaImageIO::MultiThreadImageReader::ImageToLoad::SetImage(), and UpdateUnloadPriority().

Referenced by creaImageIO::GimmickView::GetDefaultImage(), creaImageIO::GimmickView::isSingle(), creaImageIO::GimmickView::readImages1(), creaImageIO::GimmickView::readImages2(), creaImageIO::GimmickView::readImages3(), creaImageIO::GimmickView::readImages4(), creaImageIO::GimmickView::ReadImagesNotThreaded(), and creaImageIO::GimmickView::ReadImagesNotThreadedInVector().

348  {
349  // Start();
350  // std::cout << "** MultiThreadImageReader::GetImage('"<<filename<<"')"
351  // <<std::endl;
352 
353  do
354  {
355  // wxMutexLocker lock(GetMultiThreadImageReaderUserMutex()); //mMutex);
356 
357  // std::cout << "** MultiThreadImageReader::GetImage('"<<filename
358  // <<"') lock ok"
359  // <<std::endl;
360 
361  // if (mNumberOfThreadedReadersRunning==0)
362  // if (mThreadedImageReaderList.size()==0)
363  if (true)
364  {
365  ImageToLoad itl(this,filename);
366  ImageMapType::iterator i = mImages.find(&itl);
367  if (i!=mImages.end())
368  {
369  ImageToLoadPtr pitl = const_cast<ImageToLoadPtr>(i->first);
370  // Already inserted
371  if (pitl->GetImage() != 0)
372  {
373  // Already read
376  return pitl->GetImage();
377  }
378  }
379  ImageToLoadPtr pitl = new ImageToLoad(this,filename,0);
380  mImages[pitl] = 0;
381  pitl->SetImage(mReader->ReadImage(filename));
384  return pitl->GetImage();
385  }
386 
387  /*
388  mRequestedFilename = filename;
389  mRequestedImage = 0;
390  ImageToLoad itl(this,filename);
391  ImageMapType::iterator i = mImages.find(&itl);
392  if (i!=mImages.end())
393  {
394  // Already inserted in queue
395  if (i->first->GetImage() != 0)
396  {
397  // Already read : ok : return it
398  return i->first->GetImage();
399  }
401  ImageToLoadPtr pitl = const_cast<ImageToLoadPtr>(i->first);
402  pitl->SetPriority( GetMaximalPriorityWithoutLocking() + 1 );
403  pitl->SetUser( this );
404  // Already in queue
405  if (pitl->Index()>=0)
406  {
407  // Re-sort the queue
408  mQueue.upsort(pitl->Index());
409  }
410  // Not read but not in queue = being read = ok
411  else
412  {
413  pitl->SetUser( this );
414  }
415  }
416  else
417  {
418 
419  // Never requested before or unloaded
420  ImageToLoadPtr pitl =
421  new ImageToLoad(this,filename,
422  GetMaximalPriorityWithoutLocking() + 1);
423  mImages[pitl] = 0;
424  mQueue.insert(pitl);
425  }
426  */
427  }
428  while (0);
429 
430  // std::cout << "Waiting..."<<std::endl;
431 
432  /*
433  // Waiting that it is read
434  int n = 0;
435  do
436  {
437  // std::cout << n++ << std::endl;
438  wxMilliSleep(10);
439  do
440  {
441  // wxMutexLocker lock(mMutex);
442  wxMutexLocker lock(GetMultiThreadImageReaderUserMutex());
443  if (mRequestedImage!=0)
444  {
445  return mRequestedImage;
446  }
447  }
448  while (0);
449  }
450  while (true);
451  //
452  */
453  }

Here is the call graph for this function:

Here is the caller graph for this function:

vtkImageData* creaImageIO::MultiThreadImageReader::GetImage ( const std::string &  filename)

(no user callback but image returned)

Request the image "filename" immediately Blocks until image loaded

int creaImageIO::MultiThreadImageReader::GetMaximalPriority ( )

Definition at line 557 of file creaImageIOMultiThreadImageReader.cpp.

References GetMaximalPriorityWithoutLocking(), and creaImageIO::MultiThreadImageReaderUser::GetMultiThreadImageReaderUserMutex().

Referenced by creaImageIO::GimmickView::GetMaximalPriority().

558  {
559  wxMutexLocker lock(GetMultiThreadImageReaderUserMutex()); //mMutex);
561  }

Here is the call graph for this function:

Here is the caller graph for this function:

int creaImageIO::MultiThreadImageReader::GetMaximalPriority ( )
int creaImageIO::MultiThreadImageReader::GetMaximalPriorityWithoutLocking ( )
protected

Definition at line 566 of file creaImageIOMultiThreadImageReader.cpp.

References mQueue, and mUnloadQueue.

Referenced by GetImage(), and GetMaximalPriority().

567  {
568  long max = 0;
569  if (mQueue.size()>0)
570  {
571  max = mQueue.top()->GetPriority();
572  }
573  if (mUnloadQueue.size()>0)
574  {
575  int max2 = mUnloadQueue.top()->GetPriority();
576  if (max2>max) max=max2;
577  }
578  return max;
579  }

Here is the caller graph for this function:

int creaImageIO::MultiThreadImageReader::GetMaximalPriorityWithoutLocking ( )
protected
wxMutex& creaImageIO::MultiThreadImageReaderUser::GetMultiThreadImageReaderUserMutex ( )
inlineinherited

Definition at line 84 of file creaImageIOMultiThreadImageReader.h.

Referenced by GetMaximalPriority(), Request(), SignalImageRead(), Start(), and Stop().

Here is the caller graph for this function:

QMutex* creaImageIO::MultiThreadImageReaderUser::GetMultiThreadImageReaderUserMutex ( )
inlineinherited

Definition at line 85 of file creaImageIOQMultiThreadImageReader.h.

void creaImageIO::MultiThreadImageReaderUser::MultiThreadImageReaderEventLock ( )
inlineinherited

Definition at line 80 of file creaImageIOMultiThreadImageReader.h.

Referenced by creaImageIO::ThreadedImageReader::Entry().

Here is the caller graph for this function:

void creaImageIO::MultiThreadImageReaderUser::MultiThreadImageReaderEventLock ( )
inlineinherited

Definition at line 81 of file creaImageIOQMultiThreadImageReader.h.

void creaImageIO::MultiThreadImageReaderUser::MultiThreadImageReaderEventUnlock ( )
inlineinherited

Definition at line 82 of file creaImageIOMultiThreadImageReader.h.

Referenced by creaImageIO::ThreadedImageReader::Entry().

Here is the caller graph for this function:

void creaImageIO::MultiThreadImageReaderUser::MultiThreadImageReaderEventUnlock ( )
inlineinherited

Definition at line 83 of file creaImageIOQMultiThreadImageReader.h.

void creaImageIO::MultiThreadImageReader::OnMultiThreadImageReaderEvent ( const std::string &  filename,
MultiThreadImageReaderUser::EventType  type,
vtkImageData *  image 
)
virtual

The virtual method to overload by MultiThreadImageReader users It is called when an image has been loaded or unloaded Provides :

  • The image file name which was requested
  • The type of event
  • If type==ImageLoaded the image pointer, else NULL pointer

Reimplemented from creaImageIO::MultiThreadImageReaderUser.

Definition at line 323 of file creaImageIOMultiThreadImageReader.cpp.

References creaImageIO::MultiThreadImageReaderUser::ImageLoaded, creaImageIO::MultiThreadImageReaderUser::ThreadedReaderStarted, and creaImageIO::MultiThreadImageReaderUser::ThreadedReaderStopped.

326  {
328  (filename == mRequestedFilename))
329  {
330  mRequestedImage = image;
331  }
333  {
335  // std::cout << "#TR=" << mNumberOfThreadedReadersRunning << std::endl;
336  }
338  {
339 
341  // std::cout << "#TR=" << mNumberOfThreadedReadersRunning << std::endl;
342  }
343  }
void creaImageIO::MultiThreadImageReader::OnMultiThreadImageReaderEvent ( const std::string &  filename,
EventType  type,
vtkImageData *  image 
)
virtual

The virtual method to overload by MultiThreadImageReader users It is called when an image has been loaded or unloaded Provides :

  • The image file name which was requested
  • The type of event
  • If type==ImageLoaded the image pointer, else NULL pointer

Reimplemented from creaImageIO::MultiThreadImageReaderUser.

void creaImageIO::MultiThreadImageReader::Request ( MultiThreadImageReaderUser user,
const std::string &  filename,
int  priority 
)

Request the image "filename" with a given priority When the image is ready (or an error occurred) The observer's callback is invoked

Already requested : change the priority

Already requested : change the priority

Definition at line 250 of file creaImageIOMultiThreadImageReader.cpp.

References creaImageIO::MultiThreadImageReader::ImageToLoad::GetImage(), creaImageIO::MultiThreadImageReaderUser::GetMultiThreadImageReaderUserMutex(), creaImageIO::MultiThreadImageReader::ImageToLoad::Index(), mImages, mNumberOfThreadedReadersRunning, mQueue, mReader, creaImageIO::ImageReader::ReadImage(), creaImageIO::MultiThreadImageReader::ImageToLoad::SetImage(), creaImageIO::MultiThreadImageReader::ImageToLoad::SetPriority(), creaImageIO::MultiThreadImageReader::ImageToLoad::SetUser(), SignalImageRead(), and UpdateUnloadPriority().

Referenced by creaImageIO::GimmickView::RequestReading().

253  {
254  wxMutexLocker lock(GetMultiThreadImageReaderUserMutex()); //mMutex);
255 
257 // if (mThreadedImageReaderList.size()==0)
258  {
259  // no detached reader : use self reader
260  ImageToLoad itl(user,filename);
261  ImageMapType::iterator i = mImages.find(&itl);
262  if (i!=mImages.end())
263  {
264  ImageToLoadPtr pitl = const_cast<ImageToLoadPtr>(i->first);
265  // Already inserted
266  if (pitl->GetImage() != 0)
267  {
268  // Already read
269  pitl->SetUser(user);
270  UpdateUnloadPriority(pitl,priority);
271  SignalImageRead(pitl,false);
272  return; // pitl->GetImage();
273  }
274  }
275  ImageToLoadPtr pitl = new ImageToLoad(user,filename,0);
276  mImages[pitl] = 0;
277  pitl->SetImage(mReader->ReadImage(filename));
278  UpdateUnloadPriority(pitl,priority);
279  SignalImageRead(pitl,true);
280  // return pitl->GetImage();
281  return;
282  }
283 
284  ImageToLoad itl(user,filename);
285  ImageMapType::iterator i = mImages.find(&itl);
286  if (i!=mImages.end())
287  {
288  // Already inserted
289  if (i->first->GetImage() != 0)
290  {
291  // Already read : ok :signal the user
292  UpdateUnloadPriority(i->first,priority);
293  SignalImageRead(i->first,false);
294  return;
295  }
297  ImageToLoadPtr pitl = const_cast<ImageToLoadPtr>(i->first);
298  pitl->SetPriority(priority);
299  // Already in queue
300  if (pitl->Index()>=0)
301  {
302  // Re-sort the queue
303  mQueue.upsort(pitl->Index());
304  }
305  // Not read but not in queue = being read = ok
306  else
307  {
308 
309  }
310  }
311  else
312  {
313  // Never requested before or unloaded
314  ImageToLoadPtr pitl = new ImageToLoad(user,filename,priority);
315  mImages[pitl] = 0;
316  mQueue.insert(pitl);
317  }
318  }

Here is the call graph for this function:

Here is the caller graph for this function:

void creaImageIO::MultiThreadImageReader::Request ( MultiThreadImageReaderUser user,
const std::string &  filename,
int  priority 
)

Request the image "filename" with a given priority When the image is ready (or an error occurred) The observer's callback is invoked

void creaImageIO::MultiThreadImageReader::SignalImageRead ( ImageToLoadPtr  p,
bool  purge 
)
protected

The callback from threaded readers when an image is read.

Definition at line 457 of file creaImageIOMultiThreadImageReader.cpp.

References creaImageIO::MultiThreadImageReader::ImageToLoad::GetFilename(), creaImageIO::MultiThreadImageReader::ImageToLoad::GetImage(), creaImageIO::MultiThreadImageReaderUser::GetMultiThreadImageReaderUserMutex(), creaImageIO::MultiThreadImageReader::ImageToLoad::GetUser(), GimmickMessage, creaImageIO::MultiThreadImageReaderUser::ImageLoaded, creaImageIO::MultiThreadImageReaderUser::ImageUnloaded, creaImageIO::MultiThreadImageReader::ImageToLoad::Index(), mImages, mTotalMem, mTotalMemMax, creaImageIO::MultiThreadImageReaderUser::MultiThreadImageReaderSendEvent(), and mUnloadQueue.

Referenced by creaImageIO::ThreadedImageReader::Entry(), and Request().

459  {
460 
461 // std::cout << "MultiThreadImageReader::SignalImageRead" <<std::endl;
462  // std::cout << "this="<<this <<std::endl;
463  // std::cout << "user="<<p->GetUser() <<std::endl;
464 
465  if ( p->GetUser() == this )
467 
468  p->GetUser()->MultiThreadImageReaderSendEvent
469  (p->GetFilename(),
471  p->GetImage());
472 
473  /*
474  AN ATTEMPT TO UNLOAD OLDEST IMAGE IF EXCEEDED A CERTAIN MEMORY QUOTA
475  BUGGY : TO FIX
476  */
477  if (!purge) return;
478  GimmickMessage(5,"Image '"<<p->GetFilename()<<"' read"<<std::endl);
479 
480  // wxMutexLocker lock(GetMultiThreadImageReaderUserMutex());
481 
482  mUnloadQueue.insert(p);
483  p->GetImage()->UpdateInformation();
484  p->GetImage()->PropagateUpdateExtent();
485  long ImMem = p->GetImage()->GetEstimatedMemorySize();
486  mTotalMem += ImMem;
487 
488  GimmickMessage(5,"==> Image in memory = "<<mUnloadQueue.size()<<std::endl);
489  GimmickMessage(5,"==> Total mem = "<<mTotalMem<<" Ko"<<std::endl);
490 
491  // return;
492 
493  while (mTotalMem > mTotalMemMax)
494  {
495  GimmickMessage(5,
496  " ! Exceeded max of "
497  << mTotalMemMax << " Ko : unloading oldest image ... "
498  << std::endl);
499  if ( mUnloadQueue.size() <= 1 )
500  {
501  GimmickMessage(5,
502  " Only one image : cannot load AND unload it !!"
503  <<std::endl);
504  break;
505 
506  }
507  ImageToLoadPtr unload = mUnloadQueue.remove_top();
508  MultiThreadImageReaderUser* user = unload->GetUser();
509 
510  /*
511  if ((user!=0)&&(user!=this))
512  {
513  user->GetMultiThreadImageReaderUserMutex().Lock();
514  }
515  */
516 
517  std::string filename = unload->GetFilename();
518 
519  GimmickMessage(5,"'" << filename << "'" << std::endl);
520  mTotalMem -= unload->GetImage()->GetEstimatedMemorySize();
521 
522  GimmickMessage(5," ==> Total mem = "<<mTotalMem<<" Ko "<<std::endl);
523 
524  if (user!=0)
525  {
526  // std::cout << "unlock..."<<std::endl;
527  // user->GetMultiThreadImageReaderUserMutex().Unlock();
528  // std::cout << "event"<<std::endl;
529  user->MultiThreadImageReaderSendEvent
530  (filename,
532  0);
533  // std::cout << "event ok"<<std::endl;
534  }
535 
536  if (unload->Index()>=0)
537  {
538  // GimmickMessage(5,"still in queue"<<std::endl);
539  }
540  unload->Index() = -1;
541 
542 
543  ImageMapType::iterator it = mImages.find(unload);
544  if (it!=mImages.end())
545  {
546  mImages.erase(it);
547  }
548  // std::cout << "delete..."<<std::endl;
549  delete unload;
550  // std::cout << "delete ok."<<std::endl;
551 
552  }
553  }

Here is the call graph for this function:

Here is the caller graph for this function:

void creaImageIO::MultiThreadImageReader::SignalImageRead ( ImageToLoadPtr  p,
bool  purge 
)
protected

The callback from threaded readers when an image is read.

bool creaImageIO::MultiThreadImageReader::Start ( )

Starts the reader = create the threads which start to check periodically the queue of requested images to read

Definition at line 120 of file creaImageIOMultiThreadImageReader.cpp.

References creaImageIO::MultiThreadImageReaderUser::GetMultiThreadImageReaderUserMutex(), mNumberOfThreadedReadersRunning, and mThreadedImageReaderList.

Referenced by creaImageIO::GimmickView::RequestReading().

121  {
122 
123  // std::cout << "#### MultiThreadImageReader::Start()"
124  // <<std::endl;
125  if (mNumberOfThreadedReadersRunning > 0) return true;
126 
127  ThreadedImageReaderListType::iterator i;
128  for (i =mThreadedImageReaderList.begin();
129  i!=mThreadedImageReaderList.end();
130  i++)
131  {
132  (*i)->Create();
133  if ( (*i)->Run() != wxTHREAD_NO_ERROR )
134  {
135  std::cout << "ERROR starting a thread"<< std::endl;
136  return false;
137  }
138  else
139  {
140  std::cout << " ===> Thread "<<(*i)->GetCurrentId()
141  <<" successfully created"<< std::endl;
142 
143  }
144  }
145  wxMutexLocker locker(GetMultiThreadImageReaderUserMutex());
146  // std::cout << "EO Start : #Threads running = "
147  // << mNumberOfThreadedReadersRunning<<std::endl;
148 
149  return true;
150  }

Here is the call graph for this function:

Here is the caller graph for this function:

bool creaImageIO::MultiThreadImageReader::Start ( )

Starts the reader = create the threads which start to check periodically the queue of requested images to read

void creaImageIO::MultiThreadImageReader::Stop ( )

Stops the reader = stops the threads and delete the images loaded.

Definition at line 154 of file creaImageIOMultiThreadImageReader.cpp.

References creaImageIO::MultiThreadImageReaderUser::GetMultiThreadImageReaderUserMutex(), mDone, mImages, mNumberOfThreadedReadersRunning, and mThreadedImageReaderList.

Referenced by creaImageIO::GimmickView::stopReader(), and ~MultiThreadImageReader().

155  {
156 // std::cout << "#### MultiThreadImageReader::Stop()"
157 // <<std::endl;
158  // std::cout << "Sending stop order to the threads..."<<std::endl;
159  if (mDone) return;
160 
161  ThreadedImageReaderListType::iterator i;
162  for (i =mThreadedImageReaderList.begin();
163  i!=mThreadedImageReaderList.end();
164  i++)
165  { std::cout << " ===> Thread "<<(*i)->GetCurrentId()
166  <<" successfully stopped"<< std::endl;
167  if((*i)->IsAlive())
168  {(*i)->Pause();
169  (*i).reset();
170  // (*i)->Delete();
171  }
172  }
173  mThreadedImageReaderList.clear();
174  // Wait a little to be sure that all threads have stopped
175  // A better way to do this ?
176  // wxMilliSleep(1000);
177  // New method : the threads generate a stop event when they have finished
178  // We wait until all threads have stopped
179 // std::cout << "Waiting for stop signals..."<<std::endl;
180  do
181  {
182  // Sleep a little
183  wxMilliSleep(10);
184  // Lock
185  {
186  wxMutexLocker locker(GetMultiThreadImageReaderUserMutex());
187 // std::cout << "#Threads running = "
188 // << mNumberOfThreadedReadersRunning<<std::endl;
189  // Break if all readers have stopped
191  {
192  break;
193  }
194  }
195  }
196  while (true);
197 // std::cout << "All threads stopped : OK "<<std::endl;
198 
199  ImageMapType::iterator j;
200  for (j =mImages.begin();
201  j!=mImages.end();
202  ++j)
203 
204  {
205  delete j->first;
206  }
207  mImages.clear();
208  mDone = true;
209  }

Here is the call graph for this function:

Here is the caller graph for this function:

void creaImageIO::MultiThreadImageReader::Stop ( )

Stops the reader = stops the threads and delete the images loaded.

void creaImageIO::MultiThreadImageReader::UpdateUnloadPriority ( ImageToLoadPtr  p,
int  priority 
)
protected

Definition at line 224 of file creaImageIOMultiThreadImageReader.cpp.

References creaImageIO::MultiThreadImageReader::ImageToLoad::GetPriority(), mUnloadQueue, creaImageIO::MultiThreadImageReader::ImageToLoad::SetPriority(), and creaImageIO::MultiThreadImageReader::ImageToLoad::UnloadIndex().

Referenced by GetImage(), and Request().

226  {
227  // not in unload queue : ciao
228  if (p->UnloadIndex()<0) return;
229  int old_prio = p->GetPriority();
230  if (priority > old_prio)
231  {
232  p->SetPriority(priority);
233  mUnloadQueue.downsort(p->UnloadIndex());
234  }
235  else if ( old_prio > priority )
236  {
237  p->SetPriority(priority);
238  mUnloadQueue.upsort(p->UnloadIndex());
239  }
240  }

Here is the call graph for this function:

Here is the caller graph for this function:

void creaImageIO::MultiThreadImageReader::UpdateUnloadPriority ( ImageToLoadPtr  p,
int  priority 
)
protected

Friends And Related Function Documentation

Definition at line 103 of file creaImageIOMultiThreadImageReader.h.

Referenced by MultiThreadImageReader().

Member Data Documentation

ImageToLoadPtrPriorityComparator creaImageIO::MultiThreadImageReader::mComparator
protected

Comparator for the image to load queue.

Definition at line 238 of file creaImageIOMultiThreadImageReader.h.

Referenced by MultiThreadImageReader().

bool creaImageIO::MultiThreadImageReader::mDone
protected

Definition at line 140 of file creaImageIOMultiThreadImageReader.h.

Referenced by MultiThreadImageReader(), and Stop().

ImageMapType creaImageIO::MultiThreadImageReader::mImages
protected
ImageToLoadPtrIndexer creaImageIO::MultiThreadImageReader::mIndexer
protected

Indexer for the image to load queue.

Definition at line 240 of file creaImageIOMultiThreadImageReader.h.

Referenced by MultiThreadImageReader().

int creaImageIO::MultiThreadImageReader::mNumberOfThreadedReadersRunning
protected

The number of currently running threaded readers.

Definition at line 251 of file creaImageIOMultiThreadImageReader.h.

Referenced by MultiThreadImageReader(), Request(), Start(), and Stop().

IndexedHeap< ImageToLoadPtr, ImageToLoadPtrPriorityComparator, ImageToLoadPtrIndexer > creaImageIO::MultiThreadImageReader::mQueue
protected
ImageReader * creaImageIO::MultiThreadImageReader::mReader
protected

If number of threads == 0 then uses an internal non-threaded reader.

Definition at line 263 of file creaImageIOMultiThreadImageReader.h.

Referenced by getAttributes(), GetImage(), MultiThreadImageReader(), Request(), and ~MultiThreadImageReader().

std::string creaImageIO::MultiThreadImageReader::mRequestedFilename
protected

For GetImage : the filename requested.

The mutex used to access safely internal data from any thread LG : Removed ! We now use the embedded mutex in User from which we inherit...

Definition at line 258 of file creaImageIOMultiThreadImageReader.h.

vtkImageData * creaImageIO::MultiThreadImageReader::mRequestedImage
protected

For GetImage : the image requested.

Definition at line 260 of file creaImageIOMultiThreadImageReader.h.

ThreadedImageReaderListType creaImageIO::MultiThreadImageReader::mThreadedImageReaderList
protected
long creaImageIO::MultiThreadImageReader::mTotalMem
protected

Definition at line 273 of file creaImageIOMultiThreadImageReader.h.

Referenced by SignalImageRead().

long creaImageIO::MultiThreadImageReader::mTotalMemMax
protected

Definition at line 274 of file creaImageIOMultiThreadImageReader.h.

Referenced by SignalImageRead().

IndexedHeap< ImageToLoadPtr, ImageToLoadPtrInversePriorityComparator, ImageToUnloadPtrIndexer > creaImageIO::MultiThreadImageReader::mUnloadQueue
protected

The image to unload priority queue.

The type of list of images loaded used to unload oldest image when memory limit exceeded

Definition at line 270 of file creaImageIOMultiThreadImageReader.h.

Referenced by GetMaximalPriorityWithoutLocking(), SignalImageRead(), and UpdateUnloadPriority().


The documentation for this class was generated from the following files: