]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOMultiThreadImageReader.cpp
move directory
[creaImageIO.git] / src2 / creaImageIOMultiThreadImageReader.cpp
index fd76051ab7fbf4704e7baee893d3e9ae5fe26cc7..3ee50de9a2e558912cfe0d8ac0e489e2cc1cd209 100644 (file)
@@ -1,7 +1,12 @@
 #include <creaImageIOMultiThreadImageReader.h>
 #include <creaImageIOImageReader.h>
 #include <wx/utils.h>
+#include <creaImageIOSystem.h>
 
+#include <creaImageIOGimmick.h>
+#ifdef _DEBUG
+#define new DEBUG_NEW
+#endif
 namespace creaImageIO
 {
 
@@ -12,6 +17,7 @@ namespace creaImageIO
     vtkImageData* image)
   {
     wxMutexLocker lock(mMultiThreadImageReaderUserMutex);
+
     this->OnMultiThreadImageReaderEvent(filename,type,image);
   }
   //=====================================================================
@@ -29,11 +35,22 @@ namespace creaImageIO
 
     vtkImageData* Read(const std::string& filename);
     
+       struct deleter
+       {
+               void operator()(ThreadedImageReader* p)
+               {
+                       p->Delete();
+               }
+       };
+       friend struct deleter;
+
 
   private:
     ImageReader mReader;
     MultiThreadImageReader* mMultiThreadImageReader;
+       
   };
+
   //=====================================================================
 
   
@@ -42,16 +59,20 @@ namespace creaImageIO
     : //mDoNotSignal(false),
       mReader(0),
       mTotalMem(0),
-      mTotalMemMax(10000)
+      mTotalMemMax(1000000)
   {
     //    std::cout << "#### MultiThreadImageReader::MultiThreadImageReader("
     //       << " #threads= " << number_of_threads <<" )"<<std::endl;
 
+         mDone = false;
     // Create the threads
     for (int i=0; i<number_of_threads; i++) 
       {
-       ThreadedImageReader* t = new ThreadedImageReader(this);
+                 //ThreadedImageReader* t = new ThreadedImageReader(this);
+                 boost::shared_ptr<ThreadedImageReader> t(new ThreadedImageReader(this), ThreadedImageReader::deleter());
        mThreadedImageReaderList.push_back(t);
+        std::cout << "  ===> Thread "<<i
+                     <<" successfully added"<< std::endl;
       }
     mNumberOfThreadedReadersRunning = 0;
     // Init the queue
@@ -59,10 +80,10 @@ namespace creaImageIO
     mQueue.set(mIndexer);
     // 
     // no thread : alloc self reader
-    if (number_of_threads==0)
-      {
+//    if (number_of_threads==0)
+//      {
        mReader = new ImageReader();
-      }
+//      }
   }
   //=====================================================================
 
@@ -73,6 +94,8 @@ namespace creaImageIO
 
     //    std::cout << "#### MultiThreadImageReader::Start()"
     //               <<std::endl;
+         if (mNumberOfThreadedReadersRunning > 0) return true;
+         
     ThreadedImageReaderListType::iterator i;
     for (i =mThreadedImageReaderList.begin();
         i!=mThreadedImageReaderList.end();
@@ -86,8 +109,8 @@ namespace creaImageIO
          }
        else 
          {
-           //      std::cout << "  ===> Thread "<<(*i)->GetCurrentId()
-           //                <<" successfully created"<< std::endl;
+                   std::cout << "  ===> Thread "<<(*i)->GetCurrentId()
+                             <<" successfully created"<< std::endl;
            
          }
       }
@@ -105,15 +128,21 @@ namespace creaImageIO
 //                 std::cout << "#### MultiThreadImageReader::Stop()"
 //           <<std::endl;
   //  std::cout << "Sending stop order to the threads..."<<std::endl;
+         if (mDone) return;
 
     ThreadedImageReaderListType::iterator i;
     for (i =mThreadedImageReaderList.begin();
         i!=mThreadedImageReaderList.end();
         i++)
-      {
-       (*i)->Delete();
+      { std::cout << "  ===> Thread "<<(*i)->GetCurrentId()
+                             <<" successfully stopped"<< std::endl;
+                 if((*i)->IsAlive())
+                 {(*i)->Pause();
+                         (*i).reset();
+                        //                       (*i)->Delete();
+                 }
       }
-    mThreadedImageReaderList.clear();
+   mThreadedImageReaderList.clear();
     // Wait a little to be sure that all threads have stopped
     // A better way to do this ?
     //    wxMilliSleep(1000);
@@ -123,7 +152,7 @@ namespace creaImageIO
     do 
       {
        // Sleep a little
-       wxMilliSleep(10);
+               wxMilliSleep(10);
        // Lock
        {
          wxMutexLocker locker(GetMultiThreadImageReaderUserMutex());
@@ -148,6 +177,7 @@ namespace creaImageIO
        delete j->first;
       }
     mImages.clear();
+       mDone = true;
   }
   //=====================================================================
 
@@ -158,6 +188,7 @@ namespace creaImageIO
     //       <<std::endl;
     Stop();
     if (mReader) delete mReader;
+       mThreadedImageReaderList.clear();
   }
   //=====================================================================
 
@@ -186,9 +217,10 @@ namespace creaImageIO
                                        const std::string& filename, 
                                        int priority )
   {
-    wxMutexLocker lock(GetMultiThreadImageReaderUserMutex()); //mMutex);
-    
-    if (mThreadedImageReaderList.size()==0) 
+       wxMutexLocker lock(GetMultiThreadImageReaderUserMutex()); //mMutex);
+
+         if (mNumberOfThreadedReadersRunning==0)
+//    if (mThreadedImageReaderList.size()==0) 
       {
        // no detached reader : use self reader
        ImageToLoad itl(user,filename);
@@ -215,7 +247,6 @@ namespace creaImageIO
        return;
       }
 
-
     ImageToLoad itl(user,filename);
     ImageMapType::iterator i = mImages.find(&itl);
     if (i!=mImages.end())
@@ -271,7 +302,8 @@ namespace creaImageIO
       }
     else if (e==MultiThreadImageReaderUser::ThreadedReaderStopped)
       {
-       mNumberOfThreadedReadersRunning--;
+       
+                mNumberOfThreadedReadersRunning--;
        //      std::cout << "#TR=" << mNumberOfThreadedReadersRunning << std::endl;
       }
   }
@@ -280,15 +312,21 @@ namespace creaImageIO
   //=====================================================================
   vtkImageData* MultiThreadImageReader::GetImage(const std::string& filename)
   {
-    
-    //    std::cout << "** MultiThreadImageReader::GetImage('"<<filename<<"')"
-    //       <<std::endl;
+        // Start();
+    //       std::cout << "** MultiThreadImageReader::GetImage('"<<filename<<"')"
+    //          <<std::endl;
     
     do 
       {
-       wxMutexLocker lock(GetMultiThreadImageReaderUserMutex()); //mMutex);
+       //      wxMutexLocker lock(GetMultiThreadImageReaderUserMutex()); //mMutex);
                
-       if (mThreadedImageReaderList.size()==0)
+       //     std::cout << "** MultiThreadImageReader::GetImage('"<<filename
+       //             <<"') lock ok"
+       //               <<std::endl;
+    
+       //                if (mNumberOfThreadedReadersRunning==0)
+       //      if (mThreadedImageReaderList.size()==0)
+       if (true)
          {
            ImageToLoad itl(this,filename);
            ImageMapType::iterator i = mImages.find(&itl);
@@ -311,7 +349,8 @@ namespace creaImageIO
                                 GetMaximalPriorityWithoutLocking()+1);
            return pitl->GetImage();
          }
-       
+
+       /*      
        mRequestedFilename = filename;
        mRequestedImage = 0;
        ImageToLoad itl(this,filename);
@@ -350,11 +389,13 @@ namespace creaImageIO
            mImages[pitl] = 0;
            mQueue.insert(pitl);
          }
+       */
       }
     while (0);
 
     //    std::cout << "Waiting..."<<std::endl;
 
+    /*
     // Waiting that it is read
     int n = 0;
     do 
@@ -374,7 +415,7 @@ namespace creaImageIO
       }
     while (true);
     // 
-    
+    */
   }
   //=====================================================================
   
@@ -382,7 +423,8 @@ namespace creaImageIO
   void MultiThreadImageReader::SignalImageRead(ImageToLoadPtr p, 
                                               bool purge)
   {
-    //    std::cout << "MultiThreadImageReader::SignalImageRead" <<std::endl;
+    
+//    std::cout << "MultiThreadImageReader::SignalImageRead" <<std::endl;
     //    std::cout << "this="<<this <<std::endl;
     //    std::cout << "user="<<p->GetUser() <<std::endl;
 
@@ -399,6 +441,7 @@ namespace creaImageIO
       BUGGY : TO FIX 
     */
     if (!purge)  return;
+    GimmickMessage(5,"Image '"<<p->GetFilename()<<"' read"<<std::endl);
 
     //    wxMutexLocker lock(GetMultiThreadImageReaderUserMutex());
           
@@ -407,38 +450,58 @@ namespace creaImageIO
     p->GetImage()->PropagateUpdateExtent();
     long ImMem = p->GetImage()->GetEstimatedMemorySize();
     mTotalMem += ImMem;
-    //    std::cout << " ==> Total mem = "<<mTotalMem<<" Ko"<<std::endl;
+
+    GimmickMessage(5,"==> Image in memory = "<<mUnloadQueue.size()<<std::endl);
+    GimmickMessage(5,"==> Total mem       = "<<mTotalMem<<" Ko"<<std::endl);
+
+    //  return;
+
     while (mTotalMem > mTotalMemMax)
       {
-       //      std::cout 
-       //                <<"   ! Exceeded max of "
-       //                << mTotalMemMax << " : unloading oldest image ... "
-       //                << std::endl;
+       GimmickMessage(5,
+                      "   ! Exceeded max of "
+                      << mTotalMemMax << " Ko : unloading oldest image ... "
+                      << std::endl);
        if ( mUnloadQueue.size() <= 1 ) 
          {
-           //      std::cout << "Only one image : cannot load AND unload it !!"
-           //                <<std::endl;
+            GimmickMessage(5,
+                           "   Only one image : cannot load AND unload it !!"
+                           <<std::endl);
            break; 
-
+           
          }
        ImageToLoadPtr unload = mUnloadQueue.remove_top();
        MultiThreadImageReaderUser* user = unload->GetUser();
 
+       /*
        if ((user!=0)&&(user!=this)) 
          {
            user->GetMultiThreadImageReaderUserMutex().Lock();
          }
+       */
 
+       std::string filename = unload->GetFilename();
 
-       //      std::cout << "'" << unload->GetFilename() << "'" << std::endl;
+       GimmickMessage(5,"'" << filename << "'" << std::endl);
        mTotalMem -= unload->GetImage()->GetEstimatedMemorySize();
-       //      std::cout << " ==> Total mem = "<<mTotalMem<<" Ko "<<std::endl;
-       
 
-       std::string filename = unload->GetFilename();
+       GimmickMessage(5," ==> Total mem = "<<mTotalMem<<" Ko "<<std::endl);
+
+       if (user!=0) 
+         {
+           //      std::cout << "unlock..."<<std::endl;
+           //   user->GetMultiThreadImageReaderUserMutex().Unlock();
+           //      std::cout << "event"<<std::endl;
+           user->MultiThreadImageReaderSendEvent
+             (filename,
+              MultiThreadImageReaderUser::ImageUnloaded,
+              0);
+           //      std::cout << "event ok"<<std::endl;
+         }     
+
        if (unload->Index()>=0)
          {
-           //   std::cout << "still in queue"<<std::endl;
+           // GimmickMessage(5,"still in queue"<<std::endl);
          }
        unload->Index() = -1;
 
@@ -452,21 +515,7 @@ namespace creaImageIO
        delete unload;
        //          std::cout << "delete ok."<<std::endl;
 
-       if (user!=0) 
-         {
-           //      std::cout << "unlock..."<<std::endl;
-           user->GetMultiThreadImageReaderUserMutex().Unlock();
-           //      std::cout << "event"<<std::endl;
-           user->MultiThreadImageReaderSendEvent
-             (filename,
-              MultiThreadImageReaderUser::ImageUnloaded,
-              0);
-           //      std::cout << "event ok"<<std::endl;
-
-         }
       }
-    
-  
   }
   //=====================================================================
 
@@ -516,8 +565,7 @@ namespace creaImageIO
     // While was not deleted 
     while (!TestDestroy())
       {
-       //      std::cout << "### Thread "<<GetCurrentId()<<" waiting for image"
-       //        << std::endl;
+               //std::cout << "### Thread "<<GetCurrentId()<<" still alive"  << std::endl;
          
        // Lock the mutex
        mMultiThreadImageReader->MultiThreadImageReaderEventLock();