LLVM API Documentation

SlowOperationInformer.h

Go to the documentation of this file.
00001 //===- llvm/Support/SlowOperationInformer.h - Keep user informed *- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a simple object which can be used to let the user know what
00011 // is going on when a slow operation is happening, and gives them the ability to
00012 // cancel it.  Potentially slow operations can stack allocate one of these
00013 // objects, and periodically call the "progress" method to update the progress
00014 // bar.  If the operation takes more than 1 second to complete, the progress bar
00015 // is automatically shown and updated.  As such, the slow operation should not
00016 // print stuff to the screen, and should not be confused if an extra line
00017 // appears on the screen (ie, the cursor should be at the start of the line).
00018 //
00019 // If the user presses CTRL-C during the operation, the next invocation of the
00020 // progress method return true indicating that the operation was cancelled.
00021 //
00022 // Because SlowOperationInformers fiddle around with signals, they cannot be
00023 // nested, and interact poorly with threads.  The SIGALRM handler is set back to
00024 // SIGDFL, but the SIGINT signal handler is restored when the
00025 // SlowOperationInformer is destroyed.
00026 //
00027 //===----------------------------------------------------------------------===//
00028 
00029 #ifndef LLVM_SUPPORT_SLOW_OPERATION_INFORMER_H
00030 #define LLVM_SUPPORT_SLOW_OPERATION_INFORMER_H
00031 
00032 #include <string>
00033 #include <cassert>
00034 #include "llvm/Support/DataTypes.h"
00035 
00036 namespace llvm {
00037   class SlowOperationInformer {
00038     std::string OperationName;
00039     unsigned LastPrintAmount;
00040 
00041     SlowOperationInformer(const SlowOperationInformer&);   // DO NOT IMPLEMENT
00042     void operator=(const SlowOperationInformer&);          // DO NOT IMPLEMENT
00043   public:
00044     SlowOperationInformer(const std::string &Name);
00045     ~SlowOperationInformer();
00046 
00047     /// progress - Clients should periodically call this method when they can
00048     /// handle cancellation.  The Amount variable should indicate how far
00049     /// along the operation is, given in 1/10ths of a percent (in other words,
00050     /// Amount should range from 0 to 1000).  If the user cancels the operation,
00051     /// this returns true, false otherwise.
00052     bool progress(unsigned Amount);
00053 
00054     /// progress - Same as the method above, but this performs the division for
00055     /// you, and helps you avoid overflow if you are dealing with largish
00056     /// numbers.
00057     bool progress(unsigned Current, unsigned Maximum) {
00058       assert(Maximum != 0 &&
00059              "Shouldn't be doing work if there is nothing to do!");
00060       return progress(Current*uint64_t(1000UL)/Maximum);
00061     }
00062   };
00063 } // end namespace llvm
00064 
00065 #endif /* SLOW_OPERATION_INFORMER_H */