00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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
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
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
00127 UnLock();
00128 }
00129
00130 void
00131 TaskProgressManager::Run()
00132 {
00133 while(running)
00134 {
00135 CheckTasks();
00136
00137
00138
00139
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
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
00200 manager->EndTask(this);
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
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