Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

TaskProgressManager.cpp

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00019 #include<ImLib3D/TaskProgressManager.hpp>
00020 #include<ImLib3D/ImLib3D.hpp>
00021 #include<ImLib3D/ImageProgress.hpp>
00022 #include<unistd.h>
00023 void ttprintf(const char *fmt, ...);
00024 
00025 TaskProgressManager *defaultTaskProgressManager=NULL;
00026 
00027 void 
00028 TaskProgressManager::Report(int taskNum)
00029 {
00030     // check if we use standard report , or user defined report
00031     if(alternateReportManager){alternateReportManager->Report(tasks);return;}
00032 
00033     double time=DTime(refTime);
00034     string report;
00035     for(int i=0;i<=taskNum;i++)
00036     {report+=tasks[i]->name+":"+tasks[i]->ProgressLabel()+(i<taskNum ? "," : "");}
00037     report+=" ";
00038     if(taskNum<(int)tasks.size()-1)
00039     {
00040         report+="(";
00041         for(uint i=taskNum+1;i<tasks.size();i++){report+=tasks[i]->name+(i<tasks.size()-1 ? "," : "");}
00042         report+=")";
00043     }
00044     ttprintf("%s\r",report.c_str());
00045     dirtyLine=report.size();
00046     fflush(stdout);
00047     for(int i=0;i<taskNum;i++)
00048     {
00049         tasks[i]->lastReport=time;
00050     }
00051 }
00052 void 
00053 TaskProgressManager::CleanLine()
00054 {
00055     static string blanks="                                                           ";
00056     if(dirtyLine)
00057     {
00058         printf("%s\r",blanks.substr(0,min((int)blanks.size(),dirtyLine)).c_str());
00059         dirtyLine=0;
00060     }
00061 }
00062 void 
00063 TaskProgressManager::Lock()
00064 {
00065 #ifdef   HAVE_LIBPTHREAD
00066     pthread_mutex_lock   (&mutex);
00067 #endif// HAVE_LIBPTHREAD
00068 }
00069 void 
00070 TaskProgressManager::UnLock()
00071 {
00072 #ifdef   HAVE_LIBPTHREAD
00073     pthread_mutex_unlock (&mutex);
00074 #endif// HAVE_LIBPTHREAD
00075 }
00076 
00077 void 
00078 TaskProgressManager::CheckTasks()
00079 {
00080     Lock();
00081     double time=DTime(refTime);
00082     for(int i=(int)tasks.size()-1;i>=0;i--)
00083     {
00084         if((time-tasks[i]->lastReport)>reportDelay)
00085         {
00086             Report(i);
00087         }
00088     }
00089     UnLock();
00090 }
00091 void  
00092 TaskProgressManager::NewTask(Task *task)
00093 {
00094     Lock();
00095 //  printf("NewTask:%x\n",(uint)task);
00096     tasks.push_back(task);
00097     task->refTime=refTime;
00098     task->startTime=DTime(refTime);
00099     task->lastReport=task->startTime;
00100     UnLock();
00101 }
00102 void 
00103 TaskProgressManager::EndTask(const Task *task)
00104 {
00105     Lock();
00106     if(find(tasks.begin(),tasks.end(),task)==tasks.end())
00107     {ttprintf("TaskProgressManager::EndTask: Bad task id:%x!!\n",(uint)task);}
00108     else
00109     {
00110         if(reportTotalTime)
00111         {
00112             //FIXME (cleanup, this is just for use with xdu...)
00113             mprintf("\n%d task-over:",(int)rint(1000*(DTime(refTime)-task->startTime)));
00114             for(uint i=0;i<tasks.size();i++)
00115             {
00116                 string nospaces=tasks[i]->name;
00117                 Replace(nospaces," ","_");
00118                 mprintf("%s-%d",nospaces,tasks[i]->id);
00119                 if(i!=tasks.size()-1){mprintf("/");}
00120             }
00121             if(task!=tasks[tasks.size()-1]){mprintf("NOTLAST??");}
00122             printf("\n");
00123         }
00124         tasks.erase(find(tasks.begin(),tasks.end(),task),tasks.end());
00125     }
00126 //  ttprintf("EndTask: tasks remaining:%d\n",tasks.size());
00127     UnLock();
00128 }
00129 
00130 void 
00131 TaskProgressManager::Run()// never should exit
00132 {
00133     while(running)
00134     {
00135         CheckTasks();
00136 //          struct timespec delay;
00137 //          delay.tv_sec = (int)sampleDelay;
00138 //          delay.tv_nsec = (sampleDelay-(int)sampleDelay)*1000000000L;
00139 //          pthread_delay_np( &delay );
00140 
00141         usleep((int)(1000000*sampleDelay));
00142     }
00143 #ifdef HAVE_LIBPTHREAD
00144     pthread_exit(0);
00145 #endif// HAVE_LIBPTHREAD
00146 }
00147 void *
00148 TaskProgressManager::EntryPoint(void *arg)
00149 {
00150 //  mprintf("TaskProgressManager(pid:%d): monitoring thread started\n",getpid());
00151     TaskProgressManager *tpm=(TaskProgressManager *)arg;
00152     tpm->Run();
00153     return NULL;
00154 }
00155 void 
00156 TaskProgressManager::Stop()
00157 {
00158     running=false;
00159 }
00160 
00161 void TaskProgressManager::Start()
00162 {
00163 #ifdef HAVE_LIBPTHREAD
00164     running=true;
00165     pthread_create (&thread, NULL,&TaskProgressManager::EntryPoint,(void *)this);
00166 #endif// HAVE_LIBPTHREAD
00167 }
00168 TaskProgressManager::TaskProgressManager(double sampleDelay,double reportDelay):
00169     sampleDelay(sampleDelay),
00170     reportDelay(reportDelay),
00171     running(false),
00172     dirtyLine(0),
00173     alternateReportManager(NULL),
00174     reportTotalTime(false)
00175 {
00176 #ifdef HAVE_LIBPTHREAD
00177     pthread_mutex_t init=PTHREAD_MUTEX_INITIALIZER;
00178     mutex=init;
00179 #endif// HAVE_LIBPTHREAD
00180     refTime=DTime();
00181 }
00182 TaskProgressManager::~TaskProgressManager()
00183 {
00184     ;
00185 }
00186 
00187 int TaskInProgress::idcount=0;
00188 
00189 TaskInProgress::TaskInProgress(const string &_name,TaskProgressManager *_manager):
00190     id(idcount++),
00191     name(_name)
00192 {
00193     manager=_manager;
00194     manager->NewTask(this);
00195 }
00196 void 
00197 TaskInProgress::AutoKill()
00198 {
00199 //  ttprintf("autokill %x\n",(uint)this);
00200     manager->EndTask(this);
00201 }
00202 
00203 
00204 //  int
00205 //  main(int argc,char **argv)
00206 //  {
00207 //      TaskProgressManager progress(1,3);
00208 //      g_progress=&progress;
00209 //      ttprintf("start:\n");
00210 //      progress.Start();
00211 //      {
00212 //          int i=0;
00213 //          TaskTracker<int> task("toto",0,4,&i,progress);
00214 //          ttprintf("entering main loop:\n");      
00215 //          for(i=0;i<4;i++)
00216 //          {
00217 //              usleep(6000000);
00218 //              ttprintf("making2...:\n");      
00219 //              TaskInProgress task2("task2",progress);
00220 //              usleep(6000000);
00221 //          }
00222 //      }
00223 //  }
00224 void 
00225 ttprintf(const char *fmt, ...)
00226 {
00227     va_list args;
00228     va_start(args,fmt);
00229     if(imLib3D.taskProgressManager){imLib3D.taskProgressManager->CleanLine();}
00230     vprintf(fmt,args);
00231 }
00232 

Generated on Fri Jun 17 13:36:07 2005 for ImLib3D by  doxygen 1.4.2