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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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 #include "llvm/System/IncludeFile.h"
00036 
00037 namespace llvm {
00038   class SlowOperationInformer {
00039     std::string OperationName;
00040     unsigned LastPrintAmount;
00041 
00042     SlowOperationInformer(const SlowOperationInformer&);   // DO NOT IMPLEMENT
00043     void operator=(const SlowOperationInformer&);          // DO NOT IMPLEMENT
00044   public:
00045     SlowOperationInformer(const std::string &Name);
00046     ~SlowOperationInformer();
00047 
00048     /// progress - Clients should periodically call this method when they can
00049     /// handle cancellation.  The Amount variable should indicate how far
00050     /// along the operation is, given in 1/10ths of a percent (in other words,
00051     /// Amount should range from 0 to 1000).  If the user cancels the operation,
00052     /// this returns true, false otherwise.
00053     bool progress(unsigned Amount);
00054 
00055     /// progress - Same as the method above, but this performs the division for
00056     /// you, and helps you avoid overflow if you are dealing with largish
00057     /// numbers.
00058     bool progress(unsigned Current, unsigned Maximum) {
00059       assert(Maximum != 0 &&
00060              "Shouldn't be doing work if there is nothing to do!");
00061       return progress(Current*uint64_t(1000UL)/Maximum);
00062     }
00063   };
00064 } // end namespace llvm
00065 
00066 #endif /* SLOW_OPERATION_INFORMER_H */
00067 
00068 FORCE_DEFINING_FILE_TO_BE_LINKED(SupportSlowOperationInformer)