1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
/**************************************************************************
* *
* Regina - A Normal Surface Theory Calculator *
* Computational Engine *
* *
* Copyright (c) 1999-2009, Ben Burton *
* For further details contact Ben Burton (bab@debian.org). *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public *
* License along with this program; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
* MA 02110-1301, USA. *
* *
**************************************************************************/
/* end stub */
/*! \file nprogress.h
* \brief Allows external interfaces to obtain progress reports when
* running long calculations.
*/
#ifndef __NPROGRESS_H
#ifndef __DOXYGEN
#define __NPROGRESS_H
#endif
#include <ctime>
#include "shareableobject.h"
#include "utilities/nthread.h"
namespace regina {
/**
* \addtogroup progress Progress Management
* Progress reports during long calculations.
* @{
*/
/**
* An object through which external interfaces can obtain progress
* reports when running long calculations.
* The running calculation writes to this object to
* store the current state of progress, and the external interface reads
* from this object from a different thread.
*
* When writing progress information to an NProgress object, the last
* call should be to setFinished(). This informs all threads
* that the operation is finished and that the NProgress object can be
* deleted without the risk that the writing thread will attempt to
* access it again.
*
* If the operation allows it, the reading thread
* may at any time request that the operation be cancelled by calling
* cancel(). The writing thread should regularly poll isCancelled(),
* and if it detects a cancellation request should exit cleanly as soon
* as possible. Note that the writing thread should still call
* setFinished() in this situation.
*
* NProgress contains multithreading support; a mutex is used to ensure
* that the reading and writing threads do not interfere.
* Note that multithreading is in general not supported in the
* calculation engine, and the second thread must <b>only</b> read the
* current state of progress and do nothing else.
*
* NProgress also contains timing support, with measurements in both
* real time and CPU time. See the routines getRealTime() and
* totalCPUTime() for details.
*
* Subclasses of NProgress represent the various ways in which progress
* can be internally stored. Note that subclass member functions
* must lock the mutex whenever internal data is being
* accessed or modified (see NMutex::MutexLock for how this is done).
* Any public subclass member function that changes the state of
* progress must set the \a changed flag to \c true, and all public
* subclass query functions must set the \a changed flag to \c false.
*/
class NProgress : public ShareableObject, protected NMutex {
protected:
mutable bool changed;
/**< Has the state of progress changed since the last query? */
private:
bool finished;
/**< Is the operation whose progress we are reporting
* completely finished? */
bool cancelled;
/**< Has this operation been cancelled? */
time_t startReal;
/**< A real time marker representing when this progress
object was constructed. */
clock_t startCPU;
/**< A CPU time marker representing when this progress
object was constructed. */
time_t endReal;
/**< A real time marker representing when this progress
object was marked finished. */
clock_t endCPU;
/**< A CPU time marker representing when this progress
object was marked finished. */
public:
/**
* Performs basic initialisation.
* Note that the internal mutex is not locked during construction.
*
* The internal state-has-changed flag is set to \c true.
*
* \ifacespython Not present; NProgress objects should only be
* created within calculation engine routines whose progress is
* being watched.
*/
NProgress();
/**
* Destroys this object.
*/
virtual ~NProgress();
/**
* Determines if the state of progress has changed since the
* last query. A query is defined to be a call to
* getDescription(), getPercent() or any of the
* subclass-specific query routines.
*
* This routine allows interfaces to avoid calls to the slower
* query routines when they can avoid it.
*
* If no query has yet been made, this routine will return \c true.
*
* @return \c true if and only if the state of progress has
* changed since the last query.
*/
bool hasChanged() const;
/**
* Is the operation whose progress we are reporting completely
* finished?
*
* Once this routine returns \c true, it will always return \c
* true; thus there will be no need to call it again.
*
* @return \c true if and only if the operation is finished.
*/
bool isFinished() const;
/**
* Signifies that the operation whose progress we are reporting
* is completely finished. This <b>must</b> be the final
* member function call to this NProgress object made by the thread
* performing the corresponding operation. It notifies all
* other threads that the operation is complete and that this
* NProgress object can be safely deleted.
*
* This routine should still be called by the operation thread
* if it cancels itself in response to a request by an external
* interface (see cancel()).
*
* \ifacespython Not present; this should only be called from
* within the calculation engine routine whose progress is being
* watched.
*/
void setFinished();
/**
* Called by an external interface to request that the operation
* whose progress we are reporting be cancelled.
* The operation itself should regularly poll
* isCancelled() to check if an external interface has made this
* request.
*
* Note that if cancellation is not sensible or appropriate, the
* operation may freely ignore such cancellation requests and need
* not poll isCancelled() at all.
*
* This routine is made const since an external interface should be
* able to cancel an operation even though it should never
* modify the state of progress.
*/
void cancel() const;
/**
* Determines whether an external interface has requested that
* the operation whose progress we are reporting be cancelled.
*
* If the operation is polling for cancellation requests and it finds
* that isCancelled() returns \c true, it should generally exit
* (cleanly) as soon as possible with only partial or no results.
* However, if cancellation is not sensible or appropriate, the
* operation may freely ignore such cancellation requests.
*
* Note that even if the underlying operation cancels itself, it
* should still call setFinished().
*
* @return \c true if and only if an external interface has
* requested that the operation be cancelled.
*/
bool isCancelled() const;
/**
* Returns a string description of the current state of
* progress. Note that subclasses must override
* internalGetDescription(), not this routine.
*
* @return the current state of progress.
*/
std::string getDescription() const;
/**
* Determines if the state of progress can be expressed as a
* percentage.
*
* The default implementation returns \c false.
*
* @return \c true if and only if progress can be expressed as a
* percentage.
*/
virtual bool isPercent() const;
/**
* Returns the current state of progress as a percentage.
* Note that subclasses must override internalGetPercent(),
* not this routine.
*
* \pre Progress can be expressed as a percentage (see isPercent()).
*
* @return the current state of progress as a percentage.
*/
double getPercent() const;
/**
* Returns the real time elapsed since this operation began.
* This routine may be called both during and after the
* operation.
*
* If the operation has been marked as finished, the total
* elapsed time from start to finish will be reported.
* Otherwise the time elasped thus far will be reported.
*
* @return the total elapsed real time, measured in seconds.
*
* @see totalCPUTime()
*/
long getRealTime() const;
/**
* Returns the total CPU time consumed by the program from the
* beginning to the end of this operation. This routine will
* only return useful results after the operation has finished.
*
* If the operation has not yet been marked as finished, this
* routine will return 0.
*
* \warning For CPU time calculations to be correct, the same
* thread that constructs this progress object must also mark it
* finished.
*
* @return the total CPU time consumed, measured in seconds.
*
* @see getRealTime()
*/
long totalCPUTime() const;
void writeTextShort(std::ostream& out) const;
protected:
/**
* Returns a string description of the current state of progress.
*
* @return the current state of progress.
*/
virtual std::string internalGetDescription() const = 0;
/**
* Returns the current state of progress as a percentage.
*
* The default implementation returns 0.
*
* \pre Progress can be expressed as a percentage (see isPercent()).
*
* @return the current state of progress as a percentage.
*/
virtual double internalGetPercent() const;
};
/**
* A progress report that immediately claims it is finished.
* There is no need to call setFinished(); this will be done
* automatically by the constructor.
*
* \ifacespython Not present; all progress classes communicate with
* external interfaces through the NProgress interface.
*/
class NProgressFinished : public NProgress {
public:
/**
* Creates a new finished progress report.
* This constructor will automatically call setFinished().
*/
NProgressFinished();
virtual bool isPercent() const;
protected:
virtual std::string internalGetDescription() const;
virtual double internalGetPercent() const;
};
/*@}*/
// Inline functions for NProgress
inline NProgress::NProgress() : changed(true), finished(false),
cancelled(false) {
startReal = std::time(0);
startCPU = std::clock();
}
inline NProgress::~NProgress() {
}
inline bool NProgress::hasChanged() const {
return changed;
}
inline bool NProgress::isFinished() const {
MutexLock(this);
return finished;
}
inline void NProgress::setFinished() {
MutexLock(this);
endReal = std::time(0);
endCPU = std::clock();
finished = true;
}
inline void NProgress::cancel() const {
const_cast<NProgress*>(this)->cancelled = true;
}
inline bool NProgress::isCancelled() const {
return cancelled;
}
inline std::string NProgress::getDescription() const {
changed = false;
return internalGetDescription();
}
inline bool NProgress::isPercent() const {
return false;
}
inline double NProgress::getPercent() const {
changed = false;
return internalGetPercent();
}
inline void NProgress::writeTextShort(std::ostream& out) const {
out << "Progress: " << getDescription();
}
inline double NProgress::internalGetPercent() const {
return 0;
}
inline long NProgress::getRealTime() const {
MutexLock(this);
return (finished ? endReal : std::time(0)) - startReal;
}
inline long NProgress::totalCPUTime() const {
MutexLock(this);
return (finished ? (endCPU - startCPU) / CLOCKS_PER_SEC : 0);
};
// Inline functions for NProgressFinished
inline NProgressFinished::NProgressFinished() {
setFinished();
}
inline bool NProgressFinished::isPercent() const {
return true;
}
inline std::string NProgressFinished::internalGetDescription() const {
return "Finished.";
}
inline double NProgressFinished::internalGetPercent() const {
return 100;
}
} // namespace regina
#endif
|